use of org.motechproject.tasks.exception.ValidationException in project motech by motech.
the class TaskDataProviderServiceImpl method registerProvider.
@Override
@Transactional
public void registerProvider(final InputStream stream) {
final Type type = new TypeToken<TaskDataProvider>() {
}.getType();
final TaskDataProvider provider = (TaskDataProvider) motechJsonReader.readFromStream(stream, type);
if (TaskDataProviderValidator.validateIsNotEmpty(provider)) {
LOGGER.info("Registering a task data provider with name: {}", provider.getName());
Set<TaskError> errors = TaskDataProviderValidator.validate(provider);
if (!isEmpty(errors)) {
throw new ValidationException(TaskDataProviderValidator.TASK_DATA_PROVIDER, TaskError.toDtos(errors));
}
addProvider(provider);
} else {
LOGGER.info("Registering an empty task data provider with name: {} is not possible", provider.getName());
}
}
use of org.motechproject.tasks.exception.ValidationException in project motech by motech.
the class TaskServiceImplTest method shouldFailValidationIfTriggerChannelIsNotRegistered.
@Test
public void shouldFailValidationIfTriggerChannelIsNotRegistered() {
TaskTriggerInformation trigger = new TaskTriggerInformation("triggerDisplay", "triggerChannel", "triggerModule", "1.0", "subject", "triggerListenerSubject");
TaskActionInformation action = new TaskActionInformation("actionDisplay", "actionChannel", "actionModule", "1.0", "subject");
Task fooTask = new TaskBuilder().withName("foo").withTrigger(trigger).withTaskConfig(new TaskConfig()).addAction(action).build();
fooTask.setEnabled(true);
when(channelService.getChannel("foo-module")).thenReturn(null);
when(channelService.getChannel("actionModule")).thenReturn(null);
Set<TaskError> errors = new HashSet<>();
errors.add(new TaskError("task.validation.error.triggerChannelNotRegistered"));
when(triggerEventService.validateTrigger(trigger)).thenReturn(errors);
expectedException.expect(ValidationException.class);
expectedException.expect(new TypeSafeMatcher<ValidationException>() {
@Override
public void describeTo(Description description) {
}
@Override
public boolean matchesSafely(ValidationException actualException) {
final TaskErrorDto triggerChannelError = new TaskErrorDto("task.validation.error.triggerChannelNotRegistered");
final TaskErrorDto actionChannelError = new TaskErrorDto("task.validation.error.actionChannelNotRegistered");
Set<TaskErrorDto> taskErrors = actualException.getTaskErrors();
return taskErrors.contains(triggerChannelError) && taskErrors.contains(actionChannelError);
}
});
taskService.save(fooTask);
}
use of org.motechproject.tasks.exception.ValidationException in project motech by motech.
the class TasksBlueprintApplicationContextTracker method addingService.
@Override
public Object addingService(ServiceReference reference) {
Bundle module = reference.getBundle();
ApplicationContext applicationContext = (ApplicationContext) super.addingService(reference);
if (isMDS(module.getSymbolicName())) {
return applicationContext;
}
LOGGER.debug("Staring to process {}", applicationContext.getDisplayName());
synchronized (getLock()) {
if (contextInvalidOrProcessed(reference, applicationContext)) {
return applicationContext;
}
markAsProcessed(applicationContext);
}
try {
Resource resource = applicationContext.getResource("classpath:task-channel.json");
if (resource.exists()) {
try (InputStream stream = resource.getInputStream()) {
channelService.registerChannel(stream, module.getSymbolicName(), module.getVersion().toString());
}
LOGGER.info("Registered channel for {} successfully.", module.getSymbolicName());
}
} catch (IOException | ValidationException e) {
LOGGER.error(e.getMessage(), e);
}
try {
taskAnnotationBeanPostProcessor.processAnnotations(applicationContext);
} catch (ValidationException e) {
LOGGER.error(e.getMessage(), e);
}
LOGGER.debug("Processed {}", applicationContext.getDisplayName());
return applicationContext;
}
use of org.motechproject.tasks.exception.ValidationException 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