Add unit tests to interceptors increase coverage 61/17061/1
authorVenkata Harish K Kajur <vk250x@att.com>
Tue, 3 Oct 2017 14:05:27 +0000 (10:05 -0400)
committerVenkata Harish K Kajur <vk250x@att.com>
Tue, 3 Oct 2017 14:06:04 +0000 (10:06 -0400)
Issue-ID: AAI-213
Change-Id: I181ea41c3fb752e8566dbd0a0e27e39767006df3
Signed-off-by: Venkata Harish K Kajur <vk250x@att.com>
aai-resources/src/test/java/org/onap/aai/interceptors/AAILogJAXRSInInterceptorTest.java
aai-resources/src/test/java/org/onap/aai/interceptors/AAILogJAXRSOutInterceptorTest.java [new file with mode: 0644]
aai-resources/src/test/java/org/onap/aai/interceptors/PostAaiAjscInterceptorTest.java
aai-resources/src/test/java/org/onap/aai/interceptors/PreAaiAjscInterceptorTest.java

index 3b1dac2..40de5a9 100644 (file)
@@ -26,35 +26,179 @@ import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
 import org.junit.Before;
 import org.junit.Test;
-import org.onap.aai.AAISetup;
 
 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.mock;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
-public class AAILogJAXRSInInterceptorTest extends AAISetup {
+public class AAILogJAXRSInInterceptorTest {
 
     private AAILogJAXRSInInterceptor aaiLogJAXRSInInterceptor;
 
+    private Message message;
+    private Exchange exchange;
+    private InputStream is;
+    private Map<String, List<String>> 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 {
 
-        Message message = mock(Message.class);
-        Exchange exchange = new ExchangeImpl();
-        InputStream is = getClass().getClassLoader().getResourceAsStream("logback.xml");
+        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<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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(Message.QUERY_STRING)).thenReturn("/somestring");
         when(message.get("CamelHttpUrl")).thenReturn("/somestring");
         aaiLogJAXRSInInterceptor.handleMessage(message);
     }
-}
+
+    @Test
+    public void testHandleMessageWhenQueryExistsAndUriEcho() throws IOException {
+
+        Map<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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);
+    }
+}
\ No newline at end of file
diff --git a/aai-resources/src/test/java/org/onap/aai/interceptors/AAILogJAXRSOutInterceptorTest.java b/aai-resources/src/test/java/org/onap/aai/interceptors/AAILogJAXRSOutInterceptorTest.java
new file mode 100644 (file)
index 0000000..52c6a42
--- /dev/null
@@ -0,0 +1,111 @@
+/**
+ * ============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.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<String, List<String>> 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);
+    }
+
+}
\ No newline at end of file
index 2842e26..6efb0d1 100644 (file)
@@ -71,4 +71,4 @@ public class PostAaiAjscInterceptorTest {
 
         assertTrue("Expecting the post interceptor to return success regardless", success);
     }
-}
+}
\ No newline at end of file
index 49b5664..35651c3 100644 (file)
@@ -27,7 +27,8 @@ import org.mockito.Mockito;
 
 import javax.servlet.http.HttpServletRequest;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.anyString;
 
 public class PreAaiAjscInterceptorTest {
@@ -59,4 +60,4 @@ public class PreAaiAjscInterceptorTest {
         assertTrue("Expecting the post interceptor to return success regardless", success);
     }
 
-}
+}
\ No newline at end of file