acbbae5fb8a0a02508c9bc218c7a7236f9c552df
[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 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  */\r
23 package org.onap.aai.sa.auth;\r
24 \r
25 import org.junit.Assert;\r
26 import org.junit.Before;\r
27 import org.junit.Test;\r
28 import org.mockito.Mock;\r
29 import org.mockito.MockitoAnnotations;\r
30 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;\r
31 \r
32 import javax.ws.rs.core.Cookie;\r
33 import javax.ws.rs.core.HttpHeaders;\r
34 import java.io.File;\r
35 import java.io.IOException;\r
36 import java.lang.reflect.Field;\r
37 import java.lang.reflect.Modifier;\r
38 \r
39 public class SearchDbServiceAuthTest {\r
40 \r
41     @Mock\r
42     HttpHeaders headers;\r
43 \r
44     @Mock\r
45     Cookie mockedCookie;\r
46 \r
47     @Before\r
48     public void setUp() throws NoSuchFieldException, IllegalAccessException, IOException {\r
49         MockitoAnnotations.initMocks(this);\r
50         System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
51         setFinalStatic(System.getProperty("AJSC_HOME")+"/src/test/resources/json/search_policy.json");\r
52     }\r
53 \r
54     @Test\r
55     public void testAuthUser(){\r
56         SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
57         String auth = aaiAuth.authUser(headers, "user-1", "function-1");\r
58         Assert.assertEquals(auth, "AAI_9101");\r
59     }\r
60 \r
61     @Test\r
62     public void testAuthCookie_NullCookie(){\r
63         SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
64         Cookie cookie = null;\r
65         Assert.assertFalse(aaiAuth.authCookie(cookie, "function-1", new StringBuilder("user-1")));\r
66     }\r
67 \r
68     @Test\r
69     public void testAuthCookie_NotNullCookie(){\r
70         SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
71         boolean retValue = aaiAuth.authCookie(mockedCookie, "GET:testFunction", new StringBuilder("testuser"));\r
72         Assert.assertTrue(retValue);\r
73     }\r
74 \r
75     static void setFinalStatic(String fieldValue) throws NoSuchFieldException, SecurityException,\r
76             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