Skip to content
Snippets Groups Projects
Commit 1bf3f8d2 authored by Solomon Ayalew's avatar Solomon Ayalew
Browse files

commit f5bc5b79

Author: Solomon Ayalew <solxget@amazon.com> 
Date: Thu Aug 31 2023 15:14:14 GMT-0700 (Pacific Daylight Time) 

    adding unit testes
parent fc4b7437
No related branches found
No related tags found
1 merge request!437merge code to gitlab
......@@ -76,8 +76,9 @@ phases:
- if [ "$GIT_SECRETS_SCAN_RESULT" = "FAILED" ]; then echo "Secrets detected!" && exit 1; fi
- echo "Building primary service assemblies..."
- mvn -ntp -B test install sonar:sonar -pl .,notification-core,provider/notification-aws -Ddeployment.environment=prod -Dsonar.login=${SONAR_USERNAME} -Dsonar.password=${SONAR_PASSWORD} -Dsonar.branch.name=${BRANCH_NAME}
- mvn -ntp -B test install -pl .,notification-core,provider/notification-aws -Ddeployment.environment=prod
- mvn sonar:sonar -pl .,provider/notification-aws -Dsonar.scm.provider=git -Dsonar.login=${SONAR_USERNAME} -Dsonar.password=${SONAR_PASSWORD} -Dsonar.branch.name=${BRANCH_NAME}
- echo "Building integration testing assemblies and gathering artifacts..."
- ./testing/notification-test-aws/build-aws/prepare-dist.sh
......
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
......@@ -203,10 +203,6 @@
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<propertyName>jacocoArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>report</id>
......
......@@ -75,7 +75,7 @@ public class AwsCognitoClient {
return provider;
}
public static AWSCognitoIdentityProvider generateCognitoClient(String region)
public AWSCognitoIdentityProvider generateCognitoClient(String region)
{
if (System.getenv("AWS_COGNITO_REGION") != null) {
region = System.getenv("AWS_COGNITO_REGION");
......
package org.opengroup.osdu.notification.provider.aws.security;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.nio.ByteBuffer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.powermock.reflect.Whitebox;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.kms.AWSKMS;;
@RunWith(MockitoJUnitRunner.class)
public class KmsHelperTest {
@InjectMocks
private KmsHelper kmsHelper;
@Mock
private DpsHeaders dpsHeaders;
@Mock
private ByteBuffer ciphertext;
private static MockedConstruction<K8sLocalParameterProvider> mockedConstruction;
@BeforeClass
public static void setup() {
mockedConstruction = Mockito.mockConstruction(K8sLocalParameterProvider.class,
(mock, context) -> {
//implement initializer for mock. Set return value for object A mock methods
when(mock.getParameterAsString("notification-sqs-url")).thenReturn(
"test-sqs-url");
});
}
@AfterClass
public static void close(){
mockedConstruction.close();
}
@Before
public void initTest() {
Whitebox.setInternalState(kmsHelper, "amazonRegion", "us-east-1");
Whitebox.setInternalState(kmsHelper, "kmsEndpoint", "aws.kms.endpoint");
}
@Test
public void init_InitalizesAsExpected() {
kmsHelper.init();
}
@Test(expected = SdkClientException.class)
public void encrypt_EncryptsData() {
kmsHelper.init();
kmsHelper.encrypt("plain text");
}
@Test(expected = SdkClientException.class )
public void decryptThorwsFormMockedKmsClient() {
kmsHelper.init();
kmsHelper.decrypt(ciphertext, "dataPartitionId");
}
}
......@@ -16,6 +16,10 @@ package org.opengroup.osdu.notification.provider.aws.security;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
......@@ -24,8 +28,7 @@ import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.cryptographic.SignatureServiceException;
import javax.crypto.Mac;
import java.security.NoSuchAlgorithmException;
import com.google.gson.JsonSyntaxException;
@RunWith(MockitoJUnitRunner.class)
public class ThreadSignatureServiceTest {
......@@ -106,4 +109,8 @@ public class ThreadSignatureServiceTest {
threadSignatureService.verifyHmacSignature("invalidHmac", SECRET);
}
@Test(expected = JsonSyntaxException.class)
public void verifyHmacSignature_() throws SignatureServiceException {
threadSignatureService.verifyHmacSignature(HMAC, SECRET);
}
}
package org.opengroup.osdu.notification.provider.aws.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider;
import com.amazonaws.services.sqs.AmazonSQS;
import io.jsonwebtoken.lang.Assert;
@RunWith(MockitoJUnitRunner.class)
public class AwsCognitoClientTest {
private AwsCognitoClient awsCognitoClient;
@Mock
private AmazonSQS sqsClient;
@Mock
private AWSCognitoIdentityProvider provider;
@Before
public void setUp() {
awsCognitoClient = new AwsCognitoClient("region", "awsCognitoClientId", "awsCognitoAuthFlow",
"awsCognitoAuthParamsUser", "awsCognitoAuthParamsPassword");
}
@Test
public void constuctorCreatesObject() {
Assert.isInstanceOf(AwsCognitoClient.class, awsCognitoClient);
}
@Test
public void getToken() {
awsCognitoClient.provider = provider;
when(provider.initiateAuth(any())).thenReturn(null);
String expected = "";
String actual = awsCognitoClient.getToken("username", "password", "tokenType");
assertEquals(expected, actual);
}
@Test
public void getProvider() {
AWSCognitoIdentityProvider object = awsCognitoClient.getProvider();
assertTrue(object instanceof AWSCognitoIdentityProvider);
}
@Test
public void generateCognitoClient() {
AWSCognitoIdentityProvider object = awsCognitoClient.generateCognitoClient("us-east-1");
assertTrue(object instanceof AWSCognitoIdentityProvider);
}
//the code on production side seems useless. hence useless test here just for coverage.
@Test
public void setPassword() {
awsCognitoClient.provider = provider;
when(provider.adminSetUserPassword(any())).thenReturn(null);
awsCognitoClient.setPassword("username", "password", "user-pool-id");
}
}
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