Search in sources :

Example 1 with FieldParameter

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

the class TaskDataProviderServiceImplTest method shouldRegisterProviderFromInputStream.

@Test
public void shouldRegisterProviderFromInputStream() {
    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);
    when(motechJsonReader.readFromStream(inputStream, type)).thenReturn(provider);
    taskDataProviderService.registerProvider(inputStream);
    verify(motechJsonReader).readFromStream(inputStream, type);
    verify(dataProviderDataService).create(provider);
}
Also used : 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) ArrayList(java.util.ArrayList) FieldParameter(org.motechproject.tasks.domain.mds.task.FieldParameter) Test(org.junit.Test)

Example 2 with FieldParameter

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

the class TaskServiceImplTest method shouldImportTask.

@Test
public void shouldImportTask() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    action.getValues().put("phone", "{{ad.providerName.Test#1.id}}");
    List<String> filtersManipulations = new ArrayList<>();
    Task given = new TaskBuilder().withName("test").withTrigger(trigger).addAction(action).addDataSource(new DataSource("providerName", 1234L, 1L, "Test", "id", asList(new Lookup("id", "trigger.value")), true)).addFilterSet(new FilterSet(asList(new Filter("displayName", "ad.providerName.Test#1.id", UNICODE, true, OperatorType.EXIST.getValue(), "", filtersManipulations)))).isEnabled(true).build();
    TaskDataProvider provider = new TaskDataProvider();
    provider.setName("providerName");
    provider.setId(56789L);
    provider.setObjects(asList(new TaskDataProviderObject("display", "Test", asList(new LookupFieldsParameter("id", asList("id"))), asList(new FieldParameter("display", "id", UNICODE)))));
    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));
    when(providerService.getProviders()).thenReturn(asList(provider));
    when(channelService.getChannel(trigger.getModuleName())).thenReturn(triggerChannel);
    when(channelService.getChannel(action.getModuleName())).thenReturn(actionChannel);
    when(triggerEventService.triggerExists(given.getTrigger())).thenReturn(true);
    when(providerService.getProvider("providerName")).thenReturn(provider);
    String json = mapper.writeValueAsString(given);
    taskService.importTask(json);
    action.getValues().put("phone", "{{ad.providerName.Test#1.id}}");
    Task expected = new TaskBuilder().withName("test").withTrigger(trigger).addAction(action).addDataSource(new DataSource("providerName", 56789L, 1L, "Test", "id", asList(new Lookup("id", "trigger.value")), true)).addFilterSet(new FilterSet(asList(new Filter("displayName", "ad.providerName.Test#1.id", UNICODE, true, OperatorType.EXIST.getValue(), "", filtersManipulations)))).isEnabled(true).build();
    Task actual = verifyCreateAndCaptureTask();
    assertEquals(expected, actual);
    verify(taskMigrationManager).migrateTask(actual);
}
Also used : TaskBuilder(org.motechproject.tasks.domain.mds.task.builder.TaskBuilder) Task(org.motechproject.tasks.domain.mds.task.Task) FilterSet(org.motechproject.tasks.domain.mds.task.FilterSet) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ArrayList(java.util.ArrayList) 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) ActionParameterBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder) Filter(org.motechproject.tasks.domain.mds.task.Filter) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) Lookup(org.motechproject.tasks.domain.mds.task.Lookup) FieldParameter(org.motechproject.tasks.domain.mds.task.FieldParameter) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 3 with FieldParameter

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

the class TaskDataProviderServiceImplTest method shouldUnregisterDataProvider.

@Test
public void shouldUnregisterDataProvider() {
    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(dataProviderDataService.findByName(PROVIDER_NAME)).thenReturn(provider);
    taskDataProviderService.unregister(PROVIDER_NAME);
    verify(dataProviderDataService).delete(provider);
}
Also used : TaskDataProviderObject(org.motechproject.tasks.domain.mds.task.TaskDataProviderObject) TaskDataProvider(org.motechproject.tasks.domain.mds.task.TaskDataProvider) LookupFieldsParameter(org.motechproject.tasks.domain.mds.task.LookupFieldsParameter) ArrayList(java.util.ArrayList) FieldParameter(org.motechproject.tasks.domain.mds.task.FieldParameter) Test(org.junit.Test)

Example 4 with FieldParameter

use of org.motechproject.tasks.domain.mds.task.FieldParameter 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 5 with FieldParameter

use of org.motechproject.tasks.domain.mds.task.FieldParameter 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)

Aggregations

ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 FieldParameter (org.motechproject.tasks.domain.mds.task.FieldParameter)5 LookupFieldsParameter (org.motechproject.tasks.domain.mds.task.LookupFieldsParameter)5 TaskDataProvider (org.motechproject.tasks.domain.mds.task.TaskDataProvider)5 TaskDataProviderObject (org.motechproject.tasks.domain.mds.task.TaskDataProviderObject)5 Type (java.lang.reflect.Type)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 MotechEvent (org.motechproject.event.MotechEvent)1 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)1 Channel (org.motechproject.tasks.domain.mds.channel.Channel)1 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)1 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)1 ActionEventBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder)1 ActionParameterBuilder (org.motechproject.tasks.domain.mds.channel.builder.ActionParameterBuilder)1 DataSource (org.motechproject.tasks.domain.mds.task.DataSource)1 Filter (org.motechproject.tasks.domain.mds.task.Filter)1