CancelSchedulesAction extends Action { @NonNull @Override public ActionResult perform(@NonNull ActionArguments arguments) { InAppAutomation automation; try { automation = actionAutomationCallable.call(); } catch (Exception e) { return ActionResult.newErrorResult(e); } JsonValue jsonValue = arguments.getValue().toJsonValue(); if (jsonValue.isString() && ALL.equalsIgnoreCase(jsonValue.getString())) { automation.cancelSchedules(Schedule.TYPE_ACTION); return ActionResult.newEmptyResult(); } JsonValue groupsJson = jsonValue.optMap().opt(GROUPS); if (groupsJson.isString()) { automation.cancelScheduleGroup(groupsJson.optString()); } else if (groupsJson.isJsonList()) { for (JsonValue value : groupsJson.optList()) { if (value.isString()) { automation.cancelScheduleGroup(value.optString()); } } } JsonValue idsJson = jsonValue.optMap().opt(IDS); if (idsJson.isString()) { automation.cancelSchedule(idsJson.optString()); } else if (idsJson.isJsonList()) { for (JsonValue value : idsJson.optList()) { if (value.isString()) { automation.cancelSchedule(value.optString()); } } } return ActionResult.newEmptyResult(); } CancelSchedulesAction(); @VisibleForTesting  CancelSchedulesAction(@NonNull Callable<InAppAutomation> actionAutomationCallable); @Override boolean acceptsArguments(@NonNull ActionArguments arguments); @NonNull @Override ActionResult perform(@NonNull ActionArguments arguments); @NonNull
    static final String DEFAULT_REGISTRY_NAME; @NonNull
    static final String DEFAULT_REGISTRY_SHORT_NAME; @NonNull
    static final String GROUPS; @NonNull
    static final String IDS; @NonNull
    static final String ALL; }
@Test public void testPerformCancelAll() { JsonValue argValue = JsonValue.wrap(CancelSchedulesAction.ALL); ActionArguments args = createArgs(Action.SITUATION_MANUAL_INVOCATION, argValue); action.perform(args); verify(automation).cancelSchedules(Schedule.TYPE_ACTION); }
@Test public void testPerformGroups() { List<String> groups = new ArrayList<>(); groups.add("group 1"); groups.add("group 2"); JsonMap argValue = JsonMap.newBuilder() .put(CancelSchedulesAction.GROUPS, JsonValue.wrapOpt(groups)) .build(); ActionArguments args = createArgs(Action.SITUATION_MANUAL_INVOCATION, argValue); action.perform(args); for (String group : groups) { verify(automation, atLeastOnce()).cancelScheduleGroup(group); } }
@Test public void testPerformSingleGroup() { JsonMap argValue = JsonMap.newBuilder() .put(CancelSchedulesAction.GROUPS, "group 1") .build(); ActionArguments args = createArgs(Action.SITUATION_MANUAL_INVOCATION, argValue); action.perform(args); verify(automation).cancelScheduleGroup("group 1"); }
@Test public void testPerformIds() { List<String> ids = new ArrayList<>(); ids.add("id 1"); ids.add("id 2"); JsonMap argValue = JsonMap.newBuilder() .put(CancelSchedulesAction.IDS, JsonValue.wrapOpt(ids)) .build(); ActionArguments args = createArgs(Action.SITUATION_MANUAL_INVOCATION, argValue); action.perform(args); verify(automation).cancelSchedule(ids.get(0)); verify(automation).cancelSchedule(ids.get(1)); }
@Test public void testPerformSingleId() { JsonMap argValue = JsonMap.newBuilder() .put(CancelSchedulesAction.IDS, "id 1") .build(); ActionArguments args = createArgs(Action.SITUATION_MANUAL_INVOCATION, argValue); action.perform(args); verify(automation).cancelSchedule("id 1"); }