Commit e9ac77c8 authored by Komal Makkar's avatar Komal Makkar
Browse files

Added tests

parent 187df94e
......@@ -158,11 +158,6 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<excludes>
<exclude>**/*Application.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
......
......@@ -19,11 +19,8 @@ import org.opengroup.osdu.core.common.model.search.IdToken;
import org.opengroup.osdu.core.common.provider.interfaces.IJwtCache;
import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.inject.Named;
@Component
public class JwtCache implements IJwtCache<String, IdToken> {
private VmCache<String, IdToken> cache;
......@@ -31,7 +28,9 @@ public class JwtCache implements IJwtCache<String, IdToken> {
// Azure service account id_token can be requested only for 1 hr
private final static int EXPIRED_AFTER = 59;
public JwtCache(@Named("MAX_CACHE_VALUE_SIZE") String cacheSize){
@Autowired
public JwtCache(AppProperties appProperties){
String cacheSize = appProperties.getMaxCacheSize();
cache = new VmCache<>(EXPIRED_AFTER * 60, Integer.parseInt(cacheSize));
}
......
......@@ -17,16 +17,12 @@ package org.opengroup.osdu.notification.provider.azure.util;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.notification.provider.interfaces.IAppProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Configuration;
import javax.inject.Named;
@Component
@Configuration
public class AppProperties implements IAppProperties {
@Value("${app.entitlements}")
......@@ -44,15 +40,9 @@ public class AppProperties implements IAppProperties {
@Value("${aad.oboApi}")
private String aadOboAPI;
@Value("${app.maxCacheSize")
private String CacheValueSize;
@Autowired
private SecretClient secretClient;
@Autowired
private JaxRsDpsLog logger;
private String authURL;
private String authClientID;
......@@ -97,8 +87,6 @@ public class AppProperties implements IAppProperties {
return this.authURL;
}
@Bean
@Named("MAX_CACHE_VALUE_SIZE")
public String getMaxCacheSize() {
return maxCacheSize;
}
......@@ -106,13 +94,11 @@ public class AppProperties implements IAppProperties {
private 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));
throw new IllegalStateException(String.format("No secret found with name %s", secretName));
}
String secretValue = secret.getValue();
if (secretValue == null) {
logger.error(String.format("Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
throw new IllegalStateException(String.format(
"Secret unexpectedly missing from KeyVault response for secret with name %s", secretName));
}
......
/*
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
......@@ -15,41 +14,145 @@
package org.opengroup.osdu.notification.cache;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.search.IdToken;
import org.opengroup.osdu.notification.provider.azure.cache.JwtCache;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import javax.inject.Inject;
import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
//@RunWith(MockitoJUnitRunner.class)
@RunWith(SpringRunner.class)
@RunWith(MockitoJUnitRunner.class)
public class JWTCacheTest {
private static String testTenantName = "testTenant";
@InjectMocks
private JwtCache sut;
@Mock
private AppProperties appProperties;
@MockBean(name = "MAX_CACHE_VALUE_SIZE")
private String CacheSize;
@Mock
private IdToken idToken;
private JwtCache sut;
@Before
public void setup() {
initMocks(this);
when(appProperties.getMaxCacheSize()).thenReturn("10");
sut = new JwtCache(appProperties);
}
@Test
public void should_throwWhenCacheSizeIsInvalid_JwtCache(){
// Set up
when(appProperties.getMaxCacheSize()).thenReturn(null);
try {
// Act
sut = new JwtCache(appProperties);
sut = new JwtCache( );
// Assert
fail("Should throw exception");
} catch (Exception exception){
Assertions.assertEquals(exception.getClass(), NumberFormatException.class);
}
// Set Up
when(appProperties.getMaxCacheSize()).thenReturn("");
try {
// Act
sut = new JwtCache(appProperties);
// Assert
fail("Should throw exception");
} catch (Exception exception){
Assertions.assertEquals(exception.getClass(), NumberFormatException.class);
}
// Set Up
when(appProperties.getMaxCacheSize()).thenReturn("-1");
try {
// Act
sut = new JwtCache(appProperties);
// Assert
fail("Should throw exception");
} catch (Exception exception){
Assertions.assertEquals(exception.getClass(), IllegalArgumentException.class);
}
}
@Test
public void should_return_getPut(){
// Act
IdToken observed = sut.get("test Tenant");
// Assert
Assert.assertNull(observed);
// Set Up
sut.put(testTenantName, idToken);
// Act
observed = sut.get(testTenantName);
// Asset
Assert.assertEquals(idToken, observed);
// Act
observed = sut.get("notInCache");
// Assert
Assert.assertNull(observed);
try {
// Act
sut.put(null, null);
} catch (Exception e){
Assert.assertEquals(NullPointerException.class, e.getClass());
}
}
@Test
public void put_should_return(){
sut.put("",null);
public void should_returnAppropriate_delete() {
// Set Up
sut.put(testTenantName, idToken);
// Act
sut.delete(testTenantName);
// Assert
IdToken observed = sut.get(testTenantName);
Assert.assertNull(observed);
try {
// Act
sut.delete("notInCache");
// Assert
} catch (Exception e){
Assert.assertEquals(NullPointerException.class, e.getClass());
}
}
@Test
public void should_returnAppropriate_clearAll() {
// Set Up
sut.put(testTenantName, idToken);
// Act
sut.clearAll();
// Assert
IdToken observed = sut.get(testTenantName);
Assert.assertNull(observed);
}
}
*/
/*
// 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.security;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.opengroup.osdu.notification.provider.azure.security.SecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
import org.springframework.security.config.annotation.web.configurers.HttpBasicConfigurer;
import org.springframework.web.context.WebApplicationContext;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
public class SecurityConfigTest {
@InjectMocks
private SecurityConfig sut;
@Mock
HttpSecurity httpSecurity;
@Mock
HttpBasicConfigurer<HttpSecurity> httpBasicConfigurer;
@Mock
CsrfConfigurer<HttpSecurity> csrfConfigurer;
@Before
public void init() {
initMocks(this);
}
@Test
public void testConfigure() throws Exception {
when(httpSecurity.httpBasic()).thenReturn(httpBasicConfigurer);
when(httpSecurity.csrf()).thenReturn(csrfConfigurer);
when(httpBasicConfigurer.disable()).thenReturn(httpSecurity);
when(csrfConfigurer.disable()).thenReturn(httpSecurity);
try {
sut.configure(httpSecurity);
//verify(this.httpSecurity, times(1)).csrf().disable();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
*/
......@@ -20,7 +20,6 @@ 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;
......@@ -28,7 +27,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.MockitoAnnotations.initMocks;
public class AppPropertiesTests {
public class AppPropertiesTest {
@InjectMocks
private AppProperties sut;
@Mock
private SecretClient kv;
......@@ -36,12 +38,6 @@ public class AppPropertiesTests {
@Mock
private KeyVaultSecret secret;
@Mock
private JaxRsDpsLog logger;
@InjectMocks
private AppProperties sut;
@Before
public void init() {
initMocks(this);
......@@ -50,30 +46,13 @@ public class AppPropertiesTests {
@Test
public void should_throwWhenSecretNameIsNull_getKeyVaultSecret() {
// Set-Up
doReturn(null).when(kv).getSecret("secret-name");
doReturn(null).when(kv).getSecret("app-dev-sp-password");
// Act
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
sut.getAuthClientSecret();
});
IllegalStateException exception = assertThrows(IllegalStateException.class, () -> sut.getAuthClientSecret());
// 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.getAuthClientSecret();
});
// Assert
assertEquals("Secret unexpectedly missing from KeyVault response for secret with name secret-name", exception.getMessage());
assertEquals("No secret found with name app-dev-sp-password", exception.getMessage());
}
@Test
......@@ -105,14 +84,13 @@ public class AppPropertiesTests {
@Test
public void should_returnRightAuthURL_getCosmosKey() {
// Set-Up
doReturn("cosmos-endpoint-secret").when(secret).getValue();
doReturn("test").when(secret).getValue();
doReturn(secret).when(kv).getSecret("app-dev-sp-tenant-id");
// Act
String secretValue = sut.getAuthURL();
// Assert
assertEquals( "cosmos-endpoint-secret", secretValue);
assertEquals( "https://login.microsoftonline.com/test/oauth2/token/", secretValue);
}
}
......@@ -20,7 +20,6 @@ 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.AzureCosmosProperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
......@@ -39,9 +38,6 @@ public class AzureCosmosPropertiesTest {
@Mock
private KeyVaultSecret secret;
@Mock
private JaxRsDpsLog logger;
@Before
public void init() {
initMocks(this);
......
......@@ -26,8 +26,8 @@ import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
import org.opengroup.osdu.notification.provider.azure.util.AzureServiceAccountValidatorImpl;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(MockitoJUnitRunner.class)
public class AzureServiceAccountValidatorImplTest {
private static String invalidAADClientID = "testInvalidAADClientID";
private static String invalidJWT = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJkdW1teUBkdW1teS5jb20iLCJpc3MiOiJkdW1teUBkdW1teS5jb20iLCJhdWQiOiJkdW1teS5kdW1teS5jb20iLCJpYXQiOjE1NTYxMzcyNzMsImV4cCI6MTU1NjIzMDk3OSwicHJvdmlkZXIiOiJkdW1teS5jb20iLCJjbGllbnQiOiJkdW1teS5jb20iLCJ1c2VyaWQiOiJkdW1teXRlc3Rlci5jb20iLCJlbWFpbCI6ImR1bW15dGVzdGVyLmNvbSIsImF1dGh6IjoiIiwibGFzdG5hbWUiOiJkdW1teSIsImZpcnN0bmFtZSI6ImR1bW15IiwiY291bnRyeSI6IiIsImNvbXBhbnkiOiIiLCJqb2J0aXRsZSI6IiIsInN1YmlkIjoiZHVtbXlpZCIsImlkcCI6ImR1bW15IiwiaGQiOiJkdW1teS5jb20iLCJkZXNpZCI6ImR1bW15aWQiLCJjb250YWN0X2VtYWlsIjoiZHVtbXlAZHVtbXkuY29tIiwianRpIjoiNGEyMWYyYzItZjU5Yy00NWZhLTk0MTAtNDNkNDdhMTg4ODgwIn0.nkiyKtfXXxAlC60iDjXuB2EAGDfZiVglP-CyU1T4etc";
......@@ -41,6 +41,7 @@ public class AzureServiceAccountValidatorImplTest {
@Before
public void setup() {
initMocks(this);
when(this.appProperties.getAadClientID()).thenReturn(invalidAADClientID);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment