package com.salesforce.dva.argus.service;

import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;

import com.salesforce.dva.argus.AbstractTest;
import com.salesforce.dva.argus.entity.*;
import com.salesforce.dva.argus.service.alert.DefaultAlertService.NotificationContext;
import com.salesforce.dva.argus.service.alert.notifier.CallbackNotifier;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;

/**
 * Created by mingzhong on 26.01.17.
 */
public class CallbackServiceTest extends AbstractTest {
	private static final String expression =
			"DIVIDE(-1h:argus.jvm:file.descriptor.open{host=unknown-host}:avg, -1h:argus.jvm:file.descriptor.max{host=unknown-host}:avg)";

	@Test
	public void testCallbackNotifier() {

		final UserService userService = system.getServiceFactory().getUserService();
		Alert alert = new Alert(userService.findAdminUser(),
				userService.findAdminUser(),
				"alert_name",
				expression,
				"* * * * *");
		final Trigger trigger = new Trigger(alert, Trigger.TriggerType.GREATER_THAN_OR_EQ, "trigger_name", 2D, 5);

		final String jsonBody = "{ \"uri\" : \"localhost:8080\", \"method\" : \"POST\", \"header\": { \"Content-Type\": \"application/json\" }, \"body\": \"{\\\"triggerName\\\": \\\"«trigger.name>»\\\", \\\"alertName\\\": \\\"«alert.name»\\\"}\",\"template\": \"ST4\"}";
		final Notification notification = new Notification("notification_name",
				alert,
				"notifier_ame",
				Collections.singletonList(jsonBody),
				23);

		alert.setTriggers(Collections.singletonList(trigger));
		alert.setNotifications(Collections.singletonList(notification));
		alert = system.getServiceFactory().getAlertService().updateAlert(alert);

		NotificationContext context = new NotificationContext(alert,
				trigger,
				notification,
				1418319600000L,
				"foo",
				"scope:metric");

		// Test
		CallbackNotifier notifier = (CallbackNotifier) system.getServiceFactory()
				.getAlertService()
				.getNotifier(AlertService.SupportedNotifier.CALLBACK);
		int notificationCounter = 3;
		IntStream.range(0, notificationCounter).forEach(i -> notifier.sendNotification(context));
		assertThat("Unexpected number of triggered alerts.", notifier.getAllNotifications(alert).size(), is(notificationCounter));
	}
}

@@ -1882,85 +1882,128 @@
 %09%09%09%09
-trigger,%0A%09%09%09%09notification,%0A%09%09%09%091418319600000L
+alert.getTriggers().get(0),%0A%09%09%09%09notification,%0A%09%09%09%09System.currentTimeMillis()
 ,%0A%09%09%09%09
-%22foo%22
+0.0
 ,%0A%09%09%09%09
+new Metric(
 %22scope
-:
+%22, %22
 metric%22)
 ;%0A%0A%09
@@ -1998,18 +1998,18 @@
 metric%22)
+)
 ;
-%0A
 %0A%09%09// Te
@@ -2346,17 +2346,21 @@
 lerts.%22,
- 
+%0A%09%09%09%09
 notifier
@@ -2394,17 +2394,21 @@
 .size(),
- 
+%0A%09%09%09%09
 is(notif

Fix test