/*
* UsersTest.java
* JUnit based test
*
* Created on April 15, 2007, 8:52 AM
*/
package test.admin;
import java.io.File;
import org.atomojo.app.client.XMLRepresentationParser;
import org.infoset.xml.Document;
import org.infoset.xml.Element;
import org.infoset.xml.Name;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.representation.StringRepresentation;
import test.util.APPTestCase;
import test.util.TestConfig;
/**
*
* @author alex
*/
public class UsersTest extends APPTestCase
{
TestConfig config;
Reference adminUserLoc;
public UsersTest(String testName)
{
super(testName);
}
protected void setUp() throws Exception
{
File dir = TestConfig.getTestDir("users");
dir.delete();
config = new TestConfig("users","localhost","localhost",8080);
config.startServer();
adminUserLoc = new Reference(config.getServerLocation().toString()+"admin/users/");
adminUserLoc = adminUserLoc.normalize();
}
protected void tearDown() throws Exception
{
config.stopServer();
}
public void testUsers() {
Client client = new Client(new Context(),adminUserLoc.getSchemeProtocol()) {
public void handle(Request request,Response response) {
request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,"admin","admin"));
super.handle(request,response);
}
};
Response response = client.handle(makePost(adminUserLoc,new StringRepresentation("<user xmlns='http://www.atomojo.org/Vocabulary/Admin/2007/1/0' alias='testuser.1' password='mypassword.1'/>",MediaType.APPLICATION_XML)));
assertTrue(response.getStatus().isSuccess());
Reference testUser1Loc = new Reference(adminUserLoc.toString()+"testuser.1");
response = client.handle(makeGet(testUser1Loc));
assertTrue(response.getStatus().isSuccess());
try {
XMLRepresentationParser parser = new XMLRepresentationParser();
Document doc = parser.load(response.getEntity());
Element top = doc.getDocumentElement();
assertTrue("testuser.1".equals(top.getAttributeValue("alias")));
} catch (Exception ex) {
fail("Exception on get user testuser.1 response: "+ex.getMessage());
}
response = client.handle(makePost(adminUserLoc,new StringRepresentation("<user xmlns='http://www.atomojo.org/Vocabulary/Admin/2007/1/0' alias='testuser.2' password='mypassword.2'/>",MediaType.APPLICATION_XML)));
assertTrue(response.getStatus().isSuccess());
Reference testUser2Loc = new Reference(adminUserLoc.toString()+"testuser.2");
response = client.handle(makeGet(testUser2Loc));
assertTrue(response.getStatus().isSuccess());
try {
XMLRepresentationParser parser = new XMLRepresentationParser();
Document doc = parser.load(response.getEntity());
Element top = doc.getDocumentElement();
assertTrue("testuser.2".equals(top.getAttributeValue("alias")));
} catch (Exception ex) {
fail("I/O exception on get user testuser.1 response: "+ex.getMessage());
}
response = client.handle(makePost(testUser1Loc,new StringRepresentation("<user xmlns='http://www.atomojo.org/Vocabulary/Admin/2007/1/0' alias='testuser.1' password='mypassword.1.changed'><name>Test User</name></user>",MediaType.APPLICATION_XML)));
assertTrue(response.getStatus().isSuccess());
response = client.handle(makeGet(testUser1Loc));
assertTrue(response.getStatus().isSuccess());
try {
XMLRepresentationParser parser = new XMLRepresentationParser();
Document doc = parser.load(response.getEntity());
Element top = doc.getDocumentElement();
assertTrue("Test User".equals(top.getFirstElementNamed(Name.create("{http://www.atomojo.org/Vocabulary/Admin/2007/1/0}name")).getText()));
} catch (Exception ex) {
fail("Exception on get user testuser.1 response: "+ex.getMessage());
}
response = client.handle(makeDelete(testUser2Loc));
assertTrue(response.getStatus().isSuccess());
}
}