/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.alerts;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.alerts.actions.AlertAction;
import org.elasticsearch.alerts.actions.EmailAlertAction;
import org.elasticsearch.alerts.triggers.ScriptedTrigger;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class AlertSerializationTest extends ElasticsearchIntegrationTest {

    @Test
    public void testAlertSerialization() throws Exception {

        SearchRequest request = new SearchRequest();
        request.indices("my-index");
        List<AlertAction> actions = new ArrayList<>();
        actions.add(new EmailAlertAction("message", "foo@bar.com"));
        Alert alert = new Alert("test-serialization",
                request,
                new ScriptedTrigger("return true", ScriptService.ScriptType.INLINE, "groovy"),
                actions,
                "0/5 * * * * ? *",
                new DateTime(),
                0,
                false);


        XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
        alert.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);

        final AlertsStore alertsStore =
                internalCluster().getInstance(AlertsStore.class, internalCluster().getMasterName());

        Alert parsedAlert = alertsStore.parseAlert("test-serialization", jsonBuilder.bytes());
        assertEquals(parsedAlert, alert);

        logger.error(XContentHelper.convertToJson(jsonBuilder.bytes(),false,true));

    }

}

@@ -718,65 +718,8 @@
 ry;%0A
-import org.elasticsearch.common.xcontent.XContentHelper;%0A
 impo
@@ -1976,27 +1976,46 @@
 lert
-, alert
+.enabled(), alert.enabled()
 );%0A
-%0A
         
 logg
@@ -2014,90 +2014,456 @@
     
-logger.error(XContentHelper.convertToJson(jsonBuilder.bytes(),false,true
+assertEquals(parsedAlert.version(), alert.version());%0A        assertEquals(parsedAlert.actions(), alert.actions());%0A        assertEquals(parsedAlert.lastActionFire().getMillis(), alert.lastActionFire().getMillis());%0A        assertEquals(parsedAlert.schedule(), alert.schedule());%0A        assertEquals(parsedAlert.getSearchRequest().source(), alert.getSearchRequest().source());%0A        assertEquals(parsedAlert.trigger(), alert.trigger(
 ));%0A
-%0A
     %7D%0A%0A
+%0A%0A%0A
 %7D%0A

TEST fix the alert serialization test to look at each field instead of compairing alerts directly.