Format Java code to ONAP standard
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / auth / SearchDbServiceAuthTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017-2018 Amdocs\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 package org.onap.aai.sa.auth;\r
22 \r
23 import java.io.File;\r
24 import java.io.IOException;\r
25 import java.lang.reflect.Field;\r
26 import java.lang.reflect.Modifier;\r
27 import org.junit.Assert;\r
28 import org.junit.Before;\r
29 import org.junit.Test;\r
30 import org.mockito.Mock;\r
31 import org.mockito.MockitoAnnotations;\r
32 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;\r
33 import org.springframework.http.HttpHeaders;\r
34 \r
35 // import javax.servlet.http.Cookie;\r
36 \r
37 public class SearchDbServiceAuthTest {\r
38 \r
39     @Mock\r
40     HttpHeaders headers;\r
41 \r
42     // @Mock\r
43     // Cookie mockedCookie;\r
44 \r
45     @Before\r
46     public void setUp() throws NoSuchFieldException, IllegalAccessException, IOException {\r
47         MockitoAnnotations.initMocks(this);\r
48         System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
49         setFinalStatic(System.getProperty("AJSC_HOME") + "/src/test/resources/json/search_policy.json");\r
50     }\r
51 \r
52     @Test\r
53     public void testAuthUser() {\r
54         SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
55         String auth = aaiAuth.authUser(headers, "user-1", "function-1");\r
56         Assert.assertEquals(auth, "AAI_9101");\r
57     }\r
58 \r
59     // @Test\r
60     // public void testAuthCookie_NullCookie(){\r
61     // SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
62     // Cookie cookie = null;\r
63     // Assert.assertFalse(aaiAuth.authCookie(cookie, "function-1", new StringBuilder("user-1")));\r
64     // }\r
65 \r
66     // @Test\r
67     // public void testAuthCookie_NotNullCookie(){\r
68     // SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
69     // Cookie cookie = new Cookie ( "TestCookie", "TestValue");\r
70     // // Cookie cookie = new Cookie ( "TestCookie", "TestValue" );\r
71     // boolean retValue = aaiAuth.authCookie(cookie, "GET:testFunction", new StringBuilder("testuser"));\r
72     // Assert.assertTrue(retValue);\r
73     // }\r
74 \r
75     static void setFinalStatic(String fieldValue)\r
76             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {\r
77         Field configField = SearchDbConstants.class.getDeclaredField("SDB_AUTH_CONFIG_FILENAME");\r
78         configField.setAccessible(true);\r
79 \r
80         Field modifiersField = Field.class.getDeclaredField("modifiers");\r
81         modifiersField.setAccessible(true);\r
82         modifiersField.setInt(configField, configField.getModifiers() & ~Modifier.FINAL);\r
83 \r
84         configField.set(null, fieldValue);\r
85     }\r
86 }\r