use of org.motechproject.tasks.domain.mds.channel.Channel in project motech by motech.
the class TaskServiceImplTest method shouldSaveTask.
@Test
public void shouldSaveTask() {
Map<String, String> map = new HashMap<>();
map.put("phone", "12345");
TaskConfig config = new TaskConfig().add(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true));
action.setValues(map);
Task task = new Task("name", trigger, asList(action), config, true, false);
task.setNumberOfRetries(5);
task.setRetryTaskOnFailure(true);
Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SEND", "", asList(new EventParameter("test", "value")), "")), null);
ActionEvent actionEvent = new ActionEventBuilder().setDisplayName("receive").setSubject("RECEIVE").setDescription("").setActionParameters(null).build();
actionEvent.addParameter(new ActionParameterBuilder().setDisplayName("Phone").setKey("phone").build(), true);
Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(actionEvent));
TaskDataProvider provider = new TaskDataProvider("TestProvider", asList(new TaskDataProviderObject("test", "Test", asList(new LookupFieldsParameter("id", asList("id"))), null)));
provider.setId(1234L);
when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
when(providerService.getProvider("TestProvider")).thenReturn(provider);
when(triggerEventService.triggerExists(task.getTrigger())).thenReturn(true);
taskService.save(task);
verify(triggerHandler).registerHandlerFor(task.getTrigger().getEffectiveListenerSubject());
// Because task has set number of retries to 5, it should register retries handler for this task
verify(triggerHandler).registerHandlerFor(task.getTrigger().getEffectiveListenerRetrySubject(), true);
verifyCreateAndCaptureTask();
}
use of org.motechproject.tasks.domain.mds.channel.Channel in project motech by motech.
the class TaskServiceImplTest method shouldThrowActionNotFoundExceptionWhenChannelNotContainsActions.
@Test(expected = ActionNotFoundException.class)
public void shouldThrowActionNotFoundExceptionWhenChannelNotContainsActions() throws ActionNotFoundException {
Channel c = new Channel();
when(channelService.getChannel("test-action")).thenReturn(c);
taskService.getActionEventFor(action);
}
use of org.motechproject.tasks.domain.mds.channel.Channel in project motech by motech.
the class TaskAnnotationBeanPostProcessorTest method shouldNotRegisterSameActionTwice.
@Test
public void shouldNotRegisterSameActionTwice() {
Channel channel = new Channel(CHANNEL_NAME, MODULE_NAME, MODULE_VERSION);
when(channelService.getChannel(MODULE_NAME)).thenReturn(channel);
ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
processor.postProcessAfterInitialization(new TestActionWithoutParam(), null);
processor.postProcessAfterInitialization(new TestActionWithoutParam(), null);
verify(channelService, times(2)).addOrUpdate(captor.capture());
assertChannel(captor.getValue());
}
use of org.motechproject.tasks.domain.mds.channel.Channel in project motech by motech.
the class TaskAnnotationBeanPostProcessorTest method shouldAddActionWithParams.
@Test
public void shouldAddActionWithParams() throws Exception {
Channel channel = new Channel(CHANNEL_NAME, MODULE_NAME, MODULE_VERSION);
channel.addActionTaskEvent(new ActionEventBuilder().setDisplayName(ACTION_DISPLAY_NAME).setSubject(ACTION_DISPLAY_NAME).setDescription("").setActionParameters(null).build());
channel.addActionTaskEvent(new ActionEventBuilder().setDisplayName(ACTION_DISPLAY_NAME).setDescription("").setServiceInterface(TestAction.class.getName()).setServiceMethod("action").setActionParameters(null).build());
when(channelService.getChannel(MODULE_NAME)).thenReturn(channel);
ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
processor.postProcessAfterInitialization(new TestActionWithParam(), null);
verify(channelService).addOrUpdate(captor.capture());
Channel actualChannel = captor.getValue();
assertNotNull(actualChannel);
assertEquals(CHANNEL_NAME, actualChannel.getDisplayName());
assertEquals(MODULE_NAME, actualChannel.getModuleName());
assertEquals(MODULE_VERSION, actualChannel.getModuleVersion());
assertNotNull(actualChannel.getActionTaskEvents());
assertEquals(channel.getActionTaskEvents().size(), actualChannel.getActionTaskEvents().size());
ActionEvent actualActionEvent = (ActionEvent) find(actualChannel.getActionTaskEvents(), new Predicate() {
@Override
public boolean evaluate(Object object) {
return object instanceof ActionEvent && ((ActionEvent) object).hasService() && ((ActionEvent) object).getActionParameters().size() > 1 && ((ActionEvent) object).getPostActionParameters().size() > 0;
}
});
assertNotNull(actualActionEvent);
assertEquals(ACTION_DISPLAY_NAME, actualActionEvent.getDisplayName());
assertEquals(TestAction.class.getName(), actualActionEvent.getServiceInterface());
assertEquals(METHOD_NAME, actualActionEvent.getServiceMethod());
assertEquals(getExpectedActionParameters(), actualActionEvent.getActionParameters());
assertEquals(getExpectedPostActionParameters(), actualActionEvent.getPostActionParameters());
}
use of org.motechproject.tasks.domain.mds.channel.Channel in project motech by motech.
the class ChannelServiceImpl method addOrUpdate.
@Override
@Transactional
public synchronized void addOrUpdate(final Channel channel) {
Set<TaskError> errors = ChannelValidator.validate(channel);
if (!isEmpty(errors)) {
throw new ValidationException(ChannelValidator.CHANNEL, TaskError.toDtos(errors));
}
// MOTECH-3049 There is a bug in datanucleus and sometimes ActionParameter is not found in TaskBundleClassLoader,
// so we temporary change contextClassLoader to TaskBundleClassLoader while updating channel
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(channel.getClass().getClassLoader());
final Channel existingChannel = getChannel(channel.getModuleName());
if (existingChannel != null && !existingChannel.equals(channel)) {
LOGGER.debug("Updating channel {}", channel.getDisplayName());
existingChannel.setActionTaskEvents(channel.getActionTaskEvents());
existingChannel.setTriggerTaskEvents(channel.getTriggerTaskEvents());
existingChannel.setDescription(channel.getDescription());
existingChannel.setDisplayName(channel.getDisplayName());
existingChannel.setModuleName(channel.getModuleName());
existingChannel.setModuleVersion(channel.getModuleVersion());
channelsDataService.update(existingChannel);
sendChannelUpdatedEvent(channel);
} else if (existingChannel == null) {
LOGGER.debug("Creating channel {}", channel.getDisplayName());
channelsDataService.create(channel);
}
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
LOGGER.info(String.format("Saved channel: %s", channel.getDisplayName()));
}
Aggregations