package osmo.tester.unittests.generation;

import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import osmo.common.TestUtils;
import osmo.tester.OSMOConfiguration;
import osmo.tester.coverage.TestCoverage;
import osmo.tester.generator.endcondition.Length;
import osmo.tester.generator.endcondition.Time;
import osmo.tester.model.ModelFactory;
import osmo.tester.model.Requirements;
import osmo.tester.model.TestModels;
import osmo.tester.optimizer.multiosmo.GeneratorTask;
import osmo.tester.optimizer.multiosmo.MultiOSMO;
import osmo.tester.unittests.testmodels.ErrorModelSleepy;
import osmo.tester.unittests.testmodels.ValidTestModel2;

import java.util.List;

import static org.junit.Assert.*;

/** @author Teemu Kanstren */
public class MultiOSMOTests {
  @After
  public void restore() {
    TestUtils.endOutputCapture();
  }
  
  @Test
  public void defaultFactory() {
    TestUtils.recursiveDelete("osmo-output");
    MultiOSMO mosmo = new MultiOSMO(4);
    TestUtils.startOutputCapture();
    try {
      mosmo.generate(new Time(1), 444);
      fail("Generation without any model objects should fail.");
    } catch (Exception e) {
      //expected
    }
    String output = TestUtils.getOutput();
    assertEquals("Message for default factory", MultiOSMO.ERROR_MSG+System.getProperty("line.separator"), output);
  }

  @Test
  public void generate4() throws Exception {
    Thread.sleep(100);
    TestUtils.recursiveDelete("osmo-output");
    MultiOSMO mosmo = new MultiOSMO(4);
    OSMOConfiguration config = mosmo.getConfig();
    config.setSequenceTraceRequested(true);
    config.setFactory(new MyModelFactory());
    config.setTestEndCondition(new Length(10));
    config.setSuiteEndCondition(new Time(2));
    mosmo.generate(new Time(1), 444);
//    List<String> reports = TestUtils.listFiles("osmo-output", ".csv", false);
//    assertEquals("Number of reports generated", 4, reports.size());
    List<String> traces = TestUtils.listFiles("osmo-output", ".html", false);
    assertEquals("Number of HTML traces generated", 4, traces.size());
    List<String> xmls = TestUtils.listFiles("osmo-output", ".xml", false);
    assertEquals("Number of XML traces generated", 4, xmls.size());
  }

  @Ignore
  @Test
  public void generate4times3() {
    TestUtils.recursiveDelete("osmo-output");
    MultiOSMO mosmo = new MultiOSMO(4);
    OSMOConfiguration config = mosmo.getConfig();
    config.setSequenceTraceRequested(true);
    config.setFactory(new MyModelFactory());
    config.setTestEndCondition(new Length(10));
    config.setSuiteEndCondition(new Time(2));
    mosmo.generate(new Time(6), 444);
    List<String> reports = TestUtils.listFiles("osmo-output", ".csv", false);
    assertEquals("Number of reports generated", 5, reports.size());
    List<String> traces = TestUtils.listFiles("osmo-output", ".html", false);
    assertEquals("Number of traces generated", 4, traces.size());
  }
  
  @Test
  public void errorInTest() {
    MultiOSMO mosmo = new MultiOSMO(2);
    OSMOConfiguration config = mosmo.getConfig();
    config.setSequenceTraceRequested(true);
    config.setFactory(new ErrorModelFactory());
    config.setTestEndCondition(new Length(30));
    config.setSuiteEndCondition(new Length(2));
    config.setStopGenerationOnError(false);
    GeneratorTask.Result result = mosmo.generate(new Time(1), 444);
    int steps = result.getCoverage().getTotalSteps();
    assertTrue("Number of generated steps from MOSMO should be less than 120 (generators (2) x steps (30) x tests (2) configured) due to assertion e in model, was "+steps, steps < 1200);
    assertTrue("Number of generated steps from MOSMO should be divisible by 2 due to e in step 2", steps %2 == 0);
    //if we run long test sets, the value is bigger due to some JVM optimizations. just one test is shorter
    assertTrue("Number of generated steps from MOSMO should be 12 to 20([6, 8, 10] x 2) was " + steps, 12 <= steps && steps <= 20);
  }

  private static class MyModelFactory implements ModelFactory {
    @Override
    public void createModelObjects(TestModels addHere) {
      addHere.add(new ValidTestModel2(new Requirements()));
    }
  }

  private static class ErrorModelFactory implements ModelFactory {
    @Override
    public void createModelObjects(TestModels addHere) {
      addHere.add(new ErrorModelSleepy());
    }
  }
}

@@ -34,24 +34,116 @@
 eration;%0D%0A%0D%0A
+import static java.util.Arrays.stream;%0D%0A%0D%0Aimport java.io.File;%0D%0A%0D%0Aimport org.junit.Before;%0D%0A
 import org.j
@@ -886,16 +886,254 @@
 ests %7B%0D%0A
+  private static final String OUTPUT_FOLDER = %22osmo-output%22;%0D%0A%0D%0A  @Before%0D%0A  public void clearFiles() %7B%0D%0A    final File outputFolder = new File(OUTPUT_FOLDER);%0D%0A    stream(outputFolder.listFiles()).forEach(file -%3E file.delete());%0D%0A  %7D%0D%0A%0D%0A
   @After
@@ -1252,55 +1252,8 @@
  %7B%0D%0A
-    TestUtils.recursiveDelete(%22osmo-output%22);%0D%0A
     
@@ -1748,55 +1748,8 @@
 );%0D%0A
-    TestUtils.recursiveDelete(%22osmo-output%22);%0D%0A
     
@@ -2107,37 +2107,37 @@
 s.listFiles(
-%22osmo-output%22
+OUTPUT_FOLDER
 , %22.csv%22, fa
@@ -2256,37 +2256,37 @@
 s.listFiles(
-%22osmo-output%22
+OUTPUT_FOLDER
 , %22.html%22, f
@@ -2405,37 +2405,37 @@
 s.listFiles(
-%22osmo-output%22
+OUTPUT_FOLDER
 , %22.xml%22, fa
@@ -2576,55 +2576,8 @@
  %7B%0D%0A
-    TestUtils.recursiveDelete(%22osmo-output%22);%0D%0A
     
@@ -2933,37 +2933,37 @@
 s.listFiles(
-%22osmo-output%22
+OUTPUT_FOLDER
 , %22.csv%22, fa
@@ -3084,29 +3084,29 @@
 stFiles(
-%22osmo-output%22
+OUTPUT_FOLDER
 , %22.html

Clear files instead of deleting ouput folder