Merge "Added stopwatch filter"
diff --git a/pom.xml b/pom.xml
index f78118d..fdc0cc5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -347,7 +347,13 @@
<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
index 0000000..b2b98b6
--- /dev/null
+++ b/src/main/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilter.java
@@ -0,0 +1,55 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia 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.onap.dmaap.dbcapi.resources;
+
+import com.att.eelf.configuration.EELFLogger;
+import java.time.Clock;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
+import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
+
+public class RequestTimeLogFilter extends BaseLoggingClass implements ContainerRequestFilter, ContainerResponseFilter {
+
+ private final EELFLogger log;
+ private Clock clock;
+
+ public RequestTimeLogFilter() {
+ this(auditLogger, Clock.systemDefaultZone());
+ }
+
+ RequestTimeLogFilter(EELFLogger logger, Clock clock) {
+ this.log = logger;
+ this.clock = clock;
+ }
+
+ @Override
+ public void filter(ContainerRequestContext requestContext) {
+ requestContext.setProperty("start", clock.millis());
+ }
+
+ @Override
+ public void filter(ContainerRequestContext requestContext, ContainerResponseContext containerResponseContext) {
+ long startTime = (long) requestContext.getProperty("start");
+ long elapsedTime = clock.millis() - startTime;
+ log.info("Request took {} ms", elapsedTime);
+ }
+}
diff --git a/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java b/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java
index 2283ea2..2244b73 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java
@@ -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 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
index 6dc8fe9..0000000
--- a/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * org.onap.dmaap
- * ================================================================================
- * Copyright (C) 2019 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.onap.dmaap.dbcapi.service;
-
-import static com.att.eelf.configuration.Configuration.MDC_BEGIN_TIMESTAMP;
-import static com.att.eelf.configuration.Configuration.MDC_ELAPSED_TIME;
-import static com.att.eelf.configuration.Configuration.MDC_END_TIMESTAMP;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.TimeZone;
-import org.slf4j.MDC;
-
-
-public class StopWatch {
-
- private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
- private final static SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
- private final static TimeZone utc = TimeZone.getTimeZone("UTC");
-
- private long startTimestamp;
- private long elapsedTime;
-
- static {
- isoFormatter.setTimeZone(utc);
- }
-
- public void start() {
- startTimestamp = System.currentTimeMillis();
- MDC.put(MDC_BEGIN_TIMESTAMP, isoFormatter.format(new Date(startTimestamp)));
- }
-
- public void stop() {
- long endTimestamp = System.currentTimeMillis();
- elapsedTime = endTimestamp - startTimestamp;
- MDC.put(MDC_END_TIMESTAMP, isoFormatter.format(new Date(endTimestamp)));
- MDC.put(MDC_ELAPSED_TIME, String.valueOf(elapsedTime));
- }
-
- public void resetElapsedTime() {
- elapsedTime = 0;
- }
-
- public long getElapsedTime() {
- return elapsedTime;
- }
-}
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
index 0000000..0c88c0c
--- /dev/null
+++ b/src/test/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilterTest.java
@@ -0,0 +1,78 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia 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.onap.dmaap.dbcapi.resources;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import com.att.eelf.configuration.EELFLogger;
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneId;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerResponseContext;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class RequestTimeLogFilterTest {
+
+ private Clock clock ;
+ private RequestTimeLogFilter requestTimeLogFilter;
+ public static final long START = 1L;
+ @Mock
+ private ContainerRequestContext requestContext;
+ @Mock
+ private ContainerResponseContext responseContext;
+ @Mock
+ private EELFLogger logger;
+
+
+ @Before
+ public void setup() {
+ clock = Clock.fixed(Instant.parse("1970-01-01T00:00:10Z"), ZoneId.systemDefault());
+ requestTimeLogFilter = new RequestTimeLogFilter(logger, clock);
+ }
+
+ @Test
+ public void shouldHaveDefaultConstructor() {
+ assertNotNull(new RequestTimeLogFilter());
+ }
+
+ @Test
+ public void filterShouldSetStartTimestampProperty() {
+ requestTimeLogFilter.filter(requestContext);
+ verify(requestContext).setProperty("start",clock.millis());
+ }
+
+ @Test
+ public void filterShouldPrintElapsedTime() {
+ when(requestContext.getProperty("start")).thenReturn(START);
+
+ requestTimeLogFilter.filter(requestContext, responseContext);
+
+ verify(logger).info(anyString(),eq(clock.millis() - START));
+ }
+}
\ 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
index b4b6af2..0000000
--- a/src/test/java/org/onap/dmaap/dbcapi/service/StopWatchTest.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * org.onap.dmaap
- * ================================================================================
- * Copyright (C) 2019 Nokia 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.onap.dmaap.dbcapi.service;
-
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertEquals;
-import org.junit.Test;
-import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
-
-public class StopWatchTest extends BaseLoggingClass {
-
- private StopWatch stopWatch = new StopWatch();
-
- @Test
- public void stopWatchShouldReturnElapsedTime() throws InterruptedException {
- stopWatch.start();
- Thread.sleep(50);
- stopWatch.stop();
- assertTrue(stopWatch.getElapsedTime() > 0);
- }
-
- @Test
- public void resetShouldSetElapsedTime() {
- stopWatch.start();
- stopWatch.stop();
- stopWatch.resetElapsedTime();
- assertEquals(0,stopWatch.getElapsedTime());
-
- }
-
-}
\ No newline at end of file