/*
 * 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.xpack.watcher.support;

import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.equalTo;

public class WatcherScriptTests extends ESTestCase {

    public void testParseScript() throws IOException {
        WatcherScript script = new WatcherScript(randomAsciiOfLengthBetween(1, 5),
                                                randomFrom(ScriptType.values()),
                                                randomBoolean() ? null : randomFrom("custom", "mustache"),
                                                randomBoolean() ? null : randomFrom(emptyMap(), singletonMap("foo", "bar")));

        try (XContentParser parser = createParser(script)) {
            assertThat(WatcherScript.parse(parser), equalTo(script));
        }
    }

    public void testParseScriptWithCustomLang() throws IOException {
        final String lang = randomFrom("custom", "painful");
        final WatcherScript script = new WatcherScript("my-script", randomFrom(ScriptType.values()), lang, null);

        try (XContentParser parser = createParser(script)) {
            WatcherScript result = WatcherScript.parse(parser, WatcherScript.DEFAULT_LANG);
            assertThat(result.script(), equalTo(script.script()));
            assertThat(result.type(), equalTo(script.type()));
            assertThat(result.lang(), equalTo(lang));
            assertThat(result.params(), equalTo(script.params()));
        }
    }

    public void testParseScriptWithDefaultLang() throws IOException {
        final WatcherScript script = new WatcherScript("my-script", randomFrom(ScriptType.values()), null, null);

        try (XContentParser parser = createParser(script)) {
            WatcherScript result = WatcherScript.parse(parser, WatcherScript.DEFAULT_LANG);
            assertThat(result.script(), equalTo(script.script()));
            assertThat(result.type(), equalTo(script.type()));
            assertThat(result.lang(), equalTo(WatcherScript.DEFAULT_LANG));
            assertThat(result.params(), equalTo(script.params()));
        }
    }

    private static XContentParser createParser(WatcherScript watcherScript) throws IOException {
        final XContent xContent = randomFrom(XContentType.values()).xContent();

        XContentBuilder builder = XContentBuilder.builder(xContent);
        watcherScript.toXContent(builder, ToXContent.EMPTY_PARAMS);

        XContentParser parser = XContentHelper.createParser(builder.bytes());
        assertNull(parser.currentToken());
        parser.nextToken();
        return parser;
    }
}

@@ -745,31 +745,23 @@
 eption;%0A
-%0A
 import 
-static 
 java.uti
@@ -777,17 +777,8 @@
 ions
-.emptyMap
 ;%0Aim
@@ -782,23 +782,16 @@
 %0Aimport 
-static 
 java.uti
@@ -796,34 +796,14 @@
 til.
-Collections.singleton
 Map;%0A
+%0A
 impo
@@ -945,32 +945,198 @@
 s IOException %7B%0A
+        final Map%3CString, Object%3E params =%0A                randomFrom(Collections.%3CString, Object%3EemptyMap(), Collections.singletonMap(%22foo%22, (Object)%22bar%22), null);%0A%0A
         WatcherS
@@ -1334,33 +1334,8 @@
     
- randomBoolean() ? null :
  ran
@@ -1362,16 +1362,22 @@
 ustache%22
+, null
 ),%0A     
@@ -1423,83 +1423,14 @@
     
-randomBoolean() ? null : randomFrom(emptyMap(), singletonMap(%22foo%22, %22bar%22))
+params
 );%0A%0A

Change use of generics in test