public void testCreateAndUpdateAndGetBinaryAsset(@ArquillianResource URL baseURL) throws Exception {
//Query if the asset exist
URL url = new URL(baseURL, "rest/packages/restPackage1/assets/Error-image");
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);
byte[] authEncBytes = Base64.encodeBase64("admin:admin".getBytes());
connection.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection.connect();
//The asset should not exist
assertEquals(404, connection.getResponseCode());
//Create the asset from binary
url = new URL(baseURL, "rest/packages/restPackage1/assets");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM);
connection.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
connection.setRequestProperty("Slug", "Error-image.gif");
connection.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection.setDoOutput(true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] data = new byte[1000];
int count = 0;
InputStream is = this.getClass().getResourceAsStream("Error-image.gif");
while((count = is.read(data,0,1000)) != -1) {
out.write(data, 0, count);
}
connection.getOutputStream ().write(out.toByteArray());
out.close();
assertEquals(200, connection.getResponseCode());
//Get the asset meta data and verify
url = new URL(baseURL, "rest/packages/restPackage1/assets/Error-image");
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.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection.connect();
//System.out.println(IOUtils.toString(connection.getInputStream()));
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();
assertEquals("Error-image", entry.getTitle());
ExtensibleElement metadataExtension = entry.getExtension(Translator.METADATA);
ExtensibleElement formatExtension = metadataExtension.getExtension(Translator.FORMAT);
assertEquals("gif", formatExtension.getSimpleExtension(Translator.VALUE));
assertTrue(entry.getPublished() != null);
//Get the asset binary and verify
url = new URL(baseURL, "rest/packages/restPackage1/assets/Error-image/binary");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", MediaType.APPLICATION_OCTET_STREAM);
connection.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection.connect();
//System.out.println(IOUtils.toString(connection.getInputStream()));
assertEquals(200, connection.getResponseCode());
assertEquals(MediaType.APPLICATION_OCTET_STREAM, connection.getContentType());
in = connection.getInputStream();
assertNotNull(in);
//Update asset binary
url = new URL(baseURL, "rest/packages/restPackage1/assets/Error-image/binary");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
connection.setRequestProperty("Content-Type", MediaType.APPLICATION_OCTET_STREAM);
connection.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
byte[] data2 = new byte[1000];
int count2 = 0;
InputStream is2 = this.getClass().getResourceAsStream("Error-image-new.gif");
while((count2 = is2.read(data2,0,1000)) != -1) {
out2.write(data2, 0, count2);
}
connection.getOutputStream ().write(out2.toByteArray());
out2.close();
assertEquals(204, connection.getResponseCode());
//Roll back changes.
url = new URL(baseURL, "rest/packages/restPackage1/assets/Error-image");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(("admin:admin".getBytes())));
connection.setRequestMethod("DELETE");
connection.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection.connect();
assertEquals(204, connection.getResponseCode());
//Verify the package is indeed deleted
url = new URL(baseURL, "rest/packages/restPackage1/assets/Error-image");
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.setRequestProperty("Authorization", "Basic "
+ new String(authEncBytes));
connection.connect();