Rework the CldsUserJson
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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 java.io.UnsupportedEncodingException;
31 import java.security.GeneralSecurityException;
32
33 import org.apache.commons.codec.DecoderException;
34 import org.apache.commons.codec.binary.Hex;
35 import org.apache.commons.lang3.ArrayUtils;
36 import org.junit.Test;
37
38 /**
39  * Test Crypto Utils with Spring.
40  */
41 public class CryptoUtilsTest {
42     private CryptoUtils cryptoUtils = new CryptoUtils();
43     final String        data        = "This is a test string";
44
45     /**
46      * This method tests encryption.
47      * 
48      * @throws GeneralSecurityException
49      * @throws DecoderException
50      * @throws UnsupportedEncodingException
51      */
52     @Test
53     public final void testEncryption() throws GeneralSecurityException, DecoderException, UnsupportedEncodingException {
54         String encodedString = cryptoUtils.encrypt(data);
55         assertNotNull(encodedString);
56         assertEquals(data, cryptoUtils.decrypt(encodedString));
57     }
58
59     /**
60      * This method tests encryption.
61      * 
62      * @throws GeneralSecurityException
63      * @throws DecoderException
64      * @throws UnsupportedEncodingException
65      */
66     @Test
67     public final void testEncryptedStringIsDifferent()
68             throws GeneralSecurityException, DecoderException, UnsupportedEncodingException {
69         String encodedString1 = cryptoUtils.encrypt(data);
70         String encodedString2 = cryptoUtils.encrypt(data);
71         byte[] encryptedMessage1 = Hex.decodeHex(encodedString1.toCharArray());
72         byte[] encryptedMessage2 = Hex.decodeHex(encodedString2.toCharArray());
73         assertNotNull(encryptedMessage1);
74         assertNotNull(encryptedMessage2);
75         assertNotEquals(encryptedMessage1, encryptedMessage2);
76         byte[] subData1 = ArrayUtils.subarray(encryptedMessage1, 16, encryptedMessage1.length);
77         byte[] subData2 = ArrayUtils.subarray(encryptedMessage2, 16, encryptedMessage2.length);
78         assertNotEquals(subData1, subData2);
79     }
80 }