Skip to content

Commit

Permalink
Added unit test for DefaultJsonResponseHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Sabin committed Aug 14, 2018
1 parent 3572431 commit 0a89f42
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
import static org.weakref.jmx.internal.guava.base.Preconditions.checkNotNull;

public class DefaultJsonResponseHandler<T> implements JsonResponseHandler
{
Expand Down Expand Up @@ -48,9 +49,10 @@ public DefaultJsonResponseHandler(Class<T> clazz, DataPointTypeRegistry typeRegi

public DefaultJsonResponseHandler(Type type, DataPointTypeRegistry typeRegistry)
{
checkNotNull(typeRegistry, "typeRegistry must not be null");
mapper = new JsonMapper(typeRegistry);
successfulResponseCodes = ImmutableSet.of(200, 204);
this.type = type;
this.type = checkNotNull(type, "type must not be null");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,167 @@
package org.kairosdb.client.response;

import com.google.common.reflect.TypeToken;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.io.Resources;
import com.proofpoint.http.client.HeaderName;
import com.proofpoint.http.client.Request;
import com.proofpoint.http.client.Response;
import com.proofpoint.http.client.UnexpectedResponseException;
import org.junit.Before;
import org.junit.Test;
import org.kairosdb.client.DataPointTypeRegistry;
import org.kairosdb.client.JsonMapper;
import org.kairosdb.client.builder.RollupTask;

import java.lang.reflect.Type;
import java.util.List;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import static org.junit.Assert.*;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DefaultJsonResponseHandlerTest
{
private Request mockRequest;
private Response mockResponse;
private DefaultJsonResponseHandler<String> handler;

@Before
public void setup()
{
mockRequest = mock(Request.class);
mockResponse = mock(Response.class);

when(mockResponse.getHeaders()).thenReturn(ImmutableListMultimap.of(HeaderName.of(CONTENT_TYPE), APPLICATION_JSON.toString()));
when(mockResponse.getHeader(CONTENT_TYPE)).thenReturn(APPLICATION_JSON.toString());

handler = new DefaultJsonResponseHandler<>(String.class);
}

@Test
public void test_ErrorResponse()
{
when(mockResponse.getStatusCode()).thenReturn(500);
when(mockResponse.getStatusMessage()).thenReturn("The status message");

try
{
handler.handle(mockRequest, mockResponse);
assertFalse("expected exception", false);
}
catch (UnexpectedResponseException e)
{
assertThat(e.getStatusCode(), equalTo(500));
assertThat(e.getStatusMessage(), equalTo("The status message"));
}
}

@Test
public void test_noContentType()
{
when(mockResponse.getStatusCode()).thenReturn(200);
when(mockResponse.getStatusMessage()).thenReturn("OK");
when(mockResponse.getHeaders()).thenReturn(ImmutableListMultimap.of());

try
{
handler.handle(mockRequest, mockResponse);
assertFalse("expected exception", false);
}
catch (UnexpectedResponseException e)
{
assertThat(e.getMessage(), equalTo("Content-Type is not set for response"));
}
}

@Test
public void test_invalidMediaType()
{
when(mockResponse.getStatusCode()).thenReturn(200);
when(mockResponse.getStatusMessage()).thenReturn("OK");
when(mockResponse.getHeader(CONTENT_TYPE)).thenReturn("application/bogus");

try
{
handler.handle(mockRequest, mockResponse);
assertFalse("expected exception", false);
}
catch (UnexpectedResponseException e)
{
assertThat(e.getMessage(), equalTo("Expected application/json response from server but got application/bogus"));
}
}

@Test
public void test_badJsonRequest() throws IOException
{
when(mockResponse.getStatusCode()).thenReturn(400);
when(mockResponse.getStatusMessage()).thenReturn("Bad Request");
when(mockResponse.getInputStream()).thenReturn(new ByteArrayInputStream("{\"errors\":[\"error message\"]}".getBytes()));

try
{
handler.handle(mockRequest, mockResponse);
assertFalse("expected exception", false);
}
catch (UnexpectedResponseException e)
{
assertThat(e.getMessage(), equalTo("Expected response code to be [200, 204], but was 400: Errors: error message\n"));
}
}

@Test
public void test_JsonSyntaxException() throws IOException
{
when(mockResponse.getStatusCode()).thenReturn(200);
when(mockResponse.getStatusMessage()).thenReturn("OK");
when(mockResponse.getInputStream()).thenReturn(new ByteArrayInputStream("{bogus}".getBytes()));

try
{
handler.handle(mockRequest, mockResponse);
assertFalse("expected exception", false);
}
catch (IllegalArgumentException e)
{
assertThat(e.getMessage(), equalTo("Unable to create parse JSON response:\n"));
}
}

@Test
public void test_RuntimeException() throws IOException
{
when(mockResponse.getStatusCode()).thenReturn(200);
when(mockResponse.getStatusMessage()).thenReturn("OK");
when(mockResponse.getInputStream()).thenThrow(new IOException());

try
{
handler.handle(mockRequest, mockResponse);
assertFalse("expected exception", false);
}
catch (RuntimeException e)
{
assertThat(e.getMessage(), equalTo("Error reading JSON response from server"));
}
}

@Test
public void test()
public void test() throws IOException
{
// DefaultJsonResponseHandler<String> handler = new DefaultJsonResponseHandler<String>(String.class);
String json = Resources.toString(Resources.getResource("rollup.json"), Charsets.UTF_8);
DefaultJsonResponseHandler<RollupTask> handler = new DefaultJsonResponseHandler<>(RollupTask.class);
when(mockResponse.getStatusCode()).thenReturn(200);
when(mockResponse.getStatusMessage()).thenReturn("OK");
when(mockResponse.getInputStream()).thenReturn(new ByteArrayInputStream(json.getBytes()));

RollupTask task = handler.handle(mockRequest, mockResponse);

Type type = new TypeToken<List<QueryResult>>(){}.getType();
DefaultJsonResponseHandler<List<QueryResult>> listDefaultJsonResponseHandler = new DefaultJsonResponseHandler<>(type);
JsonMapper mapper = new JsonMapper(new DataPointTypeRegistry());
assertThat(task, equalTo(mapper.fromJson(json, RollupTask.class)));
}
}

0 comments on commit 0a89f42

Please sign in to comment.