/** * Tests that we can update basic s-ramp meta data. * * @throws Exception */ @Test public void testUpdateMetaData() 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 Assert.assertEquals(artifactFileName, artifact.getName()); // Now update the artifact artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.XsdDocument()); artifact.setName("My PO"); artifact.setDescription("A new description of the PO.xsd artifact."); artifact.setVersion("2.0.13"); persistenceManager.updateArtifact(artifact, ArtifactType.XsdDocument()); // Now verify the meta-data was updated artifact = persistenceManager.getArtifact(artifact.getUuid(), ArtifactType.XsdDocument()); Assert.assertEquals("My PO", artifact.getName()); Assert.assertEquals("A new description of the PO.xsd artifact.", artifact.getDescription()); Assert.assertEquals("2.0.13", artifact.getVersion()); }
/** * Uploads a single deployment to S-RAMP. * * @param deploymentType * @param fileName * @param client * @param tempFile * @param responseParams * @param version * @throws Exception */ private void uploadSingleDeployment( String deploymentType, String fileName, File tempFile, Map<String, String> responseParams, String version) throws Exception { ArtifactType at = ArtifactType.valueOf(deploymentType); if (at.isExtendedType()) { at = ArtifactType.ExtendedDocument(at.getExtendedType()); } String uuid = null; // First, upload the deployment InputStream contentStream = null; try { contentStream = FileUtils.openInputStream(tempFile); BaseArtifactType artifact = at.newArtifactInstance(); artifact.setName(fileName); artifact.setVersion(version); artifact = clientAccessor.getClient().uploadArtifact(artifact, contentStream); responseParams.put("model", at.getArtifactType().getModel()); // $NON-NLS-1$ responseParams.put("type", at.getArtifactType().getType()); // $NON-NLS-1$ responseParams.put("uuid", artifact.getUuid()); // $NON-NLS-1$ uuid = artifact.getUuid(); } finally { IOUtils.closeQuietly(contentStream); } // Try to expand the artifact (works if an expander is available for the given artifact type). ZipToSrampArchive j2sramp = null; SrampArchive archive = null; try { j2sramp = ZipToSrampArchiveRegistry.createExpander(at, tempFile); if (j2sramp != null) { j2sramp.setContextParam(DefaultMetaDataFactory.PARENT_UUID, uuid); archive = j2sramp.createSrampArchive(); clientAccessor.getClient().uploadBatch(archive); } } finally { SrampArchive.closeQuietly(archive); ZipToSrampArchive.closeQuietly(j2sramp); } }
@Test public void testZipPackage() throws Exception { ArtificerArchive archive = null; InputStream xsd1ContentStream = null; InputStream xsd2ContentStream = null; File zipFile = null; InputStream zipStream = null; ClientRequest request = null; try { // Create a test s-ramp archive archive = new ArtificerArchive(); xsd1ContentStream = this.getClass().getResourceAsStream("/sample-files/xsd/PO.xsd"); BaseArtifactType metaData = new XsdDocument(); metaData.setArtifactType(BaseArtifactEnum.XSD_DOCUMENT); metaData.setName("PO.xsd"); archive.addEntry("schemas/PO.xsd", metaData, xsd1ContentStream); xsd2ContentStream = this.getClass().getResourceAsStream("/sample-files/xsd/XMLSchema.xsd"); metaData = new XsdDocument(); metaData.setArtifactType(BaseArtifactEnum.XSD_DOCUMENT); metaData.setName("XMLSchema.xsd"); metaData.setVersion("1.0"); archive.addEntry("schemas/XMLSchema.xsd", metaData, xsd2ContentStream); zipFile = archive.pack(); zipStream = FileUtils.openInputStream(zipFile); // Now POST the archive to the s-ramp repository (POST to /s-ramp as application/zip) request = clientRequest("/s-ramp"); request.body(MediaType.APPLICATION_ZIP, zipStream); ClientResponse<MultipartInput> clientResponse = request.post(MultipartInput.class); // Process the response - it should be multipart/mixed with each part being // itself an http response with a code, content-id, and an s-ramp atom entry // body MultipartInput response = clientResponse.getEntity(); List<InputPart> parts = response.getParts(); Map<String, BaseArtifactType> artyMap = new HashMap<String, BaseArtifactType>(); for (InputPart part : parts) { String id = part.getHeaders().getFirst("Content-ID"); HttpResponseBean rbean = part.getBody(HttpResponseBean.class, null); Assert.assertEquals(201, rbean.getCode()); Entry entry = (Entry) rbean.getBody(); BaseArtifactType artifact = ArtificerAtomUtils.unwrapSrampArtifact(entry); artyMap.put(id, artifact); } Assert.assertTrue(artyMap.keySet().contains("<schemas/PO.xsd@package>")); Assert.assertTrue(artyMap.keySet().contains("<schemas/XMLSchema.xsd@package>")); // Assertions for artifact 1 BaseArtifactType arty = artyMap.get("<schemas/PO.xsd@package>"); Assert.assertNotNull(arty); Assert.assertEquals("PO.xsd", arty.getName()); Assert.assertNull(arty.getVersion()); arty = artyMap.get("<schemas/XMLSchema.xsd@package>"); Assert.assertNotNull(arty); Assert.assertEquals("XMLSchema.xsd", arty.getName()); Assert.assertEquals("1.0", arty.getVersion()); } finally { IOUtils.closeQuietly(xsd1ContentStream); IOUtils.closeQuietly(xsd2ContentStream); ArtificerArchive.closeQuietly(archive); IOUtils.closeQuietly(zipStream); FileUtils.deleteQuietly(zipFile); } // Verify by querying // Do a query using GET with query params request = clientRequest("/s-ramp/xsd/XsdDocument"); ClientResponse<Feed> response = request.get(Feed.class); Feed feed = response.getEntity(); Assert.assertEquals(2, feed.getEntries().size()); Set<String> artyNames = new HashSet<String>(); for (Entry entry : feed.getEntries()) { artyNames.add(entry.getTitle()); } Assert.assertTrue(artyNames.contains("PO.xsd")); Assert.assertTrue(artyNames.contains("XMLSchema.xsd")); }
/** * This also tests the zipPackage method of the {@link * org.artificer.server.atom.services.BatchResource} class, but it is more thorough. It tests * adding new content, updating existing content, etc. */ @Test public void testZipPackage_Multi() throws Exception { ArtificerArchive archive = null; InputStream xsd1ContentStream = null; InputStream wsdlContentStream = null; File zipFile = null; InputStream zipStream = null; ClientRequest request = null; WsdlDocument wsdlDoc = createWsdlArtifact(); XmlDocument xmlDoc = createXmlArtifact(); String xsdUuid = null; String wsdlUuid = null; String xmlUuid = null; try { // Create a test s-ramp archive archive = new ArtificerArchive(); // A new XSD document xsd1ContentStream = this.getClass().getResourceAsStream("/sample-files/xsd/PO.xsd"); BaseArtifactType metaData = new XsdDocument(); metaData.setArtifactType(BaseArtifactEnum.XSD_DOCUMENT); metaData.setUuid(UUID.randomUUID().toString()); // will be ignored metaData.setName("PO.xsd"); archive.addEntry("schemas/PO.xsd", metaData, xsd1ContentStream); // Update an existing WSDL document (content and meta-data) wsdlContentStream = this.getClass().getResourceAsStream("/sample-files/wsdl/sample-updated.wsdl"); metaData = wsdlDoc; metaData.setVersion("2.0"); ArtificerModelUtils.setCustomProperty(metaData, "foo", "bar"); archive.addEntry("wsdl/sample.wsdl", metaData, wsdlContentStream); // Update an existing XML document (meta-data only) metaData = xmlDoc; metaData.setVersion("3.0"); ArtificerModelUtils.setCustomProperty(metaData, "far", "baz"); archive.addEntry("core/PO.xml", metaData, null); zipFile = archive.pack(); zipStream = FileUtils.openInputStream(zipFile); // Now POST the archive to the s-ramp repository (POST to /s-ramp as application/zip) request = clientRequest("/s-ramp"); request.body(MediaType.APPLICATION_ZIP, zipStream); ClientResponse<MultipartInput> clientResponse = request.post(MultipartInput.class); // Process the response - it should be multipart/mixed with each part being // itself an http response with a code, content-id, and an s-ramp atom entry // body MultipartInput response = clientResponse.getEntity(); List<InputPart> parts = response.getParts(); Map<String, HttpResponseBean> respMap = new HashMap<String, HttpResponseBean>(); for (InputPart part : parts) { String id = part.getHeaders().getFirst("Content-ID"); HttpResponseBean rbean = part.getBody(HttpResponseBean.class, null); respMap.put(id, rbean); } // Should be three responses. Assert.assertEquals(3, respMap.size()); Assert.assertTrue(respMap.keySet().contains("<schemas/PO.xsd@package>")); Assert.assertTrue(respMap.keySet().contains("<wsdl/sample.wsdl@package>")); Assert.assertTrue(respMap.keySet().contains("<core/PO.xml@package>")); // Assertions for artifact 1 (PO.xsd) HttpResponseBean httpResp = respMap.get("<schemas/PO.xsd@package>"); Assert.assertEquals(201, httpResp.getCode()); Assert.assertEquals("Created", httpResp.getStatus()); Entry entry = (Entry) httpResp.getBody(); BaseArtifactType artifact = ArtificerAtomUtils.unwrapSrampArtifact(entry); Assert.assertEquals("PO.xsd", artifact.getName()); Assert.assertNull(artifact.getVersion()); Long size = ((XsdDocument) artifact).getContentSize(); Assert.assertTrue(size >= 2376L); xsdUuid = artifact.getUuid(); // Assertions for artifact 2 (sample.wsdl) httpResp = respMap.get("<wsdl/sample.wsdl@package>"); Assert.assertEquals(200, httpResp.getCode()); Assert.assertEquals("OK", httpResp.getStatus()); entry = (Entry) httpResp.getBody(); artifact = ArtificerAtomUtils.unwrapSrampArtifact(entry); Assert.assertEquals("sample.wsdl", artifact.getName()); Assert.assertEquals("2.0", artifact.getVersion()); wsdlUuid = artifact.getUuid(); // Assertions for artifact 3 (PO.xml) httpResp = respMap.get("<core/PO.xml@package>"); Assert.assertEquals(200, httpResp.getCode()); Assert.assertEquals("OK", httpResp.getStatus()); entry = (Entry) httpResp.getBody(); artifact = ArtificerAtomUtils.unwrapSrampArtifact(entry); Assert.assertEquals("PO.xml", artifact.getName()); Assert.assertEquals("3.0", artifact.getVersion()); xmlUuid = artifact.getUuid(); } finally { IOUtils.closeQuietly(xsd1ContentStream); IOUtils.closeQuietly(wsdlContentStream); ArtificerArchive.closeQuietly(archive); IOUtils.closeQuietly(zipStream); FileUtils.deleteQuietly(zipFile); } // Verify by querying // Do a query using GET with query params Map<String, BaseArtifactType> artyMap = new HashMap<String, BaseArtifactType>(); request = clientRequest("/s-ramp/xsd/XsdDocument"); ClientResponse<Feed> response = request.get(Feed.class); Feed feed = response.getEntity(); Assert.assertEquals(1, feed.getEntries().size()); for (Entry entry : feed.getEntries()) { String uuid = entry.getId().toString().replace("urn:uuid:", ""); request = clientRequest("/s-ramp/xsd/XsdDocument/" + uuid); BaseArtifactType artifact = ArtificerAtomUtils.unwrapSrampArtifact(request.get(Entry.class).getEntity()); artyMap.put(artifact.getUuid(), artifact); } request = clientRequest("/s-ramp/wsdl/WsdlDocument"); response = request.get(Feed.class); feed = response.getEntity(); Assert.assertEquals(1, feed.getEntries().size()); for (Entry entry : feed.getEntries()) { String uuid = entry.getId().toString().replace("urn:uuid:", ""); request = clientRequest("/s-ramp/wsdl/WsdlDocument/" + uuid); BaseArtifactType artifact = ArtificerAtomUtils.unwrapSrampArtifact(request.get(Entry.class).getEntity()); artyMap.put(artifact.getUuid(), artifact); } request = clientRequest("/s-ramp/core/XmlDocument"); response = request.get(Feed.class); feed = response.getEntity(); Assert.assertEquals(1, feed.getEntries().size()); for (Entry entry : feed.getEntries()) { String uuid = entry.getId().toString().replace("urn:uuid:", ""); request = clientRequest("/s-ramp/core/XmlDocument/" + uuid); BaseArtifactType artifact = ArtificerAtomUtils.unwrapSrampArtifact(request.get(Entry.class).getEntity()); artyMap.put(artifact.getUuid(), artifact); } Assert.assertEquals(3, artyMap.size()); // Assertions for artifact 1 (PO.xsd) BaseArtifactType artifact = artyMap.get(xsdUuid); Assert.assertEquals("PO.xsd", artifact.getName()); Assert.assertNull(artifact.getVersion()); // Assertions for artifact 2 (sample.wsdl) artifact = artyMap.get(wsdlUuid); Assert.assertEquals("sample.wsdl", artifact.getName()); Assert.assertEquals("2.0", artifact.getVersion()); // Assertions for artifact 3 (PO.xml) artifact = artyMap.get(xmlUuid); Assert.assertEquals("PO.xml", artifact.getName()); Assert.assertEquals("3.0", artifact.getVersion()); }
/** * Puts the artifact into the s-ramp repository. * * @param gavInfo * @param resourceInputStream * @throws TransferFailedException */ private void doPutArtifact(final MavenGavInfo gavInfo, InputStream resourceInputStream) throws TransferFailedException { // See the comment in {@link SrampWagon#fillInputData(InputData)} about why we're doing this // context classloader magic. ClassLoader oldCtxCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(SrampWagon.class.getClassLoader()); File tempResourceFile = null; ZipToSrampArchive expander = null; SrampArchive archive = null; BaseArtifactType artifactGrouping = null; try { // First, stash the content in a temp file - we may need it multiple times. tempResourceFile = stashResourceContent(resourceInputStream); resourceInputStream = FileUtils.openInputStream(tempResourceFile); ArchiveInfo archiveInfo = ZipToSrampArchiveRegistry.inspectArchive(resourceInputStream); ArtifactType artifactType = getArtifactType(gavInfo, archiveInfo.type); resourceInputStream = FileUtils.openInputStream(tempResourceFile); // Is the artifact grouping option enabled? if (isPrimaryArtifact(gavInfo) && getParamFromRepositoryUrl("artifactGrouping") != null) { // $NON-NLS-1$ artifactGrouping = ensureArtifactGrouping(); } // Only search for existing artifacts by GAV info here BaseArtifactType artifact = findExistingArtifactByGAV(client, gavInfo); // If we found an artifact, we should update its content. If not, we should upload // the artifact to the repository. if (artifact != null) { throw new TransferFailedException( Messages.i18n.format( "ARTIFACT_UPDATE_NOT_ALLOWED", gavInfo.getFullName())); // $NON-NLS-1$ } else { // Upload the content, then add the maven properties to the artifact // as meta-data artifact = client.uploadArtifact(artifactType, resourceInputStream, gavInfo.getName()); SrampModelUtils.setCustomProperty( artifact, "maven.groupId", gavInfo.getGroupId()); // $NON-NLS-1$ SrampModelUtils.setCustomProperty( artifact, "maven.artifactId", gavInfo.getArtifactId()); // $NON-NLS-1$ SrampModelUtils.setCustomProperty( artifact, "maven.version", gavInfo.getVersion()); // $NON-NLS-1$ artifact.setVersion(gavInfo.getVersion()); if (gavInfo.getClassifier() != null) { SrampModelUtils.setCustomProperty( artifact, "maven.classifier", gavInfo.getClassifier()); // $NON-NLS-1$ } if (gavInfo.getSnapshotId() != null && !gavInfo.getSnapshotId().equals("")) { // $NON-NLS-1$ SrampModelUtils.setCustomProperty( artifact, "maven.snapshot.id", gavInfo.getSnapshotId()); // $NON-NLS-1$ } SrampModelUtils.setCustomProperty(artifact, "maven.type", gavInfo.getType()); // $NON-NLS-1$ // Also create a relationship to the artifact grouping, if necessary if (artifactGrouping != null) { SrampModelUtils.addGenericRelationship( artifact, "groupedBy", artifactGrouping.getUuid()); // $NON-NLS-1$ SrampModelUtils.addGenericRelationship( artifactGrouping, "groups", artifact.getUuid()); // $NON-NLS-1$ client.updateArtifactMetaData(artifactGrouping); } client.updateArtifactMetaData(artifact); this.archive.addEntry(gavInfo.getFullName(), artifact, null); } // Now also add "expanded" content to the s-ramp repository expander = ZipToSrampArchiveRegistry.createExpander(artifactType, tempResourceFile); if (expander != null) { expander.setContextParam(DefaultMetaDataFactory.PARENT_UUID, artifact.getUuid()); expander.addMetaDataProvider( new MetaDataProvider() { @Override public void provideMetaData(BaseArtifactType artifact) { SrampModelUtils.setCustomProperty( artifact, "maven.parent-groupId", gavInfo.getGroupId()); // $NON-NLS-1$ SrampModelUtils.setCustomProperty( artifact, "maven.parent-artifactId", gavInfo.getArtifactId()); // $NON-NLS-1$ SrampModelUtils.setCustomProperty( artifact, "maven.parent-version", gavInfo.getVersion()); // $NON-NLS-1$ SrampModelUtils.setCustomProperty( artifact, "maven.parent-type", gavInfo.getType()); // $NON-NLS-1$ } }); archive = expander.createSrampArchive(); client.uploadBatch(archive); } } catch (Throwable t) { throw new TransferFailedException(t.getMessage(), t); } finally { Thread.currentThread().setContextClassLoader(oldCtxCL); SrampArchive.closeQuietly(archive); ZipToSrampArchive.closeQuietly(expander); FileUtils.deleteQuietly(tempResourceFile); } }