From 50d9062ab591b4f3d860de99fb898d86a0b90f1e Mon Sep 17 00:00:00 2001
From: komakkar <komakkar@microsoft.com>
Date: Fri, 4 Sep 2020 14:08:04 +0530
Subject: [PATCH] Added Tests.

---
 provider/notification-azure/pom.xml           |  28 +++--
 .../provider/azure/util/AppProperties.java    |   2 +-
 .../azure/util/AzureCosmosProperties.java     |   3 +-
 .../pubsub/EventGridHandshakeHandlerTest.java |   6 +-
 .../EventGridRequestBodyExtractorTest.java    |   2 +-
 .../notificaion/util/AppPropertiesTests.java  | 118 ++++++++++++++++++
 .../util/AzureCosmosPropertiesTest.java       |  27 ++--
 .../AzureServiceAccountValidatorImplTest.java |   4 +-
 .../util/ServiceAccountClientImplTest.java    |   2 +-
 .../notification/util/AppPropertiesTests.java |  34 -----
 10 files changed, 165 insertions(+), 61 deletions(-)
 rename provider/notification-azure/src/test/java/org/opengroup/osdu/{notification => notificaion}/pubsub/EventGridHandshakeHandlerTest.java (94%)
 rename provider/notification-azure/src/test/java/org/opengroup/osdu/{notification => notificaion}/pubsub/EventGridRequestBodyExtractorTest.java (99%)
 create mode 100644 provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AppPropertiesTests.java
 rename provider/notification-azure/src/test/java/org/opengroup/osdu/{notification => notificaion}/util/AzureCosmosPropertiesTest.java (82%)
 rename provider/notification-azure/src/test/java/org/opengroup/osdu/{notification => notificaion}/util/AzureServiceAccountValidatorImplTest.java (94%)
 rename provider/notification-azure/src/test/java/org/opengroup/osdu/{notification => notificaion}/util/ServiceAccountClientImplTest.java (99%)
 delete mode 100644 provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AppPropertiesTests.java

diff --git a/provider/notification-azure/pom.xml b/provider/notification-azure/pom.xml
index 0e44f42fc..95a5b1e22 100644
--- a/provider/notification-azure/pom.xml
+++ b/provider/notification-azure/pom.xml
@@ -110,13 +110,6 @@
             <version>2.1.6.RELEASE</version>
         </dependency>
 
-        <!--<dependency>
-            <groupId>org.mockito</groupId>
-            <artifactId>mockito-core</artifactId>
-            <version>3.0.0</version>
-            <scope>test</scope>
-        </dependency>-->
-
         <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter</artifactId>
@@ -130,9 +123,26 @@
             <version>2.23.0</version>
             <scope>test</scope>
         </dependency>
-
     </dependencies>
 
+    <repositories>
+        <repository>
+            <id>${gitlab-server}</id>
+            <url>https://community.opengroup.org/api/v4/groups/17/-/packages/maven</url>
+        </repository>
+    </repositories>
+
+    <distributionManagement>
+        <repository>
+            <id>${gitlab-server}</id>
+            <url>https://community.opengroup.org/api/v4/projects/44/packages/maven</url>
+        </repository>
+        <snapshotRepository>
+            <id>${gitlab-server}</id>
+            <url>https://community.opengroup.org/api/v4/projects/44/packages/maven</url>
+        </snapshotRepository>
+    </distributionManagement>
+
     <build>
         <plugins>
             <plugin>
@@ -165,7 +175,7 @@
                         <configuration>
                             <classifier>spring-boot</classifier>
                             <mainClass>
-                                org.opengroup.osdu.notification.provider.azure.Application
+                                azure.Application
                             </mainClass>
                         </configuration>
                     </execution>
diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java
index 32f048a46..8cbc9bb09 100644
--- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java
+++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AppProperties.java
@@ -103,7 +103,7 @@ public class AppProperties implements IAppProperties {
         return maxCacheSize;
     }
 
-    private String getKeyVaultSecret(SecretClient kv, String secretName) {
+    public String getKeyVaultSecret(SecretClient kv, String secretName) {
         KeyVaultSecret secret = kv.getSecret(secretName);
         if (secret == null) {
             logger.error(String.format("Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java
index 81bca28a6..258c92293 100644
--- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java
+++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/AzureCosmosProperties.java
@@ -6,11 +6,12 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
 import org.springframework.stereotype.Component;
 
 import javax.inject.Named;
 
-@Component
+@Configuration
 public class AzureCosmosProperties {
 
     @Value("${tenantinfo.container.name}")
diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/pubsub/EventGridHandshakeHandlerTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/pubsub/EventGridHandshakeHandlerTest.java
similarity index 94%
rename from provider/notification-azure/src/test/java/org/opengroup/osdu/notification/pubsub/EventGridHandshakeHandlerTest.java
rename to provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/pubsub/EventGridHandshakeHandlerTest.java
index 68f87776f..dd3fafc8f 100644
--- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/pubsub/EventGridHandshakeHandlerTest.java
+++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/pubsub/EventGridHandshakeHandlerTest.java
@@ -13,7 +13,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.opengroup.osdu.notification.pubsub;
+package org.opengroup.osdu.notificaion.pubsub;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -46,7 +46,7 @@ public class EventGridHandshakeHandlerTest {
     private EventGridHandshakeHandler sut;
 
     @Test
-    public void should_returnValidResponse_getHandshakeResponse() throws IOException {
+    public void should_returnValidResponse_getHandshakeResponse() {
         // Set up
         when(sut.getHandshakeResponse()).thenReturn("testValidationCode");
         String expectedResponse = "{\"ValidationResponse\":\"testValidationCode\"}";
@@ -59,7 +59,7 @@ public class EventGridHandshakeHandlerTest {
     }
 
     @Test
-    public void should_throw_getHandshakeResponse() throws IOException {
+    public void should_throw_getHandshakeResponse() {
         // Set up
         when(sut.getHandshakeResponse())
                 .thenThrow(new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error", "" ));
diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/pubsub/EventGridRequestBodyExtractorTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/pubsub/EventGridRequestBodyExtractorTest.java
similarity index 99%
rename from provider/notification-azure/src/test/java/org/opengroup/osdu/notification/pubsub/EventGridRequestBodyExtractorTest.java
rename to provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/pubsub/EventGridRequestBodyExtractorTest.java
index ad8754131..c00bd0fc3 100644
--- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/pubsub/EventGridRequestBodyExtractorTest.java
+++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/pubsub/EventGridRequestBodyExtractorTest.java
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.opengroup.osdu.notification.pubsub;
+package org.opengroup.osdu.notificaion.pubsub;
 
 import org.junit.Assert;
 import org.junit.Test;
diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AppPropertiesTests.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AppPropertiesTests.java
new file mode 100644
index 000000000..0d612026b
--- /dev/null
+++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AppPropertiesTests.java
@@ -0,0 +1,118 @@
+// 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.notificaion.util;
+
+import com.azure.security.keyvault.secrets.SecretClient;
+import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
+import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+public class AppPropertiesTests {
+
+    @Mock
+    private SecretClient kv;
+
+    @Mock
+    private KeyVaultSecret secret;
+
+    @Mock
+    private JaxRsDpsLog logger;
+
+    @InjectMocks
+    private AppProperties sut;
+
+    @Before
+    public void init() {
+        initMocks(this);
+    }
+
+    @Test
+    public void should_throwWhenSecretNameIsNull_getKeyVaultSecret() {
+        // Set-Up
+        doReturn(null).when(kv).getSecret("secret-name");
+
+        // Act
+        IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
+            sut.getKeyVaultSecret(kv, "secret-name");
+        });
+
+        // Assert
+        assertEquals("No secret found with name secret-name", exception.getMessage());
+    }
+
+    @Test
+    public void should_throwWhenSecretIsMissing_getKeyVaultSecret() {
+        // Set-Up
+        doReturn(null).when(secret).getValue();
+        doReturn(secret).when(kv).getSecret("secret-name");
+
+        // Act
+        IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
+            sut.getKeyVaultSecret(kv, "secret-name");
+        });
+
+        // Assert
+        assertEquals("Secret unexpectedly missing from KeyVault response for secret with name secret-name", exception.getMessage());
+    }
+
+    @Test
+    public void should_returnRightClientAuth_getCosmosKey() {
+        // Set-Up
+        doReturn("client-auth-secret").when(secret).getValue();
+        doReturn(secret).when(kv).getSecret("app-dev-sp-password");
+
+        // Act
+        String secretValue = sut.getAuthClientSecret();
+
+        // Assert
+        assertEquals( "client-auth-secret", secretValue);
+    }
+
+    @Test
+    public void should_returnRightCosmosSecret_getCosmosKey() {
+        // Set-Up
+        doReturn("cosmos-endpoint-secret").when(secret).getValue();
+        doReturn(secret).when(kv).getSecret("app-dev-sp-username");
+
+        // Act
+        String secretValue = sut.getAuthClientID();
+
+        // Assert
+        assertEquals( "cosmos-endpoint-secret", secretValue);
+    }
+
+    @Test
+    public void should_returnRightAuthURL_getCosmosKey() {
+        // Set-Up
+        doReturn("cosmos-endpoint-secret").when(secret).getValue();
+        doReturn(secret).when(kv).getSecret("app-dev-sp-tenant-id");
+
+        // Act
+        String secretValue = sut.getAuthURL();
+
+        // Assert
+        assertEquals( "cosmos-endpoint-secret", secretValue);
+    }
+
+}
diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureCosmosPropertiesTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AzureCosmosPropertiesTest.java
similarity index 82%
rename from provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureCosmosPropertiesTest.java
rename to provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AzureCosmosPropertiesTest.java
index 5c424e74c..a58cfabb5 100644
--- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureCosmosPropertiesTest.java
+++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AzureCosmosPropertiesTest.java
@@ -12,22 +12,22 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.opengroup.osdu.notification.util;
+package org.opengroup.osdu.notificaion.util;
 
 import com.azure.security.keyvault.secrets.SecretClient;
 import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.Before;
+import org.junit.Test;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
+import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
 import org.opengroup.osdu.notification.provider.azure.util.AzureCosmosProperties;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.MockitoAnnotations.initMocks;
 
-@ExtendWith(MockitoExtension.class)
 public class AzureCosmosPropertiesTest {
 
     @InjectMocks
@@ -39,8 +39,16 @@ public class AzureCosmosPropertiesTest {
     @Mock
     private KeyVaultSecret secret;
 
+    @Mock
+    private JaxRsDpsLog logger;
+
+    @Before
+    public void init() {
+        initMocks(this);
+    }
+
     @Test
-    public void kvSecretChecksForNullResponse() {
+    public void should_throwWhenSecretNameIsNull_getKeyVaultSecret() {
         // Set-Up
         doReturn(null).when(kv).getSecret("secret-name");
 
@@ -52,8 +60,9 @@ public class AzureCosmosPropertiesTest {
         // Assert
         assertEquals("No secret found with name secret-name", exception.getMessage());
     }
+
     @Test
-    public void kvSecretChecksForNullValueWithinResponse() {
+    public void should_throwWhenSecretIsMissing_getKeyVaultSecret() {
         // Set-Up
         doReturn(null).when(secret).getValue();
         doReturn(secret).when(kv).getSecret("secret-name");
@@ -68,7 +77,7 @@ public class AzureCosmosPropertiesTest {
     }
 
     @Test
-    public void configReturnsCorrectSecretCosmosKey() {
+    public void should_returnRightCosmosKey_getCosmosKey() {
         // Set-Up
         doReturn("cosmos-key-secret").when(secret).getValue();
         doReturn(secret).when(kv).getSecret("cosmos-primary-key");
@@ -81,7 +90,7 @@ public class AzureCosmosPropertiesTest {
     }
 
     @Test
-    public void configReturnsCorrectSecretCosmosEndpoint() {
+    public void should_returnRightCosmosSecret_getCosmosKey() {
         // Set-Up
         doReturn("cosmos-endpoint-secret").when(secret).getValue();
         doReturn(secret).when(kv).getSecret("cosmos-endpoint");
diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureServiceAccountValidatorImplTest.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AzureServiceAccountValidatorImplTest.java
similarity index 94%
rename from provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureServiceAccountValidatorImplTest.java
rename to provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AzureServiceAccountValidatorImplTest.java
index 0899d9a4c..6589e257e 100644
--- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AzureServiceAccountValidatorImplTest.java
+++ b/provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/AzureServiceAccountValidatorImplTest.java
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.opengroup.osdu.notification.util;
+package org.opengroup.osdu.notificaion.util;
 
 import org.junit.Assert;
 import org.junit.Before;
@@ -41,7 +41,7 @@ public class AzureServiceAccountValidatorImplTest {
 
     @Before
     public void setup() {
-        when(this.appProperties.getAadClientID()).thenReturn(this.invalidAADClientID);
+        when(this.appProperties.getAadClientID()).thenReturn(invalidAADClientID);
     }
 
     @Test
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/notificaion/util/ServiceAccountClientImplTest.java
similarity index 99%
rename from provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java
rename to provider/notification-azure/src/test/java/org/opengroup/osdu/notificaion/util/ServiceAccountClientImplTest.java
index d7099cb5a..4e991a7db 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/notificaion/util/ServiceAccountClientImplTest.java
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.opengroup.osdu.notification.util;
+package org.opengroup.osdu.notificaion.util;
 
 import org.apache.http.HttpStatus;
 import org.junit.Assert;
diff --git a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AppPropertiesTests.java b/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AppPropertiesTests.java
deleted file mode 100644
index 72b605e1a..000000000
--- a/provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/AppPropertiesTests.java
+++ /dev/null
@@ -1,34 +0,0 @@
-// 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.util;
-
-import com.azure.security.keyvault.secrets.SecretClient;
-import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-import org.opengroup.osdu.notification.provider.azure.util.AzureCosmosProperties;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.Mockito.doReturn;
-
-@ExtendWith(MockitoExtension.class)
-public class AppPropertiesTests {
-
-
-}
-- 
GitLab