Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOSIP-30573] #238

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.mosip.kernel.clientcrypto.test.exception;

import io.mosip.kernel.clientcrypto.exception.ClientCryptoException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ClientCryptoExceptionTest {

@Test
public void testConstructorWithErrorCodeAndMessage() {
String errorCode = "ERR_CRYPTO_001";
String errorMessage = "Crypto error";

ClientCryptoException exception = new ClientCryptoException(errorCode, errorMessage);

assertEquals(errorCode, exception.getErrorCode());
}

@Test
public void testConstructorWithErrorCodeMessageAndRootCause() {
String errorCode = "ERR_CRYPTO_001";
String errorMessage = "Crypto error";
Throwable rootCause = new RuntimeException("Root cause exception");

ClientCryptoException exception = new ClientCryptoException(errorCode, errorMessage, rootCause);

assertEquals(errorCode, exception.getErrorCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.mosip.kernel.cryptomanager.test.exception;

import io.mosip.kernel.cryptomanager.exception.CryptoManagerSerivceException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class CryptoManagerServiceExceptionTest {

@Test
public void testConstructorWithErrorCodeAndMessage() {
String errorCode = "ERR_CRYPTO_001";
String errorMessage = "Crypto error";
CryptoManagerSerivceException exception = new CryptoManagerSerivceException(errorCode, errorMessage);
assertEquals(errorCode, exception.getErrorCode());
}

@Test
public void testConstructorWithErrorCodeMessageAndRootCause() {
String errorCode = "ERR_CRYPTO_001";
String errorMessage = "Crypto error";
Throwable rootCause = new RuntimeException("Root cause exception");
CryptoManagerSerivceException exception = new CryptoManagerSerivceException(errorCode, errorMessage);
assertEquals(errorCode, exception.getErrorCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.mosip.kernel.cryptomanager.test.exception;

import io.mosip.kernel.core.exception.ServiceError;
import io.mosip.kernel.cryptomanager.exception.KeymanagerServiceException;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class KeymanagerServiceExceptionTest {
@Test
public void testKeymanagerServiceException() {
List<ServiceError> errorList = new ArrayList<>();
errorList.add(new ServiceError("errorCode1", "errorDescription1"));
errorList.add(new ServiceError("errorCode2", "errorDescription2"));
KeymanagerServiceException exception = new KeymanagerServiceException(errorList);
List<ServiceError> exceptionErrorList = exception.getList();
assertEquals(errorList, exceptionErrorList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.mosip.kernel.cryptomanager.test.exception;

import io.mosip.kernel.cryptomanager.exception.ParseResponseException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ParseResponseExceptionTest {

@Test
public void testConstructorWithErrorCodeAndMessage() {
String errorCode = "ERR_CRYPTO_001";
String errorMessage = "Crypto error";
ParseResponseException exception = new ParseResponseException(errorCode, errorMessage);
assertEquals(errorCode, exception.getErrorCode());
}

@Test
public void testConstructorWithErrorCodeMessageAndRootCause() {
String errorCode = "ERR_CRYPTO_001";
String errorMessage = "Crypto error";
Throwable rootCause = new RuntimeException("Root cause exception");
ParseResponseException exception = new ParseResponseException(errorCode, errorMessage,rootCause);
assertEquals(errorCode, exception.getErrorCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.mosip.kernel.keymanager.hsm.test;

import io.mosip.kernel.core.keymanager.spi.KeyStore;
import io.mosip.kernel.core.util.CryptoUtil;
import io.mosip.kernel.keymanager.hsm.health.HSMHealthCheck;
import io.mosip.kernel.keymanagerservice.helper.KeymanagerDBHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import reactor.core.publisher.Mono;

import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class HSMHealthCheckTest {
@Mock
private KeyStore keyStore;

@Mock
private CryptoUtil cryptoUtil;

@Mock
private KeymanagerDBHelper dbHelper;

@InjectMocks
private HSMHealthCheck hsmHealthCheck;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testHealthCheckDisabled() throws Exception {
Mono<Health> healthMono = hsmHealthCheck.health();
Health health = healthMono.block();
assertEquals(Status.UP, health.getStatus());
assertEquals("HEALTH_CHECK_NOT_ENABLED", health.getDetails().get("Info: "));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package io.mosip.kernel.keymanager.hsm.test;

import io.mosip.kernel.core.keymanager.exception.KeystoreProcessingException;
import io.mosip.kernel.core.keymanager.model.CertificateParameters;
import io.mosip.kernel.keymanager.hsm.constant.KeymanagerErrorCode;
import io.mosip.kernel.keymanager.hsm.impl.offline.OLKeyStoreImpl;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class OLKeyStoreImplTest {

@Test
public void testGetAllAlias() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getAllAlias();
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGetKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getKey("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGetAsymmetricKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getAsymmetricKey("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGetPrivateKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getPrivateKey("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGetPublicKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getPublicKey("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGetCertificate() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getCertificate("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGetSymmetricKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getSymmetricKey("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testDeleteKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.deleteKey("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGenerateAndStoreAsymmetricKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
CertificateParameters certificateParameters=new CertificateParameters();
keystore.generateAndStoreAsymmetricKey("alias","signKeyAlias",certificateParameters);
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGenerateAndStoreSymmetricKey() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.generateAndStoreSymmetricKey("alias");
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

@Test
public void testGetKeystoreProviderName() throws Exception {
OLKeyStoreImpl keystore = new OLKeyStoreImpl(null);
try {
keystore.getKeystoreProviderName();
} catch (KeystoreProcessingException e) {
assertEquals(KeymanagerErrorCode.OFFLINE_KEYSTORE_ACCESS_ERROR.getErrorCode(), e.getErrorCode());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.mosip.kernel.keymigrate.service.test;
import io.mosip.kernel.keymigrate.dto.KeyMigrateBaseKeyRequestDto;
import io.mosip.kernel.keymigrate.dto.KeyMigrateBaseKeyResponseDto;
import io.mosip.kernel.keymigrate.dto.ZKKeyMigrateCertficateResponseDto;
import io.mosip.kernel.keymigrate.dto.ZKKeyMigrateRequestDto;
import io.mosip.kernel.keymigrate.dto.ZKKeyMigrateResponseDto;
import io.mosip.kernel.keymigrate.service.spi.KeyMigratorService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.when;

public class KeyMigratorServiceTest {

@Mock
private KeyMigratorService keyMigratorService;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testMigrateBaseKey() {
KeyMigrateBaseKeyRequestDto requestDto = new KeyMigrateBaseKeyRequestDto();
KeyMigrateBaseKeyResponseDto expectedResponse = new KeyMigrateBaseKeyResponseDto();
when(keyMigratorService.migrateBaseKey(requestDto)).thenReturn(expectedResponse);
KeyMigrateBaseKeyResponseDto actualResponse = keyMigratorService.migrateBaseKey(requestDto);
Assert.assertEquals(expectedResponse, actualResponse);
}

@Test
public void testGetZKTempCertificate() {
ZKKeyMigrateCertficateResponseDto expectedResponse = new ZKKeyMigrateCertficateResponseDto();
when(keyMigratorService.getZKTempCertificate()).thenReturn(expectedResponse);
ZKKeyMigrateCertficateResponseDto actualResponse = keyMigratorService.getZKTempCertificate();
Assert.assertEquals(expectedResponse, actualResponse);
}

@Test
public void testMigrateZKKeys() {
ZKKeyMigrateRequestDto requestDto = new ZKKeyMigrateRequestDto();
ZKKeyMigrateResponseDto expectedResponse = new ZKKeyMigrateResponseDto();
when(keyMigratorService.migrateZKKeys(requestDto)).thenReturn(expectedResponse);
ZKKeyMigrateResponseDto actualResponse = keyMigratorService.migrateZKKeys(requestDto);
Assert.assertEquals(expectedResponse, actualResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.mosip.kernel.lkeymanager.test.exception;

import io.mosip.kernel.core.exception.ServiceError;
import io.mosip.kernel.lkeymanager.exception.InvalidArgumentsException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class InvalidArguementsExceptionTest {

@Mock
private InvalidArgumentsException exception;

@Test
public void testGetList() {
List<ServiceError> errorList = new ArrayList<>();
errorList.add(new ServiceError("ERROR_CODE_1", "Error 1"));
errorList.add(new ServiceError("ERROR_CODE_2", "Error 2"));
Mockito.when(exception.getList()).thenReturn(errorList);
List<ServiceError> retrievedList = exception.getList();
Assert.assertEquals(errorList, retrievedList);
}

@Test
public void testGetList_EmptyList() {
List<ServiceError> errorList = new ArrayList<>();
Mockito.when(exception.getList()).thenReturn(errorList);
List<ServiceError> retrievedList = exception.getList();
Assert.assertTrue(retrievedList.isEmpty());
}

}
Loading