Search in sources :

Example 1 with Unmarshaller

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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(com.amazonaws.transform.Unmarshaller) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) Test(org.junit.Test)

Example 2 with Unmarshaller

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);
}
Also used : CRC32(java.util.zip.CRC32) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(com.amazonaws.transform.Unmarshaller) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) Test(org.junit.Test)

Example 3 with Unmarshaller

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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CRC32(java.util.zip.CRC32) Unmarshaller(com.amazonaws.transform.Unmarshaller) JsonUnmarshallerContext(com.amazonaws.transform.JsonUnmarshallerContext) Test(org.junit.Test)

Example 4 with Unmarshaller

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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StaxUnmarshallerContext(com.amazonaws.transform.StaxUnmarshallerContext) Unmarshaller(com.amazonaws.transform.Unmarshaller) Test(org.junit.Test)

Example 5 with Unmarshaller

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));
}
Also used : Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) Unmarshaller(com.amazonaws.transform.Unmarshaller) Test(org.junit.Test)

Aggregations

Unmarshaller (com.amazonaws.transform.Unmarshaller)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Test (org.junit.Test)6 JsonUnmarshallerContext (com.amazonaws.transform.JsonUnmarshallerContext)3 ArrayList (java.util.ArrayList)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 InputStream (java.io.InputStream)2 CRC32 (java.util.zip.CRC32)2 Node (org.w3c.dom.Node)2 StaxUnmarshallerContext (com.amazonaws.transform.StaxUnmarshallerContext)1 IOException (java.io.IOException)1