/** * Ensures that the required ArtifactGrouping is present in the repository. * * @throws SrampAtomException * @throws SrampClientException */ private BaseArtifactType ensureArtifactGrouping() throws SrampClientException, SrampAtomException { String groupingName = getParamFromRepositoryUrl("artifactGrouping"); // $NON-NLS-1$ if (groupingName == null || groupingName.trim().length() == 0) { logger.warn(Messages.i18n.format("NO_ARTIFACT_GROUPING_NAME")); // $NON-NLS-1$ return null; } QueryResultSet query = client .buildQuery("/s-ramp/ext/ArtifactGrouping[@name = ?]") .parameter(groupingName) .count(2) .query(); //$NON-NLS-1$ if (query.size() > 1) { logger.warn( Messages.i18n.format("MULTIPLE_ARTIFACT_GROUPSING_FOUND", groupingName)); // $NON-NLS-1$ return null; } else if (query.size() == 1) { ArtifactSummary summary = query.get(0); return client.getArtifactMetaData(summary.getType(), summary.getUuid()); } else { ExtendedArtifactType groupingArtifact = new ExtendedArtifactType(); groupingArtifact.setArtifactType(BaseArtifactEnum.EXTENDED_ARTIFACT_TYPE); groupingArtifact.setExtendedType("ArtifactGrouping"); // $NON-NLS-1$ groupingArtifact.setName(groupingName); groupingArtifact.setDescription( Messages.i18n.format("ARTIFACT_GROUPING_DESCRIPTION")); // $NON-NLS-1$ return client.createArtifact(groupingArtifact); } }
@Test public void testPersistDuplicateArtifact() throws Exception { String artifactFileName = "s-ramp-press-release.pdf"; InputStream pdf = this.getClass().getResourceAsStream("/sample-files/core/" + artifactFileName); Document document = new Document(); document.setName(artifactFileName); document.setArtifactType(BaseArtifactEnum.DOCUMENT); document.setUuid("12345"); // amazing - that's the same UUID as my luggage! BaseArtifactType artifact = persistenceManager.persistArtifact(document, pdf); Assert.assertNotNull(artifact); // Now try to persist another artifact of the same type with the same UUID. pdf = this.getClass().getResourceAsStream("/sample-files/core/" + artifactFileName); document = new Document(); document.setName(artifactFileName + "-2"); document.setArtifactType(BaseArtifactEnum.DOCUMENT); document.setUuid("12345"); // amazing - that's the same UUID as my luggage! try { persistenceManager.persistArtifact(document, pdf); Assert.fail("Expected an ArtifactAlreadyExistsException."); } catch (ArtifactAlreadyExistsException e) { // Expected this! Assert.assertEquals("Artifact with UUID 12345 already exists.", e.getMessage()); } // Now try to persist another artifact with a *different* type but the same UUID. pdf = this.getClass().getResourceAsStream("/sample-files/core/" + artifactFileName); ExtendedArtifactType extendedArtifact = new ExtendedArtifactType(); extendedArtifact.setArtifactType(BaseArtifactEnum.EXTENDED_ARTIFACT_TYPE); extendedArtifact.setExtendedType("FooArtifactType"); extendedArtifact.setName("MyExtendedArtifact"); extendedArtifact.setUuid("12345"); try { persistenceManager.persistArtifact(document, pdf); Assert.fail("Expected an ArtifactAlreadyExistsException."); } catch (ArtifactAlreadyExistsException e) { // Expected this! Assert.assertEquals("Artifact with UUID 12345 already exists.", e.getMessage()); } }
@Test public void testPersistArtifact_ExtendedArtifactType() throws Exception { ExtendedArtifactType extendedArtifact = new ExtendedArtifactType(); extendedArtifact.setArtifactType(BaseArtifactEnum.EXTENDED_ARTIFACT_TYPE); extendedArtifact.setExtendedType("FooArtifactType"); extendedArtifact.setName("MyExtendedArtifact"); extendedArtifact.setDescription("This is a simple description for testing."); BaseArtifactType artifact = persistenceManager.persistArtifact(extendedArtifact, null); Assert.assertNotNull(artifact); log.info("persisted extended artifact to JCR, returned artifact uuid=" + artifact.getUuid()); // print out the derived node if (log.isDebugEnabled()) { persistenceManager.printArtifactGraph(artifact.getUuid(), ArtifactType.XmlDocument()); } Assert.assertEquals(ExtendedArtifactType.class, artifact.getClass()); String name = ((ExtendedArtifactType) artifact).getName(); String description = ((ExtendedArtifactType) artifact).getDescription(); Assert.assertEquals("MyExtendedArtifact", name); Assert.assertEquals("This is a simple description for testing.", description); }