use of com.amazonaws.transform.Unmarshaller in project aws-sdk-android by aws-amplify.
the class JsonResponseHandlerTest method testHandleWithNoCRC32.
@Test
public void testHandleWithNoCRC32() throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
HttpResponse response = new HttpResponse.Builder().statusText("testResponse").statusCode(200).header("testKey", "testValue").content(bais).build();
Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() {
@Override
public String unmarshall(JsonUnmarshallerContext in) throws Exception {
in.getReader().beginObject();
in.getReader().nextName();
return in.getReader().nextString();
}
};
JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller);
AmazonWebServiceResponse<String> awsResponse = toTest.handle(response);
assertEquals(awsResponse.getResult(), "Content");
}
use of com.amazonaws.transform.Unmarshaller in project aws-sdk-android by aws-amplify.
the class JsonResponseHandlerTest method testHandleNeedsConnectionLeftOpen.
@Test
public void testHandleNeedsConnectionLeftOpen() throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
CRC32 crc32 = new CRC32();
crc32.update("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
HttpResponse response = new HttpResponse.Builder().statusText("testResponse").statusCode(200).header("testKey", "testValue").content(bais).build();
final List<InputStream> capture = new ArrayList<InputStream>();
Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() {
@Override
public String unmarshall(JsonUnmarshallerContext in) throws Exception {
capture.add(in.getHttpResponse().getContent());
return "OpenConnection";
}
};
JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller);
toTest.needsConnectionLeftOpen = true;
assertTrue(toTest.needsConnectionLeftOpen());
AmazonWebServiceResponse<String> awsResponse = toTest.handle(response);
assertEquals(awsResponse.getResult(), "OpenConnection");
assertSame(capture.get(0), bais);
}
use of com.amazonaws.transform.Unmarshaller in project aws-sdk-android by aws-amplify.
the class JsonResponseHandlerTest method testHandleWithCRC32.
@Test
public void testHandleWithCRC32() throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
CRC32 crc32 = new CRC32();
crc32.update("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8));
HttpResponse response = new HttpResponse.Builder().statusText("testResponse").statusCode(200).header("testKey", "testValue").header("x-amz-crc32", String.valueOf(crc32.getValue())).content(bais).build();
Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() {
@Override
public String unmarshall(JsonUnmarshallerContext in) throws Exception {
in.getReader().beginObject();
in.getReader().nextName();
return in.getReader().nextString();
}
};
JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller);
// Is a no-op. just adding for code coverage of the no-op
toTest.registerAdditionalMetadataExpressions(null);
AmazonWebServiceResponse<String> awsResponse = toTest.handle(response);
assertEquals(awsResponse.getResult(), "Content");
}
use of com.amazonaws.transform.Unmarshaller in project aws-sdk-android by aws-amplify.
the class StaxResponseHandlerTest method testHandleWithContent.
@Test
public void testHandleWithContent() throws Exception {
final ByteArrayInputStream bais = new ByteArrayInputStream(("<data>Content</data>").getBytes(StringUtils.UTF8));
final HttpResponse response = new HttpResponse.Builder().header("testKey", "testValue").header("x-amzn-RequestId", "99").content(bais).build();
Unmarshaller<String, StaxUnmarshallerContext> unmarshaller = new Unmarshaller<String, StaxUnmarshallerContext>() {
@Override
public String unmarshall(StaxUnmarshallerContext in) throws Exception {
in.nextEvent();
String content = in.readText();
assertEquals(content, "Content");
assertEquals(in.getHeader("testKey"), "testValue");
return content;
}
};
StaxResponseHandler<String> handler = new StaxResponseHandler<String>(unmarshaller);
AmazonWebServiceResponse<String> awsr = handler.handle(response);
assertEquals(awsr.getResponseMetadata().getRequestId(), "99");
assertEquals(awsr.getResult(), "Content");
}
use of com.amazonaws.transform.Unmarshaller in project aws-sdk-android by aws-amplify.
the class DefaultErrorResponseHandlerTest method testDefaultErrorResponseHandler.
@Test
public void testDefaultErrorResponseHandler() throws Exception {
List<Unmarshaller<AmazonServiceException, Node>> unmarshallerList = new ArrayList<Unmarshaller<AmazonServiceException, Node>>();
String xmlResponse = "<error>TestError</error>";
HttpResponse errorResponse = new HttpResponse.Builder().statusCode(400).statusText("Error").content(new ByteArrayInputStream(xmlResponse.getBytes(StringUtils.UTF8))).build();
final List<Boolean> called = new ArrayList<Boolean>();
Unmarshaller<AmazonServiceException, Node> incorrectUnmarshaller = new Unmarshaller<AmazonServiceException, Node>() {
@Override
public AmazonServiceException unmarshall(Node in) throws Exception {
called.add(true);
return null;
}
};
Unmarshaller<AmazonServiceException, Node> correctUnmarshaller = new Unmarshaller<AmazonServiceException, Node>() {
@Override
public AmazonServiceException unmarshall(Node in) throws Exception {
called.add(true);
in = in.getFirstChild();
assertEquals(in.getNodeName(), "error");
assertEquals(in.getTextContent(), "TestError");
AmazonServiceException ase = new AmazonServiceException("TestAse");
return ase;
}
};
unmarshallerList.add(incorrectUnmarshaller);
unmarshallerList.add(correctUnmarshaller);
DefaultErrorResponseHandler handler = new DefaultErrorResponseHandler(unmarshallerList);
AmazonServiceException e = handler.handle(errorResponse);
assertEquals(e.getErrorMessage(), "TestAse");
assertEquals(e.getStatusCode(), 400);
assertEquals(called.size(), 2);
assertTrue(called.get(0));
assertTrue(called.get(1));
}
Aggregations