package ske.ekstkom.kanalvalg;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import ske.ekstkom.statemachine.LogAction;
import ske.ekstkom.statemachine.Signal;
import ske.ekstkom.statemachine.State;
import ske.ekstkom.statemachine.StateMachine;

public class UtsendelseTest {

	private LogAction logAction;
	private StateMachine printStateMachine;

	// Eventer/hendelser
	public static final String RETUR_MOTTATT = "retur_mottatt"; // returpost
	public static final String UTSENDING_UTFOERT = "utsending_utfoert";
	public static final String FEIL_I_UTSKRIFT = "feil_i_utskrift";
	public static final String SEND_TIL_PRINT = "send_til_print";
	public static final String FEIL_I_RENDERING = "feil_i_rendering";
	public static final String SEND_TIL_RENDERING = "send_til_rendering";
	public static final String FEIL_I_DAL = "feil_i_dal";

	@Before
	public void initPrintStateMachine() {
		logAction = new LogAction("Logging Action");

		State feilet = State.named("Feilet").build();

		State sendtUt = State.named("SendtUt")
				//
				.transition("til feilet").guardedBy(UtsendelseGuard.by(RETUR_MOTTATT)).withAction(logAction).to(feilet)
				.build();

		State sendtTilPrint = State
				.named("SendtTilPrint")
				//
				.transition("til_sendt_ut").guardedBy(UtsendelseGuard.by(UTSENDING_UTFOERT)).withAction(logAction)
				.to(sendtUt)
				//
				.transition("til_feil").guardedBy(UtsendelseGuard.by(FEIL_I_UTSKRIFT)).withAction(logAction).to(feilet)
				.build();

		State tilRendering = State
				.named("TilRendering")
				//
				.transition("Til_sendt_til_print").guardedBy(UtsendelseGuard.by(SEND_TIL_PRINT)).withAction(logAction)
				.to(sendtTilPrint)
				//
				.transition("til_feil").guardedBy(UtsendelseGuard.by(FEIL_I_RENDERING)).withAction(logAction)
				.to(feilet).build();

		// Starttilstand for printutsending
		State utsendelseBestilt = State
				.named("UtsendelseBestilt")
				//
				.transition("til_rendering").guardedBy(UtsendelseGuard.by(SEND_TIL_RENDERING)).withAction(logAction)
				.to(tilRendering)
				//
				.transition("til_feil").guardedBy(UtsendelseGuard.by(FEIL_I_DAL)).withAction(logAction).to(feilet)
				.build();

		printStateMachine = StateMachine.named("PrintStateMachine").initState(utsendelseBestilt).build();
	}

	@Test
	public void skal_gjennom_alle_tilstand() {

		printStateMachine.execute(Signal.create(SEND_TIL_RENDERING));
		printStateMachine.execute(Signal.create(SEND_TIL_PRINT));
		printStateMachine.execute(Signal.create(UTSENDING_UTFOERT));
		printStateMachine.execute(Signal.create(RETUR_MOTTATT));

		// validate active state
		Assert.assertEquals("Feilet", printStateMachine.getActiveState().getName());

		// validate actions occurred
		logAction.assertNumberOfExecute(4);

		// validate signals with no matching transitions
		Assert.assertEquals(0, printStateMachine.numberOfSignalsNotMatchedTransitions());
	}

	@Test
	public void skal_feile_i_rendering() {
		printStateMachine.execute(Signal.create(SEND_TIL_RENDERING));
		printStateMachine.execute(Signal.create(FEIL_I_RENDERING));

		Assert.assertEquals("Feilet", printStateMachine.getActiveState().getName());

		logAction.assertNumberOfExecute(2);

		Assert.assertEquals(0, printStateMachine.numberOfSignalsNotMatchedTransitions());
	}

	@Test
	public void skal_feile_i_rendering_og_ignorere_irrelevanet_signaler() {
		printStateMachine.execute(Signal.create(SEND_TIL_RENDERING));
		printStateMachine.execute(Signal.create(FEIL_I_RENDERING));
		printStateMachine.execute(Signal.create(FEIL_I_RENDERING));
		printStateMachine.execute(Signal.create(FEIL_I_RENDERING));
		printStateMachine.execute(Signal.create(FEIL_I_RENDERING));

		Assert.assertEquals("Feilet", printStateMachine.getActiveState().getName());

		// Ikke så relevante assert...
		logAction.assertNumberOfExecute(2);
		Assert.assertEquals(3, printStateMachine.numberOfSignalsNotMatchedTransitions());
	}
}

@@ -858,16 +858,26 @@
 _dal%22;%0A%0A
+%09// Given%0A
 %09@Before
@@ -2342,16 +2342,26 @@
 nd() %7B%0A%0A
+%09%09// When%0A
 %09%09printS
@@ -2408,32 +2408,32 @@
 IL_RENDERING));%0A
-
 %09%09printStateMach
@@ -2608,29 +2608,12 @@
 %09// 
-validate active state
+Then
 %0A%09%09A
@@ -2693,39 +2693,8 @@
 );%0A%0A
-%09%09// validate actions occurred%0A
 %09%09lo
@@ -2730,60 +2730,8 @@
 (4);
-%0A%0A%09%09// validate signals with no matching transitions
 %0A%09%09A
@@ -2857,24 +2857,35 @@
 endering() %7B
+%0A%0A%09%09// When
 %0A%09%09printStat
@@ -2992,32 +2992,42 @@
 I_RENDERING));%0A%0A
+%09%09// Then%0A
 %09%09Assert.assertE
@@ -3296,16 +3296,27 @@
 aler() %7B
+%0A%0A%09%09// When
 %0A%09%09print
@@ -3617,24 +3617,34 @@
 NDERING));%0A%0A
+%09%09// Then%0A
 %09%09Assert.ass

Given - when - then