EncryptionTool unit tests 69/35969/2
authorJakub Dudycz <jakub.dudycz@nokia.com>
Thu, 15 Mar 2018 10:52:18 +0000 (11:52 +0100)
committerTakamune Cho <tc012c@att.com>
Thu, 15 Mar 2018 15:01:01 +0000 (15:01 +0000)
Improved code coverage.

Change-Id: I8882a9fd274b4ce03ef784aac89cdfbb786af4fe
Issue-ID: APPC-742
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
appc-common/src/test/java/org/onap/appc/encryption/EncryptionToolTest.java [moved from appc-common/src/test/java/org/onap/appc/encryption/TestEncryption.java with 51% similarity]

 package org.onap.appc.encryption;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
-import org.onap.appc.encryption.EncryptionTool;
 
-public class TestEncryption {
+public class EncryptionToolTest {
+
+    private static final String PLAIN_TEXT = "text to encrypt";
+    private static final String EMPTY_STR = "";
+
+    private EncryptionTool encryptionTool = EncryptionTool.getInstance();
+
+    @Test
+    public void should_return_prefix_given_empty_string() {
+        assertEquals("enc:", encryptionTool.encrypt(EMPTY_STR));
+    }
+
+    @Test
+    public void should_return_null_given_null() {
+        assertNull(encryptionTool.encrypt(null));
+    }
 
     @Test
-    public void testEncryptionDecryption() {
-        String plain = "AppC";
-        String enc = EncryptionTool.getInstance().encrypt(plain);
-        assertNotEquals(plain, enc);
-        String dec = EncryptionTool.getInstance().decrypt(enc);
-        assertNotEquals(enc, dec);
-        assertEquals(plain, dec);
-        System.out.println(String.format("%s = [%s]", plain, enc));
+    public void should_encrypt_given_string() {
+        String encrypted = encryptionTool.encrypt(PLAIN_TEXT);
+
+        assertNotEquals(encrypted, PLAIN_TEXT);
+        assertTrue(encrypted.startsWith(EncryptionTool.ENCRYPTED_VALUE_PREFIX));
+    }
+
+    @Test
+    public void should_not_decrypt_string_when_not_starting_with_prefix() {
+
+        assertNull(encryptionTool.decrypt(null));
+        assertEquals("mdi/12!dsao91", encryptionTool.decrypt("mdi/12!dsao91"));
     }
 
+    @Test
+    public void should_decrypt_given_encrypted_string() {
+        String encrypted = encryptionTool.encrypt(PLAIN_TEXT);
+
+        assertEquals(PLAIN_TEXT, encryptionTool.decrypt(encrypted));
+    }
 }