From 1352f6659c4256d28cab16527b2256bf1fccd8e3 Mon Sep 17 00:00:00 2001
From: nikhilsingh <nikhilsingh@microsoft.com>
Date: Tue, 22 Jun 2021 11:09:38 +0530
Subject: [PATCH] Commit 4 contents : 1-Indentation and Review comments

---
 .../osdu/notification/api/PubsubEndpoint.java |  5 ++-
 .../osdu/notification/auth/GsaAuth.java       |  1 +
 .../osdu/notification/auth/HmacAuth.java      |  5 ++-
 .../auth/factory/AuthFactory.java             |  8 ++--
 .../auth/interfaces/SecretAuth.java           |  2 +
 .../service/NotificationHandler.java          | 40 ++++++++++---------
 .../service/SubscriptionHandler.java          | 10 ++---
 .../notification/api/PubsubEndpointTests.java |  1 +
 .../service/NotificationHandlerTests.java     | 26 +++++++-----
 .../service/SubscriptionHandlerTests.java     |  4 +-
 10 files changed, 59 insertions(+), 43 deletions(-)

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 87f0da6d0..91cb0d840 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 94e9bdddf..83fd5820e 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 9369abf59..256ea88d1 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 be9465083..c80b427c9 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 15e872147..891efe1af 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 3e14f8f91..931fcbd6f 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 657feeae0..016ca4f49 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 199a9d26f..003d26d00 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 67b1dcab0..27f262b42 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 90935db8e..1cf0b3a91 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() {
-- 
GitLab