Add unit tests to increase sonar coverage
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / auth / SearchDbServiceAuthTest.java
diff --git a/src/test/java/org/onap/aai/sa/auth/SearchDbServiceAuthTest.java b/src/test/java/org/onap/aai/sa/auth/SearchDbServiceAuthTest.java
new file mode 100644 (file)
index 0000000..acbbae5
--- /dev/null
@@ -0,0 +1,86 @@
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.aai\r
+ * ================================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright © 2017 Amdocs\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *       http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ *\r
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ */\r
+package org.onap.aai.sa.auth;\r
+\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;\r
+\r
+import javax.ws.rs.core.Cookie;\r
+import javax.ws.rs.core.HttpHeaders;\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.lang.reflect.Field;\r
+import java.lang.reflect.Modifier;\r
+\r
+public class SearchDbServiceAuthTest {\r
+\r
+    @Mock\r
+    HttpHeaders headers;\r
+\r
+    @Mock\r
+    Cookie mockedCookie;\r
+\r
+    @Before\r
+    public void setUp() throws NoSuchFieldException, IllegalAccessException, IOException {\r
+        MockitoAnnotations.initMocks(this);\r
+        System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));\r
+        setFinalStatic(System.getProperty("AJSC_HOME")+"/src/test/resources/json/search_policy.json");\r
+    }\r
+\r
+    @Test\r
+    public void testAuthUser(){\r
+        SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
+        String auth = aaiAuth.authUser(headers, "user-1", "function-1");\r
+        Assert.assertEquals(auth, "AAI_9101");\r
+    }\r
+\r
+    @Test\r
+    public void testAuthCookie_NullCookie(){\r
+        SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
+        Cookie cookie = null;\r
+        Assert.assertFalse(aaiAuth.authCookie(cookie, "function-1", new StringBuilder("user-1")));\r
+    }\r
+\r
+    @Test\r
+    public void testAuthCookie_NotNullCookie(){\r
+        SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth();\r
+        boolean retValue = aaiAuth.authCookie(mockedCookie, "GET:testFunction", new StringBuilder("testuser"));\r
+        Assert.assertTrue(retValue);\r
+    }\r
+\r
+    static void setFinalStatic(String fieldValue) throws NoSuchFieldException, SecurityException,\r
+            IllegalArgumentException, IllegalAccessException {\r
+        Field configField = SearchDbConstants.class.getDeclaredField("SDB_AUTH_CONFIG_FILENAME");\r
+        configField.setAccessible(true);\r
+\r
+        Field modifiersField = Field.class.getDeclaredField( "modifiers" );\r
+        modifiersField.setAccessible( true );\r
+        modifiersField.setInt( configField, configField.getModifiers() & ~Modifier.FINAL );\r
+\r
+        configField.set(null, fieldValue);\r
+    }\r
+}\r