public static <T extends Object> T findFirst(Session session, String query, Map<String, String> bindParams, Class<T> type) { JcrTools tools = new JcrTools(); try { QueryResult result = query(session, query, bindParams); List<T> list = queryResultToList(result, 1, type); if (list != null && list.size() > 0) { return list.get(0); } else { return null; } } catch (RepositoryException e) { throw new MetadataRepositoryException("Unable to findFirst for query : " + query, e); } }
public List<JcrObject> findByTag(String tag) { String query = "SELECT * FROM [tba:taggable] AS taggable\n" + "WHERE taggable.[tba:tags] = $tag "; JcrTools tools = new JcrTools(); Map<String, String> params = new HashMap<>(); params.put("tag", tag); try { QueryResult result = JcrQueryUtil.query(getSession(), query, params); return JcrQueryUtil.queryResultToList(result, JcrObject.class); } catch (RepositoryException e) { throw new MetadataRepositoryException("Unable to find objects by tag " + tag, e); } }
public static <T extends Object> List<T> find(Session session, String query, Map<String, String> bindParams, Class<T> type, Object... args) { JcrTools tools = new JcrTools(); try { QueryResult result = query(session, query, bindParams); return queryResultToList(result, null, type, args); } catch (RepositoryException e) { throw new MetadataRepositoryException("Unable to findAll for query : " + query, e); } }
public void setUpRepositoryConfiguration() throws RepositoryException, IOException { final JcrTools jcrTools = new JcrTools(true); final Session internalSession = sessions.getInternalSession(); try {
protected void printDetails( Node node ) throws RepositoryException { if (print) { new JcrTools(true).printNode(node); } }
protected void printSubgraph( Node node ) throws RepositoryException { if (print) { JcrTools tools = new JcrTools(); tools.printSubgraph(node); } }
@Before @Override public void beforeEach() throws Exception { super.beforeEach(); tools = new JcrTools(); Node rootNode = session.getRootNode(); personNode = rootNode.addNode("Person"); personNode.setProperty("First Name", "Ryan"); personNode.setProperty("Middle Name", "Joseph"); personNode.setProperty("Last Name", "Franklin"); personNode.setProperty("Age", 37); personNode.setProperty("Children", new String[] {"Sally", "Brent", "Michael"}); addressNode = personNode.addNode("Address"); addressNode.setProperty("Street", "Frost Avenue"); addressNode.setProperty("House Number", 166); addressNode.setProperty("City", "Flagstaff"); addressNode.setProperty("State", "AZ"); addressNode.setProperty("Country", "US"); addressNode.setProperty("Zip Code", 77777); problems = new SimpleProblems(); }
/** * Get or Create a node relative to the Parent Node and return the Wrapper JcrObject */ public static <T extends JcrObject> T getOrCreateNode(Node parentNode, String name, String nodeType, Class<T> type, Object... constructorArgs) { T entity = null; try { JcrTools tools = new JcrTools(); //if versionable checkout // if(isVersionable(parentNode)){ // JcrVersionUtil.checkout(parentNode); // } Node n = tools.findOrCreateChild(parentNode, name, nodeType); entity = createJcrObject(n, type, constructorArgs); //save ?? // JcrVersionUtil.checkinRecursively(n); // if(isVersionable(parentNode)){ // JcrVersionUtil.checkin(parentNode); // } } catch (AccessDeniedException e) { log.debug("Access denied", e); throw new AccessControlException(e.getMessage()); } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed to retrieve the Node named" + name, e); } return entity; }
@Override public void beforeEach() throws Exception { startRepositoryWithConfiguration(repositoryConfiguration()); printMessage("Started repository..."); tools = new JcrTools(); }
private Supplier<Boolean> addFilesToFolder(JcrRepository repository, CyclicBarrier cyclicBarrier, String clusterNodeId, List<String> fileNames) throws RepositoryException { JcrTools tools = new JcrTools(); JcrSession session = repository.login(); return () -> { try { fileNames.forEach(fileName -> { try { tools.uploadFile(session, "/folder/" + fileName, new ByteArrayInputStream(clusterNodeId.getBytes())); } catch (RepositoryException | IOException e) { throw new RuntimeException(e); } }); cyclicBarrier.await(); session.save(); return true; } catch (ItemExistsException ies) { return false; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (session != null) { session.logout(); } } }; }
@Override @Before public void beforeEach() throws Exception { super.beforeEach(); if (startRepositoryAutomatically()) { startRepository(); } tools = new JcrTools(); }
@FixFor( "MODE-1767" ) @Test public void shouldNotOverrideManuallyAddedMimeTypePropertyToNtResourceUponSave() throws Exception { // Start the repository ... startRepositoryWithConfigurationFrom("config/simple-repo-config.json"); // Add a node under which we'll do our work ... Node node1 = session.getRootNode().addNode("node1"); session.save(); // Upload a file JcrTools tools = new JcrTools(); tools.uploadFile(session, "/node1/simple.json", getResourceFile("data/simple.json")); Node fileNode = node1.getNode("simple.json"); Node contentNode = fileNode.getNode("jcr:content"); contentNode.setProperty("jcr:mimeType", "bogus"); assertThat(contentNode.getPrimaryNodeType().getName(), is("nt:resource")); assertThat(contentNode.getProperty("jcr:mimeType").getString(), is("bogus")); assertThat(contentNode.hasProperty("jcr:data"), is(true)); // Save the session, and verify that the "jcr:mimeType" property is added ... session.save(); assertThat(contentNode.hasProperty("jcr:data"), is(true)); assertThat(contentNode.hasProperty("jcr:mimeType"), is(true)); assertThat(contentNode.getProperty("jcr:mimeType").getString(), is("bogus")); }
@FixFor( "MODE-1767" ) @Test public void shouldAutomaticallyAddMimeTypePropertyToNtResourceUponSave() throws Exception { // Start the repository ... startRepositoryWithConfigurationFrom("config/simple-repo-config.json"); // Add a node under which we'll do our work ... Node node1 = session.getRootNode().addNode("node1"); session.save(); // Upload a file JcrTools tools = new JcrTools(); tools.uploadFile(session, "/node1/simple.json", getResourceFile("data/simple.json")); Node fileNode = node1.getNode("simple.json"); Node contentNode = fileNode.getNode("jcr:content"); assertThat(contentNode.getPrimaryNodeType().getName(), is("nt:resource")); assertThat(contentNode.hasProperty("jcr:mimeType"), is(false)); assertThat(contentNode.hasProperty("jcr:data"), is(true)); // Save the session, and verify that the "jcr:mimeType" property is added ... session.save(); assertThat(contentNode.hasProperty("jcr:data"), is(true)); assertThat(contentNode.hasProperty("jcr:mimeType"), is(true)); assertThat(contentNode.getProperty("jcr:mimeType").getString(), is("application/json")); }
public void testShouldVerifyNtFileNodesHavePrimaryItem() throws Exception { Session session1 = getHelper().getSuperuserSession(); // Create node structure Node root1 = getTestRoot(session1); Node folder1 = root1.addNode("folder1", "nt:folder"); String fileName = "simple.json"; String filePath = folder1.getPath() + "/" + fileName; new JcrTools().uploadFile(session1, filePath, getClass().getResourceAsStream("/data/" + fileName)); session1.save(); // Find the primary item ... Node file1 = folder1.getNode(fileName); Node content = file1.getNode("jcr:content"); assertNotNull(file1); Item primary = file1.getPrimaryItem(); assertThat(primary, is(sameInstance((Item)content))); // Change the primary type of the "jcr:content" node to "nt:unstructured" ... content.setPrimaryType("nt:unstructured"); session1.save(); // Find the primary item (again) ... Node content2 = file1.getNode("jcr:content"); assertNotNull(file1); Item primary2 = file1.getPrimaryItem(); assertThat(primary2, is(sameInstance((Item)content2))); }
@FixFor( "MODE-1767" ) @Test public void shouldNotAddMimeTypePropertyUnderNtFileIfContentNodeIsNotNtResource() throws Exception { // Start the repository ... startRepositoryWithConfigurationFrom("config/simple-repo-config.json"); // Add a node under which we'll do our work ... Node node1 = session.getRootNode().addNode("node1"); session.save(); // Upload a file JcrTools tools = new JcrTools(); tools.uploadFile(session, "/node1/simple.json", getResourceFile("data/simple.json")); Node fileNode = node1.getNode("simple.json"); Node contentNode = fileNode.getNode("jcr:content"); assertThat(contentNode.getPrimaryNodeType().getName(), is("nt:resource")); contentNode.setPrimaryType("nt:unstructured"); assertThat(contentNode.hasProperty("jcr:mimeType"), is(false)); assertThat(contentNode.hasProperty("jcr:data"), is(true)); // Save the session, and verify that the "jcr:mimeType" property is added ... session.save(); assertThat(contentNode.hasProperty("jcr:mimeType"), is(false)); assertThat(contentNode.hasProperty("jcr:data"), is(true)); }
@Override @Before public void beforeEach() throws Exception { startRepositoryWithConfigurationFrom("config/repo-config-concurrent-tests.json"); tools = new JcrTools(); // Set the transaction timeout so that we can debug code called within the transaction ... repository.runningState().txnManager().setTransactionTimeout(500); }
@Test @FixFor( "MODE-1978" ) public void shouldAllowRemovingArtifactWithNoExistingTrash() throws Exception { JcrTools tools = new JcrTools();
@Test @FixFor("MODE-1573") public void shouldPerformRoundTripOnDocumentViewWithBinaryContent() throws Exception { JcrTools tools = new JcrTools(); File binaryFile = new File("src/test/resources/io/binary.pdf"); assert (binaryFile.exists() && binaryFile.isFile()); File outputFile = File.createTempFile("modeshape_import_export_" + System.currentTimeMillis(), "_test"); outputFile.deleteOnExit(); tools.uploadFile(session, "file", binaryFile); session.save(); session.exportDocumentView("/file", new FileOutputStream(outputFile), false, false); assertTrue(outputFile.length() > 0); session.getRootNode().getNode("file").remove(); session.save(); //sleep so that the binary can be properly cleaned up (this is done via a listener) Thread.sleep(200); session.getWorkspace().importXML("/", new FileInputStream(outputFile), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW); assertNotNull(session.getNode("/file")); assertNotNull(session.getNode("/file/jcr:content")); Property data = session.getNode("/file/jcr:content").getProperty("jcr:data"); assertNotNull(data); Binary binary = (Binary)data.getBinary(); assertNotNull(binary); assertEquals(binaryFile.length(), binary.getSize()); }
@FixFor( "MODE-1767" ) @Test public void shouldAutomaticallyAddMimeTypePropertyToNtResourceSubtypeUponSave() throws Exception { // Start the repository ... startRepositoryWithConfigurationFrom("config/simple-repo-config.json"); // Register a new node type that is a subtype of 'nt:resource' NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager(); NodeTypeTemplate template = ntMgr.createNodeTypeTemplate(); template.setDeclaredSuperTypeNames(new String[] {"nt:resource"}); template.setName("customResourceType"); NodeType ntResourceSubtype = ntMgr.registerNodeType(template, false); assertThat(ntResourceSubtype.getDeclaredSupertypes()[0].getName(), is("nt:resource")); // Add a node under which we'll do our work ... Node node1 = session.getRootNode().addNode("node1"); session.save(); // Upload a file JcrTools tools = new JcrTools(); tools.uploadFile(session, "/node1/simple.json", getResourceFile("data/simple.json")); Node fileNode = node1.getNode("simple.json"); Node contentNode = fileNode.getNode("jcr:content"); contentNode.setPrimaryType(ntResourceSubtype.getName()); assertThat(contentNode.getPrimaryNodeType().getName(), is(ntResourceSubtype.getName())); assertThat(contentNode.hasProperty("jcr:mimeType"), is(false)); assertThat(contentNode.hasProperty("jcr:data"), is(true)); // Save the session, and verify that the "jcr:mimeType" property is added ... session.save(); assertThat(contentNode.hasProperty("jcr:data"), is(true)); assertThat(contentNode.hasProperty("jcr:mimeType"), is(true)); assertThat(contentNode.getProperty("jcr:mimeType").getString(), is("application/json")); }
@FixFor( "MODE-1855" ) @Test public void shouldGetLocalNameAndNamespaceUriFromNodeAndPropertyObjects() throws Exception { // Add a node under which we'll do our work ... Node node1 = session.getRootNode().addNode("node1"); session.save(); assertLocalNameAndNamespace(node1, "node1", ""); // Add another SNS node under which we'll do our work ... Node node1a = session.getRootNode().addNode("node1"); session.save(); assertThat(node1a.getIndex(), is(2)); assertLocalNameAndNamespace(node1a, "node1", ""); // no SNS index in local name! // Upload a file JcrTools tools = new JcrTools(); tools.uploadFile(session, "/node1/simple.json", getResourceFile("data/simple.json")); Node fileNode = node1.getNode("simple.json"); Node contentNode = fileNode.getNode("jcr:content"); assertLocalNameAndNamespace(fileNode, "simple.json", ""); assertLocalNameAndNamespace(contentNode, "content", "jcr"); }