Skip to content
Snippets Groups Projects
Commit 9af3cc16 authored by David Diederich's avatar David Diederich
Browse files

Merge remote-tracking branch 'origin/logs_jacoco_changes' into master

Manual merge to rescue !23
parents 24ad1e73 f4ebadef
No related branches found
No related tags found
1 merge request!23Added UTs
Pipeline #10803 passed with warnings
Showing
with 400 additions and 99 deletions
# The file makes Jacoco understand that the Lambok's @Data's creation, should not be accounted for Jacoco's analysis.
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
\ No newline at end of file
......@@ -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));
}
......
......@@ -14,37 +14,17 @@
package org.opengroup.osdu.notification.provider.azure.security;
import com.microsoft.azure.spring.autoconfigure.aad.AADAppRoleStatelessAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AADAppRoleStatelessAuthenticationFilter appRoleAuthFilter;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and()
.authorizeRequests()
.antMatchers("/",
"/swagger",
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui.html",
"/webjars/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(appRoleAuthFilter, UsernamePasswordAuthenticationFilter.class);
public void configure(HttpSecurity http) throws Exception {
http.httpBasic().disable().csrf().disable();
}
}
......@@ -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));
}
......
......@@ -14,20 +14,17 @@
package org.opengroup.osdu.notification.provider.azure.util;
import lombok.SneakyThrows;
import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount;
import org.springframework.stereotype.Component;
import javax.naming.AuthenticationNotSupportedException;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
@Component
public class GoogleServiceAccountImpl implements IGoogleServiceAccount {
@SneakyThrows
@Override
public String getIdToken(String keyString, String audience) {
// TODO : Check if it is to be supported
throw new AuthenticationNotSupportedException();
throw new NotImplementedException();
}
}
// 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.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.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.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)
public class JWTCacheTest {
private static String testTenantName = "testTenant";
@Mock
private AppProperties appProperties;
@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);
// 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 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.di;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient;
import org.opengroup.osdu.notification.provider.azure.di.ServiceAccountJwtClientFactory;
import org.opengroup.osdu.notification.provider.azure.util.ServiceAccountJwtAzureClientImpl;
import static org.junit.Assert.fail;
import static org.mockito.MockitoAnnotations.initMocks;
public class ServiceAccountJwtClientFactoryTest {
@InjectMocks
ServiceAccountJwtClientFactory sut;
@Before
public void init() {
initMocks(this);
}
@Test
public void testGettingInstance() {
try {
// Act
Class<?> objectType = sut.getObjectType();
// Assert
Assert.assertEquals(IServiceAccountJwtClient.class, objectType);
// Act
IServiceAccountJwtClient serviceAccountJwtClient = sut.createInstance();
// Assert
Assert.assertNotNull("Should not be null", serviceAccountJwtClient);
Assert.assertEquals(serviceAccountJwtClient.getClass(), ServiceAccountJwtAzureClientImpl.class);
} catch (Exception exception) {
fail("Should not throw this exception" + exception.getMessage());
}
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@ package org.opengroup.osdu.notification.provider.azure;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
......@@ -31,6 +32,7 @@ import java.io.StringReader;
import java.util.Map;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
......@@ -113,13 +115,13 @@ public class EventGridRequestBodyExtractorTest {
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutAttributes));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -154,7 +156,7 @@ public class EventGridRequestBodyExtractorTest {
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutData));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -183,13 +185,13 @@ public class EventGridRequestBodyExtractorTest {
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n"+
" \"data\": \"dGVzdA==\",\n"+
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(requestRootWithoutDataPartitionId));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -219,16 +221,16 @@ public class EventGridRequestBodyExtractorTest {
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes \"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
String expectedData = "[{\"kind\":\"testkind\",\"id\":\"testid\",\"operationtype\":\"create\"},{\"kind\":\"testkind2\",\"id\":\"testid2\",\"operationtype\":\"update\"}]";
String expectedData = "test";
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -253,13 +255,13 @@ public class EventGridRequestBodyExtractorTest {
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes\"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
......@@ -301,4 +303,110 @@ public class EventGridRequestBodyExtractorTest {
// Assert
Assert.assertEquals(observedResponse, expectedResponse);
}
@Test
public void should_throwWhenNotHandshakeRequest_getHandshakeResponse() throws IOException {
// Set up
String validHandshakeRequestRoot =
"[{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes\"\n" +
" },\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
String expectedResponse = null;
BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
String observedResponse = this.sut.getValidationCodeForHandshake();
// Assert
Assert.assertNull(observedResponse);
}
@Test
public void should_throwWhenHandshakeRequest_extractDataFromRequestBody() throws IOException {
String inVaidRequestRoot = " [{\n" +
" \"id\": \"testId\",\n" +
" \"topic\": \"testTopic\",\n" +
" \"subject\": \"\",\n" +
" \"data\": {\n" +
" \"validationCode\": \"testValidationCode\",\n" +
" \"validationUrl\": \"testURL\"\n" +
" },\n" +
" \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" +
" \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"dataVersion\": \"2\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(inVaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
Map<String, String> observedAttributes = this.sut.extractAttributesFromRequestBody();
Assert.assertNull(observedAttributes);
}
@Test
public void should_returnNotificationId_extractNotificationIdFromRequestBody() throws IOException {
// Set Up
String vaidRequestRoot = "[{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes\"\n" +
" },\n" +
" \"data\": \"dGVzdA==\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
when(this.httpServletRequest.getHeader("Aeg-Subscription-Name")).thenReturn("NotificationId");
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
String observed = sut.extractNotificationIdFromRequestBody();
// Assert
Assert.assertEquals("NotificationId", observed);
// Set Up
when(this.httpServletRequest.getHeader("Aeg-Subscription-Name")).thenReturn(null);
try {
// Act
this.sut.extractNotificationIdFromRequestBody();
// Asset
fail("Should Throw Exception");
} catch (AppException appException){
Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode());
Assert.assertEquals("Subscription ID not found", appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
}
......@@ -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);
......@@ -53,9 +49,7 @@ public class AzureCosmosPropertiesTest {
doReturn(null).when(kv).getSecret("secret-name");
// Act
IllegalStateException exception = assertThrows(IllegalStateException.class, () ->{
sut.cosmosKey(kv);
});
IllegalStateException exception = assertThrows(IllegalStateException.class, () -> sut.cosmosKey(kv));
// Assert
assertEquals("No secret found with name cosmos-primary-key", exception.getMessage());
......
......@@ -26,11 +26,11 @@ 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";
private static String invalidJWT = "invalidJWT";
@Mock
private AppProperties appProperties;
......@@ -41,6 +41,7 @@ public class AzureServiceAccountValidatorImplTest {
@Before
public void setup() {
initMocks(this);
when(this.appProperties.getAadClientID()).thenReturn(invalidAADClientID);
}
......
// 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 org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.opengroup.osdu.notification.provider.azure.util.GoogleServiceAccountImpl;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import static org.junit.Assert.fail;
public class GoogleServiceAccountImpTest {
@Test
public void should_throw_getIdToken() {
// Setup
String audience = "testAudience";
String keyString = "keyString";
try {
// Act
new GoogleServiceAccountImpl().getIdToken(keyString, audience);
// Assert
fail("Should throw exception");
} catch (Exception e) {
Assertions.assertEquals(e.getClass(), NotImplementedException.class);
}
}
}
......@@ -43,8 +43,7 @@ import static org.mockito.Mockito.when;
public class ServiceAccountClientImplTest {
final String tenantName = "Test Tenant";
final String validToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJkdW1teUBkdW1teS5jb20iLCJpc3MiOiJkdW1teUBkdW1teS5jb20iLCJhdWQiOiJkdW1teS5kdW1teS5jb20iLCJpYXQiOjE1NTYxMzcyNzMsImV4cCI6MTU1NjIzMDk3OSwicHJvdmlkZXIiOiJkdW1teS5jb20iLCJjbGllbnQiOiJkdW1teS5jb20iLCJ1c2VyaWQiOiJkdW1teXRlc3Rlci5jb20iLCJlbWFpbCI6ImR1bW15dGVzdGVyLmNvbSIsImF1dGh6IjoiIiwibGFzdG5hbWUiOiJkdW1teSIsImZpcnN0bmFtZSI6ImR1bW15IiwiY291bnRyeSI6IiIsImNvbXBhbnkiOiIiLCJqb2J0aXRsZSI6IiIsInN1YmlkIjoiZHVtbXlpZCIsImlkcCI6ImR1bW15IiwiaGQiOiJkdW1teS5jb20iLCJkZXNpZCI6ImR1bW15aWQiLCJjb250YWN0X2VtYWlsIjoiZHVtbXlAZHVtbXkuY29tIiwianRpIjoiNGEyMWYyYzItZjU5Yy00NWZhLTk0MTAtNDNkNDdhMTg4ODgwIn0.nkiyKtfXXxAlC60iDjXuB2EAGDfZiVglP-CyU1T4etc";
final String invalidToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJkdW1teUBkdW1teS5jb20iLCJpc3MiOiJkdW1teUBkdW1teS5jb20iLCJhdWQiOiJkdW1teS5kdW1teS5jb20iLCJpYXQiOjE1NTYxMzcyNzMsImV4cCI6MTU1NjIzMDk3OSwicHJvdmlkZXIiOiJkdW1teS5jb20iLCJjbGllbnQiOiJkdW1teS5jb20iLCJ1c2VyaWQiOiJkdW1teXRlc3Rlci5jb20iLCJlbWFpbCI6ImR1bW15dGVzdGVyLmNvbSIsImF1dGh6IjoiIiwibGFzdG5hbWUiOiJkdW1teSIsImZpcnN0bmFtZSI6ImR1bW15IiwiY291bnRyeSI6IiIsImNvbXBhbnkiOiIiLCJqb2J0aXRsZSI6IiIsInN1YmlkIjoiZHVtbXlpZCIsImlkcCI6ImR1bW15IiwiaGQiOiJkdW1teS5jb20iLCJkZXNpZCI6ImR1bW15aWQiLCJjb250YWN0X2VtYWlsIjoiZHVtbXlAZHVtbXkuY29tIiwianRpIjoiNGEyMWYyYzItZjU5Yy00NWZhLTk0MTAtNDNkNDdhMTg4ODgwIn0.nkiyKtfXXxAlC60iDjXuB2EAGDfZiVglP-CyU1T4etc";
final String validToken = "validToken";
@Mock
private ITenantFactory tenantInfoServiceProvider;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment