/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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.
 */

package org.springframework.cloud.aws.actuate.metrics;

import com.amazonaws.handlers.AsyncHandler;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient;
import com.amazonaws.services.cloudwatch.model.MetricDatum;
import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * Test for the {@link BufferingCloudWatchMetricSenderTest}.
 *
 * @author Simon Buettner
 * @author Agim Emruli
 */
public class BufferingCloudWatchMetricSenderTest {

	@Captor
	private ArgumentCaptor<PutMetricDataRequest> putRequestCaptor;

	@Mock
	private AmazonCloudWatchAsyncClient amazonCloudWatchAsyncClient;

	@Before
	public void init() {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	@SuppressWarnings("unchecked")
	public void send_withMetricData_sendsDataToCloudFormation() throws Exception {
		BufferingCloudWatchMetricSender sender = new BufferingCloudWatchMetricSender("test", 10, 1, this.amazonCloudWatchAsyncClient);
		MetricDatum metric = new MetricDatum().withMetricName("test");
		sender.send(metric);

		// Wait
		Thread.sleep(15);

		verify(this.amazonCloudWatchAsyncClient, times(1)).putMetricDataAsync(this.putRequestCaptor.capture(), (AsyncHandler) any());
		assertEquals(metric.getMetricName(), this.putRequestCaptor.getValue().getMetricData().get(0).getMetricName());

		sender.destroy();
	}

	@Test
	@SuppressWarnings("unchecked")
	public void testBufferLimit() throws Exception {
		int maxBuffer = 40;
		int metrics = 50;

		BufferingCloudWatchMetricSender sender = new BufferingCloudWatchMetricSender("test", maxBuffer, 4, this.amazonCloudWatchAsyncClient);

		// Add
		for (int i = 0; i < metrics; i++) {
			sender.send(new MetricDatum().withMetricName("test"));
		}

		Thread.sleep(5);

		verify(this.amazonCloudWatchAsyncClient, times(3)).putMetricDataAsync(this.putRequestCaptor.capture(), (AsyncHandler) any());
		assertEquals("There should be three requests", 3, this.putRequestCaptor.getAllValues().size());
		assertEquals("50 metrics should be placed in three requests", maxBuffer, this.putRequestCaptor.getAllValues().get(0).getMetricData().size() + this.putRequestCaptor.getAllValues().get(1).getMetricData().size());

		sender.destroy();
	}

}

@@ -910,24 +910,48 @@
 ataRequest;%0A
+import org.junit.After;%0A
 import org.j
@@ -1073,16 +1073,44 @@
 o.Mock;%0A
+import org.mockito.Mockito;%0A
 import o
@@ -1700,24 +1700,115 @@
 (this);%0A%09%7D%0A%0A
+%09@After%0A%09public void resetMocks() %7B%0A%09%09Mockito.reset(this.amazonCloudWatchAsyncClient);%0A%09%7D%0A%0A
 %09@Test%0A%09@Sup

Reset mocks after test methods