From e0cb5cc924fe833e49266bb1301a2272b337450f Mon Sep 17 00:00:00 2001 From: Johannes Schnatterer Date: Mon, 25 Jun 2018 16:56:45 +0200 Subject: [PATCH] Adds tests for JsonEnricherContext --- .../v2/JsonMarshallingResponseFilterTest.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/scm-webapp/src/test/java/sonia/scm/api/v2/JsonMarshallingResponseFilterTest.java b/scm-webapp/src/test/java/sonia/scm/api/v2/JsonMarshallingResponseFilterTest.java index 3af1984e29..ea71449a28 100644 --- a/scm-webapp/src/test/java/sonia/scm/api/v2/JsonMarshallingResponseFilterTest.java +++ b/scm-webapp/src/test/java/sonia/scm/api/v2/JsonMarshallingResponseFilterTest.java @@ -24,7 +24,9 @@ import java.util.HashSet; import java.util.Set; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class JsonMarshallingResponseFilterTest { @@ -47,13 +49,17 @@ public class JsonMarshallingResponseFilterTest { private JsonMarshallingResponseFilter filter; + private URI expectedUri; + @Before public void setUpObjectUnderTest() throws URISyntaxException { this.enrichers = new HashSet<>(); filter = new JsonMarshallingResponseFilter(mapper, enrichers); + expectedUri = new URI("https://www.scm-manager.org/scm/api/v2/repositories"); + when(requestContext.getUriInfo()).thenReturn(uriInfo); - when(uriInfo.getRequestUri()).thenReturn(new URI("https://www.scm-manager.org/scm/api/v2/repositories")); + when(uriInfo.getRequestUri()).thenReturn(expectedUri); } @Test @@ -72,17 +78,25 @@ public class JsonMarshallingResponseFilterTest { @Test public void testFilterWithEnricher() { + Sample expectedEntity = new Sample("one-two-three"); + MediaType expectedMediaType = MediaType.valueOf(VndMediaType.USER); + + when(responseContext.hasEntity()).thenReturn(Boolean.TRUE); + when(responseContext.getEntity()).thenReturn(expectedEntity); + when(responseContext.getMediaType()).thenReturn(expectedMediaType); + enrichers.add(context -> { JsonNode node = context.getResponseEntity(); + + assertEquals(mapper.valueToTree(expectedEntity), node); + assertEquals(expectedUri, context.getRequestUri()); + assertEquals(expectedMediaType, context.getResponseMediaType()); + if (node.isObject()) { ((ObjectNode)node).put("version", 2); } }); - when(responseContext.hasEntity()).thenReturn(Boolean.TRUE); - when(responseContext.getEntity()).thenReturn(new JsonMarshallingResponseFilterTest.Sample("one-two-three")); - when(responseContext.getMediaType()).thenReturn(MediaType.valueOf(VndMediaType.USER)); - filter.filter(requestContext, responseContext); verify(responseContext).setEntity(jsonNodeCaptor.capture());