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("testRenamePackageFromXML", result.getTitle());
assertEquals("desc for testRenamePackageFromXML", result.getDescription());
assertNotNull(result.getPublished());
assertEquals(new URL(baseURL, "rest/packages/testRenamePackageFromXML/source").toExternalForm(), result.getSourceLink().toString());
assertEquals(new URL(baseURL, "rest/packages/testRenamePackageFromXML/binary").toExternalForm(), result.getBinaryLink().toString());
PackageMetadata pm = result.getMetadata();
assertFalse(pm.isArchived());
assertNotNull(pm.getCreated());
assertNotNull(pm.getUuid());
//Test rename package
p.setDescription("renamed package testRenamePackageFromXML");
p.setTitle("testRenamePackageFromXMLNew");
JAXBContext context2 = JAXBContext.newInstance(p.getClass());
Marshaller marshaller2 = context2.createMarshaller();
StringWriter sw2 = new StringWriter();
marshaller2.marshal(p, sw2);
String xml2 = sw2.toString();
URL url2 = new URL(baseURL, "rest/packages/testRenamePackageFromXML");
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();
//assertEquals (200, connection2.getResponseCode());
//Verify the new package is available after renaming
URL url3 = new URL(baseURL, "rest/packages/testRenamePackageFromXMLNew");
HttpURLConnection conn3 = (HttpURLConnection)url3.openConnection();
conn3.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
conn3.setRequestMethod("GET");
conn3.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
conn3.connect();
//System.out.println(GetContent(conn));
assertEquals (200, conn3.getResponseCode());
assertEquals(MediaType.APPLICATION_ATOM_XML, conn3.getContentType());
InputStream in = conn3.getInputStream();
assertNotNull(in);
Document<Entry> doc = abdera.getParser().parse(in);
Entry entry = doc.getRoot();
assertEquals(baseURL.getPath() + "rest/packages/testRenamePackageFromXMLNew", entry.getBaseUri().getPath());
assertEquals("testRenamePackageFromXMLNew", entry.getTitle());
assertTrue(entry.getPublished() != null);
assertEquals("renamed package testRenamePackageFromXML", entry.getSummary());
//Verify the old package does not exist after renaming
URL url4 = new URL(baseURL, "rest/packages/testRenamePackageFromXML");
HttpURLConnection conn4 = (HttpURLConnection)url4.openConnection();
conn4.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
conn4.setRequestMethod("GET");
conn4.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
conn4.connect();
//System.out.println(IOUtils.toString(connection.getInputStream()));
assertEquals (404, conn4.getResponseCode());
//Roll back changes.
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
client.addCredentials(baseURL.toExternalForm(), null, null,
new org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin"));
ClientResponse resp = client.delete(new URL(baseURL, "rest/packages/testRenamePackageFromXMLNew").toExternalForm());
assertEquals(ResponseType.SUCCESS, resp.getType());
//Verify the package is indeed deleted
URL url5 = new URL(baseURL, "rest/packages/testRenamePackageFromXMLNew");
HttpURLConnection conn5 = (HttpURLConnection)url5.openConnection();
conn5.setRequestProperty("Authorization",
"Basic " + new Base64().encodeToString(( "admin:admin".getBytes() )));
conn5.setRequestMethod("GET");
conn5.setRequestProperty("Accept", MediaType.APPLICATION_ATOM_XML);
conn5.connect();
//System.out.println(IOUtils.toString(connection.getInputStream()));
assertEquals (404, conn5.getResponseCode());