Search in sources :

Example 1 with ChannelRequest

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

the class ActionEventDeserializerParametrizedTest method shouldDeserializeJson.

@Test
public void shouldDeserializeJson() {
    ChannelRequest channelRequest = (ChannelRequest) reader.readFromString(channelAsString, type, typeAdapters);
    List<ActionEventRequest> actionTaskEvents = channelRequest.getActionTaskEvents();
    assertEquals(expected, actionTaskEvents);
}
Also used : ChannelRequest(org.motechproject.tasks.contract.ChannelRequest) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) Test(org.junit.Test)

Example 2 with ChannelRequest

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

the class ChannelRequestTest method shouldDeserializeChannelJson.

@Test
public void shouldDeserializeChannelJson() throws IOException {
    String triggerEvent = "{ displayName: 'displayName', subject: 'subject', eventParameters: [{ displayName: 'displayName', eventKey: 'eventKey' }] }";
    String channel = String.format("{displayName: %s, moduleName: %s, moduleVersion: %s, triggerTaskEvents: [%s]}", "foo", "bar", "1.0", triggerEvent);
    InputStream stream = new ByteArrayInputStream(channel.getBytes(Charset.forName("UTF-8")));
    Type type = new TypeToken<ChannelRequest>() {
    }.getType();
    StringWriter writer = new StringWriter();
    IOUtils.copy(stream, writer);
    Map<Type, Object> typeAdapters = new HashMap<>();
    typeAdapters.put(ActionEventRequest.class, new ActionEventRequestDeserializer());
    ChannelRequest channelRequest = (ChannelRequest) new MotechJsonReader().readFromString(writer.toString(), type, typeAdapters);
    assertEquals(1, channelRequest.getTriggerTaskEvents().size());
}
Also used : MotechJsonReader(org.motechproject.commons.api.json.MotechJsonReader) Type(java.lang.reflect.Type) ChannelRequest(org.motechproject.tasks.contract.ChannelRequest) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 3 with ChannelRequest

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

the class ActionParametersBundleIT method loadChannel.

private Channel loadChannel() throws IOException {
    Type type = new TypeToken<ChannelRequest>() {
    }.getType();
    HashMap<Type, Object> typeAdapters = new HashMap<>();
    typeAdapters.put(ActionEventRequest.class, new ActionEventRequestDeserializer());
    StringWriter writer = new StringWriter();
    try (InputStream stream = getClass().getResourceAsStream("/task-testmodule-channel.json")) {
        IOUtils.copy(stream, writer);
    }
    ChannelRequest channelRequest = (ChannelRequest) motechJsonReader.readFromString(writer.toString(), type, typeAdapters);
    channelRequest.setModuleName(TASK_TEST_CHANNEL_NAME);
    channelRequest.setModuleVersion("0.29.0.SNAPSHOT");
    return ChannelBuilder.fromChannelRequest(channelRequest).build();
}
Also used : Type(java.lang.reflect.Type) TaskActivityType(org.motechproject.tasks.domain.enums.TaskActivityType) ChannelRequest(org.motechproject.tasks.contract.ChannelRequest) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ActionEventRequestDeserializer(org.motechproject.tasks.contract.json.ActionEventRequestDeserializer) TaskTestObject(org.motechproject.testmodule.domain.TaskTestObject)

Example 4 with ChannelRequest

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

the class ChannelServiceImpl method registerChannel.

@Override
@Transactional
public void registerChannel(final InputStream stream, String moduleName, String moduleVersion) {
    LOGGER.info("Registering channel: {}", moduleName);
    Type type = new TypeToken<ChannelRequest>() {
    }.getType();
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(stream, writer);
        ChannelRequest channelRequest = (ChannelRequest) motechJsonReader.readFromString(writer.toString(), type, typeAdapters);
        channelRequest.setModuleName(moduleName);
        channelRequest.setModuleVersion(moduleVersion);
        registerChannel(channelRequest);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : Type(java.lang.reflect.Type) ChannelRequest(org.motechproject.tasks.contract.ChannelRequest) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ChannelRequest

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

the class ChannelsDataServiceBundleIT method loadChannels.

private List<Channel> loadChannels() throws IOException {
    Type type = new TypeToken<ChannelRequest>() {
    }.getType();
    HashMap<Type, Object> typeAdapters = new HashMap<>();
    typeAdapters.put(ActionEventRequest.class, new ActionEventRequestDeserializer());
    List<StringWriter> writers = new ArrayList<>(2);
    for (String json : Arrays.asList("/message-campaign-test-channel.json", "/pillreminder-test-channel.json")) {
        try (InputStream stream = getClass().getResourceAsStream(json)) {
            StringWriter writer = new StringWriter();
            IOUtils.copy(stream, writer);
            writers.add(writer);
        }
    }
    List<Channel> channelRequests = new ArrayList<>(2);
    for (StringWriter writer : writers) {
        ChannelRequest channelRequest = (ChannelRequest) motechJsonReader.readFromString(writer.toString(), type, typeAdapters);
        channelRequest.setModuleName(channelRequest.getDisplayName());
        channelRequest.setModuleVersion("1.0");
        channelRequests.add(ChannelBuilder.fromChannelRequest(channelRequest).build());
    }
    return channelRequests;
}
Also used : ChannelRequest(org.motechproject.tasks.contract.ChannelRequest) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Channel(org.motechproject.tasks.domain.mds.channel.Channel) ArrayList(java.util.ArrayList) Type(java.lang.reflect.Type) StringWriter(java.io.StringWriter) ActionEventRequestDeserializer(org.motechproject.tasks.contract.json.ActionEventRequestDeserializer)

Aggregations

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