Search in sources :

Example 1 with TriggerEventRequest

use of org.motechproject.tasks.contract.TriggerEventRequest in project motech by motech.

the class ChannelRequestTest method shouldDeserializeTriggerTaskEventJson.

@Test
public void shouldDeserializeTriggerTaskEventJson() throws IOException {
    String triggerEvent = "[{ displayName: 'displayName', subject: 'subject', eventParameters: [{ displayName: 'displayName', eventKey: 'eventKey' }] }]";
    InputStream stream = new ByteArrayInputStream(triggerEvent.getBytes(Charset.forName("UTF-8")));
    Type type = new TypeToken<List<TriggerEventRequest>>() {
    }.getType();
    StringWriter writer = new StringWriter();
    IOUtils.copy(stream, writer);
    List<TriggerEventRequest> triggerEventRequest = (List<TriggerEventRequest>) new MotechJsonReader().readFromString(writer.toString(), type, new HashMap<Type, Object>());
    assertEquals(1, triggerEventRequest.size());
}
Also used : MotechJsonReader(org.motechproject.commons.api.json.MotechJsonReader) Type(java.lang.reflect.Type) StringWriter(java.io.StringWriter) TriggerEventRequest(org.motechproject.tasks.contract.TriggerEventRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) List(java.util.List) Test(org.junit.Test)

Example 2 with TriggerEventRequest

use of org.motechproject.tasks.contract.TriggerEventRequest in project motech by motech.

the class ChannelBuilder method fromChannelRequest.

/**
 * Creates a builder, which allows building Channels based on the given {@code channelRequest}.
 *
 * @param channelRequest  the channel request
 * @return the created builder
 */
public static ChannelBuilder fromChannelRequest(ChannelRequest channelRequest) {
    ChannelBuilder builder = new ChannelBuilder().setDisplayName(channelRequest.getDisplayName()).setModuleName(channelRequest.getModuleName()).setModuleVersion(channelRequest.getModuleVersion());
    List<TriggerEvent> triggerEvents = new ArrayList<>();
    for (TriggerEventRequest triggerEventRequest : channelRequest.getTriggerTaskEvents()) {
        triggerEvents.add(TriggerEventBuilder.fromTriggerEventRequest(triggerEventRequest).build());
    }
    builder.setTriggerTaskEvents(triggerEvents);
    List<ActionEvent> actionEvents = new ArrayList<>();
    for (ActionEventRequest actionEventRequest : channelRequest.getActionTaskEvents()) {
        actionEvents.add(ActionEventBuilder.fromActionEventRequest(actionEventRequest).build());
    }
    builder.setActionTaskEvents(actionEvents);
    return builder;
}
Also used : TriggerEventRequest(org.motechproject.tasks.contract.TriggerEventRequest) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) ArrayList(java.util.ArrayList) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest)

Example 3 with TriggerEventRequest

use of org.motechproject.tasks.contract.TriggerEventRequest in project motech by motech.

the class ChannelServiceImplTest method shouldRegisterChannelFromChannelRequest.

@Test
public void shouldRegisterChannelFromChannelRequest() {
    List<ActionEventRequest> actionEventRequests = asList(new TestActionEventRequestBuilder().setDisplayName("actionName").setSubject("subject.foo").setDescription("action description").setServiceInterface("some.interface").setServiceMethod("method").setActionParameters(new TreeSet<ActionParameterRequest>()).setPostActionParameters(new TreeSet<ActionParameterRequest>()).createActionEventRequest());
    List<TriggerEventRequest> triggerEventsRequest = asList(new TriggerEventRequest("displayName", "subject.foo", "description", asList(new EventParameterRequest("displayName", "eventKey"))));
    ChannelRequest channelRequest = new ChannelRequest(BUNDLE_SYMBOLIC_NAME, BUNDLE_SYMBOLIC_NAME, VERSION, "", triggerEventsRequest, actionEventRequests);
    channelService.registerChannel(channelRequest);
    ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
    verify(channelsDataService).create(captor.capture());
    Channel channelToBeCreated = captor.getValue();
    assertEquals(BUNDLE_SYMBOLIC_NAME, channelToBeCreated.getDisplayName());
    assertEquals(BUNDLE_SYMBOLIC_NAME, channelToBeCreated.getModuleName());
    assertEquals(VERSION, channelToBeCreated.getModuleVersion());
    assertEquals(1, channelToBeCreated.getTriggerTaskEvents().size());
    TriggerEvent expectedTrigger = new TriggerEvent("displayName", "subject.foo", "description", asList(new EventParameter("displayName", "eventKey")), "");
    TriggerEvent actualTrigger = channelToBeCreated.getTriggerTaskEvents().get(0);
    assertEquals(expectedTrigger, actualTrigger);
    assertEquals(1, channelToBeCreated.getActionTaskEvents().size());
    ActionEvent expectedAction = new ActionEventBuilder().setDisplayName("actionName").setSubject("subject.foo").setDescription("action description").setServiceInterface("some.interface").setServiceMethod("method").setActionParameters(new TreeSet<>()).setPostActionParameters(new TreeSet<>()).build();
    ActionEvent actualAction = channelToBeCreated.getActionTaskEvents().get(0);
    assertEquals(expectedAction, actualAction);
}
Also used : ChannelRequest(org.motechproject.tasks.contract.ChannelRequest) TriggerEvent(org.motechproject.tasks.domain.mds.channel.TriggerEvent) ActionEvent(org.motechproject.tasks.domain.mds.channel.ActionEvent) EventParameterRequest(org.motechproject.tasks.contract.EventParameterRequest) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ActionEventBuilder(org.motechproject.tasks.domain.mds.channel.builder.ActionEventBuilder) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) TestActionEventRequestBuilder(org.motechproject.tasks.contract.builder.TestActionEventRequestBuilder) EventParameter(org.motechproject.tasks.domain.mds.channel.EventParameter) TriggerEventRequest(org.motechproject.tasks.contract.TriggerEventRequest) TreeSet(java.util.TreeSet) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest) Test(org.junit.Test)

Aggregations

TriggerEventRequest (org.motechproject.tasks.contract.TriggerEventRequest)3 Test (org.junit.Test)2 ActionEventRequest (org.motechproject.tasks.contract.ActionEventRequest)2 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)2 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 MotechJsonReader (org.motechproject.commons.api.json.MotechJsonReader)1 ActionParameterRequest (org.motechproject.tasks.contract.ActionParameterRequest)1 ChannelRequest (org.motechproject.tasks.contract.ChannelRequest)1 EventParameterRequest (org.motechproject.tasks.contract.EventParameterRequest)1 TestActionEventRequestBuilder (org.motechproject.tasks.contract.builder.TestActionEventRequestBuilder)1 Channel (org.motechproject.tasks.domain.mds.channel.Channel)1 EventParameter (org.motechproject.tasks.domain.mds.channel.EventParameter)1