From bb2528208c8bb2951e08820bfd30f8d8a5fe1a3a Mon Sep 17 00:00:00 2001 From: aditya Date: Mon, 8 Jan 2018 15:41:05 -0600 Subject: [PATCH] Add unit tests to increase sonar coverage Add unit tests to auth, rest and searchdbabstraction. Add search_policy.json to src/test/resources to support junits. Issue-ID: AAI-608 Change-Id: I295623d1735136bf4723d87ab4df21f8c124a414 Signed-off-by: Aditya Gajulapalli --- .../onap/aai/sa/auth/SearchDbServiceAuthTest.java | 86 +++ .../java/org/onap/aai/sa/rest/BulkRequestTest.java | 143 +++++ .../java/org/onap/aai/sa/rest/DocumentTest.java | 644 +++++++++++++++++++++ .../config/ElasticSearchConfigTest.java | 52 ++ .../dao/ElasticSearchResultItemTest.java | 121 ++++ .../searchapi/RangeQueryTest.java | 153 +++++ .../searchapi/TermQueryTest.java | 68 +++ src/test/resources/json/search_policy.json | 37 ++ 8 files changed, 1304 insertions(+) create mode 100644 src/test/java/org/onap/aai/sa/auth/SearchDbServiceAuthTest.java create mode 100644 src/test/java/org/onap/aai/sa/rest/BulkRequestTest.java create mode 100644 src/test/java/org/onap/aai/sa/rest/DocumentTest.java create mode 100644 src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/config/ElasticSearchConfigTest.java create mode 100644 src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchResultItemTest.java create mode 100644 src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/RangeQueryTest.java create mode 100644 src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/TermQueryTest.java create mode 100644 src/test/resources/json/search_policy.json 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 index 0000000..acbbae5 --- /dev/null +++ b/src/test/java/org/onap/aai/sa/auth/SearchDbServiceAuthTest.java @@ -0,0 +1,86 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.sa.auth; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants; + +import javax.ws.rs.core.Cookie; +import javax.ws.rs.core.HttpHeaders; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class SearchDbServiceAuthTest { + + @Mock + HttpHeaders headers; + + @Mock + Cookie mockedCookie; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException, IOException { + MockitoAnnotations.initMocks(this); + System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/')); + setFinalStatic(System.getProperty("AJSC_HOME")+"/src/test/resources/json/search_policy.json"); + } + + @Test + public void testAuthUser(){ + SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth(); + String auth = aaiAuth.authUser(headers, "user-1", "function-1"); + Assert.assertEquals(auth, "AAI_9101"); + } + + @Test + public void testAuthCookie_NullCookie(){ + SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth(); + Cookie cookie = null; + Assert.assertFalse(aaiAuth.authCookie(cookie, "function-1", new StringBuilder("user-1"))); + } + + @Test + public void testAuthCookie_NotNullCookie(){ + SearchDbServiceAuth aaiAuth = new SearchDbServiceAuth(); + boolean retValue = aaiAuth.authCookie(mockedCookie, "GET:testFunction", new StringBuilder("testuser")); + Assert.assertTrue(retValue); + } + + static void setFinalStatic(String fieldValue) throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + Field configField = SearchDbConstants.class.getDeclaredField("SDB_AUTH_CONFIG_FILENAME"); + configField.setAccessible(true); + + Field modifiersField = Field.class.getDeclaredField( "modifiers" ); + modifiersField.setAccessible( true ); + modifiersField.setInt( configField, configField.getModifiers() & ~Modifier.FINAL ); + + configField.set(null, fieldValue); + } +} diff --git a/src/test/java/org/onap/aai/sa/rest/BulkRequestTest.java b/src/test/java/org/onap/aai/sa/rest/BulkRequestTest.java new file mode 100644 index 0000000..ae8f619 --- /dev/null +++ b/src/test/java/org/onap/aai/sa/rest/BulkRequestTest.java @@ -0,0 +1,143 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.sa.rest; + +import org.junit.Assert; +import org.junit.Test; + +public class BulkRequestTest { + + @Test + public void testBulkRequest_Delete(){ + BulkRequest request = new BulkRequest(); + BulkOperation operation = new BulkOperation(); + Document document = new Document(); + BulkMetaData metaData = getMetaData(); + operation.setMetaData(metaData); + operation.setDocument(document); + Assert.assertNotNull(operation.getDocument()); + Assert.assertNotNull(operation.getMetaData()); + Assert.assertNotNull(operation.toString()); + + request.setDelete(operation); + Assert.assertNotNull(request.getDelete()); + Assert.assertEquals(operation, request.getOperation()); + Assert.assertEquals(BulkRequest.OperationType.DELETE, request.getOperationType()); + Assert.assertTrue(request.toString().contains("delete:")); + + } + + @Test + public void testBulkRequest_Update(){ + BulkRequest request = new BulkRequest(); + BulkOperation operation = new BulkOperation(); + Document document = new Document(); + BulkMetaData metaData = getMetaData(); + operation.setMetaData(metaData); + operation.setDocument(document); + Assert.assertNotNull(operation.getDocument()); + Assert.assertNotNull(operation.getMetaData()); + Assert.assertNotNull(operation.toString()); + + request.setUpdate(operation); + Assert.assertNotNull(request.getUpdate()); + Assert.assertEquals(operation, request.getOperation()); + Assert.assertEquals(BulkRequest.OperationType.UPDATE, request.getOperationType()); + Assert.assertTrue(request.toString().contains("update:")); + + } + + @Test + public void testBulkRequest_Create(){ + BulkRequest request = new BulkRequest(); + BulkOperation operation = new BulkOperation(); + Document document = new Document(); + BulkMetaData metaData = getMetaData(); + operation.setMetaData(metaData); + operation.setDocument(document); + Assert.assertNotNull(operation.getDocument()); + Assert.assertNotNull(operation.getMetaData()); + Assert.assertNotNull(operation.toString()); + + request.setCreate(operation); + Assert.assertNotNull(request.getCreate()); + Assert.assertEquals(operation, request.getOperation()); + Assert.assertEquals(BulkRequest.OperationType.CREATE, request.getOperationType()); + Assert.assertTrue(request.toString().contains("create:")); + + } + + @Test + public void testBulkRequest_Undefined(){ + BulkRequest request = new BulkRequest(); + Assert.assertNull(request.getOperation()); + Assert.assertNull(request.getOperationType()); + Assert.assertEquals("UNDEFINED", request.toString()); + } + @Test + public void testGetIndex(){ + BulkRequest request = new BulkRequest(); + BulkOperation operation = new BulkOperation(); + BulkMetaData metaData = new BulkMetaData(); + metaData.setUrl("/test/indexes/index1"); + operation.setMetaData(metaData); + request.setCreate(operation); + String index = request.getIndex(); + Assert.assertEquals(index, "index1"); + } + + @Test + public void testGetId(){ + BulkRequest request = new BulkRequest(); + BulkOperation operation = new BulkOperation(); + BulkMetaData metaData = new BulkMetaData(); + metaData.setUrl("/test/documents/document1"); + operation.setMetaData(metaData); + request.setCreate(operation); + String index = request.getId(); + Assert.assertEquals(index, "document1"); + } + + @Test + public void testApiUtils(){ + Assert.assertEquals("services/search-data-service/v1/search/indexes/index1", + ApiUtils.buildIndexUri("index1")); + Assert.assertEquals("services/search-data-service/v1/search/indexes/index1/documents/document1", + ApiUtils.buildDocumentUri("index1", "document1")); + Assert.assertTrue(ApiUtils.validateIndexUri("services/search-data-service/v1/search/indexes/index1")); + Assert.assertTrue(ApiUtils.validateDocumentUri + ("services/search-data-service/v1/search/indexes/index1/documents/document1", true)); + Assert.assertTrue(ApiUtils.validateDocumentUri + ("services/search-data-service/v1/search/indexes/index1/documents/document1", false)); + } + + private BulkMetaData getMetaData() { + BulkMetaData metaData = new BulkMetaData(); + metaData.setUrl("http://127.0.0.1"); + metaData.setEtag("etag-1"); + Assert.assertEquals(metaData.getUrl(), "http://127.0.0.1"); + Assert.assertEquals(metaData.getEtag(), "etag-1"); + Assert.assertNotNull(metaData.toString()); + return metaData; + } +} diff --git a/src/test/java/org/onap/aai/sa/rest/DocumentTest.java b/src/test/java/org/onap/aai/sa/rest/DocumentTest.java new file mode 100644 index 0000000..2f502f9 --- /dev/null +++ b/src/test/java/org/onap/aai/sa/rest/DocumentTest.java @@ -0,0 +1,644 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.sa.rest; + +import com.fasterxml.jackson.core.JsonProcessingException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreDataEntity; +import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface; +import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException; +import org.onap.aai.sa.searchdbabstraction.entity.DocumentOperationResult; +import org.onap.aai.sa.searchdbabstraction.entity.ErrorResult; +import org.onap.aai.sa.searchdbabstraction.entity.SearchHits; +import org.onap.aai.sa.searchdbabstraction.entity.SearchOperationResult; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; + +public class DocumentTest { + + @Mock + SearchServiceApi searchServiceApi; + + @Mock + HttpServletRequest request; + + @Mock + HttpHeaders headers; + + @Mock + HttpServletResponse httpResponse; + + @Mock + DocumentStoreInterface documentStore; + + @Mock + MultivaluedMap multivaluedMap; + + @InjectMocks + IndexApi indexApi; + + DocumentApi documentApi; + + @Before + public void setUp(){ + MockitoAnnotations.initMocks(this); + documentApi = new DocumentApi(searchServiceApi); + } + + @Test + public void testDocumentClass_AllMethods() throws JsonProcessingException { + Document doc = new Document(); + doc.setField("name-1", "value-1"); + Assert.assertTrue(doc.getFields().size()==1); + Assert.assertTrue(doc.toJson().contains("value-1")); + Assert.assertNotNull(doc.toString()); + Assert.assertTrue(doc.toString().contains("name-1")); + } + + @Test + public void testProcessPost_NullContent(){ + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = null; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Response response = documentApi.processPost(content, request, headers, httpResponse, "index", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.BAD_REQUEST.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessPost_NotNullContent() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Response response = documentApi.processPost(content, request, headers, httpResponse, "index", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessPost_ValidRequest() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + DocumentOperationResult result = new DocumentOperationResult(); + result.setResultCode(150); + result.setError(new ErrorResult("type-1", "reason-1")); + result.setFailureCause("test-failure"); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Mockito.when(documentStore.createDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class), + Mockito.anyBoolean())).thenReturn(result); + Mockito.doNothing().when(httpResponse).setHeader(Mockito.anyString(), Mockito.anyString()); + Response response = documentApi.processPost(content, request, headers, httpResponse, "index", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessSearchWithGet_Created() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + SearchOperationResult result = new SearchOperationResult(); + result.setResultCode(201); + SearchHits hits = new SearchHits(); + hits.setTotalHits("2"); + result.setSearchResult(hits); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result); + Response response = documentApi.processSearchWithGet(content, request, headers, "index-1", + "query-text", documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.CREATED.getStatusCode() == response.getStatus()); + + } + + @Test + public void testProcessSearchWithGet_ValidateThrowsException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + SearchOperationResult result = new SearchOperationResult(); + result.setResultCode(201); + SearchHits hits = new SearchHits(); + hits.setTotalHits("2"); + result.setSearchResult(hits); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result); + Response response = documentApi.processSearchWithGet(content, request, headers, "index-1", + "query-text", documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + + } + + @Test + public void testProcessSearchWithGet_ValidateIsFalse() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + SearchOperationResult result = new SearchOperationResult(); + result.setResultCode(201); + SearchHits hits = new SearchHits(); + hits.setTotalHits("2"); + result.setSearchResult(hits); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(false); + Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result); + Response response = documentApi.processSearchWithGet(content, request, headers, "index-1", + "query-text", documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + + } + + @Test + public void testProcessSearchWithGet_InvalidResult() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + SearchOperationResult result = new SearchOperationResult(); + result.setResultCode(302); + SearchHits hits = new SearchHits(); + hits.setTotalHits("2"); + result.setSearchResult(hits); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Mockito.when(documentStore.search(Mockito.anyString(), Mockito.anyString())).thenReturn(result); + Response response = documentApi.processSearchWithGet(content, request, headers, "index-1", + "query-text", documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FOUND.getStatusCode() == response.getStatus()); + + } + + @Test + public void testProcessPut_NullContent(){ + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = null; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Response response = documentApi.processPut(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.BAD_REQUEST.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessPut_RequestThrowsException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Response response = documentApi.processPut(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessPut_RequestInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(false); + Response response = documentApi.processPut(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessPut_ResultInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + DocumentOperationResult result = new DocumentOperationResult(); + result.setResultCode(302); + result.setError(new ErrorResult("type-1", "reason-1")); + result.setFailureCause("test-failure"); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Mockito.when(documentStore.updateDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class), + Mockito.anyBoolean())).thenReturn(result); + Response response = documentApi.processPut(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FOUND.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessDelete_RequestThrowsException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Response response = documentApi.processDelete(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessDelete_RequestInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(false); + Response response = documentApi.processDelete(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessDelete_ResultInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + DocumentOperationResult result = new DocumentOperationResult(); + result.setResultCode(302); + result.setError(new ErrorResult("type-1", "reason-1")); + result.setFailureCause("test-failure"); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Mockito.when(documentStore.deleteDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class))) + .thenReturn(result); + Response response = documentApi.processDelete(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FOUND.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessGet_RequestThrowsException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Response response = documentApi.processGet(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessGet_RequestInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(false); + Response response = documentApi.processGet(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessGet_ResultInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + DocumentOperationResult result = new DocumentOperationResult(); + result.setResultCode(302); + result.setError(new ErrorResult("type-1", "reason-1")); + result.setFailureCause("test-failure"); + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Mockito.when(documentStore.getDocument(Mockito.anyString(), Mockito.any(DocumentStoreDataEntity.class))) + .thenReturn(result); + Response response = documentApi.processGet(content, request, headers, httpResponse, "index","id-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FOUND.getStatusCode() == response.getStatus()); + } + + @Test + public void testQueryWithGetWithPayload_NullContent(){ + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = null; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Response response = documentApi.queryWithGetWithPayload(content, request, headers, "index-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.BAD_REQUEST.getStatusCode() == response.getStatus()); + } + + @Test + public void testQueryWithGetWithPayload_RequestThrowsException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Response response = documentApi.queryWithGetWithPayload(content, request, headers, "index-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testQueryWithGetWithPayload_RequestInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String content = "content"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(false); + Response response = documentApi.queryWithGetWithPayload(content, request, headers, "index-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testCreateProcessIndex_IndexApi_RequestInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(false); + Response response = indexApi.processCreateIndex("document-1", request, headers, "index-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testCreateProcessIndex_IndexApi_RequestThrowsException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Response response = indexApi.processCreateIndex("document-1", request, headers, "index-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testCreateProcessIndex_IndexApi_NullDocument() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + String documentSchema= null; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Response response = indexApi.processCreateIndex(documentSchema, request, headers, "index-1", + documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessDelete_IndexApi_RequestInvalid() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(false); + Response response = indexApi.processDelete("document-1", request, headers, documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessDelete_IndexApi_RequestThrowsException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenThrow(IllegalArgumentException.class); + Response response = indexApi.processDelete("document-1", request, headers, documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()); + } + + @Test + public void testProcessDelete_IndexApi_DeleteIndexException() throws Exception { + String transactionId = "transactionId-1"; + String remoteAddr = "http://127.0.0.1"; + Mockito.when(headers.getRequestHeaders()).thenReturn(multivaluedMap); + Mockito.when(multivaluedMap.getFirst(Mockito.anyString())).thenReturn(transactionId); + Mockito.when(request.getRemoteAddr()).thenReturn(remoteAddr); + Mockito.when(request.getMethod()).thenReturn("testMethod"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("http://127.0.0.1")); + Mockito.when(request.getRemoteHost()).thenReturn("localhost"); + Mockito.when(searchServiceApi.validateRequest(Mockito.any(HttpHeaders.class), + Mockito.any(HttpServletRequest.class), Mockito.any(ApiUtils.Action.class), Mockito.anyString())) + .thenReturn(true); + Mockito.when(documentStore.deleteIndex(Mockito.anyString())).thenThrow(DocumentStoreOperationException.class); + Response response = indexApi.processDelete("document-1", request, headers, documentStore); + Assert.assertNotNull(response); + Assert.assertTrue(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() == response.getStatus()); + } +} diff --git a/src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/config/ElasticSearchConfigTest.java b/src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/config/ElasticSearchConfigTest.java new file mode 100644 index 0000000..305963d --- /dev/null +++ b/src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/config/ElasticSearchConfigTest.java @@ -0,0 +1,52 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.sa.searchdbabstraction.elasticsearch.config; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Properties; + +public class ElasticSearchConfigTest { + + ElasticSearchConfig elasticSearchConfig; + + @Before + public void setUp(){ + Properties prop = new Properties(); + prop.put("es.cluster-name", "cluster-1"); + prop.put("es.ip-address", "127.0.0.1"); + prop.put("es.http-port", "9001"); + elasticSearchConfig = new ElasticSearchConfig(prop); + } + + @Test + public void testAllGetMethods(){ + Assert.assertEquals(elasticSearchConfig.getClusterName(), "cluster-1"); + Assert.assertEquals(elasticSearchConfig.getIpAddress(), "127.0.0.1"); + Assert.assertEquals(elasticSearchConfig.getHttpPort(), "9001"); + Assert.assertEquals(elasticSearchConfig.getJavaApiPort(), "9300"); + Assert.assertNotNull(elasticSearchConfig.toString()); + } +} diff --git a/src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchResultItemTest.java b/src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchResultItemTest.java new file mode 100644 index 0000000..3233d4b --- /dev/null +++ b/src/test/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchResultItemTest.java @@ -0,0 +1,121 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao; + +import org.junit.Assert; +import org.junit.Test; + +public class ElasticSearchResultItemTest { + + @Test + public void testAllMethods(){ + ElasticSearchShardStatus shardStatus = new ElasticSearchShardStatus(); + shardStatus.setTotal(10); + shardStatus.setSuccessful(0); + shardStatus.setFailed(1); + Assert.assertEquals(shardStatus.getTotal(), 10); + Assert.assertEquals(shardStatus.getSuccessful(), 0); + Assert.assertEquals(shardStatus.getFailed(), 1); + + ElasticSearchCause cause = new ElasticSearchCause(); + cause.setType("type-1"); + cause.setReason("reason-1"); + Assert.assertEquals(cause.getType(), "type-1"); + Assert.assertEquals(cause.getReason(), "reason-1"); + + ElasticSearchError error = new ElasticSearchError(); + error.setType("type-1"); + error.setReason("reason-1"); + error.setCausedBy(cause); + Assert.assertEquals(error.getType(), "type-1"); + Assert.assertEquals(error.getReason(), "reason-1"); + Assert.assertNotNull(error.getCausedBy()); + error.setAdditionalProperties("name-1", "value-1"); + Assert.assertNotNull(error.getAdditionalProperties()); + + //Create Status + ElasticSearchResultItem resultItem1 = new ElasticSearchResultItem(); + resultItem1.setCreate(getStatus(shardStatus, error)); + Assert.assertNotNull(resultItem1.getCreate()); + Assert.assertEquals(resultItem1.operationType(), "create"); + Assert.assertEquals(resultItem1.operationStatus(), resultItem1.getCreate()); + Assert.assertTrue(resultItem1.toString().contains("create")); + Assert.assertNotNull(resultItem1.toJson()); + + //Index Status + ElasticSearchResultItem resultItem2 = new ElasticSearchResultItem(); + resultItem2.setIndex(getStatus(shardStatus, error)); + Assert.assertNotNull(resultItem2.getIndex()); + Assert.assertEquals(resultItem2.operationType(), "update"); + Assert.assertEquals(resultItem2.operationStatus(), resultItem2.getIndex()); + Assert.assertTrue(resultItem2.toString().contains("index")); + Assert.assertNotNull(resultItem2.toJson()); + + //Delete Status + ElasticSearchResultItem resultItem3 = new ElasticSearchResultItem(); + resultItem3.setDelete(getStatus(shardStatus, error)); + Assert.assertNotNull(resultItem3.getDelete()); + Assert.assertEquals(resultItem3.operationType(), "delete"); + Assert.assertEquals(resultItem3.operationStatus(), resultItem3.getDelete()); + Assert.assertTrue(resultItem3.toString().contains("delete")); + Assert.assertNotNull(resultItem3.toJson()); + + //Unknown Status + ElasticSearchResultItem resultItem4 = new ElasticSearchResultItem(); + Assert.assertEquals(resultItem4.operationType(), "unknown"); + Assert.assertNull(resultItem4.operationStatus()); + + //ElasticSearchBulkOperationResult + ElasticSearchResultItem[] resultItems = {resultItem1, resultItem2, resultItem3}; + ElasticSearchBulkOperationResult result = new ElasticSearchBulkOperationResult(); + result.setErrors(true); + result.setTook(new Integer(10)); + result.setItems(resultItems); + Assert.assertTrue(result.getErrors()); + Assert.assertEquals(result.getTook(), new Integer(10)); + Assert.assertNotNull(result.getItems()); + Assert.assertNotNull(result.toString()); + } + + private ElasticSearchOperationStatus getStatus(ElasticSearchShardStatus shardStatus, ElasticSearchError error) { + ElasticSearchOperationStatus status = new ElasticSearchOperationStatus(); + status.setIndex("index-1"); + status.setType("type-1"); + status.setId("id-1"); + status.setVersion("1.0"); + status.setShards(shardStatus); + status.setStatus(new Integer(1)); + status.setError(error); + status.setAdditionalProperties("REQUEST_URL", "http://127.0.0.1"); + Assert.assertEquals(status.getIndex(), "index-1"); + Assert.assertEquals(status.getType(), "type-1"); + Assert.assertEquals(status.getId(), "id-1"); + Assert.assertEquals(status.getVersion(), "1.0"); + Assert.assertEquals(status.getStatus(), new Integer(1)); + Assert.assertNotNull(status.getShards()); + Assert.assertNotNull(status.getError()); + Assert.assertNotNull(status.getAdditionalProperties()); + return status; + } + +} diff --git a/src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/RangeQueryTest.java b/src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/RangeQueryTest.java new file mode 100644 index 0000000..cd8e2c0 --- /dev/null +++ b/src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/RangeQueryTest.java @@ -0,0 +1,153 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.sa.searchdbabstraction.searchapi; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.junit.Test; +import org.onap.aai.sa.rest.TestUtils; + +import java.io.File; +import java.io.IOException; +import java.util.Date; + +public class RangeQueryTest { + + @Test(expected=IllegalArgumentException.class) + public void testSetGt(){ + + RangeQuery rq = new RangeQuery(); + rq.setLt(new String("2x")); + Assert.assertEquals("2x", rq.getLt()); + Assert.assertNotNull(rq.toElasticSearch()); + Assert.assertNotNull(rq.toString()); + rq.setGt(new Integer(1)); + } + + @Test(expected=IllegalArgumentException.class) + public void testSetGte(){ + + RangeQuery rq = new RangeQuery(); + rq.setGt(new Integer(1)); + Assert.assertNotNull(rq.toElasticSearch()); + Assert.assertNotNull(rq.toString()); + rq.setGte(new Integer(1)); + } + + @Test(expected=IllegalArgumentException.class) + public void testSetLt(){ + + RangeQuery rq = new RangeQuery(); + rq.setLt(new Integer(1)); + rq.setFormat("format-1"); + Assert.assertEquals(rq.getFormat(), "format-1"); + Assert.assertNotNull(rq.toElasticSearch()); + Assert.assertNotNull(rq.toString()); + + rq.setGt(new Integer(1)); + Assert.assertNotNull(rq.toElasticSearch()); + Assert.assertNotNull(rq.toString()); + rq.setLt(new String("10")); + } + + @Test(expected=IllegalArgumentException.class) + public void testSetLte(){ + RangeQuery rq = new RangeQuery(); + rq.setGt(new Integer(1)); + rq.setTimeZone("CT"); + Assert.assertEquals(rq.getTimeZone(), "CT"); + Assert.assertNotNull(rq.toElasticSearch()); + Assert.assertNotNull(rq.toString()); + + rq.setLte(new String("10")); + } + + @Test + public void testSearchStatementAggregations() throws IOException { + File queryWithSubrangeFile = new File("src/test/resources/json/queries/query-with-subrange.json"); + String queryWithSubrangeStr = TestUtils.readFileToString(queryWithSubrangeFile); + + ObjectMapper mapper = new ObjectMapper(); + SearchStatement ss = mapper.readValue(queryWithSubrangeStr, SearchStatement.class); + + Aggregation a1 = getAggregationObject(); + Aggregation a2 = getAggregationObject(); + Aggregation[] aggs= new Aggregation[] {a1, a2}; + ss.setAggregations(aggs); + Assert.assertNotNull(ss.toString()); + } + + private Aggregation getAggregationObject(){ + Aggregation a = new Aggregation(); + + AggregationStatement as = new AggregationStatement(); + DateHistogramAggregation dha = new DateHistogramAggregation(); + dha.setField("field-1"); + dha.setInterval("interval-1"); + Assert.assertEquals(dha.getInterval(), "interval-1"); + dha.setTimeZone("CT"); + Assert.assertEquals(dha.getTimeZone(), "CT"); + dha.setFormat("format-1"); + Assert.assertEquals(dha.getFormat(), "format-1"); + dha.setSize(10); + dha.setMinThreshold(1); + Assert.assertNotNull(dha.toElasticSearch()); + Assert.assertNotNull(dha.toString()); + as.setDateHist(dha); + as.toString(); + + as.getNestedPath(); + + DateRangeAggregation dra = new DateRangeAggregation(); + dra.setField("field-1"); + dra.setMinThreshold(1); + dra.setFormat("format-1"); + Assert.assertEquals(dra.getFormat(), "format-1"); + DateRange dr = new DateRange(); + dr.setFromDate("01-12-2017"); + Assert.assertEquals(dr.getFromDate(), "01-12-2017"); + dr.setToDate("21-12-2017"); + Assert.assertEquals(dr.getToDate(), "21-12-2017"); + DateRange[] drs = {dr}; + dra.setDateRanges(drs); + Assert.assertTrue(dra.getDateRanges().length==1); + Assert.assertNotNull(dra.toElasticSearch()); + Assert.assertNotNull(dra.toString()); + as.setDateRange(dra); + as.toString(); + + as.getNestedPath(); + + GroupByAggregation gba = new GroupByAggregation(); + gba.setField("field-1"); + gba.setMinThreshold(1); + Assert.assertNotNull(gba.toElasticSearch()); + Assert.assertNotNull(gba.toString()); + as.setGroupBy(gba); + Assert.assertNotNull(as.toString()); + + a.setStatement(as); + Assert.assertNotNull(a.toString()); + return a; + } +} diff --git a/src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/TermQueryTest.java b/src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/TermQueryTest.java new file mode 100644 index 0000000..fd16574 --- /dev/null +++ b/src/test/java/org/onap/aai/sa/searchdbabstraction/searchapi/TermQueryTest.java @@ -0,0 +1,68 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.sa.searchdbabstraction.searchapi; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +public class TermQueryTest { + + @Test + public void testAllMethods() throws IOException { + + String field = "searchTags.nested"; + String stringValue = "theValue.nested"; + String termQueryWithStringValueJson = "{\"field\": \"" + field + "\", \"value\": \"" + stringValue + "\"}"; + String termQueryWithStringValueExpectedES = "{\"term\": {\"" + field + "\" : \"" + stringValue + "\"}}"; + + ObjectMapper mapper = new ObjectMapper(); + TermQuery stringTermQuery = mapper.readValue(termQueryWithStringValueJson, TermQuery.class); + Assert.assertEquals(stringValue, stringTermQuery.getValue()); + Assert.assertEquals("searchTags.nested", stringTermQuery.getField()); + stringTermQuery.setOperator("operator-1"); + Assert.assertEquals("operator-1", stringTermQuery.getOperator()); + stringTermQuery.setSearchAnalyzer("search-1"); + Assert.assertEquals("search-1", stringTermQuery.getSearchAnalyzer()); + + String field1 = "searchTags-1 searchTags.second"; + String stringValue1 = "theValue-1 theValue.second"; + String multiFieldTermQueryJSon = "{\"field\": \"" + field1 + "\", \"value\": \"" + stringValue1 + "\"}"; + TermQuery multiFieldTermQuery = mapper.readValue(multiFieldTermQueryJSon, TermQuery.class); + multiFieldTermQuery.setOperator("and"); + multiFieldTermQuery.setSearchAnalyzer("search-1"); + Assert.assertNotNull(multiFieldTermQuery.toElasticSearch()); + Assert.assertNotNull(multiFieldTermQuery.pathForNestedField(field1)); + + String field2 = "search11 search2"; + String stringValue2 = "theValue1 theValue2"; + String multiFieldTermJSon = "{\"field\": \"" + field2 + "\", \"value\": \"" + stringValue2 + "\"}"; + TermQuery multiFieldTerm = mapper.readValue(multiFieldTermJSon, TermQuery.class); + multiFieldTerm.setOperator("or"); + multiFieldTerm.setSearchAnalyzer("search-1"); + Assert.assertNotNull(multiFieldTerm.toElasticSearch()); + } + +} diff --git a/src/test/resources/json/search_policy.json b/src/test/resources/json/search_policy.json new file mode 100644 index 0000000..ab8d0e3 --- /dev/null +++ b/src/test/resources/json/search_policy.json @@ -0,0 +1,37 @@ +{ + "roles": [{ + "name": "testRole", + "functions": [{ + "name": "testFunction", + "methods": [{ + "name": "GET" + }, { + "name": "DELETE" + }, { + "name": "PUT" + } + ] + } + ], + "users": [{ + "username": "testUser" + } + ] + }, { + "name": "testBasicAuth", + "functions": [{ + "name": "testBasicAuthFuncyion", + "methods": [{ + "name": "GET" + } + ] + } + ], + "users": [{ + "user": "testBasicAuthUser", + "pass": "OBF:1ytc1vu91v2p1rxf1mqh1v8s1z0d1msn1san1mqf1z0h1v9u1msl1rvf1v1p1vv11yta" + } + ] + } + ] +} \ No newline at end of file -- 2.16.6