From a18e92fcaec89cdbf0207788a3ae70a33958fc7e Mon Sep 17 00:00:00 2001
From: komakkar <komakkar@microsoft.com>
Date: Mon, 28 Sep 2020 15:57:36 +0530
Subject: [PATCH] Revert "intermediate"

This reverts commit d10e6539
---
 .../ServiceAccountJwtAzureClientImpl.java     | 51 ++++++++++---------
 .../util/ServiceAccountClientImplTest.java    | 21 +++++---
 2 files changed, 39 insertions(+), 33 deletions(-)

diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java
index f760f531b..90720d7e6 100644
--- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java
+++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java
@@ -34,24 +34,14 @@ import java.util.concurrent.*;
 @Component
 public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClient {
 
-    private final AppProperties config;
-
-    private final ITenantFactory tenantInfoServiceProvider;
-
-    private final IJwtCache<String, IdToken> tenantJwtCache;
+    @Autowired
+    private AppProperties config;
 
-    private final ExecutorService executorService;
+    @Autowired
+    private ITenantFactory tenantInfoServiceProvider;
 
     @Autowired
-    ServiceAccountJwtAzureClientImpl(AppProperties appProperties,
-                                     ITenantFactory tenantInfoServiceProvider,
-                                     IJwtCache<String, IdToken> tenantJwtCache) {
-        this.config = appProperties;
-        this.tenantJwtCache = tenantJwtCache;
-        this.tenantInfoServiceProvider = tenantInfoServiceProvider;
-
-        this.executorService =  Executors.newFixedThreadPool(1);;
-    }
+    private IJwtCache tenantJwtCache;
 
     public String getIdToken(String tenantName) {
         TenantInfo tenant = this.tenantInfoServiceProvider.getTenantInfo(tenantName);
@@ -60,27 +50,38 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien
         }
 
         String ACCESS_TOKEN = "";
-        // TODO : Refactor to move ID token form Common.Core.model.search to Common.core
-        IdToken cachedToken = this.tenantJwtCache.get(tenant.getName());
+        ExecutorService service = null;
 
-        if ((cachedToken != null) && !IdToken.refreshToken(cachedToken)) {
-            return "Bearer " + cachedToken.getTokenValue();
-        }
+        try {
+            // TODO : Refactor to move ID token form Common.Core.model.search to Common.core
+            IdToken cachedToken = (IdToken) this.tenantJwtCache.get(tenant.getName());
 
-        ACCESS_TOKEN = getAccessToken();
-        IdToken idToken = IdToken.builder().tokenValue(ACCESS_TOKEN).expirationTimeMillis(JWT.decode(ACCESS_TOKEN).getExpiresAt().getTime()).build();
+            if ((cachedToken != null) && !IdToken.refreshToken(cachedToken)) {
+                return "Bearer " + cachedToken.getTokenValue();
+            }
 
-        this.tenantJwtCache.put(tenant.getName(), idToken);
+            // TODO : Control the thread count via config and pool should be created once.
+            service = Executors.newFixedThreadPool(1);
 
+            ACCESS_TOKEN = getAccessToken(service);
+            IdToken idToken = IdToken.builder().tokenValue(ACCESS_TOKEN).expirationTimeMillis(JWT.decode(ACCESS_TOKEN).getExpiresAt().getTime()).build();
+            this.tenantJwtCache.put(tenant.getName(), idToken);
+        }  finally {
+            if(service != null) {
+                service.shutdown();
+            }
+        }
         return "Bearer " + ACCESS_TOKEN;
     }
 
-    private String getAccessToken(){
+    // TODO : Refactor for making it test-able.
+    // THIS METHOD IS PUBLIC ONLY TO ENABLE UNIT TESTING
+    public String getAccessToken(ExecutorService service) {
         AuthenticationContext context = null;
         ClientCredential credential = null;
         String ACCESS_TOKEN = null;
         try {
-            context = new AuthenticationContext(this.config.getAuthURL(), false, this.executorService);
+            context = new AuthenticationContext(this.config.getAuthURL(), false, service);
             credential = new ClientCredential(this.config.getAuthClientID(), this.config.getAuthClientSecret());
 
             Future<AuthenticationResult> future = context.acquireToken(this.config.getAadClientID(), credential, null);
diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java
index c2004ce6e..6951d39a9 100644
--- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java
+++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java
@@ -32,10 +32,12 @@ import org.opengroup.osdu.notification.provider.azure.cache.JwtCache;
 import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
 import org.opengroup.osdu.notification.provider.azure.util.ServiceAccountJwtAzureClientImpl;
 
+import java.util.concurrent.ExecutorService;
+
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.when;
 import static org.mockito.MockitoAnnotations.initMocks;
+import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ServiceAccountClientImplTest {
@@ -53,6 +55,9 @@ public class ServiceAccountClientImplTest {
     @Mock
     private IdToken idToken;
 
+    @Mock
+    private ExecutorService executorService;
+
     @Mock
     private AppProperties appProperties;
 
@@ -109,7 +114,7 @@ public class ServiceAccountClientImplTest {
     public void should_updateCache_getIdTokenTest() {
         // Set up
         when(tenantJwtCacheMock.get(any())).thenReturn(idToken);
-        String expectedToken = "Bearer " + idToken.getTokenValue();
+        String expectedToken = "Bearer " +idToken.getTokenValue();
 
         // Act
         String returnedToken = this.sut.getIdToken(tenantName);
@@ -118,7 +123,7 @@ public class ServiceAccountClientImplTest {
         Assert.assertEquals(expectedToken, returnedToken);
     }
 
-    /*@Test
+    @Test
     public void should_return403GivenInvalidApplicationProperties_getAccessToken() {
         when(appProperties.getAuthURL()).thenReturn("https://login.microsoftonline.com/s/oauth2/token/");
         when(appProperties.getAuthClientID()).thenReturn("testAuthClientID");
@@ -127,15 +132,15 @@ public class ServiceAccountClientImplTest {
 
         try {
             // Act
-            sut.getIdToken(tenantName);
+            sut.getAccessToken(executorService);
 
             // Assert
-            //fail("Should throw exception");
+            fail("Should throw exception");
         } catch (AppException appException) {
-            //Assert.assertEquals(HttpStatus.SC_FORBIDDEN, appException.getError().getCode());
+            Assert.assertEquals(HttpStatus.SC_FORBIDDEN, appException.getError().getCode());
         } catch (Exception e) {
-            //fail("Should not throw this exception" + e.getMessage());
+            fail("Should not throw this exception" + e.getMessage());
         }
-    }*/
+    }
 }
 
-- 
GitLab