TaskServiceImpl implements TaskService { @Override @Transactional public String exportTask(Long taskId) { Task task = getTask(taskId); if (null != task) { LOGGER.info("Exporting task: {} with ID: {}", task.getName(), task.getId()); JsonNode node = new ObjectMapper().valueToTree(task.toDto()); removeIgnoredFields(node); return node.toString(); } else { throw new TaskNotFoundException(taskId); } }  @Override @Transactional Set<TaskError> save(final Task task); @Override @Transactional void setEnabledOrDisabled(final Task task); @Override @Transactional ActionEvent getActionEventFor(TaskActionInformation taskActionInformation); @Override @Transactional List<Task> getAllTasks(); @Override @Transactional List<Task> findTasksByName(String name); @Override @Transactional List<Task> findActiveTasksForTrigger(final TriggerEvent trigger); @Override @Transactional List<Task> findActiveTasksForTriggerSubject(final String subject); @Override TasksEventParser findCustomParser(String name); @Override @Transactional Task getTask(Long taskId); @Override @Transactional void deleteTask(Long taskId); @MotechListener(subjects = CHANNEL_UPDATE_SUBJECT) @Transactional void validateTasksAfterChannelUpdate(MotechEvent event); @MotechListener(subjects = DATA_PROVIDER_UPDATE_SUBJECT) @Transactional void validateTasksAfterTaskDataProviderUpdate(MotechEvent event); @Override @Transactional String exportTask(Long taskId); @Override @Transactional Task importTask(String json); @Override @Transactional List<Task> findTasksDependentOnModule(final String moduleName); @Autowired void setTasksDataService(TasksDataService tasksDataService); @Autowired void setChannelService(ChannelService channelService); @Autowired void setProviderService(TaskDataProviderService providerService); @Autowired void setEventRelay(EventRelay eventRelay); @Autowired void setBundleContext(BundleContext bundleContext); @Autowired void setTaskValidator(TaskValidator taskValidator); @Autowired void setTriggerEventService(TriggerEventService triggerEventService); @Autowired void setTaskMigrationManager(TaskMigrationManager taskMigrationManager); static final List<String> IGNORED_FIELDS; }
@Test public void shouldConvertTaskToJSON() throws Exception { Map<String, String> values = new HashMap<>(); values.put("id", "1"); action.setValues(values); long taskId = 12345L; Task expected = new TaskBuilder() .withName("test") .withTrigger(trigger) .addAction(action) .addDataSource(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true)) .isEnabled(true) .build(); ObjectMapper mapper = new ObjectMapper(); when(tasksDataService.findById(taskId)).thenReturn(expected); String json = taskService.exportTask(taskId); JsonNode node = mapper.readTree(json); notContainsIgnoreFields(node); assertNotNull((node).findValue("id")); assertEquals(expected, mapper.readValue(node, Task.class)); }
@Test(expected = TaskNotFoundException.class) public void shouldThrowExceptionDuringExportingIfTaskNotFound() { long taskId = 12345; when(tasksDataService.findById(taskId)).thenReturn(null); taskService.exportTask(taskId); }