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 testGetBooks() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookstore/books/feed"; Feed feed = getFeed(endpointAddress, null); assertEquals( "http://localhost:" + PORT + "/bookstore/bookstore/books/feed", feed.getBaseUri().toString()); assertEquals("Collection of Books", feed.getTitle()); getAndCompareJson( "http://localhost:" + PORT + "/bookstore/bookstore/books/feed", "resources/expected_atom_books_json.txt", "application/json"); getAndCompareJson( "http://localhost:" + PORT + "/bookstore/bookstore/books/jsonfeed", "resources/expected_atom_books_jsonfeed.txt", "application/json, text/html, application/xml;q=0.9," + " application/xhtml+xml, image/png, image/jpeg, image/gif," + " image/x-xbitmap, */*;q=0.1"); Entry entry = addEntry(endpointAddress); entry = addEntry(endpointAddress + "/relative"); endpointAddress = "http://localhost:" + PORT + "/bookstore/bookstore/books/subresources/123"; entry = getEntry(endpointAddress, null); assertEquals("CXF in Action", entry.getTitle()); getAndCompareJson( "http://localhost:" + PORT + "/bookstore/bookstore/books/entries/123", "resources/expected_atom_book_json.txt", "application/json"); getAndCompareJson( "http://localhost:" + PORT + "/bookstore/bookstore/books/entries/123?_type=" + "application/json", "resources/expected_atom_book_json.txt", "*/*"); getAndCompareJson( "http://localhost:" + PORT + "/bookstore/bookstore/books/entries/123?_type=" + "json", "resources/expected_atom_book_json.txt", "*/*"); // do the same using extension mappings getAndCompareJson( "http://localhost:" + PORT + "/bookstore/bookstore/books/entries/123.json", "resources/expected_atom_book_json.txt", "*/*"); // do the same using extension mappings & matrix parameters getAndCompareJson( "http://localhost:" + PORT + "/bookstore/bookstore/books/entries/123.json;a=b", "resources/expected_atom_book_json_matrix.txt", "*/*"); }
@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")); }
@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); }