<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>
--- /dev/null
+/*-\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
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 {
*/
public ApplicationConfig() {
- register(org.onap.dmaap.dbcapi.resources.AuthorizationFilter.class);
+ register(AuthorizationFilter.class).
+ register(RequestTimeLogFilter.class);
}
}
+++ /dev/null
-/*-\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
--- /dev/null
+/*-\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
+++ /dev/null
-/*-\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