package org.stagemonitor.alerting.alerter;

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

import java.util.Arrays;
import java.util.HashMap;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.stagemonitor.alerting.AlertingPlugin;
import org.stagemonitor.alerting.ThresholdMonitoringReporterTest;
import org.stagemonitor.alerting.check.CheckResult;
import org.stagemonitor.alerting.incident.ConcurrentMapIncidentRepository;
import org.stagemonitor.alerting.incident.Incident;
import org.stagemonitor.core.MeasurementSession;
import org.stagemonitor.core.util.JsonUtils;

public class IncidentServletTest {

	private IncidentServlet incidentServlet;
	private Incident incident;

	@Before
	public void setUp() throws Exception {
		AlertingPlugin alertingPlugin = mock(AlertingPlugin.class);
		ConcurrentMapIncidentRepository incidentRepository = new ConcurrentMapIncidentRepository();
		incident = new Incident(ThresholdMonitoringReporterTest.createCheckCheckingMean(1, 5),
				new MeasurementSession("testApp", "testHost2", "testInstance"),
				Arrays.asList(new CheckResult("test", 10, CheckResult.Status.CRITICAL),
						new CheckResult("test", 10, CheckResult.Status.ERROR)));
		incidentRepository.createIncident(incident);
		when(alertingPlugin.getIncidentRepository()).thenReturn(incidentRepository);
		incidentServlet = new IncidentServlet(alertingPlugin);
	}

	@Test
	public void testGetIncidents() throws Exception {
		MockHttpServletResponse response = new MockHttpServletResponse();
		incidentServlet.service(new MockHttpServletRequest("GET", "/stagemonitor/incidents"), response);
		String expected = JsonUtils.toJson(new HashMap<String, Object>() {{
			put("status", CheckResult.Status.CRITICAL);
			put("incidents", Arrays.asList(incident));
		}});
		Assert.assertEquals(expected, response.getContentAsString());
	}
}

@@ -858,16 +858,56 @@
 ncident;
+%0A%09private AlertingPlugin alertingPlugin;
 %0A%0A%09@Befo
@@ -951,31 +951,16 @@
 ion %7B%0A%09%09
-AlertingPlugin 
 alerting
@@ -2034,11 +2034,388 @@
 g());%0A%09%7D
+%0A%0A%09@Test%0A%09public void testGetIncidentsIncidentRepositoryNull() throws Exception %7B%0A%09%09when(alertingPlugin.getIncidentRepository()).thenReturn(null);%0A%0A%09%09MockHttpServletResponse response = new MockHttpServletResponse();%0A%09%09incidentServlet.service(new MockHttpServletRequest(%22GET%22, %22/stagemonitor/incidents%22), response);%0A%09%09Assert.assertEquals(%22%7B%7D%22, response.getContentAsString());%0A%09%7D
 %0A%7D%0A

Add another test for IncidentServlet