Search in sources :

Example 1 with SecureVault

use of org.wso2.carbon.secvault.SecureVault in project carbon-apimgt by wso2.

the class FileEncryptionUtilityTestCase method testEncryptFiles.

@Test(priority = 2, description = "Test complete flow of encrypting files")
public void testEncryptFiles() throws Exception {
    FileEncryptionConfigurations config = new FileEncryptionConfigurations();
    List<String> filesToEncrypt = new ArrayList<>();
    filesToEncrypt.add(testFileToEncrypt);
    config.setFilesToEncrypt(filesToEncrypt);
    SecureVault secureVault = Mockito.mock(SecureVault.class);
    FileEncryptionUtility fileEncryptionUtility = FileEncryptionUtility.getInstance();
    fileEncryptionUtility.setConfig(config);
    fileEncryptionUtility.setAesKeyFileLocation();
    fileEncryptionUtility.setSecureVault(secureVault);
    Answer nonEncryptedAesKey = invocation -> {
        Object[] args = invocation.getArguments();
        return args[0];
    };
    Mockito.when(secureVault.encrypt(Mockito.anyString().getBytes())).thenAnswer(nonEncryptedAesKey);
    Mockito.when(secureVault.decrypt(Mockito.anyString().getBytes())).thenAnswer(nonEncryptedAesKey);
    fileEncryptionUtility.createAndStoreAESKey();
    fileEncryptionUtility.encryptFiles();
    Assert.assertTrue(Files.notExists(Paths.get(originalFilePath)));
    Assert.assertEquals(fileEncryptionUtility.readFromEncryptedFile(encryptedFilePath), someText);
}
Also used : AfterClass(org.testng.annotations.AfterClass) Files(java.nio.file.Files) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test) File(java.io.File) APIFileUtils(org.wso2.carbon.apimgt.core.util.APIFileUtils) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) List(java.util.List) Assert(org.testng.Assert) Paths(java.nio.file.Paths) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SecureVault(org.wso2.carbon.secvault.SecureVault) FileEncryptionConfigurations(org.wso2.carbon.apimgt.core.configuration.models.FileEncryptionConfigurations) FileEncryptionConfigurations(org.wso2.carbon.apimgt.core.configuration.models.FileEncryptionConfigurations) Answer(org.mockito.stubbing.Answer) SecureVault(org.wso2.carbon.secvault.SecureVault) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Mockito (org.mockito.Mockito)1 Answer (org.mockito.stubbing.Answer)1 Assert (org.testng.Assert)1 AfterClass (org.testng.annotations.AfterClass)1 BeforeClass (org.testng.annotations.BeforeClass)1 Test (org.testng.annotations.Test)1 FileEncryptionConfigurations (org.wso2.carbon.apimgt.core.configuration.models.FileEncryptionConfigurations)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 APIFileUtils (org.wso2.carbon.apimgt.core.util.APIFileUtils)1 SecureVault (org.wso2.carbon.secvault.SecureVault)1