/*
 * Copyright (c) 2014-2019 Aurélien Broszniowski
 *
 * Licensed 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 io.rainfall;

import io.rainfall.configuration.ConcurrencyConfig;
import io.rainfall.generator.ByteArrayGenerator;
import io.rainfall.statistics.StatisticsHolder;
import io.rainfall.store.client.StoreClientService;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;

import static io.rainfall.configuration.ReportingConfig.report;
import static io.rainfall.configuration.ReportingConfig.text;
import static io.rainfall.execution.Executions.times;
import static io.rainfall.store.client.StoreClientServiceFactory.defaultService;
import static io.rainfall.store.core.TestRun.Status.COMPLETE;
import static io.rainfall.store.core.TestRun.Status.FAILED;
import static java.util.concurrent.TimeUnit.MINUTES;

/**
 * @author Aurelien Broszniowski
 */

public class RainfallStoreTest {

  @Test
  public void testRecord() throws SyntaxException {


    ConcurrencyConfig concurrency = ConcurrencyConfig.concurrencyConfig()
        .threads(4).timeout(60, MINUTES);


    ObjectGenerator<byte[]> valueGenerator = ByteArrayGenerator.fixedLengthByteArray(1000);

    System.out.println("----------> Test phase");
    StoreClientService perfStoreClientService = defaultService("http://localhost:4567/performance");
    long runId = perfStoreClientService.addRun("example1", getClass().getCanonicalName(), "1.0");

    Random rnd = new Random();

    try {
      Runner.setUp(
          Scenario.scenario("Test phase")
              .exec(new Operation() {
                      @Override
                      public void exec(StatisticsHolder statisticsHolder, Map<Class<? extends Configuration>, Configuration> configurations, List<AssertionEvaluator> assertions) throws TestException {
                        statisticsHolder.record("name1", rnd.nextInt(1000000), TestResult.ONE);
                        statisticsHolder.record("name2", rnd.nextInt(1000000), TestResult.TWO);

                      }

                      @Override
                      public List<String> getDescription() {
                        return Arrays.asList("example");
                      }
                    }
              )
              )
          .executed(times(10000))
          .config(concurrency, report(TestResult.class)
              .log(text())

          )
          .start();

      perfStoreClientService.setStatus(runId, COMPLETE);

    } catch (Exception e) {
      perfStoreClientService.setStatus(runId, FAILED);
      throw e;
    }

  }

  public static enum TestResult {
    ONE, TWO
  }
}

@@ -24,10 +24,10 @@
 4-20
-19
+20
  Aur
@@ -1077,16 +1077,71 @@
 g.text;%0A
+import static io.rainfall.execution.Executions.during;%0A
 import s
@@ -1389,16 +1389,69 @@
 FAILED;%0A
+import static io.rainfall.unit.TimeDivision.seconds;%0A
 import s
@@ -1638,126 +1638,8 @@
 %7B%0A%0A%0A
-    ConcurrencyConfig concurrency = ConcurrencyConfig.concurrencyConfig()%0A        .threads(4).timeout(60, MINUTES);%0A%0A%0A
     
@@ -2367,32 +2367,45 @@
                 
+ long start =
  statisticsHolde
@@ -2410,63 +2410,92 @@
 der.
-record(%22name1%22, rnd.nextInt(1000000), TestResult.ONE
+getTimeInNs();%0A%0A                        long end = statisticsHolder.getTimeInNs(
 );%0A
+%0A
     
@@ -2543,35 +2543,32 @@
 rd(%22
-name2%22, rnd.nextInt(1000000
+operation%22, (end - start
 ), T
@@ -2581,15 +2581,14 @@
 ult.
-TWO
+ONE
 );%0A
-%0A
     
@@ -2747,23 +2747,35 @@
 asList(%22
-example
+Operation execution
 %22);%0A    
@@ -2834,32 +2834,34 @@
     )%0A      
+)%0A
         
 )%0A          
@@ -2840,32 +2840,61 @@
       )%0A        
+  .warmup(during(30, seconds)
 )%0A          .exe
@@ -2935,19 +2935,92 @@
 fig(
-concurrency
+ConcurrencyConfig.concurrencyConfig()%0A              .threads(4).timeout(60, MINUTES)
 , re

implement missing functionalities improve example for Rainfall Store bump up versions