2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.external.authorization.util;
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertTrue;
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;
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({ EcompExternalAuthProperties.class, CipherUtil.class })
58 public class EcompExternalAuthUtilsTest {
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";
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);
84 public void base64encodeKeyForAAFBasicAuthTest() throws Exception {
86 CipherUtil.decryptPKC("test_password", SystemProperties.getProperty(SystemProperties.Decryption_Key)))
87 .thenReturn("test_decrypted_password");
88 HttpHeaders actual = EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth();
89 assertNotNull(actual);
92 @Test(expected = NullPointerException.class)
93 public void base64encodeKeyForAAFBasicAuthDecryptPassExceptionTest() throws Exception {
95 CipherUtil.decryptPKC("test_password", SystemProperties.getProperty(SystemProperties.Decryption_Key)))
96 .thenThrow(new NullPointerException());
97 EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth();
101 public void checkNameSpaceMatchingTest() throws Exception {
102 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app.role.name", "com.test.app.role");
106 public void checkNameSpaceNotMatchingTest() throws Exception {
107 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
108 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role.name", "com.test.app.role");
112 public void checkNameSpaceMatchingLengthTest() throws Exception {
113 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role", "com.test.app2.role.test");
117 public void isJsonValidTest() {
118 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"}");
123 public void isJsonValidFailTest() {
124 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"");