Search in sources :

Example 16 with TaskDataProvider

use of org.motechproject.tasks.domain.mds.task.TaskDataProvider in project motech by motech.

the class TaskDataProviderServiceImplTest method shouldRegisterProviderFromString.

@Test
public void shouldRegisterProviderFromString() throws IOException {
    String fieldParameterAsJson = "{ displayName: 'displayName', fieldKey: 'fieldKey' }";
    String lookupFieldsAsJson = "['lookupField']";
    String objectAsJson = String.format("{ displayName: 'displayName', type: 'type', lookupFields: %s, fields: [%s]}", lookupFieldsAsJson, fieldParameterAsJson);
    String providerAsJson = String.format("{ name: '%s', objects: [%s] }", PROVIDER_NAME, objectAsJson);
    Type type = new TypeToken<TaskDataProvider>() {
    }.getType();
    StringWriter writer = new StringWriter();
    List<LookupFieldsParameter> lookupFields = asList(new LookupFieldsParameter("lookupField", asList("lookupField")));
    List<FieldParameter> fields = asList(new FieldParameter("displayName", "fieldKey"));
    List<TaskDataProviderObject> objects = new ArrayList<>();
    objects.add(new TaskDataProviderObject("displayName", "type", lookupFields, fields));
    TaskDataProvider provider = new TaskDataProvider(PROVIDER_NAME, objects);
    when(motechJsonReader.readFromStream(any(InputStream.class), eq(type))).thenReturn(provider);
    taskDataProviderService.registerProvider(providerAsJson);
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    verify(motechJsonReader).readFromStream(captor.capture(), eq(type));
    verify(dataProviderDataService).create(provider);
    InputStream value = captor.getValue();
    IOUtils.copy(value, writer);
    assertTrue(value instanceof ByteArrayInputStream);
    assertEquals(providerAsJson, writer.toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) Type(java.lang.reflect.Type) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) FieldParameter(org.motechproject.tasks.domain.mds.task.FieldParameter) Test(org.junit.Test)

Example 17 with TaskDataProvider

use of org.motechproject.tasks.domain.mds.task.TaskDataProvider in project motech by motech.

the class TaskDataProviderServiceImplTest method shouldSendEventWhenProviderWasUpdated.

@Test
public void shouldSendEventWhenProviderWasUpdated() {
    Type type = new TypeToken<TaskDataProvider>() {
    }.getType();
    List<TaskDataProviderObject> objects = new ArrayList<>();
    List<LookupFieldsParameter> lookupFields = asList(new LookupFieldsParameter("lookupField", asList("lookupField")));
    List<FieldParameter> fields = asList(new FieldParameter("displayName", "fieldKey"));
    objects.add(new TaskDataProviderObject("displayName", "type", lookupFields, fields));
    TaskDataProvider provider = new TaskDataProvider(PROVIDER_NAME, objects);
    List<TaskDataProviderObject> updatedObjects = new ArrayList<>(objects);
    updatedObjects.add(new TaskDataProviderObject("displayName2", "type2", lookupFields, fields));
    TaskDataProvider updatedProvider = new TaskDataProvider(PROVIDER_NAME, updatedObjects);
    when(motechJsonReader.readFromStream(inputStream, type)).thenReturn(updatedProvider);
    when(dataProviderDataService.findByName(PROVIDER_NAME)).thenReturn(provider);
    ArgumentCaptor<MotechEvent> captor = ArgumentCaptor.forClass(MotechEvent.class);
    taskDataProviderService.registerProvider(inputStream);
    ArgumentCaptor<TaskDataProvider> taskDataProviderArgumentCaptor = ArgumentCaptor.forClass(TaskDataProvider.class);
    verify(dataProviderDataService).update(taskDataProviderArgumentCaptor.capture());
    assertEquals(PROVIDER_NAME, taskDataProviderArgumentCaptor.getValue().getName());
    verify(dataProviderDataService).update(provider);
    verify(eventRelay).sendEventMessage(captor.capture());
    MotechEvent event = captor.getValue();
    assertEquals(DATA_PROVIDER_UPDATE_SUBJECT, event.getSubject());
    assertEquals(PROVIDER_NAME, event.getParameters().get(DATA_PROVIDER_NAME));
}
Also used : ArrayList(java.util.ArrayList) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) Type(java.lang.reflect.Type) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) FieldParameter(org.motechproject.tasks.domain.mds.task.FieldParameter) MotechEvent(org.motechproject.event.MotechEvent) Test(org.junit.Test)

Example 18 with TaskDataProvider

use of org.motechproject.tasks.domain.mds.task.TaskDataProvider in project motech by motech.

the class TaskDataProviderServiceImplTest method shouldNotRegisterProviderWhenProviderObjectsIsEmptyWithoutException.

@Test
public void shouldNotRegisterProviderWhenProviderObjectsIsEmptyWithoutException() {
    Type type = new TypeToken<TaskDataProvider>() {
    }.getType();
    TaskDataProvider provider = new TaskDataProvider();
    when(motechJsonReader.readFromStream(inputStream, type)).thenReturn(provider);
    taskDataProviderService.registerProvider(inputStream);
    verify(dataProviderDataService, never()).create(provider);
}
Also used : TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) Type(java.lang.reflect.Type) Test(org.junit.Test)

Example 19 with TaskDataProvider

use of org.motechproject.tasks.domain.mds.task.TaskDataProvider in project motech by motech.

the class TaskServiceImplTest method shouldValidateTasksAfterChannelUpdateForValidTaskDataProviders.

@Test
public void shouldValidateTasksAfterChannelUpdateForValidTaskDataProviders() {
    TaskConfig config = new TaskConfig().add(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true));
    Task task = new Task("name", trigger, asList(action), config, true, false);
    Set<TaskError> existingErrors = new HashSet<>();
    existingErrors.add(new TaskError("task.validation.error.providerObjectLookupNotExist"));
    task.addValidationErrors(existingErrors);
    TaskDataProvider provider = new TaskDataProvider("TestProvider", asList(new TaskDataProviderObject("test", "Test", null, null)));
    provider.setId(1234L);
    LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();
    hashMap.put("displayName", "id");
    ArrayList<String> list = new ArrayList<>();
    list.add("id");
    hashMap.put("fields", list);
    provider.getObjects().get(0).setLookupFields(asList((Object) hashMap));
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SEND", "", asList(new EventParameter("test", "value")), "")), null);
    Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(new ActionEventBuilder().setDisplayName("receive").setSubject("RECEIVE").setDescription("").setActionParameters(null).build()));
    when(tasksDataService.retrieveAll()).thenReturn(asList(task));
    when(providerService.getProvider(provider.getName())).thenReturn(provider);
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    taskService.validateTasksAfterTaskDataProviderUpdate(getProviderUpdateEvent(provider.getName()));
    Task actualTask = verifyCreateAndCaptureTask();
    assertTrue(task.isEnabled());
    assertTrue(actualTask.getValidationErrors().isEmpty());
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) TaskError(org.motechproject.tasks.domain.mds.task.TaskError) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) LinkedHashMap(java.util.LinkedHashMap) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with TaskDataProvider

use of org.motechproject.tasks.domain.mds.task.TaskDataProvider in project motech by motech.

the class TaskServiceImplTest method shouldValidateTasksAfterChannelUpdateForInvalidTaskDataProviders.

@Test
public void shouldValidateTasksAfterChannelUpdateForInvalidTaskDataProviders() {
    TaskConfig config = new TaskConfig().add(new DataSource("TestProvider", 1234L, 1L, "Test", "id", "specifiedName", asList(new Lookup("id", "trigger.value")), true));
    Task task = new Task("name", trigger, asList(action), config, true, false);
    task.setId(12345l);
    List<LookupFieldsParameter> lookupFields = new ArrayList<>();
    lookupFields.add(new LookupFieldsParameter("property", asList("property")));
    TaskDataProvider provider = new TaskDataProvider("TestProvider", asList(new TaskDataProviderObject("test", "Test", lookupFields, null)));
    provider.setId(1234L);
    Channel triggerChannel = new Channel("test", "test-trigger", "0.15", "", asList(new TriggerEvent("send", "SEND", "", asList(new EventParameter("test", "value")), "")), null);
    Channel actionChannel = new Channel("test", "test-action", "0.14", "", null, asList(new ActionEventBuilder().setDisplayName("receive").setSubject("RECEIVE").setDescription("").setActionParameters(null).build()));
    when(tasksDataService.findById(12345l)).thenReturn(task);
    when(tasksDataService.retrieveAll()).thenReturn(asList(task));
    when(providerService.getProvider(provider.getName())).thenReturn(provider);
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    taskService.validateTasksAfterTaskDataProviderUpdate(getProviderUpdateEvent(provider.getName()));
    Task actualTask = verifyUpdateAndCaptureTask();
    assertFalse(actualTask.isEnabled());
    List<Object> errors = new ArrayList<>(actualTask.getValidationErrors());
    assertEquals(1, errors.size());
    assertThat(errors, hasItem(hasProperty("message", equalTo("task.validation.error.providerObjectLookupNotExist"))));
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ArrayList(java.util.ArrayList) TaskConfig(org.motechproject.tasks.domain.mds.task.TaskConfig) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) DataSource(org.motechproject.tasks.domain.mds.task.DataSource) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) Test(org.junit.Test)

Aggregations

TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)28 Test (org.junit.Test)20 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)14 LookupFieldsParameter (org.motechproject.tasks.domain.mds.task.LookupFieldsParameter)9 ArrayList (java.util.ArrayList)8 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)8 Task (org.motechproject.tasks.domain.mds.task.Task)8 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)7 TaskError (org.motechproject.tasks.domain.mds.task.TaskError)7 Type (java.lang.reflect.Type)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Channel (org.motechproject.tasks.domain.mds.channel.Channel)6 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)6 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)6 Lookup (org.motechproject.tasks.domain.mds.task.Lookup)6 FieldParameter (org.motechproject.tasks.domain.mds.task.FieldParameter)5 TaskConfig (org.motechproject.tasks.domain.mds.task.TaskConfig)5 LinkedHashMap (java.util.LinkedHashMap)3 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)3