diff --git a/marvel/src/main/java/org/elasticsearch/marvel/support/VersionUtils.java b/marvel/src/main/java/org/elasticsearch/marvel/support/VersionUtils.java index 9d917ee737b..51a2c55bd4e 100644 --- a/marvel/src/main/java/org/elasticsearch/marvel/support/VersionUtils.java +++ b/marvel/src/main/java/org/elasticsearch/marvel/support/VersionUtils.java @@ -17,11 +17,13 @@ import java.util.regex.Pattern; */ public final class VersionUtils { + public static final String VERSION_NUMBER_FIELD = "number"; + private VersionUtils() { } public static Version parseVersion(byte[] text) { - return parseVersion("", new String(text, Charset.forName("UTF-8"))); + return parseVersion(VERSION_NUMBER_FIELD, new String(text, Charset.forName("UTF-8"))); } /** diff --git a/marvel/src/test/java/org/elasticsearch/marvel/support/VersionUtilsTests.java b/marvel/src/test/java/org/elasticsearch/marvel/support/VersionUtilsTests.java new file mode 100644 index 00000000000..c00a3e1004f --- /dev/null +++ b/marvel/src/test/java/org/elasticsearch/marvel/support/VersionUtilsTests.java @@ -0,0 +1,41 @@ +/* + * 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.support; + +import org.elasticsearch.Version; +import org.elasticsearch.test.ESTestCase; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.hamcrest.Matchers.equalTo; + +public class VersionUtilsTests extends ESTestCase { + + public void testParseVersion() { + List versions = randomSubsetOf(9, Version.V_0_18_0, Version.V_0_19_0, Version.V_1_0_1, Version.V_1_2_3, Version.V_1_3_2, Version.V_1_4_6, Version.V_1_6_3, Version.V_1_7_2, Version.V_2_0_0); + for (Version version : versions) { + String output = createOutput(VersionUtils.VERSION_NUMBER_FIELD, version.number()); + assertThat(VersionUtils.parseVersion(output.getBytes(StandardCharsets.UTF_8)), equalTo(version)); + assertThat(VersionUtils.parseVersion(VersionUtils.VERSION_NUMBER_FIELD, output), equalTo(version)); + } + } + + private String createOutput(String fieldName, String value) { + return "{\n" + + " \"name\" : \"Blind Faith\",\n" + + " \"cluster_name\" : \"elasticsearch\",\n" + + " \"version\" : {\n" + + " \"" + fieldName + "\" : \"" + value + "\",\n" + + " \"build_hash\" : \"4092d253dddda0ff1ff3d1c09ac7678e757843f9\",\n" + + " \"build_timestamp\" : \"2015-10-13T08:53:10Z\",\n" + + " \"build_snapshot\" : true,\n" + + " \"lucene_version\" : \"5.2.1\"\n" + + " },\n" + + " \"tagline\" : \"You Know, for Search\"\n" + + "}\n"; + } +}