-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit test for DefaultJsonResponseHandler.
- Loading branch information
Jeff Sabin
committed
Aug 14, 2018
1 parent
3572431
commit 0a89f42
Showing
2 changed files
with
155 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 152 additions & 8 deletions
160
src/test/java/org/kairosdb/client/response/DefaultJsonResponseHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |