Validate ids
[clamp.git] / src / test / java / org / onap / clamp / clds / util / CryptoUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * 
22  */
23
24 package org.onap.clamp.clds.util;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29
30 import org.apache.commons.codec.binary.Hex;
31 import org.apache.commons.lang3.ArrayUtils;
32 import org.junit.Test;
33
34
35 public class CryptoUtilsTest {
36
37     private final String data = "This is a test string";
38
39     @Test
40     public final void testEncryption() throws Exception {
41         String encodedString = CryptoUtils.encrypt(data);
42         assertNotNull(encodedString);
43         assertEquals(data, CryptoUtils.decrypt(encodedString));
44     }
45
46     @Test
47     public final void testEncryptedStringIsDifferent() throws Exception {
48         String encodedString1 = CryptoUtils.encrypt(data);
49         String encodedString2 = CryptoUtils.encrypt(data);
50         byte[] encryptedMessage1 = Hex.decodeHex(encodedString1.toCharArray());
51         byte[] encryptedMessage2 = Hex.decodeHex(encodedString2.toCharArray());
52         assertNotNull(encryptedMessage1);
53         assertNotNull(encryptedMessage2);
54         assertNotEquals(encryptedMessage1, encryptedMessage2);
55         byte[] subData1 = ArrayUtils.subarray(encryptedMessage1, 16, encryptedMessage1.length);
56         byte[] subData2 = ArrayUtils.subarray(encryptedMessage2, 16, encryptedMessage2.length);
57         assertNotEquals(subData1, subData2);
58     }
59 }