From f745568fb2043fa139b72bf1d863df2a29ac47f3 Mon Sep 17 00:00:00 2001 From: SmithaManjunath <smanjunath@microsoft.com> Date: Thu, 7 Apr 2022 22:13:00 +0530 Subject: [PATCH] code cleanuo --- .../api/GlobalErrorControllerTest.java | 19 ++++++++--- .../notification/api/PubsubEndpointTest.java | 3 +- .../AuthorizationServiceEntitlementsTest.java | 4 +-- .../auth/factory/AuthFactoryTest.java | 14 ++++++++ .../di/CredentialHeadersProviderTest.java | 2 -- .../notification/di/RequestInfoExtTest.java | 2 -- .../di/SubscriptionCacheFactoryTest.java | 19 ++++++++--- .../errors/SpringExceptionMapperTest.java | 34 +++++++++++++------ .../logging/ResponseLogFilterTest.java | 14 ++++++++ .../service/NotificationHandlerTest.java | 8 ++--- .../service/SubscriptionHandlerTest.java | 1 - .../utils/NotificationFilterTest.java | 18 ++++++++-- 12 files changed, 102 insertions(+), 36 deletions(-) diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/api/GlobalErrorControllerTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/api/GlobalErrorControllerTest.java index 8a4ff8663..6c4570a42 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/api/GlobalErrorControllerTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/api/GlobalErrorControllerTest.java @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.api; import org.junit.Before; @@ -19,6 +33,7 @@ public class GlobalErrorControllerTest { @Mock HttpServletRequest httpServletRequest; + @InjectMocks private GlobalErrorController globalErrorController; @@ -33,7 +48,6 @@ public class GlobalErrorControllerTest { 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 @@ -41,7 +55,6 @@ public class GlobalErrorControllerTest { 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 @@ -49,7 +62,6 @@ public class GlobalErrorControllerTest { 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 @@ -69,7 +81,6 @@ public class GlobalErrorControllerTest { @Test public void getErrorPathTest() { assertEquals(globalErrorController.getErrorPath(), "/error"); - } } \ No newline at end of file diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTest.java index de0af9ef8..75c9461fd 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTest.java @@ -36,8 +36,10 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PubsubEndpointTest { + private static final String NOTIFICATION_ID = "test-notification-id"; private static final String PUBSUB_MESSAGE = "test-pubsub-message-data"; + private final HttpResponse response = new HttpResponse(); @Mock private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor; @Mock @@ -46,7 +48,6 @@ public class PubsubEndpointTest { private JaxRsDpsLog log; @InjectMocks private PubsubEndpoint sut; - private final HttpResponse response = new HttpResponse(); @Before public void setup() throws Exception { diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationServiceEntitlementsTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationServiceEntitlementsTest.java index 529de0881..82895fd06 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationServiceEntitlementsTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationServiceEntitlementsTest.java @@ -61,7 +61,7 @@ public class AuthorizationServiceEntitlementsTest { } @Test - public void should_returnUser_when_ussrHasPermission() throws EntitlementsException { + public void should_returnUser_when_userHasPermission() throws EntitlementsException { sut = createSut("service.register.user"); AuthorizationResponse result = sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.register.user"); @@ -70,7 +70,7 @@ public class AuthorizationServiceEntitlementsTest { } @Test - public void should_returnUser_when_ussrHasAnyPermission() throws EntitlementsException { + public void should_returnUser_when_userHasAnyPermission() throws EntitlementsException { sut = createSut("service.register.editor"); AuthorizationResponse result = sut.authorizeAny(DpsHeaders.createFromMap(new HashMap<>()), "service.register.user", "service.register.editor"); diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/factory/AuthFactoryTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/factory/AuthFactoryTest.java index 5c00336e8..a1acb7de9 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/factory/AuthFactoryTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/factory/AuthFactoryTest.java @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.auth.factory; import org.junit.Before; diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java index 2d604ac94..83a349952 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/di/CredentialHeadersProviderTest.java @@ -58,8 +58,6 @@ public class CredentialHeadersProviderTest { 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 diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoExtTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoExtTest.java index 366bb5a9a..3ea97c721 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoExtTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/di/RequestInfoExtTest.java @@ -64,13 +64,11 @@ public class RequestInfoExtTest { @Test 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(); - } } diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/di/SubscriptionCacheFactoryTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/di/SubscriptionCacheFactoryTest.java index 8b17bbec2..893e63502 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/di/SubscriptionCacheFactoryTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/di/SubscriptionCacheFactoryTest.java @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.di; import org.junit.Before; @@ -30,7 +44,6 @@ public class SubscriptionCacheFactoryTest { @Mock private TenantInfo tenantInfo; - @Before public void setup() { subscriptionCacheFactory = new SubscriptionCacheFactory(1234567, 1000); @@ -42,13 +55,10 @@ public class SubscriptionCacheFactoryTest { 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); } @@ -57,7 +67,6 @@ public class SubscriptionCacheFactoryTest { public void deleteTest() { subscriptionCacheFactory.delete("testKey"); assertNull(subscriptionCacheFactory.get("testKey")); - } @Test diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/errors/SpringExceptionMapperTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/errors/SpringExceptionMapperTest.java index 8594ff84d..718055c61 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/errors/SpringExceptionMapperTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/errors/SpringExceptionMapperTest.java @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.errors; import org.junit.Before; @@ -48,36 +62,35 @@ public class SpringExceptionMapperTest { 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(); + + assertNotNull(actual); 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(); + + assertNotNull(actual); assertEquals(401, appError.getCode()); assertEquals("Sample Access Denied Exception", appError.getMessage()); assertEquals("Unauthorized", appError.getReason()); - } @Test @@ -85,31 +98,30 @@ public class SpringExceptionMapperTest { 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); + 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(); + + assertNotNull(actual); assertEquals("Unknown error", appError.getReason()); assertEquals("Sample IO Exception", appError.getMessage()); assertEquals(503, appError.getCode()); - } } \ No newline at end of file diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/logging/ResponseLogFilterTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/logging/ResponseLogFilterTest.java index b9c79256c..c78c8743f 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/logging/ResponseLogFilterTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/logging/ResponseLogFilterTest.java @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.logging; import org.junit.Before; diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTest.java index 38c6e32ec..783597c91 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTest.java @@ -16,7 +16,6 @@ package org.opengroup.osdu.notification.service; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,6 +36,7 @@ import org.opengroup.osdu.notification.auth.interfaces.SecretAuth; import java.util.HashMap; import java.util.Map; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -82,7 +82,6 @@ public class NotificationHandlerTest { value.setKey("keyFile"); secret.setValue(value); gsa_subscription.setSecret(secret); - } private static void setHmac_subscription() { @@ -109,7 +108,7 @@ public class NotificationHandlerTest { when(this.secretAuth.getPushUrl(gsa_subscription.getPushEndpoint())).thenReturn(gsa_subscription.getPushEndpoint()); when(this.secretAuth.getRequestHeaders()).thenReturn(headers); HttpResponse response = this.sut.notifySubscriber(NOTIFICATION_ID, PUBSUB_MESSAGE, headers); - Assert.assertEquals(200, response.getResponseCode()); + assertEquals(200, response.getResponseCode()); } @Test @@ -122,13 +121,12 @@ public class NotificationHandlerTest { when(this.secretAuth.getPushUrl(hmac_subscription.getPushEndpoint())).thenReturn(hmac_subscription.getPushEndpoint()); when(this.secretAuth.getRequestHeaders()).thenReturn(headers); HttpResponse response = this.sut.notifySubscriber(NOTIFICATION_ID, PUBSUB_MESSAGE, headers); - Assert.assertEquals(200, response.getResponseCode()); + assertEquals(200, response.getResponseCode()); } @Test(expected = SubscriptionException.class) public void should_throwException_whenSubscriptionHandlerThrowsException() throws Exception { Map<String, String> headers = new HashMap<String, String>(); - when(subscriptionHandler.getSubscriptionFromCache(NOTIFICATION_ID)).thenThrow(new SubscriptionException("error", response)); this.sut.notifySubscriber(NOTIFICATION_ID, PUBSUB_MESSAGE, headers); fail("should throw SubscriptionException"); diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTest.java index ff1be1785..f917f1fe9 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTest.java @@ -63,7 +63,6 @@ public class SubscriptionHandlerTest { fail("should throw AppException"); } - @Test public void should_return200_whenSubscriptionGotFromRegistration() throws Exception { when(this.subscriptionCacheFactory.get(any())).thenReturn(null); diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/utils/NotificationFilterTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/utils/NotificationFilterTest.java index ba2c6125b..ed6d109d7 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/utils/NotificationFilterTest.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/utils/NotificationFilterTest.java @@ -1,3 +1,17 @@ +// Copyright © Microsoft Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package org.opengroup.osdu.notification.utils; import org.junit.Before; @@ -41,7 +55,6 @@ public class NotificationFilterTest { @Mock private HttpServletResponse servletResponse; - @Before public void setup() throws ServletException, IOException { MockitoAnnotations.initMocks(NotificationFilterTest.this); @@ -53,7 +66,6 @@ public class NotificationFilterTest { notificationFilter.doFilter(servletRequest, servletResponse, filterChain); assertNotNull(dpsHeaders.getHeaders().get("correlation-id")); - assertEquals(dpsHeaders.getHeaders().get("content-type"),"application/json"); - + assertEquals(dpsHeaders.getHeaders().get("content-type"), "application/json"); } } \ No newline at end of file -- GitLab