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 javax.xml.bind.DatatypeConverter;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.core.classloader.annotations.PrepareForTest;
54 import org.powermock.modules.junit4.PowerMockRunner;
55 import org.springframework.http.HttpHeaders;
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest({ EcompExternalAuthProperties.class, CipherUtil.class, DatatypeConverter.class })
59 public class EcompExternalAuthUtilsTest {
61 public static final String EXT_EMPTY_JSON_STRING = "{}";
62 public static final String EXT_ROLE_FIELD = "role";
63 public static final String EXT_PERM_FIELD = "perm";
64 public static final String EXT_PERM_FIELD_TYPE = "type";
65 public static final String EXT_PERM_ACCESS = ".access";
66 public static final String EXT_ROLE_FIELD_NAME = "name";
67 public static final String EXT_NULL_VALUE = "null";
68 public static final String EXT_FIELD_DESCRIPTION = "description";
69 public static final String EXT_FIELD_PERMS = "perms";
70 public static final String EXT_ROLE_FIELD_OWNER = ".owner";
71 public static final String EXT_ROLE_FIELD_ADMIN = ".admin";
75 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
76 PowerMockito.mockStatic(CipherUtil.class);
77 PowerMockito.mockStatic(DatatypeConverter.class);
78 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_NAME))
79 .thenReturn("test_username");
80 Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_PASSWORD))
81 .thenReturn("test_password");
82 MockitoAnnotations.initMocks(this);
86 public void base64encodeKeyForAAFBasicAuthTest() throws Exception {
87 HttpHeaders actual = EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth("test", "test");
88 assertNotNull(actual);
91 @Test(expected = NullPointerException.class)
92 public void base64encodeKeyForAAFBasicAuthDecryptPassExceptionTest() throws Exception {
93 Mockito.when(DatatypeConverter.printBase64Binary("test:test".getBytes()))
94 .thenThrow(new NullPointerException());
95 EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth("test", "test");
99 public void checkNameSpaceMatchingTest() throws Exception {
100 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app.role.name", "com.test.app.role");
104 public void checkNameSpaceNotMatchingTest() throws Exception {
105 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
106 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role.name", "com.test.app.role");
110 public void checkNameSpaceMatchingLengthTest() throws Exception {
111 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role", "com.test.app2.role.test");
115 public void isJsonValidTest() {
116 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"}");
121 public void isJsonValidFailTest() {
122 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"");