diff options
author | 2017-12-04 15:31:11 -0500 | |
---|---|---|
committer | 2017-12-04 15:31:29 -0500 | |
commit | 45f302a5f05223d98a23b5ca2dbf6c6fe0f823df (patch) | |
tree | f324bf64f738692c951cf9a878db97159121d0c9 /aai-resources/src/test/java/org | |
parent | 295d93f27c1c5ee0040b1ef39da3ac970b01fcdc (diff) |
Support bulk api for patch/deletes
Issue-ID: AAI-533
Change-Id: I86ece3494985da48606f356dada267fac14a7aad
Signed-off-by: Maharajh, Robby (rx2202) <rx2202@us.att.com>
Diffstat (limited to 'aai-resources/src/test/java/org')
3 files changed, 293 insertions, 157 deletions
diff --git a/aai-resources/src/test/java/org/onap/aai/rest/BulkAddConsumerTest.java b/aai-resources/src/test/java/org/onap/aai/rest/BulkAddConsumerTest.java index 28ded9d..66afeeb 100644 --- a/aai-resources/src/test/java/org/onap/aai/rest/BulkAddConsumerTest.java +++ b/aai-resources/src/test/java/org/onap/aai/rest/BulkAddConsumerTest.java @@ -21,169 +21,100 @@ */ package org.onap.aai.rest; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.apache.commons.io.IOUtils; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.Mockito; -import org.onap.aai.AAISetup; -import org.onap.aai.dbmap.AAIGraph; -import org.onap.aai.introspection.ModelInjestor; -import org.onap.aai.introspection.Version; - -import javax.ws.rs.core.*; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.when; -public class BulkAddConsumerTest extends AAISetup { - - protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json"); - - protected static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>(); - - static { - VALID_HTTP_STATUS_CODES.add(200); - VALID_HTTP_STATUS_CODES.add(201); - VALID_HTTP_STATUS_CODES.add(204); - } - - protected BulkConsumer bulkConsumer; - - protected HttpHeaders httpHeaders; +import java.io.IOException; - protected UriInfo uriInfo; +import javax.ws.rs.core.Response; - protected MultivaluedMap<String, String> headersMultiMap; - protected MultivaluedMap<String, String> queryParameters; +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; +import org.onap.aai.introspection.Version; - protected List<String> aaiRequestContextList; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; - protected List<MediaType> outputMediaTypes; +public class BulkAddConsumerTest extends BulkProcessorTestAbstraction { private static final EELFLogger logger = EELFManager.getInstance().getLogger(BulkAddConsumerTest.class.getName()); - @BeforeClass - public static void setupRest(){ - AAIGraph.getInstance(); - ModelInjestor.getInstance(); - } - - @Before - public void setup(){ - logger.info("Starting the setup for the integration tests of Rest Endpoints"); - bulkConsumer = getConsumer(); - httpHeaders = Mockito.mock(HttpHeaders.class); - uriInfo = Mockito.mock(UriInfo.class); + @Test + public void validBulkAddTest() throws IOException { - headersMultiMap = new MultivaluedHashMap<>(); - queryParameters = Mockito.spy(new MultivaluedHashMap<>()); - - headersMultiMap.add("X-FromAppId", "JUNIT"); - headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString()); - headersMultiMap.add("Real-Time", "true"); - headersMultiMap.add("Accept", "application/json"); - headersMultiMap.add("aai-request-context", ""); - - outputMediaTypes = new ArrayList<>(); - outputMediaTypes.add(APPLICATION_JSON); - - aaiRequestContextList = new ArrayList<>(); - aaiRequestContextList.add(""); - - when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); - when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap); + when(uriInfo.getPath()).thenReturn(uri); + when(uriInfo.getPath(false)).thenReturn(uri); - when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList); + String payload = getBulkPayload("pserver-transactions"); + Response response = executeRequest(payload); + assertEquals("Valid Response Code", Response.Status.CREATED.getStatusCode(), response.getStatus()); + assertEquals("Contains 3 {\"201\":null}", 3, StringUtils.countMatches(response.getEntity().toString(), "{\"201\":null}")); + } + + @Test + public void bulkProcessPayloadInBulkAddTest() throws IOException { - when(uriInfo.getQueryParameters()).thenReturn(queryParameters); - when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters); + when(uriInfo.getPath()).thenReturn(uri); + when(uriInfo.getPath(false)).thenReturn(uri); - // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable - Mockito.doReturn(null).when(queryParameters).remove(anyObject()); + String payload = getBulkPayload("pserver-bulk-process-transactions"); + Response response = executeRequest(payload); - when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON); + assertEquals("Valid Response Code", Response.Status.CREATED.getStatusCode(), response.getStatus()); + assertEquals("Contains 1 {\"201\":null}", 1, StringUtils.countMatches(response.getEntity().toString(), "{\"201\":null}")); + assertEquals("Contains 1 {\"400\":\"{", 1, StringUtils.countMatches(response.getEntity().toString(), "{\"400\":\"{")); + assertEquals("Contains 1 ERR.5.4.6118", 1, StringUtils.countMatches(response.getEntity().toString(), "ERR.5.4.6118")); } - - @Test - public void testBulkAdd() throws IOException { - - String uri = "/aai/v11/bulkadd"; + + @Test + public void bulkAddInvalidMethodTest() throws IOException { when(uriInfo.getPath()).thenReturn(uri); when(uriInfo.getPath(false)).thenReturn(uri); - String payload = getBulkPayload("pserver-transactions"); - Response response = bulkConsumer.bulkAdd( - payload, - Version.getLatest().toString(), - httpHeaders, - uriInfo, - null - ); - - System.out.println("Code: " + response.getStatus() + "\tResponse: " + response.getEntity()); - assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); + String payload = getBulkPayload("pserver-transactions-invalid-method"); + Response response = executeRequest(payload); + + assertEquals("Valid Response Code", Response.Status.CREATED.getStatusCode(), response.getStatus()); + assertEquals("Contains 1 {\"400\":\"{", 1, StringUtils.countMatches(response.getEntity().toString(), "{\"400\":\"{")); + assertEquals("Contains 1 ERR.5.4.6118", 1, StringUtils.countMatches(response.getEntity().toString(), "ERR.5.4.6118")); } @Test - public void testBulkAddThrowExceptionWhenPayloadContainsNoTransactions(){ - - String uri = "/aai/v11/bulkadd"; + public void bulkAddThrowExceptionWhenPayloadContainsNoTransactionsTest(){ when(uriInfo.getPath()).thenReturn(uri); when(uriInfo.getPath(false)).thenReturn(uri); String payload = "{\"transactions\":[]}"; - Response response = bulkConsumer.bulkAdd( - payload, - Version.getLatest().toString(), - httpHeaders, - uriInfo, - null - ); - - System.out.println("Code: " + response.getStatus() + "\tResponse: " + response.getEntity()); - assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + Response response = executeRequest(payload); + + assertEquals("Bad Request", Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("Contains error code", true, response.getEntity().toString().contains("ERR.5.4.6118")); } @Test - public void testBulkAddThrowExceptionWhenInvalidJson() throws IOException { - - String uri = "/aai/v11/bulkadd"; + public void bulkAddThrowExceptionWhenInvalidJsonTest() throws IOException { when(uriInfo.getPath()).thenReturn(uri); when(uriInfo.getPath(false)).thenReturn(uri); String payload = "{"; - Response response = bulkConsumer.bulkAdd( - payload, - Version.getLatest().toString(), - httpHeaders, - uriInfo, - null - ); - - System.out.println("Code: " + response.getStatus() + "\tResponse: " + response.getEntity()); - assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); - - // TODO - Verify the result output and check if it contains an 400 in the list - } + Response response = executeRequest(payload); - public String getBulkPayload(String bulkName) throws IOException { - return getPayload("payloads/bulk/" + bulkName + ".json"); + assertEquals("Bad Request", Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("Contains error code", true, response.getEntity().toString().contains("ERR.5.4.6111")); } - - public BulkConsumer getConsumer(){ + + @Override + protected BulkConsumer getConsumer(){ return new BulkAddConsumer(); } + + @Override + protected String getUri() { + return "/aai/" + Version.getLatest().toString() + "/bulkadd"; + } } diff --git a/aai-resources/src/test/java/org/onap/aai/rest/BulkProcessConsumerTest.java b/aai-resources/src/test/java/org/onap/aai/rest/BulkProcessConsumerTest.java index 99bccc7..92d1637 100644 --- a/aai-resources/src/test/java/org/onap/aai/rest/BulkProcessConsumerTest.java +++ b/aai-resources/src/test/java/org/onap/aai/rest/BulkProcessConsumerTest.java @@ -21,55 +21,117 @@ */ package org.onap.aai.rest; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.apache.commons.io.IOUtils; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.Mockito; -import org.onap.aai.AAISetup; -import org.onap.aai.dbmap.AAIGraph; -import org.onap.aai.introspection.ModelInjestor; -import org.onap.aai.introspection.Version; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; -import javax.ws.rs.core.*; import java.io.IOException; -import java.io.InputStream; -import java.util.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.when; +import javax.ws.rs.core.Response; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; +import org.onap.aai.introspection.Version; -public class BulkProcessConsumerTest extends BulkAddConsumerTest { +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class BulkProcessConsumerTest extends BulkProcessorTestAbstraction { private static final EELFLogger logger = EELFManager.getInstance().getLogger(BulkProcessConsumerTest.class.getName()); - @Test - public void testBulkAddCreatedWhenOneTransactionInPayloadContainsNotAllowedVerb() throws IOException { - String uri = "/aai/v11/bulkadd"; + @Test + public void bulkAddPayloadInBulkProcessTest() throws IOException { + + when(uriInfo.getPath()).thenReturn(uri); + when(uriInfo.getPath(false)).thenReturn(uri); + + String payload = getBulkPayload("pserver-transactions"); + Response response = executeRequest(payload); + + assertEquals("Valid Response Code", Response.Status.CREATED.getStatusCode(), response.getStatus()); + assertEquals("Contains 3 {\"201\":null}", 3, StringUtils.countMatches(response.getEntity().toString(), "{\"201\":null}")); + } + + @Test + public void bulkProcessPayloadTest() throws IOException { + + when(uriInfo.getPath()).thenReturn(uri); + when(uriInfo.getPath(false)).thenReturn(uri); + + String payload = getBulkPayload("pserver-bulk-process-transactions"); + Response response = executeRequest(payload); + + assertEquals("Valid Response Code", Response.Status.CREATED.getStatusCode(), response.getStatus()); + assertEquals("Contains 1 {\"201\":null}", 1, StringUtils.countMatches(response.getEntity().toString(), "{\"201\":null}")); + assertEquals("Contains 1 {\"404\":\"{", 1, StringUtils.countMatches(response.getEntity().toString(), "{\"404\":\"{")); + assertEquals("Contains 1 ERR.5.4.6114", 1, StringUtils.countMatches(response.getEntity().toString(), "ERR.5.4.6114")); + } + + @Test + public void bulkProcessComplexDeletePayloadTest() throws IOException { when(uriInfo.getPath()).thenReturn(uri); when(uriInfo.getPath(false)).thenReturn(uri); - String payload = getBulkPayload("pserver-transactions-invalid"); - Response response = bulkConsumer.bulkAdd( - payload, - Version.getLatest().toString(), - httpHeaders, - uriInfo, - null - ); - - System.out.println("Code: " + response.getStatus() + "\tResponse: " + response.getEntity()); - assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); + String payload = getBulkPayload("complex-bulk-process-transactions"); + Response response = executeRequest(payload); + + System.out.println(response.getEntity().toString()); + assertEquals("Valid Response Code", Response.Status.CREATED.getStatusCode(), response.getStatus()); + assertEquals("Contains 0 {\"201\":null}", 0, StringUtils.countMatches(response.getEntity().toString(), "{\"201\":null}")); + assertEquals("Contains 1 {\"404\":\"{", 1, StringUtils.countMatches(response.getEntity().toString(), "{\"404\":\"{")); + assertEquals("Contains 1 ERR.5.4.6114", 1, StringUtils.countMatches(response.getEntity().toString(), "ERR.5.4.6114")); } + + + @Test + public void bulkAddInvalidMethodTest() throws IOException { + + when(uriInfo.getPath()).thenReturn(uri); + when(uriInfo.getPath(false)).thenReturn(uri); + + String payload = getBulkPayload("pserver-transactions-invalid-method"); + Response response = executeRequest(payload); + assertEquals("Valid Response Code", Response.Status.CREATED.getStatusCode(), response.getStatus()); + assertEquals("Contains 1 {\"400\":\"{", 1, StringUtils.countMatches(response.getEntity().toString(), "{\"400\":\"{")); + assertEquals("Contains 1 ERR.5.4.6118", 1, StringUtils.countMatches(response.getEntity().toString(), "ERR.5.4.6118")); + } + + @Test + public void bulkAddThrowExceptionWhenPayloadContainsNoTransactionsTest(){ + + when(uriInfo.getPath()).thenReturn(uri); + when(uriInfo.getPath(false)).thenReturn(uri); + + String payload = "{\"transactions\":[]}"; + Response response = executeRequest(payload); + + assertEquals("Bad Request", Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("Contains error code", true, response.getEntity().toString().contains("ERR.5.4.6118")); + } + + @Test + public void bulkAddThrowExceptionWhenInvalidJsonTest() throws IOException { + + when(uriInfo.getPath()).thenReturn(uri); + when(uriInfo.getPath(false)).thenReturn(uri); + + String payload = "{"; + Response response = executeRequest(payload); + + assertEquals("Bad Request", Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + assertEquals("Contains error code", true, response.getEntity().toString().contains("ERR.5.4.6111")); + } + @Override - public BulkConsumer getConsumer(){ + protected BulkConsumer getConsumer(){ return new BulkProcessConsumer(); } + + @Override + protected String getUri() { + return "/aai/" + Version.getLatest().toString() + "/bulkprocess"; + } } diff --git a/aai-resources/src/test/java/org/onap/aai/rest/BulkProcessorTestAbstraction.java b/aai-resources/src/test/java/org/onap/aai/rest/BulkProcessorTestAbstraction.java new file mode 100644 index 0000000..f5d0670 --- /dev/null +++ b/aai-resources/src/test/java/org/onap/aai/rest/BulkProcessorTestAbstraction.java @@ -0,0 +1,143 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.rest; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.mockito.Mockito; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.AAIGraph; +import org.onap.aai.introspection.ModelInjestor; +import org.onap.aai.introspection.Version; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public abstract class BulkProcessorTestAbstraction extends AAISetup { + + protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json"); + + protected static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>(); + + static { + VALID_HTTP_STATUS_CODES.add(200); + VALID_HTTP_STATUS_CODES.add(201); + VALID_HTTP_STATUS_CODES.add(204); + } + + protected BulkConsumer bulkConsumer; + + protected HttpHeaders httpHeaders; + + protected UriInfo uriInfo; + + protected MultivaluedMap<String, String> headersMultiMap; + protected MultivaluedMap<String, String> queryParameters; + + protected List<String> aaiRequestContextList; + + protected List<MediaType> outputMediaTypes; + + protected String uri; + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(BulkProcessorTestAbstraction.class.getName()); + + @BeforeClass + public static void setupRest(){ + AAIGraph.getInstance(); + ModelInjestor.getInstance(); + } + + @Before + public void setup(){ + logger.info("Starting the setup for the integration tests of Rest Endpoints"); + + bulkConsumer = getConsumer(); + uri = getUri(); + httpHeaders = Mockito.mock(HttpHeaders.class); + uriInfo = Mockito.mock(UriInfo.class); + + headersMultiMap = new MultivaluedHashMap<>(); + queryParameters = Mockito.spy(new MultivaluedHashMap<>()); + + headersMultiMap.add("X-FromAppId", "JUNIT"); + headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString()); + headersMultiMap.add("Real-Time", "true"); + headersMultiMap.add("Accept", "application/json"); + headersMultiMap.add("aai-request-context", ""); + + outputMediaTypes = new ArrayList<>(); + outputMediaTypes.add(APPLICATION_JSON); + + aaiRequestContextList = new ArrayList<>(); + aaiRequestContextList.add(""); + + when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); + when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap); + + when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList); + + + when(uriInfo.getQueryParameters()).thenReturn(queryParameters); + when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters); + + // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable + Mockito.doReturn(null).when(queryParameters).remove(anyObject()); + + when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON); + } + + protected Response executeRequest(String payload) { + return bulkConsumer.bulkProcessor( + payload.replaceAll("<UUID>", UUID.randomUUID().toString()), + Version.getLatest().toString(), + httpHeaders, + uriInfo, + null + ); + } + + protected String getBulkPayload(String bulkPayloadName) throws IOException { + return getPayload("payloads/bulk/" + bulkPayloadName + ".json"); + } + + protected abstract BulkConsumer getConsumer(); + + protected abstract String getUri(); +} |