2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.util;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
30 import java.io.UnsupportedEncodingException;
31 import java.security.GeneralSecurityException;
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;
39 * Test Crypto Utils with Spring.
41 public class CryptoUtilsTest {
42 private CryptoUtils cryptoUtils = new CryptoUtils();
43 final String data = "This is a test string";
46 * This method tests encryption.
48 * @throws GeneralSecurityException
49 * @throws DecoderException
50 * @throws UnsupportedEncodingException
53 public final void testEncryption() throws GeneralSecurityException, DecoderException, UnsupportedEncodingException {
54 String encodedString = cryptoUtils.encrypt(data);
55 assertNotNull(encodedString);
56 assertEquals(data, cryptoUtils.decrypt(encodedString));
60 * This method tests encryption.
62 * @throws GeneralSecurityException
63 * @throws DecoderException
64 * @throws UnsupportedEncodingException
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);