From bf74e5113d065f7eda41968fe234a4b441e6116c Mon Sep 17 00:00:00 2001 From: andrzej-krzemien Date: Fri, 22 Jul 2022 17:49:51 +0200 Subject: [PATCH 1/2] Issue : #1 ## Description - Add new directory for test classes - Add StubFinderServiceTest and StubResponseTest class - Adding dependencies to pom.xml in core package ## Types of Changes - New feature ## Tasks - [ ] Review changes ## Review - [ ] Tests ## Deployment Notes N/A --- core/pom.xml | 24 ++++++++ .../wttech/stubway/StubFinderServiceTest.java | 59 +++++++++++++++++++ .../stubway/response/StubResponseTest.java | 36 +++++++++++ 3 files changed, 119 insertions(+) create mode 100644 core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java create mode 100644 core/src/test/java/io/wttech/stubway/response/StubResponseTest.java diff --git a/core/pom.xml b/core/pom.xml index 6753a36..c5c6595 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -95,5 +95,29 @@ 1.0.0 provided + + junit + junit + 4.13.2 + compile + + + io.wcm + io.wcm.testing.aem-mock.junit5 + 5.0.0 + test + + + org.mockito + mockito-core + 4.6.1 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + compile + diff --git a/core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java b/core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java new file mode 100644 index 0000000..e75b18a --- /dev/null +++ b/core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java @@ -0,0 +1,59 @@ +package io.wttech.stubway; + +import io.wcm.testing.mock.aem.junit5.AemContext; +import io.wcm.testing.mock.aem.junit5.AemContextExtension; +import io.wttech.stubway.response.StubResponse; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.request.RequestPathInfo; +import org.apache.sling.api.resource.ResourceResolver; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import java.io.IOException; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@ExtendWith(AemContextExtension.class) +public class StubFinderServiceTest { + public AemContext aemContext = new AemContext(); + public StubFinderService stubFinderService; + public SlingHttpServletRequest request; + public RequestPathInfo requestPathInfo; + public ResourceResolver resourceResolver; + + @Before + public void setUp() { + stubFinderService = aemContext.registerService(new StubFinderService()); + request = mock(SlingHttpServletRequest.class); + requestPathInfo = mock(RequestPathInfo.class); + resourceResolver = mock(ResourceResolver.class); + } + @Test + public void getStubResponseShouldPassWhenRequestParamsAreMatching() { + //given + //when + when(request.getRequestPathInfo()).thenReturn(requestPathInfo); + when(request.getResourceResolver()).thenReturn(resourceResolver); + StubResponse result = stubFinderService.getStubResponse(request); + //then + System.out.println(result.getInputStream()); + } + @Test + public void getStubResponseWhenRequestParamsAreNotMatching() throws IOException { + //given + //when + when(request.getRequestPathInfo()).thenReturn(requestPathInfo); + when(request.getResourceResolver()).thenReturn(resourceResolver); + when(request.getReader()).thenThrow(new IOException()); + StubResponse result = stubFinderService.getStubResponse(request); + //then + System.out.println(result.getStatusCode()); + System.out.println(result.getInputStream()); + } +} \ No newline at end of file diff --git a/core/src/test/java/io/wttech/stubway/response/StubResponseTest.java b/core/src/test/java/io/wttech/stubway/response/StubResponseTest.java new file mode 100644 index 0000000..5146a6b --- /dev/null +++ b/core/src/test/java/io/wttech/stubway/response/StubResponseTest.java @@ -0,0 +1,36 @@ +package io.wttech.stubway.response; + +import io.wttech.stubway.stub.Stub; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +public class StubResponseTest { + + @Test + public void emptyShouldReturnStubResponseWithEmptyParams() { + StubResponse empty = StubResponse.empty(); + Assertions.assertEquals((Integer) null, empty.getStatusCode()); + Assertions.assertNull(empty.getInputStream()); + } + + @Test + public void internalErrorShouldReturnStubResponseWithStatusCode500() { + StubResponse internalError = StubResponse.internalError(new Exception()); + Assertions.assertEquals(500, internalError.getStatusCode()); + } + + @Test + public void errorShouldReturnStubResponseWithStatusCode400() { + StubResponse error = StubResponse.error(new Exception()); + Assertions.assertEquals(400, error.getStatusCode()); + } + + @Test + public void foundStubShouldReturnStubResponseWithSetValues() { + Stub stub = new Stub(); + StubResponse foundStub = StubResponse.foundStub(stub); + Assertions.assertEquals(200, foundStub.getStatusCode()); + Assertions.assertNull(foundStub.getInputStream()); + } +} \ No newline at end of file From fdb59c5eb74ebb728db5acca443384f1a96cfc57 Mon Sep 17 00:00:00 2001 From: rzuranska Date: Thu, 1 Sep 2022 14:04:59 +0200 Subject: [PATCH 2/2] Fix unit tests --- core/pom.xml | 22 +++++-- .../wttech/stubway/StubFinderServiceTest.java | 59 ------------------- .../stubway/response/StubResponseTest.java | 30 +++++++--- 3 files changed, 39 insertions(+), 72 deletions(-) delete mode 100644 core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java diff --git a/core/pom.xml b/core/pom.xml index c5c6595..eeca172 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -110,14 +110,26 @@ org.mockito mockito-core - 4.6.1 + 4.1.0 test - org.junit.jupiter - junit-jupiter - RELEASE - compile + org.mockito + mockito-inline + 4.1.0 + test + + + org.mockito + mockito-junit-jupiter + 4.1.0 + test + + + com.google.code.gson + gson + 2.8.6 + test diff --git a/core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java b/core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java deleted file mode 100644 index e75b18a..0000000 --- a/core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package io.wttech.stubway; - -import io.wcm.testing.mock.aem.junit5.AemContext; -import io.wcm.testing.mock.aem.junit5.AemContextExtension; -import io.wttech.stubway.response.StubResponse; -import org.apache.sling.api.SlingHttpServletRequest; -import org.apache.sling.api.request.RequestPathInfo; -import org.apache.sling.api.resource.ResourceResolver; -import org.junit.Before; -import org.junit.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; - -import java.io.IOException; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@ExtendWith(AemContextExtension.class) -public class StubFinderServiceTest { - public AemContext aemContext = new AemContext(); - public StubFinderService stubFinderService; - public SlingHttpServletRequest request; - public RequestPathInfo requestPathInfo; - public ResourceResolver resourceResolver; - - @Before - public void setUp() { - stubFinderService = aemContext.registerService(new StubFinderService()); - request = mock(SlingHttpServletRequest.class); - requestPathInfo = mock(RequestPathInfo.class); - resourceResolver = mock(ResourceResolver.class); - } - @Test - public void getStubResponseShouldPassWhenRequestParamsAreMatching() { - //given - //when - when(request.getRequestPathInfo()).thenReturn(requestPathInfo); - when(request.getResourceResolver()).thenReturn(resourceResolver); - StubResponse result = stubFinderService.getStubResponse(request); - //then - System.out.println(result.getInputStream()); - } - @Test - public void getStubResponseWhenRequestParamsAreNotMatching() throws IOException { - //given - //when - when(request.getRequestPathInfo()).thenReturn(requestPathInfo); - when(request.getResourceResolver()).thenReturn(resourceResolver); - when(request.getReader()).thenThrow(new IOException()); - StubResponse result = stubFinderService.getStubResponse(request); - //then - System.out.println(result.getStatusCode()); - System.out.println(result.getInputStream()); - } -} \ No newline at end of file diff --git a/core/src/test/java/io/wttech/stubway/response/StubResponseTest.java b/core/src/test/java/io/wttech/stubway/response/StubResponseTest.java index 5146a6b..3b39fd4 100644 --- a/core/src/test/java/io/wttech/stubway/response/StubResponseTest.java +++ b/core/src/test/java/io/wttech/stubway/response/StubResponseTest.java @@ -1,17 +1,28 @@ package io.wttech.stubway.response; -import io.wttech.stubway.stub.Stub; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.nio.charset.Charset; + +import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Test; +import io.wttech.stubway.stub.Stub; public class StubResponseTest { @Test - public void emptyShouldReturnStubResponseWithEmptyParams() { + public void emptyShouldReturnStubResponseWithEmptyParams() throws IOException { StubResponse empty = StubResponse.empty(); - Assertions.assertEquals((Integer) null, empty.getStatusCode()); - Assertions.assertNull(empty.getInputStream()); + Assertions.assertEquals(404, empty.getStatusCode()); + String expected = "{\n" + + " \"message\": \"Stub not found\",\n" + + " \"statusCode\": 404\n" + + "}"; + Assertions.assertEquals(expected, IOUtils.toString(empty.getInputStream(), Charset.defaultCharset())); } @Test @@ -27,10 +38,13 @@ public void errorShouldReturnStubResponseWithStatusCode400() { } @Test - public void foundStubShouldReturnStubResponseWithSetValues() { - Stub stub = new Stub(); + public void foundStubShouldReturnStubResponseWithSetValues() throws IOException { + Stub stub = mock(Stub.class); + when(stub.getStatusCode()).thenReturn(200); + when(stub.getInputStream()).thenReturn(IOUtils.toInputStream("fake input stream message", Charset.defaultCharset())); StubResponse foundStub = StubResponse.foundStub(stub); Assertions.assertEquals(200, foundStub.getStatusCode()); - Assertions.assertNull(foundStub.getInputStream()); + Assertions.assertEquals("fake input stream message", + IOUtils.toString(foundStub.getInputStream(), Charset.defaultCharset())); } } \ No newline at end of file