/** * Tests that we can manage s-ramp properties on a /core/Document. * * @throws Exception */ @Test public void testUpdateProperties_Document() throws Exception { // First, add an artifact to the repo String artifactFileName = "s-ramp-press-release.pdf"; InputStream pdf = this.getClass().getResourceAsStream("/sample-files/core/" + artifactFileName); Document document = new Document(); document.setName(artifactFileName); document.setContentType("application/pdf"); document.setArtifactType(BaseArtifactEnum.DOCUMENT); BaseArtifactType artifact = persistenceManager.persistArtifact(document, pdf); Assert.assertNotNull(artifact); log.info("persisted PDF to JCR, returned artifact uuid=" + artifact.getUuid()); Assert.assertEquals(Document.class, artifact.getClass()); Assert.assertEquals(new Long(18873l), ((DocumentArtifactType) artifact).getContentSize()); // Now update the artifact artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.Document()); Assert.assertTrue("Expected 0 properties.", artifact.getProperty().isEmpty()); Property prop1 = new Property(); prop1.setPropertyName("prop1"); prop1.setPropertyValue("propval1"); artifact.getProperty().add(prop1); Property prop2 = new Property(); prop2.setPropertyName("prop2"); prop2.setPropertyValue("propval2"); artifact.getProperty().add(prop2); persistenceManager.updateArtifact(artifact, ArtifactType.Document()); // Now verify that the properties were stored artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.Document()); Assert.assertTrue("Expected 2 properties.", artifact.getProperty().size() == 2); String p1 = artifact.getProperty().get(0).getPropertyName() + "=" + artifact.getProperty().get(0).getPropertyValue(); String p2 = artifact.getProperty().get(1).getPropertyName() + "=" + artifact.getProperty().get(1).getPropertyValue(); Set<String> ps = new HashSet<String>(); ps.add(p1); ps.add(p2); Assert.assertTrue("Prop1 missing from properties.", ps.contains("prop1=propval1")); Assert.assertTrue("Prop2 missing from properties.", ps.contains("prop2=propval2")); Assert.assertFalse("Prop3 somehow existed!.", ps.contains("prop3=propval3")); // Now remove one property, add another one, and change the value of one artifact.getProperty().clear(); prop1 = new Property(); prop1.setPropertyName("prop1"); prop1.setPropertyValue("propval1-updated"); artifact.getProperty().add(prop1); Property prop3 = new Property(); prop3.setPropertyName("prop3"); prop3.setPropertyValue("propval3"); artifact.getProperty().add(prop3); persistenceManager.updateArtifact(artifact, ArtifactType.Document()); // Now verify that the properties were updated artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.Document()); Assert.assertTrue("Expected 2 properties.", artifact.getProperty().size() == 2); p1 = artifact.getProperty().get(0).getPropertyName() + "=" + artifact.getProperty().get(0).getPropertyValue(); p2 = artifact.getProperty().get(1).getPropertyName() + "=" + artifact.getProperty().get(1).getPropertyValue(); ps.clear(); ps.add(p1); ps.add(p2); Assert.assertFalse("Prop1 wasn't updated (old value detected).", ps.contains("prop1=propval1")); Assert.assertTrue( "Prop1 wasn't updated (new value not found).", ps.contains("prop1=propval1-updated")); Assert.assertFalse("Prop2 existed unexpectedly.", ps.contains("prop2=propval2")); Assert.assertTrue("Prop3 missing from properties.", ps.contains("prop3=propval3")); }
/** * Main. * * @param args */ public static void main(String[] args) throws Exception { System.out.println("\n*** Running Artificer Query Demo ***\n"); String endpoint = System.getProperty("artificer.endpoint"); String username = System.getProperty("artificer.auth.username"); String password = System.getProperty("artificer.auth.password"); if (endpoint == null || endpoint.trim().length() == 0) { endpoint = DEFAULT_ENDPOINT; } if (username == null || username.trim().length() == 0) { username = DEFAULT_USER; } if (password == null || password.trim().length() == 0) { password = DEFAULT_PASSWORD; } System.out.println("Artificer Endpoint: " + endpoint); System.out.println("Artificer User: "******"/s-ramp[@from-demo = ?]") .parameter(PropertyDemo.class.getSimpleName()) .count(1) .query(); if (rs.size() > 0) { System.out.println("It looks like you already ran this demo!"); System.out.println("I'm going to quit, because I don't want to clutter up"); System.out.println("your repository with duplicate stuff."); System.exit(1); } // Before we do anything else, we need to upload some artifacts to the // Artificer repository. ArtifactType type = ArtifactType.valueOf("Document"); System.out.print("Uploading two artifacts..."); BaseArtifactType artifact1 = client.uploadArtifact( type, PropertyDemo.class.getResourceAsStream("property-demo-doc-1.txt"), "property-demo-doc-1.txt"); BaseArtifactType artifact2 = client.uploadArtifact( type, PropertyDemo.class.getResourceAsStream("property-demo-doc-2.txt"), "property-demo-doc-2.txt"); System.out.println("uploaded."); // And then we can change their names if we want! artifact1.setName("property-demo-document-1.txt"); artifact2.setName("property-demo-document-2.txt"); // Now, we can modify the artifacts by adding some custom properties to them. ArtificerModelUtils.setCustomProperty(artifact1, "property-demo", "true"); ArtificerModelUtils.setCustomProperty(artifact1, "artifact-num", "one"); ArtificerModelUtils.setCustomProperty(artifact1, "hello", "world"); ArtificerModelUtils.setCustomProperty(artifact2, "property-demo", "true"); ArtificerModelUtils.setCustomProperty(artifact2, "artifact-num", "two"); ArtificerModelUtils.setCustomProperty(artifact2, "foo", "bar"); // Also tag these artifacts as coming from this demo. ArtificerModelUtils.setCustomProperty( artifact1, "from-demo", PropertyDemo.class.getSimpleName()); ArtificerModelUtils.setCustomProperty( artifact2, "from-demo", PropertyDemo.class.getSimpleName()); // And now update both artifacts so that the repository knows about these // new properties. System.out.print("Updating (meta-data for) both artifacts..."); client.updateArtifactMetaData(artifact1); client.updateArtifactMetaData(artifact2); System.out.println("updated."); // Next, fetch the artifact meta-data from the server and make sure the properties // we set above are really there. System.out.print("Re-fetching (meta-data for) both artifacts..."); BaseArtifactType metaData1 = client.getArtifactMetaData(type, artifact1.getUuid()); BaseArtifactType metaData2 = client.getArtifactMetaData(type, artifact2.getUuid()); System.out.println("fetched."); if (metaData1.getProperty().size() < 3) { System.out.println("Properties not found on artifact 1! Oh noes!"); System.exit(1); } else { System.out.println("All properties accounted for (artifact 1)!"); } if (metaData2.getProperty().size() < 3) { System.out.println("Properties not found on artifact 2! Oh noes!"); System.exit(1); } else { System.out.println("All properties accounted for (artifact 2)!"); } // Now we know that adding properties works. Note that of course you can also // remove properties and change their values, and that will work the same way // (just remember to call updateArtifactMetaData after you make changes). // The next thing I want to show you is how to query for artifacts in the // repository by their properties. You can query by either base property or // by custom user property. String q = "/s-ramp/core/Document[@name = 'property-demo-document-1.txt']"; QueryResultSet resultSet = client.query(q); if (resultSet.size() == 0) { System.out.println("Failed to find property-demo-document-1.txt!"); System.exit(1); } // Find all of the documents with the 'property-demo' property set to 'true' (should // be all of them) q = "/s-ramp/core/Document[@property-demo = 'true']"; resultSet = client.query(q); if (resultSet.size() == 0) { System.out.println("Failed to find document(s) with @property-demo set to 'true'!"); System.exit(1); } else { System.out.println("Search 1 succeeded."); } long total = resultSet.size(); // Find only the ones with 'artifact-num' set to 'one' (should be half of them) q = "/s-ramp/core/Document[@artifact-num = 'one']"; resultSet = client.query(q); if (resultSet.size() == 0) { System.out.println("Failed to find document(s) with @artifact-num set to 'one'!"); System.exit(1); } long ones = resultSet.size(); if (!(total / 2 == ones)) { System.out.println("The wrong number of documents with @artifact-num set to 'one' found!"); System.exit(1); } else { System.out.println("Search 2 succeeded."); } // All done. System.out.println("\n*** Demo Completed Successfully ***\n\n"); Thread.sleep(3000); }
/** * Tests that we can manage s-ramp properties. * * @throws Exception */ @Test public void testUpdateProperties() throws Exception { // First, add an artifact to the repo String artifactFileName = "PO.xsd"; InputStream POXsd = this.getClass().getResourceAsStream("/sample-files/xsd/" + artifactFileName); Document document = new Document(); document.setName(artifactFileName); document.setArtifactType(BaseArtifactEnum.XSD_DOCUMENT); BaseArtifactType artifact = persistenceManager.persistArtifact(document, POXsd); Assert.assertNotNull(artifact); log.info("persisted PO.xsd to JCR, returned artifact uuid=" + artifact.getUuid()); Assert.assertEquals(XsdDocument.class, artifact.getClass()); long size = ((DocumentArtifactType) artifact).getContentSize(); Assert.assertTrue( size >= 2376L); // Not doing an equals here due to the vagaries of Windows vs *nix line // endings // Now update the artifact artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.XsdDocument()); Assert.assertTrue("Expected 0 properties.", artifact.getProperty().isEmpty()); Property prop1 = new Property(); prop1.setPropertyName("prop1"); prop1.setPropertyValue("propval1"); artifact.getProperty().add(prop1); Property prop2 = new Property(); prop2.setPropertyName("prop2"); prop2.setPropertyValue("propval2"); artifact.getProperty().add(prop2); persistenceManager.updateArtifact(artifact, ArtifactType.XsdDocument()); // Now verify that the properties were stored artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.XsdDocument()); Assert.assertTrue("Expected 2 properties.", artifact.getProperty().size() == 2); String p1 = artifact.getProperty().get(0).getPropertyName() + "=" + artifact.getProperty().get(0).getPropertyValue(); String p2 = artifact.getProperty().get(1).getPropertyName() + "=" + artifact.getProperty().get(1).getPropertyValue(); Set<String> ps = new HashSet<String>(); ps.add(p1); ps.add(p2); Assert.assertTrue("Prop1 missing from properties.", ps.contains("prop1=propval1")); Assert.assertTrue("Prop2 missing from properties.", ps.contains("prop2=propval2")); Assert.assertFalse("Prop3 somehow existed!.", ps.contains("prop3=propval3")); // Now remove one property, add another one, and change the value of one artifact.getProperty().clear(); prop1 = new Property(); prop1.setPropertyName("prop1"); prop1.setPropertyValue("propval1-updated"); artifact.getProperty().add(prop1); Property prop3 = new Property(); prop3.setPropertyName("prop3"); prop3.setPropertyValue("propval3"); artifact.getProperty().add(prop3); persistenceManager.updateArtifact(artifact, ArtifactType.XsdDocument()); // Now verify that the properties were updated artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.XsdDocument()); Assert.assertTrue("Expected 2 properties.", artifact.getProperty().size() == 2); p1 = artifact.getProperty().get(0).getPropertyName() + "=" + artifact.getProperty().get(0).getPropertyValue(); p2 = artifact.getProperty().get(1).getPropertyName() + "=" + artifact.getProperty().get(1).getPropertyValue(); ps.clear(); ps.add(p1); ps.add(p2); Assert.assertFalse("Prop1 wasn't updated (old value detected).", ps.contains("prop1=propval1")); Assert.assertTrue( "Prop1 wasn't updated (new value not found).", ps.contains("prop1=propval1-updated")); Assert.assertFalse("Prop2 existed unexpectedly.", ps.contains("prop2=propval2")); Assert.assertTrue("Prop3 missing from properties.", ps.contains("prop3=propval3")); }