Add option to bypass user authorization
[aai/search-data-service.git] / src / test / java / org / onap / aai / sa / rest / DocumentTest.java
index d16fe87..e780e3b 100644 (file)
@@ -33,8 +33,12 @@ import org.mockito.InjectMocks;
 import org.mockito.Mock;\r
 import org.mockito.Mockito;\r
 import org.mockito.MockitoAnnotations;\r
+import org.mockito.invocation.InvocationOnMock;\r
+import org.mockito.stubbing.Answer;\r
+import org.onap.aai.sa.searchdbabstraction.elasticsearch.config.ElasticSearchConfig;\r
 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity;\r
 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;\r
+import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.ElasticSearchHttpController;\r
 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;\r
 import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult;\r
 import org.onap.aai.sa.searchdbabstraction.entity.ErrorResult;\r
@@ -43,6 +47,7 @@ import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult;
 import org.springframework.http.HttpHeaders;\r
 import org.springframework.http.HttpStatus;\r
 import org.springframework.http.ResponseEntity;\r
+import java.util.Properties;\r
 \r
 \r
 public class DocumentTest {\r
@@ -70,6 +75,9 @@ public class DocumentTest {
 \r
     DocumentApi documentApi;\r
 \r
+    @Mock\r
+    ElasticSearchHttpController httpController;\r
+\r
     @Before\r
     public void setUp() {\r
         MockitoAnnotations.initMocks(this);\r
@@ -654,4 +662,66 @@ public class DocumentTest {
         Assert.assertNotNull(response);\r
         Assert.assertTrue(HttpStatus.INTERNAL_SERVER_ERROR.value() == response.getStatusCodeValue());\r
     }\r
-}\r
+\r
+    @Test\r
+    public void testUserAuthorization() throws Exception {\r
+        String transactionId = "transactionId-1";\r
+        String remoteAddr = "http://127.0.0.1";\r
+        String content = "content";\r
+        // Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap);;\r
+        Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId);\r
+        Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr);\r
+        Mockito.when(request.getMethod()).thenReturn("testMethod");\r
+        Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1"));\r
+        Mockito.when(request.getRemoteHost()).thenReturn("localhost");\r
+        Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class),\r
+                Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString()))\r
+                .thenCallRealMethod();\r
+\r
+        Mockito.doAnswer(new Answer<ElasticSearchConfig>() {\r
+            public ElasticSearchConfig answer(InvocationOnMock invocation) {\r
+                Properties properties = new Properties();\r
+                return new ElasticSearchConfig(properties);\r
+            }\r
+        }).when(httpController).getElasticSearchConfig();\r
+\r
+        searchServiceApi.documentStore = httpController;\r
+\r
+        ResponseEntity<String> response =\r
+                documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);\r
+        Assert.assertNotNull(response);\r
+        Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());\r
+\r
+        Mockito.doAnswer(new Answer<ElasticSearchConfig>() {\r
+            public ElasticSearchConfig answer(InvocationOnMock invocation) {\r
+                Properties properties = new Properties();\r
+                properties.put(ElasticSearchConfig.ES_AUTH_ENABLED, "true");\r
+                return new ElasticSearchConfig(properties);\r
+            }\r
+        }).when(httpController).getElasticSearchConfig();\r
+\r
+\r
+        response = documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);\r
+        Assert.assertNotNull(response);\r
+        Assert.assertTrue(HttpStatus.FORBIDDEN.value() == response.getStatusCodeValue());\r
+\r
+        Mockito.doAnswer(new Answer<ElasticSearchConfig>() {\r
+            public ElasticSearchConfig answer(InvocationOnMock invocation) {\r
+                Properties properties = new Properties();\r
+                properties.put(ElasticSearchConfig.ES_AUTH_ENABLED, "false");\r
+                return new ElasticSearchConfig(properties);\r
+            }\r
+        }).when(httpController).getElasticSearchConfig();\r
+\r
+        DocumentOperationResult result = new DocumentOperationResult();\r
+        result.setResultCode(302);\r
+        result.setError(new ErrorResult("type-1", "reason-1"));\r
+        result.setFailureCause("test-failure");\r
+        Mockito.when(documentStore.createDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class),\r
+                Mockito.anyBoolean())).thenReturn(result);\r
+        response = documentApi.processPut(content, request, headers, httpResponse, "index", "id-1", documentStore);\r
+        Assert.assertNotNull(response);\r
+        Assert.assertTrue(HttpStatus.FOUND.value() == response.getStatusCodeValue());\r
+\r
+    }\r
+}
\ No newline at end of file