}
@Test @RunAsClient
public void testUpdatePackageFromJAXB(@ArquillianResource URL baseURL) throws Exception {
//create a package for fixtures
Package p = createTestPackage("testUpdatePackageFromJAXB");
p.setDescription("desc for testUpdatePackageFromJAXB");
p.getMetadata().setCheckinComment("checkincomment for testUpdatePackageFromJAXB");
JAXBContext context = JAXBContext.newInstance(p.getClass());
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(p, sw);
String xml = sw.toString();
URL url = new URL(baseURL, "rest/packages");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", MediaType.APPLICATION_XML);
connection.setRequestProperty("Content-Length", Integer.toString(xml.getBytes().length));
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (xml);
wr.flush ();
wr.close ();
assertEquals (200, connection.getResponseCode());
assertEquals(MediaType.APPLICATION_XML, connection.getContentType());
//System.out.println(IOUtils.toString(connection.getInputStream()));
Package result = unmarshalPackageXML(connection.getInputStream());
assertEquals("testUpdatePackageFromJAXB", result.getTitle());
assertEquals("desc for testUpdatePackageFromJAXB", result.getDescription());
assertNotNull(result.getPublished());
assertEquals(new URL(baseURL, "rest/packages/testUpdatePackageFromJAXB/source").toExternalForm(), result.getSourceLink().toString());
assertEquals(new URL(baseURL, "rest/packages/testUpdatePackageFromJAXB/binary").toExternalForm(), result.getBinaryLink().toString());
PackageMetadata pm = result.getMetadata();
assertFalse(pm.isArchived());
assertNotNull(pm.getCreated());
assertNotNull(pm.getUuid());
assertEquals("checkincomment for testUpdatePackageFromJAXB", pm.getCheckinComment());
//Test update package
Package p2 = createTestPackage("testUpdatePackageFromJAXB");
p2.setDescription("update package testUpdatePackageFromJAXB");
PackageMetadata meta = new PackageMetadata();
meta.setCheckinComment("checkInComment: update package testUpdatePackageFromJAXB");
p2.setMetadata(meta);
JAXBContext context2 = JAXBContext.newInstance(p2.getClass());
Marshaller marshaller2 = context2.createMarshaller();
StringWriter sw2 = new StringWriter();
marshaller2.marshal(p2, sw2);
String xml2 = sw2.toString();
URL url2 = new URL(baseURL, "rest/packages/testUpdatePackageFromJAXB");
HttpURLConnection connection2 = (HttpURLConnection)url2.openConnection();
connection2.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
connection2.setRequestMethod("PUT");
connection2.setRequestProperty("Content-Type", MediaType.APPLICATION_XML);
connection2.setRequestProperty("Content-Length", Integer.toString(xml2.getBytes().length));
connection2.setUseCaches (false);
//connection2.setDoInput(true);
connection2.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection2.getOutputStream());
out.write(xml2);
out.close();
connection2.getInputStream();
//Verify
URL url3 = new URL(baseURL, "rest/packages/testUpdatePackageFromJAXB");
HttpURLConnection connection3 = (HttpURLConnection)url3.openConnection();
connection3.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
connection3.setRequestMethod("GET");
connection3.setRequestProperty("Accept", MediaType.APPLICATION_XML);
connection3.connect();
assertEquals (200, connection3.getResponseCode());
assertEquals(MediaType.APPLICATION_XML, connection3.getContentType());
//System.out.println("------------------------");
//System.out.println(IOUtils.toString(connection.getInputStream()));
Package p3 = unmarshalPackageXML(connection3.getInputStream());
assertEquals("testUpdatePackageFromJAXB", p3.getTitle());
assertEquals("update package testUpdatePackageFromJAXB", p3.getDescription());
assertNotNull(p3.getPublished());
assertEquals(new URL(baseURL, "rest/packages/testUpdatePackageFromJAXB/source").toExternalForm(), p3.getSourceLink().toString());
assertEquals(new URL(baseURL, "rest/packages/testUpdatePackageFromJAXB/binary").toExternalForm(), p3.getBinaryLink().toString());
PackageMetadata pm3 = p3.getMetadata();
assertFalse(pm3.isArchived());
assertNotNull(pm3.getCreated());
assertNotNull(pm3.getUuid());
assertEquals("checkInComment: update package testUpdatePackageFromJAXB", pm3.getCheckinComment());
}