diff options
Diffstat (limited to 'filters/provider')
4 files changed, 211 insertions, 2 deletions
diff --git a/filters/provider/pom.xml b/filters/provider/pom.xml index 4fdd83280..a60c0be05 100755 --- a/filters/provider/pom.xml +++ b/filters/provider/pom.xml @@ -19,6 +19,8 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <!-- Ignore deprecated classes in coverage counts --> + <sonar.coverage.exclusions>**/RequestResponseDbLoggingFilter.java</sonar.coverage.exclusions> </properties> <dependencies> @@ -28,6 +30,12 @@ <version>${junit.version}</version> <scope>test</scope> </dependency> + + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> diff --git a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseDbLoggingFilter.java b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseDbLoggingFilter.java index 8b67276b0..359136565 100644 --- a/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseDbLoggingFilter.java +++ b/filters/provider/src/main/java/org/onap/ccsdk/sli/core/filters/RequestResponseDbLoggingFilter.java @@ -8,9 +8,9 @@ * 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. @@ -47,6 +47,7 @@ import org.onap.ccsdk.sli.core.sli.MessageWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Deprecated public class RequestResponseDbLoggingFilter implements Filter { private static Logger log = LoggerFactory.getLogger(RequestResponseDbLoggingFilter.class); diff --git a/filters/provider/src/test/java/org/onap/ccsdk/sli/core/filters/TestLogFilter.java b/filters/provider/src/test/java/org/onap/ccsdk/sli/core/filters/TestLogFilter.java new file mode 100644 index 000000000..a900b246c --- /dev/null +++ b/filters/provider/src/test/java/org/onap/ccsdk/sli/core/filters/TestLogFilter.java @@ -0,0 +1,71 @@ +/** + * + */ +package org.onap.ccsdk.sli.core.filters; + +import static org.junit.Assert.*; +import org.apache.commons.codec.binary.Base64; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.mockito.Mockito.*; +import java.io.IOException; +import java.util.UUID; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author dt5972 + * + */ +public class TestLogFilter { + + LogFilter logFilter; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + logFilter = new LogFilter(); + logFilter.init(null); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + logFilter.destroy(); + } + + /** + * Test method for {@link org.onap.ccsdk.sli.core.filters.LogFilter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)}. + * @throws ServletException + * @throws IOException + */ + @Test + public void testDoFilter() throws IOException, ServletException { + + // Test failed request with minimal headers + HttpServletRequest servletReq = mock(HttpServletRequest.class); + when(servletReq.getRequestURL()).thenReturn(new StringBuffer("SLI-API:healthcheck")); + when(servletReq.getPathInfo()).thenReturn("/hello:world"); + HttpServletResponse servletResp = mock(HttpServletResponse.class); + when(servletResp.getStatus()).thenReturn(400); + FilterChain filterChain = mock(FilterChain.class); + logFilter.doFilter(servletReq, servletResp, filterChain); + + // Test successful request with valid header + when(servletReq.getHeader(LogFilter.REQUEST_ID)).thenReturn(UUID.randomUUID().toString()); + when(servletReq.getHeader("Authorization")).thenReturn("Basic "+Base64.encodeBase64String("username:password".getBytes())); + when(servletResp.getStatus()).thenReturn(200); + logFilter.doFilter(servletReq, servletResp, filterChain); + + + } + +} diff --git a/filters/provider/src/test/java/org/onap/ccsdk/sli/core/filters/TestRequestResponseLoggingFilter.java b/filters/provider/src/test/java/org/onap/ccsdk/sli/core/filters/TestRequestResponseLoggingFilter.java new file mode 100644 index 000000000..9f36fe11e --- /dev/null +++ b/filters/provider/src/test/java/org/onap/ccsdk/sli/core/filters/TestRequestResponseLoggingFilter.java @@ -0,0 +1,129 @@ +/** + * + */ +package org.onap.ccsdk.sli.core.filters; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringBufferInputStream; +import java.io.StringReader; +import java.util.LinkedList; +import java.util.UUID; +import java.util.Vector; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.commons.codec.binary.Base64; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @author dt5972 + * + */ +public class TestRequestResponseLoggingFilter { + + RequestResponseLoggingFilter filter; + + private class DummyServletInputStream extends ServletInputStream { + + InputStream stream; + + public DummyServletInputStream(InputStream stream) { + this.stream = stream; + } + + + @Override + public void close() throws IOException { + super.close(); + stream.close(); + } + + + @Override + public int read() throws IOException { + return stream.read(); + } + + + + } + + private class DummyServletOutputStream extends ServletOutputStream { + + OutputStream ostr; + + public DummyServletOutputStream(OutputStream ostr) { + this.ostr = ostr; + } + + @Override + public void write(int b) throws IOException { + ostr.write(b); + } + + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + filter = new RequestResponseLoggingFilter(); + filter.init(null); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception {} + + /** + * Test method for {@link org.onap.ccsdk.sli.core.filters.RequestResponseLoggingFilter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)}. + * @throws IOException + * @throws ServletException + */ + @Test + public void testDoFilter() throws IOException, ServletException { + + HttpServletRequest request = mock(HttpServletRequest.class); + String msgBody = "hello world"; + InputStream reqInputStream = new ByteArrayInputStream(msgBody.getBytes()); + when(request.getInputStream()).thenReturn(new DummyServletInputStream(reqInputStream)); + when(request.getMethod()).thenReturn("POST"); + when(request.getRequestURL()).thenReturn(new StringBuffer("/HELLO:world")); + when(request.getPathInfo()).thenReturn("/hello:world"); + Vector<String> headerList = new Vector<>(); + headerList.add(LogFilter.REQUEST_ID); + headerList.add("Authorization"); + when(request.getHeaderNames()).thenReturn(headerList.elements()); + + when(request.getHeader(LogFilter.REQUEST_ID)).thenReturn(UUID.randomUUID().toString()); + when(request.getHeader("Authorization")).thenReturn("Basic "+Base64.encodeBase64String("username:password".getBytes())); + + HttpServletResponse response = mock(HttpServletResponse.class); + OutputStream outStr = new ByteArrayOutputStream(); + when(response.getOutputStream()).thenReturn(new DummyServletOutputStream(outStr)); + + FilterChain filterChain = mock(FilterChain.class); + + filter.doFilter(request, response, filterChain); + + + + } + +} |