/* Copyright (C) 2017 Maurits van der Schee
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tqdev.metrics.spring;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.method.HandlerMethod;

import com.tqdev.metrics.core.MetricRegistry;

/**
 * The Class MeasureRequestPathFilterTestBase contains the engine to run the
 * MeasureRequestPathFilter tests.
 */
abstract class MvcDurationInterceptorTestBase {

	/** The registry. */
	protected final MetricRegistry registry = spy(MetricRegistry.getInstance());

	/** The interceptor. */
	private final MvcDurationInterceptor interceptor = spy(new MvcDurationInterceptor(registry));

	/**
	 * Gets a mocked request.
	 *
	 * @param requestUri
	 *            the request URI
	 * @return the request
	 */
	private HttpServletRequest getRequest(long startTime) {
		HttpServletRequest request = mock(HttpServletRequest.class);
		when(request.getAttribute("startTime")).thenReturn(startTime);
		return request;
	}

	/**
	 * Simulate a request with an URI and a content type for a specified
	 * duration in nanoseconds.
	 *
	 * @param requestUri
	 *            the request URI
	 * @param contentType
	 *            the content type
	 * @param durationInNanoseconds
	 *            the duration in nanoseconds
	 * @throws Exception
	 */
	protected void request(String controllerName, String actionName, long durationInNanoseconds) {
		long now = 1510373758000000000L;
		HttpServletRequest request = getRequest(now);
		when(registry.getTime()).thenReturn(now + durationInNanoseconds);
		HandlerMethod handlerMethod = mock(HandlerMethod.class);
		doReturn(controllerName).when(interceptor).getControllerName(handlerMethod);
		doReturn(actionName).when(interceptor).getActionName(handlerMethod);
		Object handler = controllerName == null ? null : handlerMethod;
		try {
			interceptor.preHandle(request, null, null);
			interceptor.afterCompletion(request, null, handler, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
@@ -1897,34 +1897,33 @@
 %0A%09 * @param 
-requestUri
+startTime
 %0A%09 *        
@@ -1926,35 +1926,49 @@
         the 
+start time of the 
 request
- URI
 %0A%09 * @return
@@ -2224,38 +2224,36 @@
 est 
-with an URI and a content type
+on an action on a controller
  for
@@ -2264,20 +2264,16 @@
 pecified
-%0A%09 *
  duratio
@@ -2265,32 +2265,36 @@
 ecified duration
+%0A%09 *
  in nanoseconds.
@@ -2313,18 +2313,22 @@
 ram 
-requestUri
+controllerName
 %0A%09 *
@@ -2347,19 +2347,23 @@
 the 
-request URI
+controller name
 %0A%09 *
@@ -2374,18 +2374,17 @@
 ram 
-contentTyp
+actionNam
 e%0A%09 
@@ -2404,19 +2404,18 @@
 the 
-content typ
+action nam
 e%0A%09 

fix javadoc