@Test @RunAsClient public void testUpdateAssetFromAtomWithStateNotExist(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL + "rest/packages/restPackage1/assets/model1"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty( "Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML); connection.connect(); assertEquals(200, connection.getResponseCode()); assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType()); InputStream in = connection.getInputStream(); assertNotNull(in); Document<Entry> doc = abdera.getParser().parse(in); Entry entry = doc.getRoot(); // Update state ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA); ExtensibleElement stateExtension = metadataExtension.getExtension(Translator.STATE); stateExtension.<Element>getExtension(Translator.VALUE).setText("NonExistState"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty( "Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-type", MediaType.APPLICATION_ATOM_XML); connection.setDoOutput(true); entry.writeTo(connection.getOutputStream()); assertEquals(500, connection.getResponseCode()); connection.disconnect(); }
@Test @RunAsClient public void testGetAssetsAsAtom(@ArquillianResource URL baseURL) throws Exception { AbderaClient client = new AbderaClient(abdera); client.addCredentials( baseURL.toExternalForm(), null, null, new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin")); RequestOptions options = client.getDefaultRequestOptions(); options.setAccept(MediaType.APPLICATION_ATOM_XML); ClientResponse resp = client.get(new URL(baseURL, "rest/packages/restPackage1/assets").toExternalForm(), options); if (resp.getType() != ResponseType.SUCCESS) { fail("Couldn't retrieve assets-> " + resp.getStatus() + ": " + resp.getStatusText()); } // Get the entry element Document<Feed> document = resp.getDocument(); assertEquals(totalAssets, document.getRoot().getEntries().size()); }
private Entry addEntry(String endpointAddress) throws Exception { Entry e = createBookEntry(256, "AtomBook"); StringWriter w = new StringWriter(); e.writeTo(w); PostMethod post = new PostMethod(endpointAddress); post.setRequestEntity(new StringRequestEntity(w.toString(), "application/atom+xml", null)); HttpClient httpclient = new HttpClient(); String location = null; try { int result = httpclient.executeMethod(post); assertEquals(201, result); location = post.getResponseHeader("Location").getValue(); InputStream ins = post.getResponseBodyAsStream(); Document<Entry> entryDoc = abdera.getParser().parse(copyIn(ins)); assertEquals(entryDoc.getRoot().toString(), e.toString()); } finally { post.releaseConnection(); } Entry entry = getEntry(location, null); assertEquals(location, entry.getBaseUri().toString()); assertEquals("AtomBook", entry.getTitle()); return entry; }
@Test public void testNormalOperations() throws Exception { FedoraObjectUIPProcessor processor = new FedoraObjectUIPProcessor(); AccessControlService aclService = mock(AccessControlService.class); when(aclService.hasAccess( any(PID.class), any(AccessGroupSet.class), eq(Permission.editAccessControl))) .thenReturn(true); processor.setAclService(aclService); InputStream entryPart = new FileInputStream(new File("src/test/resources/atompub/metadataUnpublish.xml")); Abdera abdera = new Abdera(); Parser parser = abdera.getParser(); Document<Entry> entryDoc = parser.parse(entryPart); Entry entry = entryDoc.getRoot(); Map<String, org.jdom2.Element> originalMap = new HashMap<String, org.jdom2.Element>(); org.jdom2.Element rdfElement = new org.jdom2.Element("RDF", JDOMNamespaceUtil.RDF_NS); org.jdom2.Element descElement = new org.jdom2.Element("Description", JDOMNamespaceUtil.RDF_NS); rdfElement.addContent(descElement); org.jdom2.Element relElement = new org.jdom2.Element( ContentModelHelper.CDRProperty.isPublished.getPredicate(), JDOMNamespaceUtil.CDR_NS); relElement.setText("yes"); descElement.addContent(relElement); relElement = new org.jdom2.Element( ContentModelHelper.CDRProperty.embargoUntil.getPredicate(), JDOMNamespaceUtil.CDR_ACL_NS); relElement.setText("2013-02-01"); descElement.addContent(relElement); relElement = new org.jdom2.Element( ContentModelHelper.FedoraProperty.hasModel.name(), JDOMNamespaceUtil.FEDORA_MODEL_NS); relElement.setText(ContentModelHelper.Model.SIMPLE.name()); descElement.addContent(relElement); originalMap.put(ContentModelHelper.Datastream.RELS_EXT.getName(), rdfElement); Map<String, org.jdom2.Element> datastreamMap = AtomPubMetadataParserUtil.extractDatastreams(entry); MetadataUIP uip = mock(MetadataUIP.class); when(uip.getPID()).thenReturn(new PID("uuid:test/ACL")); when(uip.getOperation()).thenReturn(UpdateOperation.REPLACE); when(uip.getOriginalData()).thenReturn(originalMap); when(uip.getModifiedData()).thenReturn(originalMap); when(uip.getIncomingData()).thenReturn(datastreamMap); when(uip.getModifiedFiles()).thenReturn(getModifiedFiles(originalMap)); UIPUpdatePipeline pipeline = mock(UIPUpdatePipeline.class); when(pipeline.processUIP(any(UpdateInformationPackage.class))).thenReturn(uip); processor.setPipeline(pipeline); processor.setVirtualDatastreamMap(new HashMap<String, Datastream>()); DigitalObjectManager digitalObjectManager = mock(DigitalObjectManager.class); processor.setDigitalObjectManager(digitalObjectManager); processor.setOperationsMessageSender(mock(OperationsMessageSender.class)); processor.process(uip); }
private Entry getEntry(String endpointAddress, String acceptType) throws Exception { GetMethod get = new GetMethod(endpointAddress); get.setRequestHeader("Content-Type", "*/*"); if (acceptType != null) { get.setRequestHeader("Accept", acceptType); } HttpClient httpClient = new HttpClient(); try { httpClient.executeMethod(get); Document<Entry> doc = abdera.getParser().parse(copyIn(get.getResponseBodyAsStream())); return doc.getRoot(); } finally { get.releaseConnection(); } }
@Test @RunAsClient public void testGetAssetAsAtom(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL, "rest/packages/restPackage1/assets/model1"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty( "Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML); connection.connect(); assertEquals(200, connection.getResponseCode()); assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType()); // System.out.println(getContent(connection)); InputStream in = connection.getInputStream(); assertNotNull(in); Document<Entry> doc = abdera.getParser().parse(in); Entry entry = doc.getRoot(); assertEquals( baseURL.getPath() + "rest/packages/restPackage1/assets/model1", entry.getBaseUri().getPath()); assertEquals("model1", entry.getTitle()); assertNotNull(entry.getPublished()); assertNotNull(entry.getAuthor().getName()); assertEquals("desc for model1", entry.getSummary()); // assertEquals(MediaType.APPLICATION_OCTET_STREAM_TYPE.getType(), // entry.getContentMimeType().getPrimaryType()); assertEquals( baseURL.getPath() + "rest/packages/restPackage1/assets/model1/binary", entry.getContentSrc().getPath()); ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA); ExtensibleElement archivedExtension = metadataExtension.getExtension(Translator.ARCHIVED); assertEquals("false", archivedExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement stateExtension = metadataExtension.getExtension(Translator.STATE); assertEquals("Draft", stateExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement formatExtension = metadataExtension.getExtension(Translator.FORMAT); assertEquals("model.drl", formatExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement uuidExtension = metadataExtension.getExtension(Translator.UUID); assertNotNull(uuidExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement categoryExtension = metadataExtension.getExtension(Translator.CATEGORIES); assertEquals( "AssetPackageResourceTestCategory", categoryExtension.getSimpleExtension(Translator.VALUE)); }
@Test @RunAsClient public void testGetDRLAndDSLAssetsAsAtom(@ArquillianResource URL baseURL) throws Exception { AbderaClient client = new AbderaClient(abdera); client.addCredentials( baseURL.toExternalForm(), null, null, new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin")); RequestOptions options = client.getDefaultRequestOptions(); options.setAccept(MediaType.APPLICATION_ATOM_XML); ClientResponse resp = client.get( new URL(baseURL, "rest/packages/restPackage1/assets?format=drl&format=dsl") .toExternalForm(), options); if (resp.getType() != ResponseType.SUCCESS) { fail( "Couldn't retrieve DRL and DSL assets-> " + resp.getStatus() + ": " + resp.getStatusText()); } // Get the entry element Document<Feed> document = resp.getDocument(); // Check number of results assertEquals(3, document.getRoot().getEntries().size()); // Check assets names List<String> assetNames = new ArrayList<String>(); for (Entry entry : document.getRoot().getEntries()) { assetNames.add(entry.getTitle()); } assertTrue(assetNames.contains("rule1")); assertTrue(assetNames.contains("rule4")); assertTrue(assetNames.contains("myDSL")); }
@Override public Feed transform(final String feed) throws TransformerException { final StringReader reader = new StringReader(feed); final Document<Feed> feedDoc = Abdera.getNewParserFactory().getParser().parse(reader); return feedDoc.getRoot(); }
@Test @RunAsClient public void testCreateAssetFromAtom(@ArquillianResource URL baseURL) throws Exception { // Check there is no model1-New asset AbderaClient client = new AbderaClient(abdera); client.addCredentials( baseURL.toExternalForm(), null, null, new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin")); RequestOptions options = client.getDefaultRequestOptions(); options.setAccept(MediaType.APPLICATION_ATOM_XML); ClientResponse resp = client.get( new URL(baseURL, "rest/packages/restPackage1/assets/model1-New").toExternalForm()); // If the asset doesn't exist, an HTTP 500 Error is expected. :S if (resp.getType() != ResponseType.SERVER_ERROR) { fail( "I was expecting an HTTP 500 Error because 'model1-New' shouldn't exist. " + "Instead of that I got-> " + resp.getStatus() + ": " + resp.getStatusText()); } // -------------------------------------------------------------- // Get asset 'model1' from Guvnor client = new AbderaClient(abdera); client.addCredentials( baseURL.toExternalForm(), null, null, new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin")); options = client.getDefaultRequestOptions(); options.setAccept(MediaType.APPLICATION_ATOM_XML); resp = client.get(new URL(baseURL, "rest/packages/restPackage1/assets/model1").toExternalForm()); if (resp.getType() != ResponseType.SUCCESS) { fail("Couldn't retrieve 'model1' asset-> " + resp.getStatus() + ": " + resp.getStatusText()); } // Get the entry element Document<Entry> doc = resp.getDocument(); Entry entry = doc.getRoot(); // -------------------------------------------------------------- // Change the title of the asset entry.setTitle(entry.getTitle() + "-New"); // Save it as a new Asset client = new AbderaClient(abdera); client.addCredentials( baseURL.toExternalForm(), null, null, new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin")); options = client.getDefaultRequestOptions(); options.setContentType(MediaType.APPLICATION_ATOM_XML); resp = client.post( new URL(baseURL, "rest/packages/restPackage1/assets").toExternalForm(), entry, options); if (resp.getType() != ResponseType.SUCCESS) { fail("Couldn't store 'model1-New' asset-> " + resp.getStatus() + ": " + resp.getStatusText()); } // -------------------------------------------------------------- // Check that the new asset is in the repository client = new AbderaClient(abdera); client.addCredentials( baseURL.toExternalForm(), null, null, new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin")); options = client.getDefaultRequestOptions(); options.setAccept(MediaType.APPLICATION_ATOM_XML); resp = client.get( new URL(baseURL, "rest/packages/restPackage1/assets/model1-New").toExternalForm()); if (resp.getType() != ResponseType.SUCCESS) { fail( "Couldn't retrieve 'model1-New' asset-> " + resp.getStatus() + ": " + resp.getStatusText()); } // Get the entry element doc = resp.getDocument(); entry = doc.getRoot(); // Compare the title :P assertEquals(entry.getTitle(), "model1-New"); }
@Test @RunAsClient public void testUpdateAssetFromAtom(@ArquillianResource URL baseURL) throws Exception { URL url = new URL(baseURL + "rest/packages/restPackage1/assets/model1"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty( "Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML); connection.connect(); assertEquals(200, connection.getResponseCode()); assertEquals(MediaType.APPLICATION_ATOM_XML, connection.getContentType()); // System.out.println(GetContent(connection)); InputStream in = connection.getInputStream(); assertNotNull(in); Document<Entry> doc = abdera.getParser().parse(in); Entry entry = doc.getRoot(); assertEquals( baseURL.getPath() + "rest/packages/restPackage1/assets/model1", entry.getBaseUri().getPath()); assertEquals("model1", entry.getTitle()); assertNotNull(entry.getPublished()); assertNotNull(entry.getAuthor().getName()); assertEquals("desc for model1", entry.getSummary()); // assertEquals(MediaType.APPLICATION_OCTET_STREAM_TYPE.getType(), // entry.getContentMimeType().getPrimaryType()); assertEquals( baseURL.getPath() + "rest/packages/restPackage1/assets/model1/binary", entry.getContentSrc().getPath()); ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA); ExtensibleElement archivedExtension = metadataExtension.getExtension(Translator.ARCHIVED); assertEquals("false", archivedExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement stateExtension = metadataExtension.getExtension(Translator.STATE); assertEquals("Draft", stateExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement formatExtension = metadataExtension.getExtension(Translator.FORMAT); assertEquals("model.drl", formatExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement uuidExtension = metadataExtension.getExtension(Translator.UUID); assertNotNull(uuidExtension.getSimpleExtension(Translator.VALUE)); ExtensibleElement categoryExtension = metadataExtension.getExtension(Translator.CATEGORIES); assertEquals( "AssetPackageResourceTestCategory", categoryExtension.getSimpleExtension(Translator.VALUE)); connection.disconnect(); // Update category. Add a new category tag categoryExtension.addSimpleExtension(Translator.VALUE, "AssetPackageResourceTestCategory2"); // Update state stateExtension.<Element>getExtension(Translator.VALUE).setText("Dev"); // Update format formatExtension.<Element>getExtension(Translator.VALUE).setText("anotherformat"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty( "Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-type", MediaType.APPLICATION_ATOM_XML); connection.setDoOutput(true); entry.writeTo(connection.getOutputStream()); assertEquals(204, connection.getResponseCode()); connection.disconnect(); // Verify again connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty( "Authorization", "Basic " + new Base64().encodeToString(("admin:admin".getBytes()))); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML); connection.connect(); assertEquals(200, connection.getResponseCode()); // System.out.println(GetContent(connection)); in = connection.getInputStream(); assertNotNull(in); doc = abdera.getParser().parse(in); entry = doc.getRoot(); metadataExtension = entry.getExtension(Translator.METADATA); archivedExtension = metadataExtension.getExtension(Translator.ARCHIVED); assertEquals("false", archivedExtension.getSimpleExtension(Translator.VALUE)); stateExtension = metadataExtension.getExtension(Translator.STATE); assertEquals("Dev", stateExtension.getSimpleExtension(Translator.VALUE)); formatExtension = metadataExtension.getExtension(Translator.FORMAT); assertEquals("anotherformat", formatExtension.getSimpleExtension(Translator.VALUE)); categoryExtension = metadataExtension.getExtension(Translator.CATEGORIES); List<Element> categoryValues = categoryExtension.getExtensions(Translator.VALUE); assertTrue(categoryValues.size() == 2); boolean foundCategory1 = false; boolean foundCategory2 = false; for (Element cat : categoryValues) { String catgoryValue = cat.getText(); if ("AssetPackageResourceTestCategory".equals(catgoryValue)) { foundCategory1 = true; } if ("AssetPackageResourceTestCategory2".equals(catgoryValue)) { foundCategory2 = true; } } assertTrue(foundCategory1); assertTrue(foundCategory2); }
public static void main(String[] args) throws Exception { Abdera abdera = new Abdera(); AbderaClient abderaClient = new AbderaClient(abdera); Factory factory = abdera.getFactory(); // Perform introspection. This is an optional step. If you already // know the URI of the APP collection to POST to, you can skip it. Document<Service> introspection = abderaClient.get("http://localhost:9080/EmployeeService/").getDocument(); Service service = introspection.getRoot(); Collection collection = service.getCollection("Employee Directory Workspace", "itc Employee Database"); report("The Collection Element", collection.toString()); // Create the entry to post to the collection Entry entry = factory.newEntry(); entry.setId("tag:example.org,2006:foo"); entry.setTitle("This is the title"); entry.setUpdated(new AtomDate().getValue()); entry.setPublished(new AtomDate().getValue()); entry.addLink("/employee"); entry.addAuthor("James"); entry.setContent("This is the content"); report("The Entry to Post", entry.toString()); // Post the entry. Be sure to grab the resolved HREF of the collection Document<Entry> doc = abderaClient.post(collection.getResolvedHref().toString(), entry).getDocument(); // In some implementations (such as Google's GData API, the entry URI is // distinct from it's edit URI. To be safe, we should assume it may be // different IRI entryUri = doc.getBaseUri(); report("The Created Entry", doc.getRoot().toString()); // Grab the Edit URI from the entry. The entry MAY have more than one // edit link. We need to make sure we grab the right one. IRI editUri = getEditUri(doc.getRoot()); // If there is an Edit Link, we can edit the entry if (editUri != null) { // Before we can edit, we need to grab an "editable" representation doc = abderaClient.get(editUri.toString()).getDocument(); // Change whatever you want in the retrieved entry // doc.getRoot().getTitleElement().setValue("This is the changed title"); // Put it back to the server abderaClient.put(editUri.toString(), doc.getRoot()); // This is just to show that the entry has been modified doc = abderaClient.get(entryUri.toString()).getDocument(); report("The Modified Entry", doc.getRoot().toString()); } else { // Otherwise, the entry cannot be modified (no suitable edit link was found) report("The Entry cannot be modified", null); } // Delete the entry. Again, we need to make sure that we have the current // edit link for the entry doc = abderaClient.get(entryUri.toString()).getDocument(); editUri = getEditUri(doc.getRoot()); if (editUri != null) { abderaClient.delete(editUri.toString()); report("The Enry has been deleted", null); } else { report("The Entry cannot be deleted", null); } }