Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write unit tests #22

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,29 @@
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
renata-zuranska-wttech marked this conversation as resolved.
Show resolved Hide resolved
<scope>compile</scope>
</dependency>
</dependencies>
</project>
59 changes: 59 additions & 0 deletions core/src/test/java/io/wttech/stubway/StubFinderServiceTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
renata-zuranska-wttech marked this conversation as resolved.
Show resolved Hide resolved
Assertions.assertNull(empty.getInputStream());
renata-zuranska-wttech marked this conversation as resolved.
Show resolved Hide resolved
}

@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);
renata-zuranska-wttech marked this conversation as resolved.
Show resolved Hide resolved
Assertions.assertEquals(200, foundStub.getStatusCode());
Assertions.assertNull(foundStub.getInputStream());
}
}