Re-format source code
[policy/engine.git] / PolicyEngineUtils / src / test / java / org / onap / policy / utils / PeCryptoUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2019 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.onap.policy.utils;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.security.GeneralSecurityException;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.powermock.reflect.Whitebox;
31
32 public class PeCryptoUtilsTest {
33     private final String pass = "policy_user";
34     private final String secretKey = "bmpybWJrbGN4dG9wbGF3Zg==";
35     private final String encryptedPass = "enc:5ID9PoqWIzBaut+KQcAFBtci9CKDRcCNRHRjdBnXM5U=";
36     private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key";
37
38     @Before
39     public void reset() {
40         Whitebox.setInternalState(PeCryptoUtils.class, "cryptoUtils", (PeCryptoUtils) null);
41
42     }
43
44     @Test
45     public void testEncrypt() throws GeneralSecurityException {
46         assertEquals(pass, PeCryptoUtils.encrypt(pass));
47         PeCryptoUtils.initAesKey(secretKey);
48         System.out.println("original value : " + pass + "  encrypted value: " + PeCryptoUtils.encrypt(pass));
49         assertNotNull(PeCryptoUtils.encrypt(pass));
50     }
51
52     @Test
53     public void testDecrypt() throws Exception {
54         assertEquals(pass, PeCryptoUtils.decrypt(pass));
55         System.setProperty(PROP_AES_KEY, secretKey);
56         PeCryptoUtils.initAesKey(null);
57         System.clearProperty(PROP_AES_KEY);
58         assertEquals(pass, PeCryptoUtils.decrypt(encryptedPass));
59         Whitebox.setInternalState(PeCryptoUtils.class, "cryptoUtils", (PeCryptoUtils) null);
60         Whitebox.setInternalState(PeCryptoUtils.class, "secretKey", secretKey);
61         PeCryptoUtils.initAesKey(" ");
62         assertEquals(pass, PeCryptoUtils.decrypt(pass));
63     }
64
65 }