Added stopwatch filter 34/83434/4
authorPawel <pawel.kasperkiewicz@nokia.com>
Wed, 27 Mar 2019 06:31:46 +0000 (02:31 -0400)
committerPawel <pawel.kasperkiewicz@nokia.com>
Fri, 29 Mar 2019 07:05:46 +0000 (03:05 -0400)
Issue-ID: DMAAP-1109

Change-Id: I1738444ec9ce18768efdc9693c9669990660698f
Signed-off-by: Pawel <pawel.kasperkiewicz@nokia.com>
pom.xml
src/main/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilter.java [new file with mode: 0644]
src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java
src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java [deleted file]
src/test/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilterTest.java [new file with mode: 0644]
src/test/java/org/onap/dmaap/dbcapi/service/StopWatchTest.java [deleted file]

diff --git a/pom.xml b/pom.xml
index f78118d..fdc0cc5 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                <groupId>org.glassfish.jersey.inject</groupId>
                <artifactId>jersey-hk2</artifactId>
                </dependency>
-       </dependencies>
+    <dependency>
+      <groupId>pl.pragmatists</groupId>
+      <artifactId>JUnitParams</artifactId>
+      <version>1.1.0</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
        <reporting>
                <plugins>
                        <plugin>
diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilter.java
new file mode 100644 (file)
index 0000000..b2b98b6
--- /dev/null
@@ -0,0 +1,55 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.dmaap\r
+ * ================================================================================\r
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+package org.onap.dmaap.dbcapi.resources;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import java.time.Clock;\r
+import javax.ws.rs.container.ContainerRequestContext;\r
+import javax.ws.rs.container.ContainerRequestFilter;\r
+import javax.ws.rs.container.ContainerResponseContext;\r
+import javax.ws.rs.container.ContainerResponseFilter;\r
+import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;\r
+\r
+public class RequestTimeLogFilter extends BaseLoggingClass implements ContainerRequestFilter, ContainerResponseFilter {\r
+\r
+    private final EELFLogger log;\r
+    private Clock clock;\r
+\r
+    public RequestTimeLogFilter() {\r
+        this(auditLogger, Clock.systemDefaultZone());\r
+    }\r
+\r
+    RequestTimeLogFilter(EELFLogger logger, Clock clock) {\r
+        this.log = logger;\r
+        this.clock = clock;\r
+    }\r
+\r
+    @Override\r
+    public void filter(ContainerRequestContext requestContext) {\r
+        requestContext.setProperty("start", clock.millis());\r
+    }\r
+\r
+    @Override\r
+    public void filter(ContainerRequestContext requestContext, ContainerResponseContext containerResponseContext) {\r
+        long startTime = (long) requestContext.getProperty("start");\r
+        long elapsedTime = clock.millis() - startTime;\r
+        log.info("Request took {} ms", elapsedTime);\r
+    }\r
+}\r
index 2283ea2..2244b73 100644 (file)
@@ -20,8 +20,8 @@
 package org.onap.dmaap.dbcapi.server;
 
 import org.glassfish.jersey.server.ResourceConfig;
-
-
+import org.onap.dmaap.dbcapi.resources.RequestTimeLogFilter;
+import org.onap.dmaap.dbcapi.resources.AuthorizationFilter;
 
 public class ApplicationConfig extends ResourceConfig {
        
@@ -30,7 +30,8 @@ public class ApplicationConfig extends ResourceConfig {
         */
        public ApplicationConfig() {
                
-        register(org.onap.dmaap.dbcapi.resources.AuthorizationFilter.class);
+        register(AuthorizationFilter.class).
+                                       register(RequestTimeLogFilter.class);
   
     }
 }
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java b/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java
deleted file mode 100644 (file)
index 6dc8fe9..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*-\r
- * ============LICENSE_START=======================================================\r
- * org.onap.dmaap\r
- * ================================================================================\r
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-package org.onap.dmaap.dbcapi.service;\r
-\r
-import static com.att.eelf.configuration.Configuration.MDC_BEGIN_TIMESTAMP;\r
-import static com.att.eelf.configuration.Configuration.MDC_ELAPSED_TIME;\r
-import static com.att.eelf.configuration.Configuration.MDC_END_TIMESTAMP;\r
-\r
-import java.text.SimpleDateFormat;\r
-import java.util.Date;\r
-import java.util.TimeZone;\r
-import org.slf4j.MDC;\r
-\r
-\r
-public class StopWatch {\r
-\r
-    private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";\r
-    private final static SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);\r
-    private final static TimeZone utc = TimeZone.getTimeZone("UTC");\r
-\r
-    private long startTimestamp;\r
-    private long elapsedTime;\r
-\r
-    static {\r
-        isoFormatter.setTimeZone(utc);\r
-    }\r
-\r
-    public void start() {\r
-        startTimestamp = System.currentTimeMillis();\r
-        MDC.put(MDC_BEGIN_TIMESTAMP, isoFormatter.format(new Date(startTimestamp)));\r
-    }\r
-\r
-    public void stop() {\r
-        long endTimestamp = System.currentTimeMillis();\r
-        elapsedTime = endTimestamp - startTimestamp;\r
-        MDC.put(MDC_END_TIMESTAMP, isoFormatter.format(new Date(endTimestamp)));\r
-        MDC.put(MDC_ELAPSED_TIME, String.valueOf(elapsedTime));\r
-    }\r
-\r
-    public void resetElapsedTime() {\r
-        elapsedTime = 0;\r
-    }\r
-\r
-    public long getElapsedTime() {\r
-        return elapsedTime;\r
-    }\r
-}\r
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilterTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilterTest.java
new file mode 100644 (file)
index 0000000..0c88c0c
--- /dev/null
@@ -0,0 +1,78 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.dmaap\r
+ * ================================================================================\r
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+package org.onap.dmaap.dbcapi.resources;\r
+\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.mockito.Matchers.anyString;\r
+import static org.mockito.Matchers.eq;\r
+import static org.mockito.Mockito.verify;\r
+import static org.mockito.Mockito.when;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import java.time.Clock;\r
+import java.time.Instant;\r
+import java.time.ZoneId;\r
+import javax.ws.rs.container.ContainerRequestContext;\r
+import javax.ws.rs.container.ContainerResponseContext;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Mock;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
+\r
+@RunWith(MockitoJUnitRunner.class)\r
+public class RequestTimeLogFilterTest {\r
+\r
+    private Clock clock ;\r
+    private RequestTimeLogFilter requestTimeLogFilter;\r
+    public static final long START = 1L;\r
+    @Mock\r
+    private ContainerRequestContext requestContext;\r
+    @Mock\r
+    private ContainerResponseContext responseContext;\r
+    @Mock\r
+    private EELFLogger logger;\r
+\r
+\r
+    @Before\r
+    public void setup() {\r
+        clock = Clock.fixed(Instant.parse("1970-01-01T00:00:10Z"), ZoneId.systemDefault());\r
+        requestTimeLogFilter = new RequestTimeLogFilter(logger, clock);\r
+    }\r
+\r
+    @Test\r
+    public void shouldHaveDefaultConstructor() {\r
+        assertNotNull(new RequestTimeLogFilter());\r
+    }\r
+\r
+    @Test\r
+    public void filterShouldSetStartTimestampProperty() {\r
+        requestTimeLogFilter.filter(requestContext);\r
+        verify(requestContext).setProperty("start",clock.millis());\r
+    }\r
+\r
+    @Test\r
+    public void filterShouldPrintElapsedTime() {\r
+        when(requestContext.getProperty("start")).thenReturn(START);\r
+\r
+        requestTimeLogFilter.filter(requestContext, responseContext);\r
+\r
+        verify(logger).info(anyString(),eq(clock.millis() - START));\r
+    }\r
+}
\ No newline at end of file
diff --git a/src/test/java/org/onap/dmaap/dbcapi/service/StopWatchTest.java b/src/test/java/org/onap/dmaap/dbcapi/service/StopWatchTest.java
deleted file mode 100644 (file)
index b4b6af2..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*-\r
- * ============LICENSE_START=======================================================\r
- * org.onap.dmaap\r
- * ================================================================================\r
- * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-package org.onap.dmaap.dbcapi.service;\r
-\r
-import static org.junit.Assert.assertThat;\r
-import static org.junit.Assert.assertTrue;\r
-import static org.junit.Assert.assertEquals;\r
-import org.junit.Test;\r
-import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;\r
-\r
-public class StopWatchTest extends BaseLoggingClass {\r
-\r
-    private StopWatch stopWatch = new StopWatch();\r
-\r
-    @Test\r
-    public void stopWatchShouldReturnElapsedTime() throws InterruptedException {\r
-        stopWatch.start();\r
-        Thread.sleep(50);\r
-        stopWatch.stop();\r
-        assertTrue(stopWatch.getElapsedTime() > 0);\r
-    }\r
-\r
-    @Test\r
-    public void resetShouldSetElapsedTime() {\r
-        stopWatch.start();\r
-        stopWatch.stop();\r
-        stopWatch.resetElapsedTime();\r
-        assertEquals(0,stopWatch.getElapsedTime());\r
-\r
-    }\r
-\r
-}
\ No newline at end of file