/*
 * 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.marvel.agent.exporter;

import org.apache.lucene.util.Constants;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.io.IOException;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;

public class MarvelTemplateUtilsTests extends ESTestCase {

    private static final String TEST_TEMPLATE = "/monitoring-test.json";

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    public void testLoadTemplate() throws IOException {
        final int version = randomIntBetween(0, 10_000);
        String source = MarvelTemplateUtils.loadTemplate("test", version);

        assertThat(source, notNullValue());
        assertThat(source.length(), greaterThan(0));
        assertThat(source, templateEqualTo("{\n" +
                "  \"template\": \".monitoring-data-" + version + "\",\n" +
                "  \"mappings\": {\n" +
                "    \"type_1\": {\n" +
                "      \"_meta\": {\n" +
                "        \"template.version\": \"" + version + "\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}"));
    }

    public void testLoad() throws IOException {
        BytesReference source = MarvelTemplateUtils.load(TEST_TEMPLATE);
        assertThat(source, notNullValue());
        assertThat(source.length(), greaterThan(0));
    }

    public void testValidateNullSource() {
        expectedException.expect(ElasticsearchParseException.class);
        expectedException.expectMessage("Monitoring template must not be null");
        MarvelTemplateUtils.validate(null);
    }

    public void testValidateEmptySource() {
        expectedException.expect(ElasticsearchParseException.class);
        expectedException.expectMessage("Monitoring template must not be empty");
        MarvelTemplateUtils.validate(new BytesArray(""));
    }

    public void testValidateInvalidSource() {
        expectedException.expect(ElasticsearchParseException.class);
        expectedException.expectMessage("Invalid monitoring template");
        MarvelTemplateUtils.validate(new BytesArray("{\"foo\": \"bar"));
    }

    public void testValidate() {
        try {
            MarvelTemplateUtils.validate(MarvelTemplateUtils.load(TEST_TEMPLATE));
        } catch (Exception e) {
            fail("failed to validate test template: " + e.getMessage());
        }
    }

    public void testFilter() {
        assertThat(MarvelTemplateUtils.filter(new BytesArray("${monitoring.template.version}"), 0), templateEqualTo("0"));
        assertThat(MarvelTemplateUtils.filter(new BytesArray("{\"template\": \"test-${monitoring.template.version}\"}"), 1),
                templateEqualTo("{\"template\": \"test-1\"}"));
        assertThat(MarvelTemplateUtils.filter(new BytesArray("{\"template\": \"${monitoring.template.version}-test\"}"), 2),
                templateEqualTo("{\"template\": \"2-test\"}"));
        assertThat(MarvelTemplateUtils.filter(new BytesArray("{\"template\": \"test-${monitoring.template.version}-test\"}"), 3),
                templateEqualTo("{\"template\": \"test-3-test\"}"));

        final int version = randomIntBetween(0, 100);
        assertThat(MarvelTemplateUtils.filter(new BytesArray("{\"foo-${monitoring.template.version}\": " +
                        "\"bar-${monitoring.template.version}\"}"), version),
                templateEqualTo("{\"foo-" + version + "\": \"bar-" + version + "\"}"));
    }

    public static Matcher<String> templateEqualTo(String template) {
        if (Constants.WINDOWS) {
            // translate Windows line endings (\r\n) to standard ones (\n)
            return equalTo(Strings.replace(template, System.lineSeparator(), "\n"));
        }
        return equalTo(template);
    }

}

@@ -1349,35 +1349,39 @@
 %0A        assertT
-h
+empl
 at
+e
 (source, templat
@@ -1373,25 +1373,17 @@
 source, 
-templateE
+e
 qualTo(%22
@@ -3058,35 +3058,39 @@
 %0A        assertT
-h
+empl
 at
+e
 (MarvelTemplateU
@@ -3155,25 +3155,17 @@
 %22), 0), 
-templateE
+e
 qualTo(%22
@@ -3177,35 +3177,39 @@
 %0A        assertT
-h
+empl
 at
+e
 (MarvelTemplateU
@@ -3311,33 +3311,25 @@
             
-templateE
+e
 qualTo(%22%7B%5C%22t
@@ -3362,35 +3362,39 @@
 %0A        assertT
-h
+empl
 at
+e
 (MarvelTemplateU
@@ -3496,33 +3496,25 @@
             
-templateE
+e
 qualTo(%22%7B%5C%22t
@@ -3547,35 +3547,39 @@
 %0A        assertT
-h
+empl
 at
+e
 (MarvelTemplateU
@@ -3686,33 +3686,25 @@
             
-templateE
+e
 qualTo(%22%7B%5C%22t
@@ -3805,19 +3805,23 @@
  assertT
-h
+empl
 at
+e
 (MarvelT
@@ -3995,25 +3995,17 @@
         
-templateE
+e
 qualTo(%22
@@ -4088,55 +4088,74 @@
 tic 
-Matcher%3CString%3E templateEqualTo(String template
+void assertTemplate(String actual, Matcher%3C? super String%3E matcher
 ) %7B%0A
@@ -4274,31 +4274,25 @@
         
-return equalTo(
+actual = 
 Strings.
@@ -4299,24 +4299,22 @@
 replace(
-template
+actual
 , System
@@ -4336,17 +4336,16 @@
 ), %22%5Cn%22)
-)
 ;%0A      
@@ -4360,31 +4360,34 @@
     
-return equalTo(template
+assertThat(actual, matcher
 );%0A 

Fix MarvelTemplateUtilsTests.java on Windows (attempt elastic/elasticsearch#2)