Action { @NonNull final ActionResult run(@NonNull ActionArguments arguments) { try { if (!acceptsArguments(arguments)) { Logger.debug("Action %s is unable to accept arguments: %s", this, arguments); return ActionResult.newEmptyResultWithStatus(ActionResult.STATUS_REJECTED_ARGUMENTS); } Logger.info("Running action: %s arguments: %s", this, arguments); onStart(arguments); ActionResult result = perform(arguments); if (result == null) { result = ActionResult.newEmptyResult(); } onFinish(arguments, result); return result; } catch (Exception e) { Logger.error(e, "Failed to run action %s", this); return ActionResult.newErrorResult(e); } }  boolean acceptsArguments(@NonNull ActionArguments arguments); void onStart(@NonNull ActionArguments arguments); @NonNull abstract ActionResult perform(@NonNull ActionArguments arguments); void onFinish(@NonNull ActionArguments arguments, @NonNull ActionResult result); boolean shouldRunOnMainThread(); final static int SITUATION_MANUAL_INVOCATION; final static int SITUATION_PUSH_RECEIVED; final static int SITUATION_PUSH_OPENED; final static int SITUATION_WEB_VIEW_INVOCATION; final static int SITUATION_FOREGROUND_NOTIFICATION_ACTION_BUTTON; final static int SITUATION_BACKGROUND_NOTIFICATION_ACTION_BUTTON; final static int SITUATION_AUTOMATION; }
@Test public void testRun() throws ActionValueException { Bundle metadata = new Bundle(); metadata.putString("metadata_key", "metadata_value"); final ActionResult expectedResult = ActionResult.newResult(ActionValue.wrap("result")); final ActionArguments originalArguments = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, "value", metadata); TestAction action = new TestAction(true, expectedResult) { @NonNull @Override public ActionResult perform(@NonNull ActionArguments arguments) { assertEquals("Action arguments is a different instance then the passed in arguments", arguments, originalArguments); assertEquals("Bundle does not contain the passed in metadata", "metadata_value", arguments.getMetadata().get("metadata_key")); return super.perform(arguments); } @Override public void onStart(@NonNull ActionArguments arguments) { super.onStart(arguments); assertEquals("Bundle does not contain the passed in metadata", arguments.getMetadata().get("metadata_key"), "metadata_value"); assertEquals("Action arguments is a different instance then the passed in arguments", arguments, originalArguments); } @Override public void onFinish(@NonNull ActionArguments arguments, @NonNull ActionResult result) { super.onFinish(arguments, result); assertEquals("Bundle does not contain the passed in metadata", arguments.getMetadata().getString("metadata_key"), "metadata_value"); assertEquals("Action arguments is a different instance then the passed in arguments", arguments, originalArguments); assertEquals("Action result is a different instance then then the returned results", result, expectedResult); } }; ActionResult results = action.run(originalArguments); assertEquals("Action result is unexpected", expectedResult, results); assertEquals("Result should have COMPLETED status", ActionResult.STATUS_COMPLETED, expectedResult.getStatus()); assertTrue("Action.onStart is not being called", action.onStartCalled); assertTrue("Action.perform is not being called", action.performCalled); assertTrue("Action.onFinish is not being called", action.onFinishCalled); }
@Test public void testRunBadArgs() throws ActionValueException { ActionResult performResult = ActionResult.newResult(ActionValue.wrap("result")); TestAction action = new TestAction(false, performResult); ActionResult badArgsResult = action.run(ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, "value")); assertTrue("Does not accept arguments should return a 'null' result value", badArgsResult.getValue().isNull()); assertEquals("Result should have an rejected arguemnts status", ActionResult.STATUS_REJECTED_ARGUMENTS, badArgsResult.getStatus()); assertFalse("Does not accept arguments should not call any of the actions perform methods.", action.onStartCalled); assertFalse("Does not accept arguments should not call any of the actions perform methods.", action.performCalled); assertFalse("Does not accept arguments should not call any of the actions perform methods.", action.onFinishCalled); }
@Test public void testRunPerformNullResult() { ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, "value"); TestAction action = new TestAction(true, null); ActionResult result = action.run(args); assertNotNull("Result should never be null", result); assertTrue("Result should be 'null'", result.getValue().isNull()); assertEquals("Result should have the COMPLETED status", ActionResult.STATUS_COMPLETED, result.getStatus()); }