From 1c8ac9c824ae2f5bf82ff5082c3485d057ec3f1a Mon Sep 17 00:00:00 2001 From: Venkata Harish K Kajur Date: Fri, 15 Sep 2017 12:48:12 -0400 Subject: [PATCH] Add unit tests to get 30% coverage Issue-ID: AAI-212 Change-Id: I270452f725f30c620091406df6f5ce07fc2cb264 Signed-off-by: Venkata Harish K Kajur --- .../openecomp/aai/dbgraphmap/SearchGraphTest.java | 148 +++++++++++++ .../interceptors/AAILogJAXRSInInterceptorTest.java | 203 ++++++++++++++++++ .../AAILogJAXRSOutInterceptorTest.java | 236 +++++++++++++++++++++ .../interceptors/PostAaiAjscInterceptorTest.java | 73 +++++++ .../interceptors/PreAaiAjscInterceptorTest.java | 62 ++++++ .../openecomp/aai/rest/ExceptionHandlerTest.java | 155 ++++++++++++++ .../openecomp/aai/rest/util/EchoResponseTest.java | 135 ++++++++++++ 7 files changed, 1012 insertions(+) create mode 100644 aai-traversal/src/test/java/org/openecomp/aai/dbgraphmap/SearchGraphTest.java create mode 100644 aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSInInterceptorTest.java create mode 100644 aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSOutInterceptorTest.java create mode 100644 aai-traversal/src/test/java/org/openecomp/aai/interceptors/PostAaiAjscInterceptorTest.java create mode 100644 aai-traversal/src/test/java/org/openecomp/aai/interceptors/PreAaiAjscInterceptorTest.java create mode 100644 aai-traversal/src/test/java/org/openecomp/aai/rest/ExceptionHandlerTest.java create mode 100644 aai-traversal/src/test/java/org/openecomp/aai/rest/util/EchoResponseTest.java diff --git a/aai-traversal/src/test/java/org/openecomp/aai/dbgraphmap/SearchGraphTest.java b/aai-traversal/src/test/java/org/openecomp/aai/dbgraphmap/SearchGraphTest.java new file mode 100644 index 0000000..3dc2713 --- /dev/null +++ b/aai-traversal/src/test/java/org/openecomp/aai/dbgraphmap/SearchGraphTest.java @@ -0,0 +1,148 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package org.openecomp.aai.dbgraphmap; + +import com.thinkaurelius.titan.core.TitanGraph; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.aai.dbmap.DBConnectionType; +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.introspection.Loader; +import org.openecomp.aai.introspection.LoaderFactory; +import org.openecomp.aai.introspection.ModelType; +import org.openecomp.aai.introspection.Version; +import org.openecomp.aai.serialization.db.DBSerializer; +import org.openecomp.aai.serialization.engines.QueryStyle; +import org.openecomp.aai.serialization.engines.TitanDBEngine; +import org.openecomp.aai.serialization.engines.TransactionalGraphEngine; +import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder; + +import javax.ws.rs.core.*; +import java.util.*; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SearchGraphTest { + + private SearchGraph searchGraph; + + protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json"); + + private static final Set VALID_HTTP_STATUS_CODES = new HashSet<>(); + + private final static Version version = Version.getLatest(); + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + static { + VALID_HTTP_STATUS_CODES.add(200); + VALID_HTTP_STATUS_CODES.add(201); + VALID_HTTP_STATUS_CODES.add(204); + } + + private HttpHeaders httpHeaders; + + private UriInfo uriInfo; + + private MultivaluedMap headersMultiMap; + private MultivaluedMap queryParameters; + + private List aaiRequestContextList; + + private List outputMediaTypes; + + private Loader loader; + private TitanGraph graph; + + private Graph tx; + + private GraphTraversalSource g; + private TransactionalGraphEngine dbEngine; + + @Before + public void setup(){ + + System.setProperty("AJSC_HOME", "."); + System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local"); + + searchGraph = new SearchGraph(); + + httpHeaders = mock(HttpHeaders.class); + uriInfo = 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("X-FromAppId")).thenReturn(Arrays.asList("JUNIT")); + when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT")); + + 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); + loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version); + dbEngine = new TitanDBEngine( + queryStyle, + type, + loader); + } + + @Test(expected = AAIException.class) + public void testRunGenericQueryFailWhenInvalidRelationshipList() throws AAIException { + + List keys = new ArrayList<>(); + keys.add("cloud-region.cloud-owner:test-aic"); + + List includeStrings = new ArrayList<>(); + includeStrings.add("cloud-region"); + + DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "JUNIT"); + UrlBuilder urlBuilder = new UrlBuilder(version, serializer); + Response response = searchGraph.runGenericQuery(httpHeaders, "cloud-region", keys, includeStrings, 1, dbEngine, loader, urlBuilder); + System.out.println(response); + } +} diff --git a/aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSInInterceptorTest.java b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSInInterceptorTest.java new file mode 100644 index 0000000..5687d72 --- /dev/null +++ b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSInInterceptorTest.java @@ -0,0 +1,203 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package org.openecomp.aai.interceptors; + +import org.apache.cxf.message.Exchange; +import org.apache.cxf.message.ExchangeImpl; +import org.apache.cxf.message.Message; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.Mockito.*; + +public class AAILogJAXRSInInterceptorTest { + + private AAILogJAXRSInInterceptor aaiLogJAXRSInInterceptor; + + private Message message; + private Exchange exchange; + private InputStream is; + private Map> headers; + + + @Before + public void setup(){ + + aaiLogJAXRSInInterceptor = new AAILogJAXRSInInterceptor(); + + message = mock(Message.class); + exchange = spy(new ExchangeImpl()); + + is = getClass().getClassLoader().getResourceAsStream("logback.xml"); + + headers = new HashMap<>(); + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("X-TransactionId", Arrays.asList("JUNIT")); + headers.put("Content-Type", Arrays.asList("application/json")); + headers.put("Accept", Arrays.asList("application/json")); + } + + @Test + public void testHandleMessageWhenNotCamelRequest() throws IOException { + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(InputStream.class)).thenReturn(is); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + + when(message.get("CamelHttpUrl")).thenReturn("/somestring"); + aaiLogJAXRSInInterceptor.handleMessage(message); + } + + @Test + public void testHandleMessageWhenUUIDHasMultiple() throws IOException { + + Map> headers = new HashMap<>(); + + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); + headers.put("Content-Type", Arrays.asList("application/json")); + headers.put("Accept", Arrays.asList("application/json")); + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(InputStream.class)).thenReturn(is); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + + when(message.get("CamelHttpUrl")).thenReturn("/somestring"); + aaiLogJAXRSInInterceptor.handleMessage(message); + } + + @Test + public void testHandleMessageWhenMissingTransactionId() throws IOException { + + Map> headers = new HashMap<>(); + + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("Content-Type", Arrays.asList("application/json")); + headers.put("Accept", Arrays.asList("application/json")); + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(InputStream.class)).thenReturn(is); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + + when(message.get("CamelHttpUrl")).thenReturn("/somestring"); + aaiLogJAXRSInInterceptor.handleMessage(message); + } + + @Test + public void testHandleMessageWhenMissingContentType() throws IOException { + + Map> headers = new HashMap<>(); + + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); + headers.put("Accept", Arrays.asList("application/json")); + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(InputStream.class)).thenReturn(is); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + + when(message.get("CamelHttpUrl")).thenReturn("/somestring"); + aaiLogJAXRSInInterceptor.handleMessage(message); + } + + @Test + public void testHandleMessageWhenQueryExistsAndUriEcho() throws IOException { + + Map> headers = new HashMap<>(); + + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); + headers.put("Content-Type", Arrays.asList("application/json")); + headers.put("Accept", Arrays.asList("application/json")); + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(InputStream.class)).thenReturn(is); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + when(message.get(Message.QUERY_STRING)).thenReturn(null); + when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); + when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn(""); + when(message.get("CamelHttpUrl")).thenReturn("/util/echo"); + aaiLogJAXRSInInterceptor.handleMessage(message); + } + + @Test + public void testHandleMessageWhenQueryExistsAndUriTranslog() throws IOException { + + Map> headers = new HashMap<>(); + + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); + headers.put("Content-Type", Arrays.asList("application/json")); + headers.put("Accept", Arrays.asList("application/json")); + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(InputStream.class)).thenReturn(is); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + when(message.get(Message.QUERY_STRING)).thenReturn(null); + when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); + when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn(""); + when(message.get("CamelHttpUrl")).thenReturn("/translog/"); + aaiLogJAXRSInInterceptor.handleMessage(message); + } + + @Test + public void testHandleMessageWhenPutMessageKeyReturnsException() throws IOException { + + Map> headers = new HashMap<>(); + + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); + headers.put("Content-Type", Arrays.asList("application/json")); + headers.put("Accept", Arrays.asList("application/json")); + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(InputStream.class)).thenReturn(is); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + when(message.get(Message.QUERY_STRING)).thenReturn(null); + when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); + when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn(""); + when(message.get("CamelHttpUrl")).thenReturn("/translog/"); + when(message.get(Message.ENCODING)).thenReturn("http"); + when(message.get(Message.RESPONSE_CODE)).thenReturn(200); + + aaiLogJAXRSInInterceptor.handleMessage(message); + } +} diff --git a/aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSOutInterceptorTest.java b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSOutInterceptorTest.java new file mode 100644 index 0000000..4114018 --- /dev/null +++ b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/AAILogJAXRSOutInterceptorTest.java @@ -0,0 +1,236 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package org.openecomp.aai.interceptors; + +import org.apache.cxf.io.CacheAndWriteOutputStream; +import org.apache.cxf.message.Exchange; +import org.apache.cxf.message.ExchangeImpl; +import org.apache.cxf.message.Message; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.Mockito.*; + +public class AAILogJAXRSOutInterceptorTest { + + private AAILogJAXRSOutInterceptor aaiLogJAXRSOutInterceptor; + + private Message message; + private Exchange exchange; + private OutputStream out; + private Map> headers; + private Message outMessage; + private Message inMessage; + + + @Before + public void setup(){ + + aaiLogJAXRSOutInterceptor = new AAILogJAXRSOutInterceptor(); + + message = mock(Message.class); + exchange = spy(new ExchangeImpl()); + out = mock(OutputStream.class); + outMessage = mock(Message.class); + inMessage = mock(Message.class); + + + headers = new HashMap<>(); + headers.put("X-FromAppId", Arrays.asList("JUNIT")); + headers.put("X-TransactionId", Arrays.asList("JUNIT")); + headers.put("Content-Type", Arrays.asList("application/json")); + headers.put("Accept", Arrays.asList("application/json")); + } + + @Test + public void testHandleMessageWhenNotCamelRequest() throws IOException { + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(OutputStream.class)).thenReturn(out); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + when(exchange.getOutMessage()).thenReturn(outMessage); + when(outMessage.getContent(OutputStream.class)).thenReturn(out); + when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); + + when(message.get("CamelHttpUrl")).thenReturn("/somestring"); + aaiLogJAXRSOutInterceptor.handleMessage(message); + } + + @Test + public void testLogCallBack(){ + + when(message.getExchange()).thenReturn(exchange); + when(message.getContent(OutputStream.class)).thenReturn(out); + when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + when(exchange.getOutMessage()).thenReturn(outMessage); + + when(outMessage.getContent(OutputStream.class)).thenReturn(out); + when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); + when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); + when(exchange.getInMessage()).thenReturn(inMessage); + + when(inMessage.getExchange()).thenReturn(exchange); + when(inMessage.getContent(OutputStream.class)).thenReturn(out); + when(inMessage.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); + when(inMessage.get(Message.CONTENT_TYPE)).thenReturn("*/*"); + + AAILogJAXRSOutInterceptor.LoggingCallback loggingCallback = new AAILogJAXRSOutInterceptor().new LoggingCallback(message, out); + final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(out); + loggingCallback.onClose(newOut); + } + +// @Test +// public void testHandleMessageWhenUUIDHasMultiple() throws IOException { +// +// Map> headers = new HashMap<>(); +// +// headers.put("X-FromAppId", Arrays.asList("JUNIT")); +// headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); +// headers.put("Content-Type", Arrays.asList("application/json")); +// headers.put("Accept", Arrays.asList("application/json")); +// +// when(message.getExchange()).thenReturn(exchange); +// when(message.getContent(InputStream.class)).thenReturn(is); +// when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); +// when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); +// +// when(message.get("CamelHttpUrl")).thenReturn("/somestring"); +// aaiLogJAXRSOutInterceptor.handleMessage(message); +// } +// +// @Test +// public void testHandleMessageWhenMissingTransactionId() throws IOException { +// +// Map> headers = new HashMap<>(); +// +// headers.put("X-FromAppId", Arrays.asList("JUNIT")); +// headers.put("Content-Type", Arrays.asList("application/json")); +// headers.put("Accept", Arrays.asList("application/json")); +// +// when(message.getExchange()).thenReturn(exchange); +// when(message.getContent(InputStream.class)).thenReturn(is); +// when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); +// when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); +// +// when(message.get("CamelHttpUrl")).thenReturn("/somestring"); +// aaiLogJAXRSOutInterceptor.handleMessage(message); +// } +// +// @Test +// public void testHandleMessageWhenMissingContentType() throws IOException { +// +// Map> headers = new HashMap<>(); +// +// headers.put("X-FromAppId", Arrays.asList("JUNIT")); +// headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); +// headers.put("Accept", Arrays.asList("application/json")); +// +// when(message.getExchange()).thenReturn(exchange); +// when(message.getContent(InputStream.class)).thenReturn(is); +// when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); +// when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); +// +// when(message.get("CamelHttpUrl")).thenReturn("/somestring"); +// aaiLogJAXRSOutInterceptor.handleMessage(message); +// } +// +// @Test +// public void testHandleMessageWhenQueryExistsAndUriEcho() throws IOException { +// +// Map> headers = new HashMap<>(); +// +// headers.put("X-FromAppId", Arrays.asList("JUNIT")); +// headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); +// headers.put("Content-Type", Arrays.asList("application/json")); +// headers.put("Accept", Arrays.asList("application/json")); +// +// when(message.getExchange()).thenReturn(exchange); +// when(message.getContent(InputStream.class)).thenReturn(is); +// when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); +// when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); +// when(message.get(Message.QUERY_STRING)).thenReturn(null); +// when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); +// when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); +// when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true); +// when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn(""); +// when(message.get("CamelHttpUrl")).thenReturn("/util/echo"); +// aaiLogJAXRSOutInterceptor.handleMessage(message); +// } +// +// @Test +// public void testHandleMessageWhenQueryExistsAndUriTranslog() throws IOException { +// +// Map> headers = new HashMap<>(); +// +// headers.put("X-FromAppId", Arrays.asList("JUNIT")); +// headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); +// headers.put("Content-Type", Arrays.asList("application/json")); +// headers.put("Accept", Arrays.asList("application/json")); +// +// when(message.getExchange()).thenReturn(exchange); +// when(message.getContent(InputStream.class)).thenReturn(is); +// when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); +// when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); +// when(message.get(Message.QUERY_STRING)).thenReturn(null); +// when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); +// when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); +// when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true); +// when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn(""); +// when(message.get("CamelHttpUrl")).thenReturn("/translog/"); +// aaiLogJAXRSOutInterceptor.handleMessage(message); +// } +// +// @Test +// public void testHandleMessageWhenPutMessageKeyReturnsException() throws IOException { +// +// Map> headers = new HashMap<>(); +// +// headers.put("X-FromAppId", Arrays.asList("JUNIT")); +// headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja")); +// headers.put("Content-Type", Arrays.asList("application/json")); +// headers.put("Accept", Arrays.asList("application/json")); +// +// when(message.getExchange()).thenReturn(exchange); +// when(message.getContent(InputStream.class)).thenReturn(is); +// when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers); +// when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*"); +// when(message.get(Message.QUERY_STRING)).thenReturn(null); +// when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true); +// when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn(""); +// when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true); +// when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn(""); +// when(message.get("CamelHttpUrl")).thenReturn("/translog/"); +// when(message.get(Message.ENCODING)).thenReturn("http"); +// when(message.get(Message.RESPONSE_CODE)).thenReturn(200); +// +// aaiLogJAXRSOutInterceptor.handleMessage(message); +// } +} diff --git a/aai-traversal/src/test/java/org/openecomp/aai/interceptors/PostAaiAjscInterceptorTest.java b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/PostAaiAjscInterceptorTest.java new file mode 100644 index 0000000..17f5c44 --- /dev/null +++ b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/PostAaiAjscInterceptorTest.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package org.openecomp.aai.interceptors; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.aai.logging.LoggingContext; + +import javax.servlet.http.HttpServletRequest; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class PostAaiAjscInterceptorTest { + + private PostAaiAjscInterceptor postAaiAjscInterceptor; + + @Before + public void setup(){ + postAaiAjscInterceptor = new PostAaiAjscInterceptor(); + } + + @Test + public void getInstance() throws Exception { + PostAaiAjscInterceptor interceptor = PostAaiAjscInterceptor.getInstance(); + assertNotNull(interceptor); + } + + @Test + public void testAllowOrRejectIfSuccess() throws Exception { + + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + + LoggingContext.put(LoggingContext.LoggingField.RESPONSE_CODE.toString(), "SUCCESS"); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("/fadsjoifj")); + + boolean success = postAaiAjscInterceptor.allowOrReject(request, null, null); + + assertTrue("Expecting the post interceptor to return success regardless", success); + } + + @Test + public void testAllowOrRejectIfFailure() throws Exception { + + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + + LoggingContext.put(LoggingContext.LoggingField.RESPONSE_CODE.toString(), "ERR."); + Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("/fadsjoifj")); + + boolean success = postAaiAjscInterceptor.allowOrReject(request, null, null); + + assertTrue("Expecting the post interceptor to return success regardless", success); + } +} diff --git a/aai-traversal/src/test/java/org/openecomp/aai/interceptors/PreAaiAjscInterceptorTest.java b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/PreAaiAjscInterceptorTest.java new file mode 100644 index 0000000..766b99f --- /dev/null +++ b/aai-traversal/src/test/java/org/openecomp/aai/interceptors/PreAaiAjscInterceptorTest.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package org.openecomp.aai.interceptors; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.servlet.http.HttpServletRequest; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyString; + +public class PreAaiAjscInterceptorTest { + + private PreAaiAjscInterceptor preAaiAjscInterceptor; + + @Before + public void setup(){ + preAaiAjscInterceptor = new PreAaiAjscInterceptor(); + } + + @Test + public void getInstance() throws Exception { + PreAaiAjscInterceptor interceptor = PreAaiAjscInterceptor.getInstance(); + assertNotNull(interceptor); + } + + @Test + public void testAllowOrRejectIfSuccess() throws Exception { + + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + + Mockito.when(request.getRequestURI()).thenReturn("/fadsjoifj"); + Mockito.when(request.getHeader(anyString())).thenReturn("JUNIT-Test"); + Mockito.when(request.getMethod()).thenReturn("GET"); + + boolean success = preAaiAjscInterceptor.allowOrReject(request, null, null); + + assertTrue("Expecting the post interceptor to return success regardless", success); + } + +} diff --git a/aai-traversal/src/test/java/org/openecomp/aai/rest/ExceptionHandlerTest.java b/aai-traversal/src/test/java/org/openecomp/aai/rest/ExceptionHandlerTest.java new file mode 100644 index 0000000..388e8e4 --- /dev/null +++ b/aai-traversal/src/test/java/org/openecomp/aai/rest/ExceptionHandlerTest.java @@ -0,0 +1,155 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package org.openecomp.aai.rest; + +import com.fasterxml.jackson.core.JsonLocation; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.sun.istack.SAXParseException2; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ExceptionHandlerTest { + + protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json"); + + @Mock + private HttpHeaders httpHeaders; + + @Mock + private HttpServletRequest request; + + @InjectMocks + private ExceptionHandler handler = new ExceptionHandler(); + + @Before + public void setup(){ + MockitoAnnotations.initMocks(this); + + MultivaluedHashMap headersMultiMap = 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", ""); + + List outputMediaTypes = new ArrayList<>(); + outputMediaTypes.add(APPLICATION_JSON); + when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); + when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap); + } + + @Test + public void testConversionOfWebApplicationResponse() throws Exception { + + Exception exception = new WebApplicationException(); + Response response = handler.toResponse(exception); + + assertNotNull(response); + assertNull(response.getEntity()); + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + } + + @Test + public void testConversionOfWebApplicationResponseWhenUmarshalExceptionResultBadRequest() throws Exception { + + SAXParseException2 mockSaxParseException = mock(SAXParseException2.class); + Exception exception = new WebApplicationException(mockSaxParseException); + Response response = handler.toResponse(exception); + + assertNotNull(response); + assertNotNull(response.getEntity()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + } + + @Test + public void testConversionWhenJsonParseExceptionResultBadRequest() throws Exception { + + JsonLocation jsonLocation = mock(JsonLocation.class); + Exception exception = new JsonParseException("", jsonLocation); + Response response = handler.toResponse(exception); + + assertNotNull(response); + assertNotNull(response.getEntity()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + } + + @Test + public void testConversionWhenJsonMappingExceptionResultBadRequest() throws Exception { + + JsonLocation jsonLocation = mock(JsonLocation.class); + Exception exception = new JsonMappingException("", jsonLocation); + Response response = handler.toResponse(exception); + + assertNotNull(response); + assertNotNull(response.getEntity()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + } + + @Test + public void testConversionWhenUnknownExceptionResultBadRequest() throws Exception { + + Exception exception = mock(Exception.class); + Response response = handler.toResponse(exception); + + when(request.getMethod()).thenReturn("GET"); + + assertNotNull(response); + assertNotNull(response.getEntity()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + + + } + + @Test + public void testConversionWhenUnknownExceptionResultBadRequestForXmlResponseType() throws Exception { + + List outputMediaTypes = new ArrayList<>(); + outputMediaTypes.add(MediaType.valueOf("application/xml")); + when(request.getMethod()).thenReturn("GET"); + when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes); + + Exception exception = mock(Exception.class); + Response response = handler.toResponse(exception); + + assertNotNull(response); + assertNotNull(response.getEntity()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + } +} diff --git a/aai-traversal/src/test/java/org/openecomp/aai/rest/util/EchoResponseTest.java b/aai-traversal/src/test/java/org/openecomp/aai/rest/util/EchoResponseTest.java new file mode 100644 index 0000000..146c1ca --- /dev/null +++ b/aai-traversal/src/test/java/org/openecomp/aai/rest/util/EchoResponseTest.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package org.openecomp.aai.rest.util; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.ws.rs.core.*; +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.mock; +import static org.mockito.Mockito.when; + +public class EchoResponseTest { + + protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json"); + + private static final Set 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); + } + + private EchoResponse echoResponse; + + private HttpHeaders httpHeaders; + + private UriInfo uriInfo; + + private MultivaluedMap headersMultiMap; + private MultivaluedMap queryParameters; + + private List aaiRequestContextList; + + private List outputMediaTypes; + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(EchoResponseTest.class.getName()); + + @Before + public void setup(){ + logger.info("Starting the setup for the integration tests of Rest Endpoints"); + + echoResponse = new EchoResponse(); + httpHeaders = mock(HttpHeaders.class); + uriInfo = 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("X-FromAppId")).thenReturn(Arrays.asList("JUNIT")); + when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT")); + + 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); + } + + @Test + public void testEchoResultWhenValidHeaders() throws Exception { + + Response response = echoResponse.echoResult(httpHeaders, null, ""); + + assertNotNull(response); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + } + + @Test + public void testEchoResultWhenInValidHeadersThrowsBadRequest() throws Exception { + + httpHeaders = mock(HttpHeaders.class); + Response response = echoResponse.echoResult(httpHeaders, null, ""); + + assertNotNull(response); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + } + + @Test + public void testEchoResultWhenValidHeadersButMediaTypeWrong() throws Exception { + + when(httpHeaders.getAcceptableMediaTypes()).thenThrow(new IllegalStateException()) + .thenReturn(outputMediaTypes); + + Response response = echoResponse.echoResult(httpHeaders, null, ""); + + assertNotNull(response); + assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + } +} -- 2.16.6