package org.requirementsascode;


import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class FlowWithCaseStepTest {
  private String actualResult;
  private ModelRunner modelRunner;
  
  @BeforeEach
  public void setup() {
    this.modelRunner = new ModelRunner().startRecording();
  }
  
  @Test
  public void runsTrueStepWithoutEvent() {  
    Model model = Model.builder()
      .useCase("inCase Test")
        .basicFlow()
          .step("step1").inCase(() -> true).system(() -> actualResult = "step1 run")
      .build();
        
    modelRunner.run(model);
    assertEquals("step1 run", actualResult);
    assertRecordedStepNames("step1");
  }
  
  @Test
  public void runsTrueStepWithEvent() {    
    Model model = Model.builder()
      .useCase("inCase Test")
        .basicFlow()
          .step("step1").on(String.class).inCase(() -> true).system(str -> actualResult = str)
      .build();
        
    modelRunner.run(model).reactTo("event");
    assertEquals("event", actualResult);
    
    assertRecordedStepNames("step1");
  }
  
  @Test
  public void runsFalseStepTrueStep() {    
    Model model = Model.builder()
      .useCase("inCase Test")
        .basicFlow()
          .step("step1").on(String.class).inCase(() -> false).system(str -> actualResult = str)
          .step("step2").on(String.class).inCase(() -> true).system(str -> actualResult = str)
      .build();
        
    modelRunner.run(model).reactTo("event1", "event2");
    assertEquals("event2", actualResult);
    
    assertRecordedStepNames("step2");
  }
  
  @Test
  public void runsFalseStepFalseStep() {    
    Model model = Model.builder()
      .useCase("inCase Test")
        .basicFlow()
          .step("step1").user(String.class).inCase(() -> false).system(str -> actualResult = str)
          .step("step2").inCase(() -> false).system(str -> actualResult = "not evaluated")
      .build();
        
    modelRunner.run(model).reactTo("event1", "event2");
    assertNull(actualResult);
    
    assertRecordedStepNames();
  }
  
  protected void assertRecordedStepNames(String... expectedStepNames) {
    String[] actualStepNames = modelRunner.getRecordedStepNames();
    assertArrayEquals(expectedStepNames, actualStepNames);
  }
}

@@ -31,74 +31,8 @@
 ;%0A%0A%0A
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;%0A
 impo
@@ -259,16 +259,40 @@
 tepTest 
+extends AbstractTestCase
 %7B%0A  priv
@@ -320,43 +320,8 @@
 lt;%0A
-  private ModelRunner modelRunner;%0A
   %0A 
@@ -365,59 +365,37 @@
     
-this.modelRunner = new ModelRunner().startRecording
+setupWithRecordingModelRunner
 ();%0A
@@ -2143,211 +2143,6 @@
   %7D%0A
-  %0A  protected void assertRecordedStepNames(String... expectedStepNames) %7B%0A    String%5B%5D actualStepNames = modelRunner.getRecordedStepNames();%0A    assertArrayEquals(expectedStepNames, actualStepNames);%0A  %7D%0A
 %7D%0A

Extend AbstractTestCase