diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java b/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java index 87f0da6d01675aa0b5e76fb31a0cd90d1a939c2d..91cb0d840b7452210c696f8902e60b815e8076c9 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java @@ -28,6 +28,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.annotation.RequestScope; + import java.util.Map; @RestController @@ -58,8 +59,8 @@ public class PubsubEndpoint { } this.log.info(ACKNOWLEDGE); return ResponseEntity.ok(ACKNOWLEDGE); - }catch(Exception e) - { this.log.error("An exception occurred: " + e ); + } catch (Exception e) { + this.log.error("An exception occurred: " + e); return ResponseEntity.badRequest().body(NOT_ACKNOWLEDGE); } } diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/GsaAuth.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/GsaAuth.java index 94e9bdddf3ffd48537a3b326c41f6c479c4e3ff7..83fd5820e8f980afaf6e600ec88889dd5d669ac9 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/GsaAuth.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/GsaAuth.java @@ -24,6 +24,7 @@ import org.opengroup.osdu.notification.auth.interfaces.SecretAuth; import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; + import java.util.Map; @Component diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/HmacAuth.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/HmacAuth.java index 9369abf594160ca36fbe4000a1a0745b0d13e571..256ea88d1cef18e25b54400be958a6cfbbdc3ef1 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/HmacAuth.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/HmacAuth.java @@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; + @Component public class HmacAuth implements SecretAuth { @Autowired @@ -41,10 +42,10 @@ public class HmacAuth implements SecretAuth { try { String signedjwt = this.signatureService.getSignedSignature(endpoint, hmacSecret.getValue()); pushUrl += "?hmac=" + signedjwt; - }catch(Exception e) { + } catch (Exception e) { System.out.println("An exception occurred in signature service: " + e); } - return pushUrl ; + return pushUrl; } @Override diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/factory/AuthFactory.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/factory/AuthFactory.java index be946508388e6a77e5466258f593fb6a62a32b17..c80b427c921b0117517f95d103f4be3f79e94bf1 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/factory/AuthFactory.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/factory/AuthFactory.java @@ -30,9 +30,11 @@ public class AuthFactory { private GsaAuth gsaAuth; public SecretAuth getSecret(String secretType) { - switch(secretType) { - case "HMAC": return hmacAuth; - case "GSA": return gsaAuth; + switch (secretType) { + case "HMAC": + return hmacAuth; + case "GSA": + return gsaAuth; } return null; } diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/interfaces/SecretAuth.java b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/interfaces/SecretAuth.java index 15e872147af06e7bbb10a43348c114197383bd37..891efe1af5302ba22e944a5c1da58865981d14c6 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/auth/interfaces/SecretAuth.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/auth/interfaces/SecretAuth.java @@ -21,6 +21,8 @@ import java.util.Map; public interface SecretAuth { String getPushUrl(String endpoint); + void setSecret(Secret secret); + void getRequestHeaders(Map<String, String> requestHeader); } diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java b/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java index 3e14f8f91bd49985408777f916bad307760eb62f..931fcbd6fb39cd104436fb2e046154d38514bc7b 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java @@ -26,6 +26,7 @@ import org.opengroup.osdu.notification.auth.factory.AuthFactory; import org.opengroup.osdu.notification.auth.interfaces.SecretAuth; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; + import java.util.HashMap; import java.util.Map; @@ -41,26 +42,27 @@ public class NotificationHandler { private AuthFactory authFactory; private final int WAITING_TIME = 30000; - public HttpResponse notifySubscriber(String notificationId, String pubsubMessage, Map<String, String> headerAttributes) throws Exception{ - Subscription subscription = subscriptionHandler.getSubscriptionFromCache(notificationId); - Secret secret = subscription.getSecret(); - String endpoint = subscription.getPushEndpoint(); - String secretType = secret.getSecretType(); - String pushUrl = ""; - Map<String, String> requestHeader = new HashMap<>(); - // Authentication Secret - SecretAuth secretAuth= authFactory.getSecret(secretType); - secretAuth.setSecret(secret); - pushUrl = secretAuth.getPushUrl(endpoint); - secretAuth.getRequestHeaders(requestHeader); - requestHeader.put(DpsHeaders.CONTENT_TYPE, "application/json"); - requestHeader.put(DpsHeaders.CORRELATION_ID, headerAttributes.get(DpsHeaders.CORRELATION_ID)); - requestHeader.put(DpsHeaders.DATA_PARTITION_ID, headerAttributes.get(DpsHeaders.DATA_PARTITION_ID)); + public HttpResponse notifySubscriber(String notificationId, String pubsubMessage, Map<String, String> headerAttributes) throws Exception { + Subscription subscription = subscriptionHandler.getSubscriptionFromCache(notificationId); + Secret secret = subscription.getSecret(); + String endpoint = subscription.getPushEndpoint(); + String secretType = secret.getSecretType(); + String pushUrl = ""; + Map<String, String> requestHeader = new HashMap<>(); + // Authentication Secret + SecretAuth secretAuth = authFactory.getSecret(secretType); + secretAuth.setSecret(secret); + pushUrl = secretAuth.getPushUrl(endpoint); + secretAuth.getRequestHeaders(requestHeader); + + requestHeader.put(DpsHeaders.CONTENT_TYPE, "application/json"); + requestHeader.put(DpsHeaders.CORRELATION_ID, headerAttributes.get(DpsHeaders.CORRELATION_ID)); + requestHeader.put(DpsHeaders.DATA_PARTITION_ID, headerAttributes.get(DpsHeaders.DATA_PARTITION_ID)); - HttpRequest request = HttpRequest.post().url(pushUrl).headers(requestHeader).body(pubsubMessage).connectionTimeout(WAITING_TIME).build(); - HttpResponse response = httpClient.send(request); - this.log.info("sending out notification to endpoint: " + endpoint); - return response; + HttpRequest request = HttpRequest.post().url(pushUrl).headers(requestHeader).body(pubsubMessage).connectionTimeout(WAITING_TIME).build(); + HttpResponse response = httpClient.send(request); + this.log.info("sending out notification to endpoint: " + endpoint); + return response; } } diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/service/SubscriptionHandler.java b/notification-core/src/main/java/org/opengroup/osdu/notification/service/SubscriptionHandler.java index 657feeae05e33bb401f6b7ca5380534cde035314..016ca4f49a55e630b95bbdf0c7ace92359c25a02 100644 --- a/notification-core/src/main/java/org/opengroup/osdu/notification/service/SubscriptionHandler.java +++ b/notification-core/src/main/java/org/opengroup/osdu/notification/service/SubscriptionHandler.java @@ -30,6 +30,7 @@ import org.opengroup.osdu.core.common.notification.SubscriptionException; import org.opengroup.osdu.notification.di.SubscriptionCacheFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; + import java.io.IOException; import java.util.List; @@ -56,11 +57,9 @@ public class SubscriptionHandler { Subscription subscription = objectMapper.readValue(subscriptionString, Subscription.class); return subscription; } catch (IOException e) { - this.log.warning("Error Parsing subscription String to object."); - throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error in getting subscription for notificationId:" + notificationId, "Unexpected error in pushing message", e); + throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error Parsing subscription String to object", "Unexpected error in pushing message", e); } catch (SubscriptionException se) { - this.log.warning("Error query subscription from registration."); - throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error in getting subscription for notificationId:" + notificationId, "Unexpected error in pushing message", se); + throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error query subscription from registration", "Unexpected error in pushing message", se); } } @@ -69,7 +68,6 @@ public class SubscriptionHandler { List<Subscription> subscriptionList = service.query(notificationId); if (subscriptionList == null || subscriptionList.size() == 0) { - this.log.warning(String.format("Subscription with notification ID %s not found in registration", notificationId)); throw new AppException(HttpStatus.SC_NOT_FOUND, "Not found subscription for notificationId:" + notificationId, "Subscription not found"); } @@ -79,6 +77,7 @@ public class SubscriptionHandler { return jsonSubscription; } + //unit test purpose protected ObjectMapper getObjectMapper() { if (this.objectMapper == null) { @@ -86,6 +85,7 @@ public class SubscriptionHandler { } return this.objectMapper; } + //unit test purpose void setObjectMapper(ObjectMapper objectMapper) { this.objectMapper = objectMapper; diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java index 199a9d26ff70d553ddea50f64b9be1c8334eed5b..003d26d002b31defd3ca1c51c7667d4aa5536325 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java @@ -28,6 +28,7 @@ import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExt import org.opengroup.osdu.notification.service.NotificationHandler; import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.ResponseEntity; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTests.java b/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTests.java index 67b1dcab0afe2b2f00ff452fc24eba0f288384fd..27f262b42872f23a383f22ea7647c9db0f8b68b1 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTests.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/service/NotificationHandlerTests.java @@ -31,8 +31,10 @@ import org.opengroup.osdu.core.common.notification.SubscriptionException; import org.opengroup.osdu.notification.auth.factory.AuthFactory; import org.opengroup.osdu.notification.auth.interfaces.SecretAuth; import org.powermock.modules.junit4.PowerMockRunner; + import java.util.HashMap; import java.util.Map; + import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -59,50 +61,51 @@ public class NotificationHandlerTests { private static Subscription hmac_subscription; @BeforeClass - public static void setup(){ + public static void setup() { setHmac_subscription(); setGsa_subscription(); } + @Test public void should_return200_whenPubsubMessageValidAndSuccessCodeReturnedFromClient_gsa() throws Exception { response.setResponseCode(200); - Map<String,String> headers = new HashMap<String, String>(); + Map<String, String> headers = new HashMap<String, String>(); when(this.subscriptionHandler.getSubscriptionFromCache(this.NOTIFICATION_ID)).thenReturn(gsa_subscription); when(this.authFactory.getSecret(any())).thenReturn(secretAuth); when(this.httpClient.send(any())).thenReturn(response); when(this.secretAuth.getPushUrl(gsa_subscription.getPushEndpoint())).thenReturn(gsa_subscription.getPushEndpoint()); Mockito.doNothing().when(secretAuth).getRequestHeaders(headers); - HttpResponse response = this.sut.notifySubscriber(this.NOTIFICATION_ID,this.PUBSUB_MESSAGE,headers); + HttpResponse response = this.sut.notifySubscriber(this.NOTIFICATION_ID, this.PUBSUB_MESSAGE, headers); Assert.assertEquals(200, response.getResponseCode()); } @Test public void should_return200_whenPubsubMessageValidAndSuccessCodeReturnedFromClient_hmac() throws Exception { response.setResponseCode(200); - Map<String,String> headers = new HashMap<String, String>(); + Map<String, String> headers = new HashMap<String, String>(); when(this.subscriptionHandler.getSubscriptionFromCache(this.NOTIFICATION_ID)).thenReturn(hmac_subscription); when(this.authFactory.getSecret(any())).thenReturn(secretAuth); when(this.httpClient.send(any())).thenReturn(response); when(this.secretAuth.getPushUrl(hmac_subscription.getPushEndpoint())).thenReturn(hmac_subscription.getPushEndpoint()); Mockito.doNothing().when(secretAuth).getRequestHeaders(headers); - HttpResponse response = this.sut.notifySubscriber(this.NOTIFICATION_ID,this.PUBSUB_MESSAGE,headers); + HttpResponse response = this.sut.notifySubscriber(this.NOTIFICATION_ID, this.PUBSUB_MESSAGE, headers); Assert.assertEquals(200, response.getResponseCode()); } @Test(expected = SubscriptionException.class) public void should_throwException_whenSubscriptionHandlerThrowsException() throws Exception { - Map<String,String> headers = new HashMap<String, String>(); + Map<String, String> headers = new HashMap<String, String>(); when(this.authFactory.getSecret(any())).thenReturn(secretAuth); when(this.httpClient.send(any())).thenReturn(response); when(this.secretAuth.getPushUrl(gsa_subscription.getPushEndpoint())).thenReturn(gsa_subscription.getPushEndpoint()); Mockito.doNothing().when(secretAuth).getRequestHeaders(headers); when(subscriptionHandler.getSubscriptionFromCache(this.NOTIFICATION_ID)).thenThrow(new SubscriptionException("error", response)); - this.sut.notifySubscriber(this.NOTIFICATION_ID,this.PUBSUB_MESSAGE,headers); + this.sut.notifySubscriber(this.NOTIFICATION_ID, this.PUBSUB_MESSAGE, headers); fail("should throw SubscriptionException"); } - private static void setGsa_subscription(){ - gsa_subscription =new Subscription(); + private static void setGsa_subscription() { + gsa_subscription = new Subscription(); gsa_subscription.setName("gsa_test_subscription"); gsa_subscription.setPushEndpoint("http:///gsa-challenge"); gsa_subscription.setDescription("Description"); @@ -118,8 +121,9 @@ public class NotificationHandlerTests { gsa_subscription.setSecret(secret); } - private static void setHmac_subscription(){ - hmac_subscription =new Subscription(); + + private static void setHmac_subscription() { + hmac_subscription = new Subscription(); hmac_subscription.setName("hamc_test_subscription"); hmac_subscription.setPushEndpoint("http://challenge"); hmac_subscription.setDescription("Description"); diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTests.java b/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTests.java index 90935db8e59613d349952ad3ab8a9d37a8380ee6..1cf0b3a91a4d2a833855cc28a435bb66a4354293 100644 --- a/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTests.java +++ b/notification-core/src/test/java/org/opengroup/osdu/notification/service/SubscriptionHandlerTests.java @@ -33,9 +33,11 @@ import org.opengroup.osdu.core.common.notification.SubscriptionException; import org.opengroup.osdu.core.common.notification.SubscriptionService; import org.opengroup.osdu.notification.di.SubscriptionCacheFactory; import org.powermock.modules.junit4.PowerMockRunner; + import java.io.IOException; import java.util.ArrayList; import java.util.List; + import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -98,7 +100,7 @@ public class SubscriptionHandlerTests { queryResult.add(subscription); when(subscriptionService.query(any())).thenReturn(queryResult); Subscription response = this.sut.getSubscriptionFromCache(NOTIFICATION_ID); - Assert.assertEquals("testSubscription",response.getName()); + Assert.assertEquals("testSubscription", response.getName()); } private String getHmacSubscription() {