98260e20144bdbd5e8c16f6c3a63418b6e476599
[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 javax.xml.bind.DatatypeConverter;
45
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.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, DatatypeConverter.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(DatatypeConverter.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                 HttpHeaders actual = EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth("test", "test");
86                 assertNotNull(actual);
87         }
88
89         @Test(expected = NullPointerException.class)
90         public void base64encodeKeyForAAFBasicAuthDecryptPassExceptionTest() throws Exception {
91                 Mockito.when(DatatypeConverter.printBase64Binary("test:test".getBytes()))
92                                 .thenThrow(new NullPointerException());
93                 EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth("test", "test");
94         }
95
96         @Test
97         public void checkNameSpaceMatchingTest() throws Exception {
98                 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app.role.name", "com.test.app.role");
99         }
100
101         @Test
102         public void checkNameSpaceNotMatchingTest() throws Exception {
103                 PowerMockito.mockStatic(EcompExternalAuthProperties.class);
104                 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role.name", "com.test.app.role");
105         }
106
107         @Test
108         public void checkNameSpaceMatchingLengthTest() throws Exception {
109                 EcompExternalAuthUtils.checkNameSpaceMatching("com.test.app2.role", "com.test.app2.role.test");
110         }
111
112         @Test
113         public void isJsonValidTest() {
114                 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"}");
115                 assertTrue(actual);
116         }
117
118         @Test
119         public void isJsonValidFailTest() {
120                 boolean actual = EcompExternalAuthUtils.isJSONValid("{\"test\":\"test\"");
121                 assertFalse(actual);
122         }
123 }