/*
 * 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.cluster.routing;


import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Arrays;

public class RoutingBackwardCompatibilityTests extends ESTestCase {

    public void testBackwardCompatibility() throws Exception {
        Path baseDir = createTempDir();
        Node node = new Node(Settings.builder().put("path.home", baseDir.toString()).build());
        try {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(RoutingBackwardCompatibilityTests.class.getResourceAsStream("/org/elasticsearch/cluster/routing/shard_routes.txt"), "UTF-8"))) {
                for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                    if (line.startsWith("#")) { // comment
                        continue;
                    }
                    String[] parts = line.split("\t");
                    assertEquals(Arrays.toString(parts), 7, parts.length);
                    final String index = parts[0];
                    final int numberOfShards = Integer.parseInt(parts[1]);
                    final String type = parts[2];
                    final String id = parts[3];
                    final String routing = "null".equals(parts[4]) ? null : parts[4];
                    final int pre20ExpectedShardId = Integer.parseInt(parts[5]);
                    final int currentExpectedShard = Integer.parseInt(parts[6]);

                    OperationRouting operationRouting = node.injector().getInstance(OperationRouting.class);
                    for (Version version : VersionUtils.allVersions()) {
                        final Settings settings = settings(version).build();
                        IndexMetaData indexMetaData = IndexMetaData.builder(index).settings(settings).numberOfShards(numberOfShards).numberOfReplicas(randomInt(3)).build();
                        MetaData.Builder metaData = MetaData.builder().put(indexMetaData, false);
                        RoutingTable routingTable = RoutingTable.builder().addAsNew(indexMetaData).build();
                        ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).routingTable(routingTable).build();
                        final int shardId = operationRouting.indexShards(clusterState, index, type, id, routing).shardId().getId();
                        if (version.before(Version.V_2_0_0_beta1)) {
                            assertEquals(pre20ExpectedShardId, shardId);
                        } else {
                            assertEquals(currentExpectedShard, shardId);
                        }
                    }
                }
            }
        } finally {
            node.close();
        }
    }

}

@@ -1492,161 +1492,8 @@
 n %7B%0A
-        Path baseDir = createTempDir();%0A        Node node = new Node(Settings.builder().put(%22path.home%22, baseDir.toString()).build());%0A        try %7B%0A    
     
@@ -1703,28 +1703,24 @@
             
-    
 for (String 
@@ -1799,28 +1799,24 @@
             
-    
 if (line.sta
@@ -1862,20 +1862,16 @@
         
- 
-   
 continue
@@ -1880,36 +1880,32 @@
                 
-    
 %7D%0A              
@@ -1906,20 +1906,16 @@
         
- 
-   
 String%5B%5D
@@ -1941,20 +1941,16 @@
 (%22%5Ct%22);%0A
-    
         
@@ -2020,36 +2020,32 @@
                 
-   
- 
 final String ind
@@ -2059,20 +2059,16 @@
 rts%5B0%5D;%0A
-    
         
@@ -2126,28 +2126,24 @@
 (parts%5B1%5D);%0A
-    
             
@@ -2184,36 +2184,32 @@
                 
-    
 final String id 
@@ -2220,20 +2220,16 @@
 rts%5B3%5D;%0A
-    
         
@@ -2310,36 +2310,32 @@
                 
-  
-  
 final int pre20E
@@ -2378,21 +2378,61 @@
 rts%5B5%5D);
-%0A    
+ // not needed anymore - old hashing is gone%0A
         
@@ -2517,20 +2517,16 @@
         
-    
 Operatio
@@ -2558,64 +2558,51 @@
  = n
-ode.injector().getInstance(OperationRouting.class);%0A    
+ew OperationRouting(Settings.EMPTY, null);%0A
     
@@ -2682,28 +2682,24 @@
             
-    
 final Settin
@@ -2731,36 +2731,32 @@
 rsion).build();%0A
-    
                 
@@ -2928,20 +2928,16 @@
         
-  
-  
 MetaData
@@ -3002,20 +3002,16 @@
 false);%0A
-    
         
@@ -3126,20 +3126,16 @@
         
-   
- 
 ClusterS
@@ -3243,36 +3243,32 @@
 Table).build();%0A
-    
                 
@@ -3383,191 +3383,8 @@
 ();%0A
-                        if (version.before(Version.V_2_0_0_beta1)) %7B%0A                            assertEquals(pre20ExpectedShardId, shardId);%0A                        %7D else %7B%0A        
     
@@ -3456,36 +3456,26 @@
             
- 
-       %7D%0A  
+%7D%0A
             
@@ -3478,93 +3478,9 @@
     
-      %7D%0A                %7D%0A            %7D%0A        %7D finally %7B%0A            node.close();
+%7D
 %0A   
@@ -3492,11 +3492,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

fix bwc test for hashing / routing and simplify test to be a real unittest