Change the header to SO
[so.git] / common / src / test / java / org / openecomp / mso / adapter_utils / tests / CryptoTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.adapter_utils.tests;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.security.GeneralSecurityException;
29
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32
33 import org.openecomp.mso.utils.CryptoUtils;
34
35 /**
36  * This class implements all test methods of the CryptoUtils features.
37  *
38  *
39  */
40 public class CryptoTest {
41
42         private static String testKey = "546573746F736973546573746F736973";
43
44      /**
45      * This method is called before any test occurs.
46      * It creates a fake tree from scratch
47      */
48     @BeforeClass
49     public static final void prepare () {
50     
51     }
52
53     /**
54      * This method implements a test of tree structure, mainly the storage of the leaves structure.
55      * @throws GeneralSecurityException 
56      */
57     @Test
58     public final void testEncryption () throws GeneralSecurityException {
59         String hexString = CryptoUtils.byteArrayToHexString("testosistestosi".getBytes());
60         
61         final String testData = "This is a test string";
62          final String nonTestData = "This is not the right String";
63          
64          String encodeString = CryptoUtils.encrypt(testData, testKey);
65         
66          assertNotNull(encodeString);
67          
68          assertTrue(testData.equals(CryptoUtils.decrypt(encodeString, testKey)));
69          assertFalse(nonTestData.equals(CryptoUtils.decrypt(encodeString, testKey)));
70          
71          String encode2String = CryptoUtils.encrypt(testData, testKey);
72          assertNotNull(encode2String);
73          
74          assertEquals(encodeString,encode2String);
75          
76          assertEquals(CryptoUtils.decrypt(encodeString, testKey),CryptoUtils.decrypt(encode2String, testKey));
77     }
78
79 }