|
From: <ian...@us...> - 2007-07-09 01:57:00
|
Revision: 220
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=220&view=rev
Author: iansmith
Date: 2007-07-08 18:57:01 -0700 (Sun, 08 Jul 2007)
Log Message:
-----------
Checkin so others can review my plan for a change to WebAPIClient. Basically, the plan calls for all the code
that actually touches the wire (doing HTTP) to be moved
into WebAPIClientWire (or whatever it should be named).
For now, I left WebAPIClient alone, except for the template create and delete code. I copied some of the HTTP-related code into WebAPIClientWire so readers can get a feel of what should be in there if this change is implemented. You can find examples of using this approach in WebAPIClient by looking for this comment:
/*
* Beginning of new style: IES HACK
*/
The tests still pass, so I believe that I have not broken anything else in WebAPIClient.
In the long term, the objective of this change would be to be able to run most of the WebAPIClient tests without needing an actual server connection, simplifying testing and allowing the tests to be run more frequently!
Further, WebAPIClientWire could be itself tested in isolation without concern for exactly what is passing over the wire (e.g. is it properly formatted XML).
Modified Paths:
--------------
spaces/trunk/src/com/ogoglio/client/WebAPIClient.java
spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java
Modified: spaces/trunk/src/com/ogoglio/client/WebAPIClient.java
===================================================================
--- spaces/trunk/src/com/ogoglio/client/WebAPIClient.java 2007-07-09 00:52:54 UTC (rev 219)
+++ spaces/trunk/src/com/ogoglio/client/WebAPIClient.java 2007-07-09 01:57:01 UTC (rev 220)
@@ -50,6 +50,8 @@
public class WebAPIClient {
+ private WebAPIClientWire wire=new WebAPIClientWire();
+
private URI spaceURI = null;
private URI serviceURI = null;
@@ -69,6 +71,10 @@
}
public WebAPIClient(URI spaceURI, URI serviceURI, String authCookie) throws IOException {
+ this(spaceURI,serviceURI, authCookie,null);
+ }
+
+ public WebAPIClient(URI spaceURI, URI serviceURI, String authCookie, WebAPIClientWire otherWire) throws IOException {
//seems that we have to allow the null spaceURI to indicate that
//the client has no interest in a space
//ArgumentUtils.assertNotNull(spaceURI);
@@ -78,6 +84,10 @@
ArgumentUtils.assertNotNull(authCookie);
this.authCookie = authCookie;
+
+ if (otherWire!=null) {
+ this.wire = otherWire;
+ }
}
public AuthDocument getAuthDocument(boolean cachedOK) throws IOException {
@@ -319,13 +329,27 @@
XMLElement templateXML = postAuthenticatedXML(getTemplateURI(authDocument.getUsername(), templateDoc.getTemplateID()), templateDoc.toString());
return new TemplateDocument(templateXML);
}
-
+ /*
+ * Beginning of new style: IES HACK
+ */
+ public String getAuthUsername() throws IOException {
+ return getAuthDocument(true).getUsername();
+ }
+ public String getAuthCookie() throws IOException {
+ return authCookie;
+ }
public TemplateDocument createTemplate(String templateName) throws IOException {
- TemplateDocument templateDoc = new TemplateDocument(-1, templateName, authDocument.getUsername(), null);
- XMLElement templateXML = postAuthenticatedXML(getTemplatesURI(authDocument.getUsername()), templateDoc.toString());
+ TemplateDocument templateDoc = new TemplateDocument(-1, templateName, getAuthUsername(), null);
+ XMLElement templateXML = wire.postAuthenticatedXML(getTemplatesURI(getAuthUsername()),templateDoc.toString(),
+ getAuthCookie());
return new TemplateDocument(templateXML);
}
-
+ public boolean deleteTemplate(long templateID) throws IOException {
+ return wire.sendDelete(getTemplateURI(getAuthUsername(), templateID),getAuthCookie());
+ }
+ /*
+ * End of new style: IES HACK
+ */
public TemplateDocument getTemplateDocument(String username, long templateID) throws IOException {
return new TemplateDocument(fetchAuthenticatedXML(getTemplateURI(username, templateID)));
}
@@ -547,7 +571,7 @@
}
}
- protected XMLElement postAuthenticatedXML(URI uri, String body) throws IOException {
+ private XMLElement postAuthenticatedXML(URI uri, String body) throws IOException {
return sendAuthenticatedXML(uri, body, "POST", authCookie);
}
@@ -840,7 +864,4 @@
public String toString() {
return "<WebAPI:" + serviceURI + ">";
}
- public boolean deleteTemplate(long templateID) throws IOException {
- return sendDelete(getTemplateURI(getAuthDocument(true).getUsername(), templateID),authCookie);
- }
}
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-07-09 00:52:54 UTC (rev 219)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-07-09 01:57:01 UTC (rev 220)
@@ -13,6 +13,7 @@
import com.agical.rmock.extension.junit.RMockTestCase;
import com.ogoglio.client.WebAPIAuthenticator;
import com.ogoglio.client.WebAPIClient;
+import com.ogoglio.client.WebAPIClientWire;
import com.ogoglio.xml.AuthDocument;
import com.ogoglio.xml.TemplateDocument;
@@ -20,14 +21,20 @@
public SyncTool mockSyncTool;
public WebAPIClient client;
+ public WebAPIClientWire wire;
+ public static URI TMPL_URI;
+
public static final String SVC="http://transmutable.gov",USER="doofus",PW="somepw";
public static final String[] CMD_LINE_ARGS = { USER, PW, SVC};
+ public static final String COOKIE="authorizationStringOfChars";
public void setUp() throws URISyntaxException{
+ wire = (WebAPIClientWire) mock(WebAPIClientWire.class);
mockSyncTool=(SyncTool)mock(SyncTool.class);
client = (WebAPIClient)mock(WebAPIClient.class,
- new Object[]{null,new URI("http://transmutable.com"),"cookie-tastic"},"client");
+ new Object[]{null,new URI("http://transmutable.com"),"cookie-tastic",wire},"client");
+ TMPL_URI=new URI("http://transmutable.gov/templates/foruser/someuser");
}
private List createFakeList(int numberOfFakeItems,String prefix) {
List result = new ArrayList();
@@ -331,7 +338,16 @@
mockSyncTool.addAndDeleteServerTemplates(client);
}
+ public void prepareClientForUsernameAndAuthCheck(int times) throws IOException {
+ client.getAuthUsername();
+ modify().multiplicity(expect.exactly(times));
+ modify().returnValue(USER);
+
+ client.getAuthCookie();
+ modify().returnValue(COOKIE);
+ }
+
public void prepareChildMock(String path, File child, String name) {
child.isDirectory();
modify().returnValue(true);
@@ -342,5 +358,41 @@
child.getName();
modify().returnValue(name);
}
+
+ public void testCreateTemplateUsesPostOnWire() throws IOException, URISyntaxException {
+ String TMPL_NAME ="myTemplate";
+
+ client.createTemplate(TMPL_NAME);
+ modify().forward();
+
+
+ client.getTemplatesURI(USER);
+ modify().returnValue(TMPL_URI);
+
+ prepareClientForUsernameAndAuthCheck(2);
+
+ wire.postAuthenticatedXML(TMPL_URI, "some xml goes here", COOKIE);
+ modify().args(is.AS_RECORDED,is.instanceOf(String.class),is.AS_RECORDED);
+ modify().returnValue(new TemplateDocument(422L,"New Template",USER,"").toElement());
+
+ startVerification();
+ client.createTemplate(TMPL_NAME);
+ }
+ public void testDeleteTemplateUsesDeleteOnWire() throws IOException, URISyntaxException {
+ long TMPL_ID=791L;
+
+ client.deleteTemplate(TMPL_ID);
+ modify().forward();
+
+ client.getTemplateURI(USER, TMPL_ID);
+ modify().returnValue(TMPL_URI);
+
+ prepareClientForUsernameAndAuthCheck(1);
+
+ wire.sendDelete(TMPL_URI, COOKIE);
+
+ startVerification();
+ client.deleteTemplate(TMPL_ID);
+ }
}
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java 2007-07-09 00:52:54 UTC (rev 219)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java 2007-07-09 01:57:01 UTC (rev 220)
@@ -134,8 +134,4 @@
return result;
}
- public void testShouldTestThatCreateAndDeleteClientSideCallsGenerateXMLProperly()
- {
- System.out.println("WARNING: Not testing client side XML generation (TODO)");
- }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|