TaskContext { public Object getTriggerValue(String key) { Object value = null; if (parameters != null) { value = getFieldValue(parameters, key); } return value; } TaskContext(Task task, Map<String, Object> parameters, Map<String, Object> metadata, TaskActivityService activityService); void addDataSourceObject(String objectId, Object dataSourceObject, boolean failIfDataNotFound); void addPostActionParameterObject(String objectId, String objectKey, Object postActionParameter, boolean failIfDataNotFound); Object getTriggerValue(String key); Object getPostActionParameterValue(String objectId, String key); Object getDataSourceObjectValue(String objectId, String field, String objectType); void publishWarningActivity(String message, String field); Task getTask(); Map<String, Object> getTriggerParameters(); Map<String, Object> getMetadata(); void setMetadata(Map<String, Object> metadata); Set<PostActionParameterObject> getPostActionParameters();  }
@Test public void testGetTriggerKey() throws Exception { Map<String, Object> parameters = new HashMap<>(); Task task = new TaskBuilder().addAction(new TaskActionInformation()).build(); TaskContext taskContext = new TaskContext(task, null, null, activityService); KeyInformation key = parse(String.format("%s.%s", TRIGGER_PREFIX, EVENT_KEY)); assertEquals(null, taskContext.getTriggerValue(key.getKey())); Map<String, String> child = new HashMap<>(); child.put("key", EVENT_KEY_VALUE); parameters.put("event", child); taskContext = new TaskContext(task, parameters, null, activityService); assertEquals(EVENT_KEY_VALUE, taskContext.getTriggerValue(key.getKey())); }
@Test(expected = IllegalStateException.class) public void testGetTriggerKeyShouldThrowException() throws Exception { MotechEvent event = mock(MotechEvent.class); when(event.getParameters()).thenReturn(new HashMap<>()); KeyInformation key = parse(String.format("%s.%s", TRIGGER_PREFIX, EVENT_KEY)); Task task = new TaskBuilder().addAction(new TaskActionInformation()).build(); new TaskContext(task, event.getParameters(), null, activityService).getTriggerValue(key.getKey()); }
@Test public void testGetNullTriggerKey() throws Exception { Map<String, Object> parameters = new HashMap<>(); MotechEvent event = mock(MotechEvent.class); KeyInformation key = parse(String.format("%s.%s", TRIGGER_PREFIX, EVENT_KEY)); Map<String, String> child = new HashMap<>(); child.put("key", null); parameters.put("event", child); Task task = new TaskBuilder().addAction(new TaskActionInformation()).build(); assertEquals(null, new TaskContext(task, parameters, null, activityService).getTriggerValue(key.getKey())); assertNull(new TaskContext(task, parameters, null, activityService).getTriggerValue(key.getKey())); }