package com.mumux.androidtesting.scenario; import com.mumux.androidtesting.actions.ActionParseException; import com.mumux.androidtesting.actions.ActionParser; import org.json.JSONArray; import org.json.JSONException; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.skyscreamer.jsonassert.JSONAssert; import java.io.BufferedReader; import java.io.StringReader; public class ScenarioTest { private final static String SCENARIO_STRING = "" + "# SCENARIO_NAME\n" + "PLANE off\n" + "(HOME)\n" + "HOME"; private final static String SCENARIO_STRING_INPUT = "" + "# SCENARIO_NAME" + "# This is the\n" + "# description.\n" + "\n" + "PLANE off\n" + "(HOME)\n" + "HOME"; private Scenario SCENARIO; private final ScenarioParser scenarioParser = new TextScenarioParser(); @Before public void setup() throws ActionParseException { SCENARIO = new Scenario("SCENARIO_NAME"); SCENARIO.addAction(ActionParser.parseAction("PLANE off")); SCENARIO.addAction(new OptionalAction(ActionParser.parseAction("HOME"))); SCENARIO.addAction(ActionParser.parseAction("HOME")); } @Test public void testToString() throws ActionParseException { Assert.assertEquals(SCENARIO_STRING, SCENARIO.toString()); } @Test public void testParsing() throws Exception { Scenario scenario = scenarioParser.parse(new StringReader(SCENARIO_STRING_INPUT)); Assert.assertEquals("SCENARIO_NAME", scenario.name); Assert.assertEquals(3, scenario.getActions().size()); Assert.assertEquals("PLANE", scenario.getActions().get(0).getName()); Assert.assertEquals("HOME", scenario.getActions().get(1).getName()); Assert.assertEquals("HOME", scenario.getActions().get(2).getName()); Assert.assertTrue(scenario.getActions().get(1) instanceof OptionalAction); } @Test public void testParsingTitle() throws Exception { Assert.assertEquals("title", TextScenarioParser.parseTitle(new BufferedReader(new StringReader("# title")))); try { TextScenarioParser.parseTitle(new BufferedReader(new StringReader(""))); throw new Exception("should not happen"); } catch (ScenarioParsingException e) { Assert.assertEquals("Missing title", e.getMessage()); } try { TextScenarioParser.parseTitle(new BufferedReader(new StringReader("XXX"))); throw new Exception("should not happen"); } catch (ScenarioParsingException e) { Assert.assertEquals("Missing title", e.getMessage()); } } @Test public void testToJson() throws JSONException { Assert.assertEquals("SCENARIO_NAME", SCENARIO.toJson().getString("name")); JSONArray actions = SCENARIO.toJson().getJSONArray("actions"); Assert.assertEquals(3, actions.length()); Assert.assertEquals("PLANE", actions.getJSONObject(0).getString("name")); JSONAssert.assertEquals("[{\"name\":\"status\",\"value\":false,\"type\":\"ON_OFF\"}]", actions.getJSONObject(0).getJSONArray("arguments").toString(), true); } @Test public void testIsRootRequired() { Assert.assertFalse(SCENARIO.isRootRequired()); SCENARIO.addAction(ActionParser.getAction("DATA")); Assert.assertTrue(SCENARIO.isRootRequired()); } }