Skip to content

Commit

Permalink
more unit tests for json-gson
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmugra committed Dec 28, 2020
1 parent 545bb43 commit 4ea0b45
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
3 changes: 1 addition & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@

<build>
<plugins>
<plugin>
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<logViolationsToConsole>true</logViolationsToConsole>
<configLocation>${project.parent.basedir}/checkstyle_checks.xml</configLocation>
<!--propertyExpansion>suppressionsFile=/checkstyle_suppressions.xml</propertyExpansion-->
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void produce(Object object, String template, String mediaType, HttpServle
throws IOException {
if (object == null) {
LOG.warn("Entry point return Object is NULL");
if (resp.getStatus() == HttpServletResponse.SC_OK) {
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
return;
}
try (JsonWriter writer = new JsonWriter(resp.getWriter())) {
gson.toJson(object, object.getClass(), writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,45 @@

import javax.servlet.http.HttpServletRequest;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.google.gson.GsonBuilder;

import net.cactusthorn.routing.RequestData;

public class SimpleGsonConsumerTest {

@Test //
public void consume() throws IOException {
private RequestData requestData;

@BeforeEach //
public void setUp() throws IOException {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);

RequestData requestData;
try (InputStream is = SimpleGsonProducerTest.class.getClassLoader().getResourceAsStream("test.json");
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader buf = new BufferedReader(reader)) {

Mockito.when(request.getReader()).thenReturn(buf);
requestData = new RequestData(request, null, 512);
}
}

@Test //
public void consume() throws IOException {
SimpleGsonConsumer consumer = new SimpleGsonConsumer();
DataObject data = (DataObject) consumer.consume(DataObject.class, null, requestData);
assertEquals("The Name \u00DF", data.getName());
assertEquals(123, data.getValue());
}

@Test //
public void consumeWithCustomGson() throws IOException {
SimpleGsonConsumer consumer = new SimpleGsonConsumer(new GsonBuilder().create());
DataObject data = (DataObject) consumer.consume(DataObject.class, null, requestData);
assertEquals("The Name \u00DF", data.getName());
assertEquals(123, data.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@

import javax.servlet.http.HttpServletResponse;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import com.google.gson.GsonBuilder;

public class SimpleGsonProducerTest {

@Test //
public void produce() throws IOException {
private HttpServletResponse response;
private StringWriter stringWriter;
private DataObject data;
private String json;

String json;
@BeforeEach //
public void setUp() throws IOException {
try (InputStream is = SimpleGsonProducerTest.class.getClassLoader().getResourceAsStream("test.json");
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {

Expand All @@ -33,18 +40,45 @@ public void produce() throws IOException {
json = builder.toString();
}

DataObject data = new DataObject("The Name \u00DF", 123);
data = new DataObject("The Name \u00DF", 123);

StringWriter stringWriter = new StringWriter();
stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);

HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
response = Mockito.mock(HttpServletResponse.class);
Mockito.when(response.getWriter()).thenReturn(writer);
}

@Test //
public void produce() throws IOException {
SimpleGsonProducer producer = new SimpleGsonProducer();

producer.produce(data, null, null, null, response);

assertEquals(json, stringWriter.toString());
}

@Test //
public void produceWithCustomGson() throws IOException {
SimpleGsonProducer producer = new SimpleGsonProducer(new GsonBuilder().create());

producer.produce(data, null, null, null, response);

assertEquals(json, stringWriter.toString());
}

@Test //
public void produceNullData() throws IOException {

Mockito.when(response.getStatus()).thenReturn(HttpServletResponse.SC_OK);

SimpleGsonProducer producer = new SimpleGsonProducer();
producer.produce(null, null, null, null, response);

ArgumentCaptor<Integer> code = ArgumentCaptor.forClass(Integer.class);

Mockito.verify(response).setStatus(code.capture());

assertEquals(HttpServletResponse.SC_NO_CONTENT, code.getValue());
}
}

0 comments on commit 4ea0b45

Please sign in to comment.