Skip to content
Snippets Groups Projects
Commit 2bdd64b5 authored by Smitha Manjunath's avatar Smitha Manjunath
Browse files

increase code coverage of notification-core to 80%

parent b038451f
No related branches found
No related tags found
3 merge requests!232Update os-core-lib-azure,!231initial commit,!203Increase code coverage of Core module to 80%
Showing
with 567 additions and 47 deletions
......@@ -290,27 +290,45 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<excludes>
<exclude>**/models/*</exclude>
<exclude>**/swagger/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-unit-tests</id>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-agent</id>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
package org.opengroup.osdu.notification.api;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class GlobalErrorControllerTest {
@InjectMocks
private GlobalErrorController globalErrorController;
@Mock
HttpServletRequest httpServletRequest;
@Before
public void setup(){
MockitoAnnotations.initMocks(GlobalErrorControllerTest.this);
when(httpServletRequest.getAttribute(eq("javax.servlet.error.exception"))).thenReturn(new Exception("sample exception"));
}
@Test
public void handleErrorGetTest(){
when(httpServletRequest.getAttribute(eq("javax.servlet.error.status_code"))).thenReturn(500);
String expected = globalErrorController.handleErrorGet(httpServletRequest);
assertEquals(expected,"{\"code\": 500, \"reason:\": \"Internal Server Error\" \"message\":\"An unknown error has occurred\" }");
}
@Test
public void handleErrorPutTest(){
when(httpServletRequest.getAttribute(eq("javax.servlet.error.status_code"))).thenReturn(404);
String expected = globalErrorController.handleErrorPut(httpServletRequest);
assertEquals(expected,"{\"code\": 404, \"reason:\": \"Not Found\" \"message\":\"sample exception\" }");
}
@Test
public void handleErrorPatchTest(){
when(httpServletRequest.getAttribute(eq("javax.servlet.error.status_code"))).thenReturn(500);
String expected = globalErrorController.handleErrorPatch(httpServletRequest);
assertEquals(expected,"{\"code\": 500, \"reason:\": \"Internal Server Error\" \"message\":\"An unknown error has occurred\" }");
}
@Test
public void handleErrorDeleteTest(){
when(httpServletRequest.getAttribute(eq("javax.servlet.error.status_code"))).thenReturn(500);
String expected = globalErrorController.handleErrorDelete(httpServletRequest);
assertEquals(expected,"{\"code\": 500, \"reason:\": \"Internal Server Error\" \"message\":\"An unknown error has occurred\" }");
}
@Test
public void handleErrorPostTest(){
when(httpServletRequest.getAttribute(eq("javax.servlet.error.status_code"))).thenReturn(500);
String expected = globalErrorController.handleErrorPost(httpServletRequest);
assertEquals(expected,"{\"code\": 500, \"reason:\": \"Internal Server Error\" \"message\":\"An unknown error has occurred\" }");
}
@Test
public void getErrorPathTest() {
assertEquals(globalErrorController.getErrorPath(),"/error");
}
}
\ No newline at end of file
......@@ -40,7 +40,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class PubsubEndpointTests {
public class PubsubEndpointTest {
@Mock
private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor;
@Mock
......
package org.opengroup.osdu.notification.auth.factory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.auth.GsaAuth;
import org.opengroup.osdu.notification.auth.HmacAuth;
import org.opengroup.osdu.notification.auth.interfaces.SecretAuth;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(MockitoJUnitRunner.class)
public class AuthFactoryTest {
@Mock
private HmacAuth hmacAuth;
@Mock
private GsaAuth gsaAuth;
@InjectMocks
AuthFactory authFactory;
@Before
public void setup(){
MockitoAnnotations.initMocks(AuthFactoryTest.this);
}
@Test(expected = AppException.class)
public void getSecretAuthTest_invalidType(){
authFactory.getSecretAuth("invalid");
}
@Test
public void getSecretAuthTest_gsaAuth(){
SecretAuth secretAuth = authFactory.getSecretAuth("GSA");
assertTrue(secretAuth instanceof GsaAuth);
}
@Test
public void getSecretAuthTest_hmacAuth(){
SecretAuth secretAuth = authFactory.getSecretAuth("HMAC");
assertTrue(secretAuth instanceof HmacAuth);
}
}
......@@ -5,17 +5,23 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient;
import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@RunWith(MockitoJUnitRunner.class)
public class CredentialHeadersProviderTest {
@Mock
private HttpServletRequest httpRequest;
......@@ -23,12 +29,38 @@ public class CredentialHeadersProviderTest {
@Mock
private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor;
@Mock
private IServiceAccountJwtClient serviceAccountJwtClient;
@InjectMocks
private CredentialHeadersProvider headersProvider;
@Before
public void setup(){
MockitoAnnotations.initMocks(CredentialHeadersProviderTest.this);
}
@Test
public void testGetIsOk() throws Exception {
when(httpRequest.getMethod()).thenReturn(RequestMethod.GET.toString());
assertNotNull(headersProvider.getObject());
}
@Test
public void getObject_patchTest() throws Exception {
when(httpRequest.getMethod()).thenReturn(RequestMethod.PATCH.toString());
Map<String,String> mockAttributes = new HashMap<>();
mockAttributes.put("data-partition-id","opendes");
when(pubsubRequestBodyExtractor.extractAttributesFromRequestBody()).thenReturn(mockAttributes);
when(serviceAccountJwtClient.getIdToken("opendes")).thenReturn("sampleAuthToken");
DpsHeaders dpsHeaders = headersProvider.getObject();
assertNotNull(headersProvider.getObject());
assertEquals(dpsHeaders.getHeaders().get("content-type"),"application/json");
assertEquals(dpsHeaders.getHeaders().get("authorization"),"sampleAuthToken");
assertEquals(dpsHeaders.getHeaders().get("data-partition-id"),"opendes");
}
}
\ No newline at end of file
......@@ -19,9 +19,18 @@ package org.opengroup.osdu.notification.di;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.http.RequestBodyExtractor;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.notification.provider.interfaces.IAppProperties;
import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
......@@ -29,15 +38,26 @@ import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.verifyNew;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
public class RequestInfoTest {
@RunWith(MockitoJUnitRunner.class)
public class RequestInfoExtTest {
@Mock
private IAppProperties config;
@Mock
private HttpServletRequest http;
@Mock
DpsHeaders headers;
@Mock
IPubsubRequestBodyExtractor requestBodyExtractor;
@Spy
@InjectMocks
private RequestInfoExt sut;
Map<String, String> httpHeaders;
......@@ -49,18 +69,19 @@ public class RequestInfoTest {
httpHeaders.put("correlation-id", "cor123");
httpHeaders.put("context", "67890");
httpHeaders.put("data-partition-id", "123");
when(http.getMethod()).thenReturn("POST");
when(http.getHeaderNames()).thenReturn(Collections.enumeration(httpHeaders.keySet()));
httpHeaders.forEach((k,v) -> when(http.getHeader(k)).thenReturn(v));
sut = new RequestInfoExt(http);
MockitoAnnotations.initMocks(RequestInfoExtTest.this);
}
@Test
public void should_includeAllHeadersExcept_when_creatingHeaders() {
Map<String,String> map = this.sut.getHeaders().getHeaders();
assertEquals("cor123", map.get("correlation-id"));
assertEquals("123", map.get("data-partition-id"));
assertEquals("aaa", map.get("authorization"));
assertEquals(null, map.get("context"));
public void assignPartitionIdIfNotInHeaderTest(){
when(http.getMethod()).thenReturn("POST");
when(headers.getPartitionId()).thenReturn(null);
when(requestBodyExtractor.extractAttributesFromRequestBody()).thenReturn(httpHeaders);
this.sut.assignPartitionIdIfNotInHeader();
verify(requestBodyExtractor,times(1)).extractAttributesFromRequestBody();
}
}
package org.opengroup.osdu.notification.di;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.core.common.cache.MultiTenantCache;
import org.opengroup.osdu.core.common.cache.RedisCache;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.multitenancy.PartitionTenantInfoFactory;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class SubscriptionCacheFactoryTest {
SubscriptionCacheFactory subscriptionCacheFactory;
@Mock
private DpsHeaders headers;
@Mock
private PartitionTenantInfoFactory tenantFactory;
@Mock
private TenantInfo tenantInfo;
@Before
public void setup(){
subscriptionCacheFactory = new SubscriptionCacheFactory(1234567,1000);
MockitoAnnotations.initMocks(SubscriptionCacheFactoryTest.this);
ReflectionTestUtils.setField(subscriptionCacheFactory,"headers",headers);
ReflectionTestUtils.setField(subscriptionCacheFactory,"tenantFactory",tenantFactory);
tenantInfo.setDataPartitionId("opendes");
when(tenantFactory.getTenantInfo(any())).thenReturn(tenantInfo);
when(headers.getPartitionIdWithFallbackToAccountId()).thenReturn("opendes");
when(tenantInfo.getDataPartitionId()).thenReturn("opendes");
subscriptionCacheFactory.put("testKey","testVal");
}
@Test
public void getTest() {
String actual = subscriptionCacheFactory.get("testKey");
assertEquals("testVal",actual);
}
@Test
public void deleteTest() {
subscriptionCacheFactory.delete("testKey");
assertNull(subscriptionCacheFactory.get("testKey"));
}
@Test
public void clearAll() {
subscriptionCacheFactory.clearAll();
assertNull(subscriptionCacheFactory.get("testKey"));
}
}
\ No newline at end of file
package org.opengroup.osdu.notification.errors;
import org.checkerframework.checker.units.qual.A;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppError;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.ValidationException;
import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.util.Objects;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(MockitoJUnitRunner.class)
public class SpringExceptionMapperTest {
@InjectMocks
private SpringExceptionMapper springExceptionMapper;
@Mock
private JaxRsDpsLog log;
@Mock
private HttpServletRequest servletRequest;
@Mock
private WebRequest request;
@Before
public void setUp() {
MockitoAnnotations.initMocks(SpringExceptionMapperTest.this);
}
@Test
public void handleAppException() {
AppException appException = new AppException(400, "Sample app exception reason", "Sample app exception message");
appException.setOriginalException(new NullPointerException());
ResponseEntity<Object> actual = springExceptionMapper.handleAppException(appException);
AppError appError = (AppError) actual.getBody();
assertNotNull(actual);
assertEquals(400, Objects.requireNonNull(appError).getCode());
assertEquals("Sample app exception reason", appError.getReason());
assertEquals("Sample app exception message", appError.getMessage());
}
@Test
public void handleValidationException() {
ValidationException validationException = new ValidationException("Sample Validation Exception");
ResponseEntity<Object> actual = springExceptionMapper.handleValidationException(validationException);
assertNotNull(actual);
AppError appError = (AppError) actual.getBody();
assertEquals(400,appError.getCode());
assertEquals("Sample Validation Exception",appError.getMessage());
assertEquals("Bad request",appError.getReason());
}
@Test
public void handleAccessDeniedException() {
AccessDeniedException accessDeniedException = new AccessDeniedException("Sample Access Denied Exception");
ResponseEntity<Object> actual = springExceptionMapper.handleAccessDeniedException(accessDeniedException,request);
assertNotNull(actual);
AppError appError = (AppError) actual.getBody();
assertEquals(401,appError.getCode());
assertEquals("Sample Access Denied Exception",appError.getMessage());
assertEquals("Unauthorized",appError.getReason());
}
@Test
public void handleGeneralException() {
Exception exception = new Exception("A General Exception");
ResponseEntity<Object> actual = springExceptionMapper.handleGeneralException(exception);
AppError appError = (AppError) actual.getBody();
assertNotNull(appError);
assertEquals(500,appError.getCode());
assertEquals("An unknown error has occurred.",appError.getMessage());
assertEquals("Server Error",appError.getReason());
}
@Test
public void handleIOException_connectionClosed() {
IOException ioException = new IOException("Sample IO Exception due to broken pipe");
ResponseEntity<Object> actual = springExceptionMapper.handleIOException(ioException);
assertEquals(actual,null);
}
@Test
public void handleIOExceptionTest() {
IOException ioException = new IOException("Sample IO Exception");
ResponseEntity<Object> actual = springExceptionMapper.handleIOException(ioException);
assertNotNull(actual);
AppError appError = (AppError) actual.getBody();
assertEquals("Unknown error",appError.getReason());
assertEquals("Sample IO Exception",appError.getMessage());
assertEquals(503,appError.getCode());
}
}
\ No newline at end of file
package org.opengroup.osdu.notification.logging;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.logging.ILogger;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.http.Request;
import org.opengroup.osdu.notification.di.RequestInfoExt;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ResponseLogFilterTest {
@InjectMocks
ResponseLogFilter responseLogFilter;
@Mock
private RequestInfoExt requestInfoExt;
@Mock
private ILogger logger;
@Mock
private FilterChain filterChain;
@Mock
private ServletContext servletContext;
@Mock
private HttpServletRequest servletRequest;
@Mock
private HttpServletResponse servletResponse;
@Spy
private DpsHeaders dpsHeaders;
@Before
public void setup() throws ServletException, IOException {
MockitoAnnotations.initMocks(ResponseLogFilterTest.this);
when(servletRequest.getServletContext()).thenReturn(servletContext);
}
@Test
public void doFilterTest() throws ServletException, IOException {
when(requestInfoExt.getHeaders()).thenReturn(dpsHeaders);
when(servletRequest.getMethod()).thenReturn("OPTIONS");
responseLogFilter.doFilter(servletRequest,servletResponse,filterChain);
verify(logger,times(1)).request(eq("notification.request"),any(Request.class),anyMap());
}
}
......@@ -22,6 +22,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.http.HttpClient;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
......@@ -38,8 +39,8 @@ import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
public class NotificationHandlerTests {
@RunWith(MockitoJUnitRunner.class)
public class NotificationHandlerTest {
@Mock
private SubscriptionHandler subscriptionHandler;
@Mock
......@@ -94,10 +95,7 @@ public class NotificationHandlerTests {
@Test(expected = SubscriptionException.class)
public void should_throwException_whenSubscriptionHandlerThrowsException() throws Exception {
Map<String, String> headers = new HashMap<String, String>();
when(this.authFactory.getSecretAuth(any())).thenReturn(secretAuth);
when(this.httpClient.send(any())).thenReturn(response);
when(this.secretAuth.getPushUrl(gsa_subscription.getPushEndpoint())).thenReturn(gsa_subscription.getPushEndpoint());
when(this.secretAuth.getRequestHeaders()).thenReturn(headers);
when(subscriptionHandler.getSubscriptionFromCache(this.NOTIFICATION_ID)).thenThrow(new SubscriptionException("error", response));
this.sut.notifySubscriber(this.NOTIFICATION_ID, this.PUBSUB_MESSAGE, headers);
fail("should throw SubscriptionException");
......
......@@ -22,6 +22,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
......@@ -44,8 +45,8 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
public class SubscriptionHandlerTests {
@RunWith(MockitoJUnitRunner.class)
public class SubscriptionHandlerTest {
@Mock
private ISubscriptionFactory subscriptionFactory;
@Mock
......@@ -68,22 +69,6 @@ public class SubscriptionHandlerTests {
fail("should throw AppException");
}
@Test(expected = AppException.class)
public void should_throwException_whenSubscriptionParsingErrorOccurs() throws Exception {
when(this.subscriptionCacheFactory.get(any())).thenReturn(null);
ISubscriptionService subscriptionService = mock(SubscriptionService.class);
when(this.subscriptionFactory.create(any())).thenReturn(subscriptionService);
String jsonSubscription = this.getHmacSubscription();
ObjectMapper objectMapper = new ObjectMapper();
Subscription subscription = objectMapper.readValue(jsonSubscription, Subscription.class);
List<Subscription> queryResult = new ArrayList<>();
queryResult.add(subscription);
when(subscriptionService.query(any())).thenReturn(queryResult);
sut.setObjectMapper(this.objectMapper);
when(this.objectMapper.readValue(anyString(), any(Class.class))).thenThrow(new IOException());
this.sut.getSubscriptionFromCache(NOTIFICATION_ID);
fail("should throw AppException");
}
@Test
public void should_return200_whenSubscriptionGotFromRegistration() throws Exception {
......
......@@ -2,6 +2,8 @@ package org.opengroup.osdu.notification.swagger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
......@@ -9,6 +11,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
@RunWith(MockitoJUnitRunner.class)
public class HomeControllerTest {
private MockMvc mockMvc;
......
package org.opengroup.osdu.notification.utils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
@RunWith(MockitoJUnitRunner.class)
public class NotificationFilterTest {
@InjectMocks
private NotificationFilter notificationFilter;
@Spy
private DpsHeaders dpsHeaders;
@Mock
private FilterChain filterChain;
@Mock
private HttpServletRequest servletRequest;
@Mock
private HttpServletResponse servletResponse;
@Before
public void setup() throws ServletException, IOException {
MockitoAnnotations.initMocks(NotificationFilterTest.this);
doNothing().when(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
}
@Test
public void doFilterTest() throws ServletException, IOException {
notificationFilter.doFilter(servletRequest, servletResponse, filterChain);
assertNotNull(dpsHeaders.getHeaders().get("correlation-id"));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment