Skip to content

Commit

Permalink
Merge pull request #64 from dcrissman/candy
Browse files Browse the repository at this point in the history
add helper methods
  • Loading branch information
jewzaam committed Mar 12, 2015
2 parents 85c3928 + 0dce8d3 commit 7c5bfa5
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
package com.redhat.lightblue.client.response;

import java.io.IOException;
import java.lang.reflect.Array;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.redhat.lightblue.client.util.ClientConstants;

public class LightblueResponse {

/**
* It is safe and encouraged to share the same mapper among threads. It is
* thread safe. So, this default instance is static.
*
* @see <a href="http://stackoverflow.com/a/3909846">The developer of the
* Jackson library's own quote.</a>
*/
public static final ObjectMapper DEFAULT_MAPPER = new ObjectMapper()
.setDateFormat(ClientConstants.getDateFormat())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

private String text;
private JsonNode json;
private final ObjectMapper mapper;

public LightblueResponse() {
public LightblueResponse(ObjectMapper mapper) {
if (mapper == null) {
throw new NullPointerException("ObjectMapper instance cannot be null");
}
this.mapper = mapper;
}

public LightblueResponse() {
this(DEFAULT_MAPPER);
}

public LightblueResponse(String responseText) {
this(responseText, DEFAULT_MAPPER);
}

public LightblueResponse(String responseText, ObjectMapper mapper) {
this(mapper);
this.text = responseText;
ObjectMapper mapper = new ObjectMapper();
try {
json = mapper.readTree(responseText);
} catch (IOException e) {
Expand All @@ -40,13 +67,52 @@ public void setJson(JsonNode json) {
this.json = json;
}

public boolean hasError(){
public boolean hasError() {
JsonNode objectTypeNode = json.get("status");
if(objectTypeNode == null){
if (objectTypeNode == null) {
return false;
}

return objectTypeNode.textValue().equalsIgnoreCase("error");
}

public int parseModifiedCount() {
return parseInt("modifiedCount");
}

public int parseMatchCount() {
return parseInt("matchCount");
}

private int parseInt(String fieldName) {
JsonNode field = json.findValue(fieldName);
if (field == null || field.isNull()) {
return 0;
}
return field.asInt();
}

@SuppressWarnings("unchecked")
public <T> T parseProcessed(final Class<T> type)
throws LightblueResponseParseException {
try {
JsonNode processedNode = json.path("processed");

//if null or an empty array
if (processedNode == null
|| processedNode.isNull()
|| processedNode.isMissingNode()
|| (processedNode.isArray() && !((ArrayNode) processedNode).iterator().hasNext())) {
if (type.isArray()) {
return (T) Array.newInstance(type.getComponentType(), 0);
}
return null;
}

return mapper.readValue(processedNode.traverse(), type);
} catch (RuntimeException | IOException e) {
throw new LightblueResponseParseException("Error parsing lightblue response: " + json.toString() + "\n", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.redhat.lightblue.client.response;

/**
* Exception thrown when a response from lightblue is not able
* to be parsed.
*
* @author dcrissman
*/
public class LightblueResponseParseException extends Exception {

private static final long serialVersionUID = -1221306072042538444L;

public LightblueResponseParseException(String message) {
super(message);
}

public LightblueResponseParseException(Throwable cause) {
super(cause);
}

public LightblueResponseParseException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,161 @@ public void testSetJson() throws JsonProcessingException, IOException {
}

@Test
public void testHasError_NoErrorElement_False(){
public void testHasError_NoErrorElement_False() {
assertFalse(testResponse.hasError());
}

@Test
public void testHasError_False(){
public void testHasError_False() {
LightblueResponse response = new LightblueResponse("{\"status\":\"successful\"}");
assertFalse(response.hasError());
}

@Test
public void testHasError_True(){
public void testHasError_True() {
LightblueResponse response = new LightblueResponse("{\"status\":\"error\"}");
assertTrue(response.hasError());
}

@Test
public void testParseProcessed_EmptyProcessed_ForArrayGeneric() throws LightblueResponseParseException {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": 0, \"processed\": [], \"status\": \"COMPLETE\"}");

Object[] results = response.parseProcessed(Object[].class);

Assert.assertNotNull(results);
Assert.assertEquals(0, results.length);
}

@Test
public void testParseProcessed_EmptyProcessed_ForSimpleGeneric() throws LightblueResponseParseException {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": 0, \"processed\": [], \"status\": \"COMPLETE\"}");

Object results = response.parseProcessed(Object.class);

Assert.assertNull(results);
}

@Test
public void testParseProcessed() throws LightblueResponseParseException {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 1, \"modifiedCount\": 0, \"processed\": [{\"_id\": \"idhash\", \"field\":\"value\"}], \"status\": \"COMPLETE\"}");

SimpleModelObject[] results = response.parseProcessed(SimpleModelObject[].class);

Assert.assertNotNull(results);
Assert.assertEquals(1, results.length);

Assert.assertEquals(new SimpleModelObject("idhash", "value"), results[0]);
}

@Test
public void testParseProcessed_NullProcessedNode() throws LightblueResponseParseException {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": 0, \"processed\": null, \"status\": \"COMPLETE\"}");

Object results = response.parseProcessed(Object.class);

Assert.assertNull(results);
}

@Test
public void testParseProcessedWithParsingError_DoesNotExist() throws LightblueResponseParseException {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": 0, \"status\": \"COMPLETE\"}");

Object[] results = response.parseProcessed(Object[].class);
Assert.assertNotNull(results);
Assert.assertEquals(0, results.length);
}

@Test(expected = LightblueResponseParseException.class)
public void testParseProcessedWithParsingError_InvalidJson() throws LightblueResponseParseException {
LightblueResponse response = new LightblueResponse("{\"processed\":\"<p>This is not json</p>\"}");

response.parseProcessed(Object[].class);
}

@Test
public void testParseModifiedCount() {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": 5, \"status\": \"COMPLETE\"}");
Assert.assertEquals(5, response.parseModifiedCount());
}

@Test
public void testParseModifiedCount_zeroValue() {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": 0, \"status\": \"COMPLETE\"}");
Assert.assertEquals(0, response.parseModifiedCount());
}

@Test
public void testParseModifiedCount_notExist() {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"status\": \"COMPLETE\"}");
Assert.assertEquals(0, response.parseModifiedCount());
}

@Test
public void testParseModifiedCount_nullValue() {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": null, \"status\": \"COMPLETE\"}");
Assert.assertEquals(0, response.parseModifiedCount());
}

@Test
public void testParseMatchCount() {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 5, \"modifiedCount\": 0, \"status\": \"COMPLETE\"}");
Assert.assertEquals(5, response.parseMatchCount());
}

@Test
public void testParseMatchCount_zeroValue() {
LightblueResponse response = new LightblueResponse("{\"matchCount\": 0, \"modifiedCount\": 0, \"status\": \"COMPLETE\"}");
Assert.assertEquals(0, response.parseMatchCount());
}

@Test
public void testParseMatchCount_notExist() {
LightblueResponse response = new LightblueResponse("{\"modifiedCount\": 0, \"status\": \"COMPLETE\"}");
Assert.assertEquals(0, response.parseMatchCount());
}

@Test
public void testParseMatchCount_nullValue() {
LightblueResponse response = new LightblueResponse("{\"matchCount\": null, \"modifiedCount\": 0, \"status\": \"COMPLETE\"}");
Assert.assertEquals(0, response.parseMatchCount());
}

private static class SimpleModelObject {

String _id = "", field = "";

public SimpleModelObject() {}

public SimpleModelObject(String _id, String field) {
super();
this._id = _id;
this.field = field;
}

public String get_id() {
return _id;
}

public void set_id(String _id) {
this._id = _id;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}

@Override
public boolean equals(Object obj) {
SimpleModelObject o = (SimpleModelObject) obj;

return _id.equals(o._id) && field.equals(o.field);
}

}

}
Loading

0 comments on commit 7c5bfa5

Please sign in to comment.