// make sure everything is clear before testing
DepartmentDatabase.clearEntries();
// create a new Department
Department newDepartment = new Department();
newDepartment.setDepartmentId("1");
newDepartment.setDepartmentName("Marketing");
JAXBContext context =
JAXBContext.newInstance(new Class<?>[] {Department.class,
DepartmentListWrapper.class});
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(newDepartment, sw);
HttpClient client = new HttpClient();
postMethod = new PostMethod(getBaseURI());
RequestEntity reqEntity =
new ByteArrayRequestEntity(sw.toString().getBytes(), "text/xml");
postMethod.setRequestEntity(reqEntity);
client.executeMethod(postMethod);
newDepartment = new Department();
newDepartment.setDepartmentId("2");
newDepartment.setDepartmentName("Sales");
sw = new StringWriter();
marshaller.marshal(newDepartment, sw);
client = new HttpClient();
postMethod = new PostMethod(getBaseURI());
reqEntity = new ByteArrayRequestEntity(sw.toString().getBytes(), "text/xml");
postMethod.setRequestEntity(reqEntity);
client.executeMethod(postMethod);
// now let's get the list of Departments that we just created
// (should be 2)
client = new HttpClient();
getAllMethod = new GetMethod(getBaseURI());
client.executeMethod(getAllMethod);
byte[] bytes = getAllMethod.getResponseBody();
assertNotNull(bytes);
InputStream bais = new ByteArrayInputStream(bytes);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object obj = unmarshaller.unmarshal(bais);
assertTrue(obj instanceof DepartmentListWrapper);
DepartmentListWrapper wrapper = (DepartmentListWrapper)obj;
List<Department> dptList = wrapper.getDepartmentList();
assertNotNull(dptList);
assertEquals(2, dptList.size());
// now get a specific Department that was created
client = new HttpClient();
getOneMethod = new GetMethod(getBaseURI() + "/1");
client.executeMethod(getOneMethod);
bytes = getOneMethod.getResponseBody();
assertNotNull(bytes);
bais = new ByteArrayInputStream(bytes);
obj = unmarshaller.unmarshal(bais);
assertTrue(obj instanceof Department);
Department dept = (Department)obj;
assertEquals("1", dept.getDepartmentId());
assertEquals("Marketing", dept.getDepartmentName());
// let's send a Head request for both an existent and non-existent
// resource
// we are testing to see if header values being set in the resource
// implementation