/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.test;

import org.elasticsearch.Version;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

/** Utilities for selecting versions in tests */
public class VersionUtils {

    private static final List<Version> SORTED_VERSIONS;
    static {
        Field[] declaredFields = Version.class.getFields();
        Set<Integer> ids = new HashSet<>();
        for (Field field : declaredFields) {
            final int mod = field.getModifiers();
            if (Modifier.isStatic(mod) && Modifier.isFinal(mod) && Modifier.isPublic(mod)) {
                if (field.getType() == Version.class) {
                    try {
                        Version object = (Version) field.get(null);
                        ids.add(object.id);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        List<Integer> idList = new ArrayList<>(ids);
        Collections.sort(idList);
        List<Version> version = new ArrayList<>();
        for (Integer integer : idList) {
            version.add(Version.fromId(integer));
        }
        SORTED_VERSIONS = Collections.unmodifiableList(version);
    }

    /** Returns immutable list of all known versions. */
    public static List<Version> allVersions() {
        return Collections.unmodifiableList(SORTED_VERSIONS);
    }

    public static Version getPreviousVersion(Version version) {
        assert SORTED_VERSIONS.indexOf(Version.CURRENT.minimumCompatibilityVersion()) == -1
            && Version.CURRENT == Version.V_5_1_0: "remove the hack below";
        if (version.equals(Version.CURRENT.minimumCompatibilityVersion())) {
            // remove this hack here once 5.0.0 is released
            version = Version.CURRENT;
        }
        int index = SORTED_VERSIONS.indexOf(version);
        assert index > 0;
        return SORTED_VERSIONS.get(index - 1);
    }

    /** Returns the {@link Version} before the {@link Version#CURRENT} */
    public static Version getPreviousVersion() {
        Version version = getPreviousVersion(Version.CURRENT);
        assert version.before(Version.CURRENT);
        return version;
    }

    /** Returns the oldest {@link Version} */
    public static Version getFirstVersion() {
        return SORTED_VERSIONS.get(0);
    }

    /** Returns a random {@link Version} from all available versions. */
    public static Version randomVersion(Random random) {
        return SORTED_VERSIONS.get(random.nextInt(SORTED_VERSIONS.size()));
    }

    /** Returns a random {@link Version} between <code>minVersion</code> and <code>maxVersion</code> (inclusive). */
    public static Version randomVersionBetween(Random random, Version minVersion, Version maxVersion) {
        int minVersionIndex = 0;
        if (minVersion != null) {
            minVersionIndex = SORTED_VERSIONS.indexOf(minVersion);
        }
        int maxVersionIndex = SORTED_VERSIONS.size() - 1;
        if (maxVersion != null) {
            maxVersionIndex = SORTED_VERSIONS.indexOf(maxVersion);
        }
        if (minVersionIndex == -1) {
            if (minVersion.id == 5000099 && Version.V_5_1_0.equals(maxVersion)) {
                assert Version.CURRENT == maxVersion;
                // this is a hack to make tests work that use randomVersionBetween(CURRENT.minimumCompatibilityVersion(), CURRENT)
                // on the 5.x branch while 5.0 has not yet been release (not in the SORTED_VERSIONS) and CURRENT is 5.1 such that
                // CURRENT.minimumCompatibilityVersion() returns 5.0.0 and we fail. This has just been added to the 5.x branch for
                // the time being, once we release 5.0 and add it to the Version.java class we will trip the assertion below.
                return random.nextBoolean() ? minVersion : maxVersion;
            }
            throw new IllegalArgumentException("minVersion [" + minVersion + "] does not exist.");
        } else if (maxVersionIndex == -1) {
            throw new IllegalArgumentException("maxVersion [" + maxVersion + "] does not exist.");
        } else if (minVersionIndex > maxVersionIndex) {
            throw new IllegalArgumentException("maxVersion [" + maxVersion + "] cannot be less than minVersion [" + minVersion + "]");
        } else {
            assert minVersion.id == 5000099 : "remove the hack above";
            // minVersionIndex is inclusive so need to add 1 to this index
            int range = maxVersionIndex + 1 - minVersionIndex;
            return SORTED_VERSIONS.get(minVersionIndex + random.nextInt(range));
        }
    }

    public static boolean isSnapshot(Version version) {
        if (Version.CURRENT.equals(version)) {
            return true;
        }
        return false;
    }
}

@@ -5313,16 +5313,38 @@
   assert
+ minVersion == null %7C%7C
  minVers
@@ -5342,33 +5342,33 @@
 %7C minVersion.id 
-=
+!
 = 5000099 : %22rem
@@ -5387,16 +5387,52 @@
  above%22;
+ // will fail once 5.0.0 is released
 %0A       

Make VersionUtils happy again with the 5.0.0 version hack