resource-registry/src/test/java/org/gcube/informationsystem/resourceregistry/instances/EncryptionTest.java

47 lines
1.6 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.resourceregistry.instances;
import java.security.Key;
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
import org.gcube.informationsystem.model.reference.properties.Encrypted;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.junit.Assert;
import org.junit.Test;
public class EncryptionTest extends ContextTest {
public static final String PLAIN_VALUE = "my-test";
@Test
public void test() throws Exception {
Encrypted encrypted = new EncryptedImpl();
String contextEncryptedValue = EncryptedImpl.encrypt(PLAIN_VALUE);
encrypted.setEncryptedValue(contextEncryptedValue);
String getContextEncryptedValue = encrypted.getEncryptedValue();
Assert.assertTrue(getContextEncryptedValue.compareTo(getContextEncryptedValue)==0);
// Decrypting with Context Key (default key)
String decryptedValue = EncryptedImpl.decrypt(contextEncryptedValue);
Assert.assertTrue(decryptedValue.compareTo(PLAIN_VALUE)==0);
// Encrypting with DB Key
Key databaseKey = DatabaseEnvironment.getDatabaseKey();
String dbEncryptedValue = EncryptedImpl.encrypt(decryptedValue, databaseKey);
// Setting the value encrypted with DB key
encrypted.setEncryptedValue(dbEncryptedValue);
String getDBEncryptedValue = encrypted.getEncryptedValue();
Assert.assertTrue(getDBEncryptedValue.compareTo(dbEncryptedValue)==0);
decryptedValue = EncryptedImpl.decrypt(getDBEncryptedValue, databaseKey);
Assert.assertTrue(decryptedValue.compareTo(PLAIN_VALUE)==0);
}
}