b35a1cda2584fb78a87db195965cdcce31442f13
[portal/sdk.git] /
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalsdk.external.authorization.util;
39
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertTrue;
43
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mockito;
48 import org.mockito.MockitoAnnotations;
49 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
50 import org.onap.portalsdk.core.util.SystemProperties;
51 import org.powermock.api.mockito.PowerMockito;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
54 import org.springframework.http.HttpHeaders;
55
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({ EcompExternalAuthProperties.class, CipherUtil.class })
58 public class EcompExternalAuthUtilsTest {
59
60         public static final String EXT_EMPTY_JSON_STRING = "{}";
61         public static final String EXT_ROLE_FIELD = "role";
62         public static final String EXT_PERM_FIELD = "perm";
63         public static final String EXT_PERM_FIELD_TYPE = "type";
64         public static final String EXT_PERM_ACCESS = ".access";
65         public static final String EXT_ROLE_FIELD_NAME = "name";
66         public static final String EXT_NULL_VALUE = "null";
67         public static final String EXT_FIELD_DESCRIPTION = "description";
68         public static final String EXT_FIELD_PERMS = "perms";
69         public static final String EXT_ROLE_FIELD_OWNER = ".owner";
70         public static final String EXT_ROLE_FIELD_ADMIN = ".admin";
71
72         @Before
73         public void setup() {
74                 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
75                 PowerMockito.mockStatic(CipherUtil.class);
76                 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_NAME))
77                                 .thenReturn("test_username");
78                 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_PASSWORD))
79                                 .thenReturn("test_password");
80                 MockitoAnnotations.initMocks(this);
81         }
82
83         @Test
84         public void base64encodeKeyForAAFBasicAuthTest() throws Exception {
85                 Mockito.when(
86                                 CipherUtil.decryptPKC("test_password", SystemProperties.getProperty(SystemProperties.Decryption_Key)))
87                                 .thenReturn("test_decrypted_password");
88                 HttpHeaders actual = EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth();
89                 assertNotNull(actual);
90         }
91
92         @Test(expected = NullPointerException.class)
93         public void base64encodeKeyForAAFBasicAuthDecryptPassExceptionTest() throws Exception {
94                 Mockito.when(
95                                 CipherUtil.decryptPKC("test_password", SystemProperties.getProperty(SystemProperties.Decryption_Key)))
96                                 .thenThrow(new NullPointerException());
97                 EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth();
98         }
99
100         @Test
101         public void checkNameSpaceMatchingTest() throws Exception {
102                 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app.role.name", "com.test.app.role");
103         }
104
105         @Test
106         public void checkNameSpaceNotMatchingTest() throws Exception {
107                 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
108                 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role.name", "com.test.app.role");
109         }
110
111         @Test
112         public void checkNameSpaceMatchingLengthTest() throws Exception {
113                 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role", "com.test.app2.role.test");
114         }
115
116         @Test
117         public void isJsonValidTest() {
118                 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"}");
119                 assertTrue(actual);
120         }
121
122         @Test
123         public void isJsonValidFailTest() {
124                 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"");
125                 assertFalse(actual);
126         }
127 }