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

import com.carrotsearch.randomizedtesting.annotations.Repeat;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

/**
 *
 */
public class WatcherVersionTests extends ElasticsearchTestCase {

    @Test
    @Repeat(iterations = 100)
    public void testStrings() throws Exception {
        boolean beta = randomBoolean();
        int buildNumber = beta ? randomIntBetween(0, 49) : randomIntBetween(0, 48);
        int major = randomIntBetween(0, 20);
        int minor = randomIntBetween(0, 20);
        int revision = randomIntBetween(0, 20);

        String build = buildNumber == 0 ? "" :
                beta ? "-beta" + buildNumber : "-rc" + buildNumber;


        String versionName = new StringBuilder()
                .append(major)
                .append(".").append(minor)
                .append(".").append(revision)
                .append(build).toString();
        WatcherVersion version = WatcherVersion.fromString(versionName);

        logger.info("version: {}", versionName);

        assertThat(version.major, is((byte) major));
        assertThat(version.minor, is((byte) minor));
        assertThat(version.revision, is((byte) revision));
        if (buildNumber == 0) {
            assertThat(version.build, is((byte) 99));
        } else if (beta) {
            assertThat(version.build, is((byte) buildNumber));
        } else {
            assertThat(version.build, is((byte) (buildNumber + 50)));
        }

        assertThat(version.number(), equalTo(versionName));
    }
}

@@ -275,70 +275,8 @@
 r;%0A%0A
-import com.carrotsearch.randomizedtesting.annotations.Repeat;%0A
 impo
@@ -525,38 +525,8 @@
 est%0A
-    @Repeat(iterations = 100)%0A
     
@@ -570,16 +570,60 @@
 ption %7B%0A
+        for (int i = 0; i %3C 100; i++) %7B%0A    
         
@@ -650,24 +650,28 @@
 mBoolean();%0A
+    
         int 
@@ -746,24 +746,28 @@
 8);%0A        
+    
 int major = 
@@ -795,24 +795,28 @@
 0);%0A        
+ 
+   
 int minor = 
@@ -844,24 +844,28 @@
 0);%0A        
+ 
+   
 int revision
@@ -897,24 +897,28 @@
 );%0A%0A        
+    
 String build
@@ -960,16 +960,20 @@
         
+  
+  
 beta ? %22
@@ -1022,24 +1022,28 @@
 ;%0A%0A%0A        
+    
 String versi
@@ -1079,32 +1079,36 @@
                 
+   
+ 
 .append(major)%0A 
@@ -1102,24 +1102,28 @@
 pend(major)%0A
+    
             
@@ -1161,32 +1161,36 @@
                 
+    
 .append(%22.%22).app
@@ -1211,32 +1211,36 @@
                 
+ 
+   
 .append(build).t
@@ -1246,24 +1246,28 @@
 toString();%0A
+    
         Watc
@@ -1336,16 +1336,20 @@
         
+  
+  
 logger.i
@@ -1374,32 +1374,36 @@
  versionName);%0A%0A
+    
         assertTh
@@ -1435,24 +1435,28 @@
 e) major));%0A
+    
         asse
@@ -1496,32 +1496,36 @@
 inor));%0A        
+    
 assertThat(versi
@@ -1567,16 +1567,20 @@
         
+    
 if (buil
@@ -1599,32 +1599,36 @@
 ) %7B%0A            
+ 
+   
 assertThat(versi
@@ -1657,24 +1657,28 @@
 ));%0A        
+    
 %7D else if (b
@@ -1688,32 +1688,36 @@
 ) %7B%0A            
+   
+ 
 assertThat(versi
@@ -1755,24 +1755,28 @@
 ));%0A        
+    
 %7D else %7B%0A   
@@ -1772,16 +1772,20 @@
  else %7B%0A
+    
         
@@ -1858,11 +1858,19 @@
     
-%7D%0A%0A
+    %7D%0A%0A    
     
@@ -1921,20 +1921,30 @@
 sionName));%0A
+        %7D%0A
     %7D%0A%7D%0A

remove @Repeat annotation from test