Permalink
Browse files

Merge remote-tracking branch 'dadoonet/httpcodes' into dadoonet-httpc…

…odes

Conflicts:
	src/main/java/net/linkfluence/jspore/SpecParser.java
	src/test/java/net/linkfluence/jspore/SpecParserTest.java
  • Loading branch information...
2 parents f102532 + 8f14c2d commit 12348b9548fb71173c3e25c45d89f9bc0787e7a1 @nicoo committed Jan 7, 2013
@@ -9,7 +9,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.ning.http.client.RequestBuilder;
-
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
@@ -4,8 +4,7 @@
import com.google.common.base.Strings;
import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
import java.util.Map.Entry;
/**
@@ -17,11 +16,13 @@
public URL baseUrl;
public String version;
public String name;
-
+
+ public final Collection<Integer> expectedStatus;
public final Map<String, Method> routes;
public Model() {
routes = new HashMap<String, Method>();
+ expectedStatus = new HashSet<Integer>();
}
public void addMethod(Method method){
@@ -55,7 +56,9 @@ public void merge(Model m){
if(!Strings.isNullOrEmpty(name)){
this.name = m.name;
}
-
+
+ this.expectedStatus.addAll(m.expectedStatus);
+
for(Entry<String, Method> e : m.routes.entrySet()){
this.routes.put(e.getKey(), e.getValue());
}
@@ -2,16 +2,15 @@
*/
package net.linkfluence.jspore;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
-
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
/**
* Internal spec parser.
@@ -69,13 +68,14 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
throw new InvalidSporeSpecException("Cannot parse input spec stream");
}
Model m = new Model();
- m.version = root.get(VERSION).asText();
- m.baseUrl = new URL(root.get(BASE_URL).asText());
- m.name = root.get(NAME).asText();
-
+ m.version = root.get(VERSION).getTextValue();
+ m.baseUrl = new URL(root.get(BASE_URL).getTextValue());
+ m.name = root.get(NAME).getTextValue();
+ m.expectedStatus.addAll(readIntegerCollection(root.get(EXPECTED_STATUS)));
+
// read all object properties to find method
JsonNode methods = root.get(METHODS);
- Iterator<Entry<String, JsonNode>> nodeIt = methods.fields();
+ Iterator<Entry<String, JsonNode>> nodeIt = methods.getFields();
while(nodeIt.hasNext()){
Entry<String, JsonNode> e = nodeIt.next();
String methodName = e.getKey();
@@ -85,6 +85,10 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
String path = n.get(PATH).asText();
Collection<String> requiredParams = readStringCollection(n.get(REQUIRED_PARAMS));
Collection<Integer> expectedStatuses = readIntegerCollection(n.get(EXPECTED_STATUS));
+ // If no status is defined on a method level, we use the default model expected statuses
+ if (expectedStatuses.isEmpty()) {
+ expectedStatuses.addAll(m.expectedStatus);
+ }
Collection<String> optionalParams = readStringCollection(n.get(OPTIONAL_PARAMS));
Map<String, String> headers = readStringMap(n.get(HEADERS));
boolean authentication = false;
@@ -113,7 +117,7 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
private Collection<String> readStringCollection(JsonNode jsonArray){
Collection<String> ret = new ArrayList<String>();
if(jsonArray != null){
- Iterator<JsonNode> it = jsonArray.elements();
+ Iterator<JsonNode> it = jsonArray.getElements();
while(it.hasNext()){
ret.add(it.next().asText());
}
@@ -124,7 +128,7 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
private Collection<Integer> readIntegerCollection(JsonNode jsonArray){
Collection<Integer> ret = new ArrayList<Integer>();
if(jsonArray != null){
- Iterator<JsonNode> it = jsonArray.elements();
+ Iterator<JsonNode> it = jsonArray.getElements();
while(it.hasNext()){
ret.add(it.next().asInt());
}
@@ -135,7 +139,7 @@ private Model parse(JsonNode root) throws InvalidSporeSpecException, MalformedUR
private Map<String, String> readStringMap(JsonNode jsonMap) {
Map<String, String> map = new HashMap<String, String>();
if(jsonMap != null){
- Iterator<Entry<String, JsonNode>> mapIt = jsonMap.fields();
+ Iterator<Entry<String, JsonNode>> mapIt = jsonMap.getFields();
while(mapIt.hasNext()){
Entry<String, JsonNode> e = mapIt.next();
map.put(e.getKey(), e.getValue().asText());
@@ -105,7 +105,9 @@ private Method getMethod(String methodName) throws SporeException {
// response checking
int statusCode = res.getStatusCode();
if(!context.expectedStatus.isEmpty() && !context.expectedStatus.contains(statusCode)){
- throw new SporeException("Receive unexpected response status: " + statusCode);
+ String errorContent = res.getResponseBody();
+ throw new SporeException("Receive unexpected response status: " + statusCode
+ + ", message: " + errorContent);
}
if(this.responseMiddleware != null){
@@ -2,12 +2,13 @@
*/
package net.linkfluence.jspore.middleware.format;
-import com.fasterxml.jackson.databind.ObjectMapper;
import com.ning.http.client.RequestBuilder;
import com.ning.http.client.Response;
import net.linkfluence.jspore.Method;
import net.linkfluence.jspore.SporeException;
import net.linkfluence.jspore.middleware.Middleware;
+import org.codehaus.jackson.map.ObjectMapper;
+
/**
* Format request/result payload as JSON.
@@ -2,19 +2,16 @@
*/
package net.linkfluence.jspore;
-import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.ImmutableMap;
-import net.linkfluence.jspore.middleware.auth.Basic;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
-
+import net.linkfluence.jspore.middleware.auth.Basic;
+import org.codehaus.jackson.JsonNode;
import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* set the appropriate value to USERNAME and PASS to get these
@@ -28,33 +25,37 @@
private static Logger logger = LoggerFactory.getLogger(GitHubPrivateTest.class);
private static final String USERNAME = "nicoo";
- private static final String PASS = "xxxxxx";
+ private static final String PASS = "YOUHAVETOCHANGETHISPASSWORD";
- private final Spore<JsonNode> spore;
-
- public GitHubPrivateTest() throws FileNotFoundException, IOException, InvalidSporeSpecException {
+ private Spore<JsonNode> buildSpore(String login, String password) throws IOException, InvalidSporeSpecException {
URL url = this.getClass().getResource("/github.json");
File spec = new File(url.getFile());
- spore = new Spore.Builder<JsonNode>()
+ return new Spore.Builder<JsonNode>()
.addMiddleware(Spore.JSON)
- .addMiddleware(new Basic(USERNAME, PASS))
+ .addMiddleware(new Basic(login, password))
.addSpecContent(spec)
.setDebug(true)
.build();
}
@Test
- public void test_get_profile() throws SporeException {
- SporeResult<JsonNode> result = spore.call("get_profile",
- new ImmutableMap.Builder<String, String>()
- .build());
- JsonNode message = result.body.get("message");
- if (message != null) {
- assertEquals("Bad credentials", message.asText());
- logger.warn("You did not set USERNAME and PASS in GitHubPrivateTest class. So Github answers 'Bad credentials'.");
- } else {
+ public void test_get_profile() throws Exception {
+ SporeResult<JsonNode> result = null;
+ try {
+ result = buildSpore(USERNAME, PASS).call("get_profile",
+ new ImmutableMap.Builder<String, String>()
+ .build());
assertEquals("User", result.body.get("type").asText());
+ } catch (SporeException e) {
+ logger.warn("You did not set USERNAME and PASS in GitHubPrivateTest class. So Github answers 'Bad credentials'.", e);
}
}
+
+ @Test(expected = SporeException.class)
+ public void test_bad_credentials() throws Exception {
+ buildSpore(USERNAME, "BADPASSWORD").call("get_profile",
+ new ImmutableMap.Builder<String, String>()
+ .build());
+ }
}
@@ -2,20 +2,17 @@
*/
package net.linkfluence.jspore;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.collect.ImmutableMap;
-import org.junit.Test;
-
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.node.ArrayNode;
+import static org.junit.Assert.*;
+import org.junit.Test;
/**
* Test the github spore spec.
@@ -73,7 +70,7 @@ public void test_get_repo_info() throws SporeException {
@Test
public void test_list_following() throws SporeException {
SporeResult<JsonNode> result = spore.call("list_following", publicParams);
- Iterator<JsonNode> users = result.body.elements();
+ Iterator<JsonNode> users = result.body.getElements();
boolean hasNgrunwald = false;
while (users.hasNext()) {
if (users.next().get("login").asText().equals("ngrunwald")) {
@@ -86,7 +83,7 @@ public void test_list_following() throws SporeException {
@Test
public void test_list_followers() throws SporeException {
SporeResult<JsonNode> result = spore.call("list_followers", publicParams);
- Iterator<JsonNode> users = result.body.elements();
+ Iterator<JsonNode> users = result.body.getElements();
boolean hasDocteurZ = false;
while (users.hasNext()) {
if (users.next().get("login").asText().equals("docteurZ")) {
@@ -99,7 +96,7 @@ public void test_list_followers() throws SporeException {
@Test
public void test_list_watched_repos() throws SporeException {
SporeResult<JsonNode> result = spore.call("list_watched_repos", publicParams);
- Iterator<JsonNode> watchedrepos = result.body.elements();
+ Iterator<JsonNode> watchedrepos = result.body.getElements();
boolean hasCljRome = false;
while (watchedrepos.hasNext()) {
if (watchedrepos.next().get("full_name").asText().equals("ngrunwald/clj-rome")) {
@@ -1,14 +1,10 @@
package net.linkfluence.jspore;
-import com.fasterxml.jackson.databind.JsonNode;
-import org.junit.Test;
-
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
import java.net.URL;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import org.codehaus.jackson.JsonNode;
+import static org.junit.Assert.*;
+import org.junit.Test;
/**
*
@@ -18,32 +14,7 @@
@Test
public void testParseSporeString() throws IOException, InvalidSporeSpecException {
- String spec = "{ "
- + "\"name\" : \"Amazon S3\", "
- + "\"version\" : \"0.1\", "
- + "\"base_url\" : \"http://s3.amazonaws.com\", "
- + "\"methods\" : { "
- + " \"get_service\" : { "
- + " \"path\" : \"/\", "
- + " \"method\" : \"GET\", "
- + " \"headers\" : { "
- + " \"Date\" : \"AWS\" "
- + " } "
- + " }, "
- + " \"delete_bucket\" : {"
- + " \"path\" : \"/\","
- + " \"method\" : \"DELETE\","
- + " \"headers\" : {"
- + " \"Date\" : \"AWS\""
- + " },"
- + " \"optional_params\" : ["
- + " \"bucket\""
- + " ],"
- + " \"expected_status\" : [ 204 ]"
- + " }"
- + "}"
- + "}"
- + "}";
+ String spec = readFromClasspath("/amazons3.json");
SpecParser parser = new SpecParser();
Model m = parser.parse(spec);
@@ -52,17 +23,26 @@ public void testParseSporeString() throws IOException, InvalidSporeSpecException
assertEquals("Amazon S3", m.name);
assertEquals("0.1", m.version);
assertEquals("http://s3.amazonaws.com", m.baseUrl.toString());
+ assertEquals(1, m.expectedStatus.size());
assertNotNull(m.getMethod("get_service"));
assertNotNull(m.getMethod("delete_bucket"));
Method getService = m.getMethod("get_service");
assertEquals("/", getService.path);
assertEquals("GET", getService.httpMethod);
+ assertEquals(1, getService.expectedStatus.size());
+ for (Integer status : getService.expectedStatus) {
+ assertEquals(200, status.intValue());
+ }
Method deleteBucket = m.getMethod("delete_bucket");
assertEquals("/", deleteBucket.path);
assertEquals("DELETE", deleteBucket.httpMethod);
+ assertEquals(1, deleteBucket.expectedStatus.size());
+ for (Integer status : deleteBucket.expectedStatus) {
+ assertEquals(204, status.intValue());
+ }
}
@@ -74,5 +54,14 @@ public void testLoadGitHubSpec() throws IOException, InvalidSporeSpecException{
.addSpecContent(spec)
.build();
}
-
+
+ protected static String readFromClasspath(String url) {
+ InputStream in = SpecParserTest.class.getResourceAsStream(url);
+ return convertStreamToString(in);
+ }
+
+ private static String convertStreamToString(java.io.InputStream is) {
+ java.util.Scanner s = new java.util.Scanner(is, "UTF-8").useDelimiter("\\A");
+ return s.hasNext() ? s.next() : "";
+ }
}
@@ -0,0 +1,26 @@
+{
+ "name": "Amazon S3",
+ "base_url": "http://s3.amazonaws.com",
+ "version": "0.1",
+ "methods": {
+ "get_service": {
+ "path": "/",
+ "method": "GET",
+ "headers": {
+ "Date": "AWS"
+ }
+ },
+ "delete_bucket": {
+ "path": "/",
+ "method": "DELETE",
+ "headers": {
+ "Date": "AWS"
+ },
+ "optional_params": [
+ "bucket"
+ ],
+ "expected_status": [ 204 ]
+ }
+ },
+ "expected_status": [ 200 ]
+}

0 comments on commit 12348b9

Please sign in to comment.