Search in sources :

Example 1 with MotechJsonReader

use of org.motechproject.commons.api.json.MotechJsonReader 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 2 with MotechJsonReader

use of org.motechproject.commons.api.json.MotechJsonReader 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 3 with MotechJsonReader

use of org.motechproject.commons.api.json.MotechJsonReader in project motech by motech.

the class ActionEventDeserializerTest method shouldDeserializeActionParameters.

@Test
public void shouldDeserializeActionParameters() throws IOException {
    String json = "{\"displayName\":\"externalId\",\"type\":\"UNICODE\",\"key\":\"ExternalId\"}";
    TypeToken<ActionParameterRequest> typeToken = new TypeToken<ActionParameterRequest>() {
    };
    ActionParameterRequest actionParameterRequest = (ActionParameterRequest) new MotechJsonReader().readFromString(json, typeToken.getType());
    assertNotNull(actionParameterRequest);
    assertThat(actionParameterRequest.getDisplayName(), is("externalId"));
    assertThat(actionParameterRequest.getOrder(), nullValue());
}
Also used : MotechJsonReader(org.motechproject.commons.api.json.MotechJsonReader) TypeToken(com.google.gson.reflect.TypeToken) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest) Test(org.junit.Test)

Example 4 with MotechJsonReader

use of org.motechproject.commons.api.json.MotechJsonReader in project motech by motech.

the class ActionEventDeserializerTest method shouldDeserializeActionEvent.

@Test
public void shouldDeserializeActionEvent() throws IOException {
    String json = "{description=\"description\", displayName=\"displayName\", subject=\"subject\", serviceInterface=\"serviceInterface\", serviceMethod=\"serviceMethod\"," + "actionParameters=[{\"displayName\":\"externalId\",\"type\":\"UNICODE\",\"key\":\"ExternalId\",\"order\":3},{\"displayName\":\"motechId\",\"type\":\"INTEGER\",\"key\":\"MotechId\"}]," + "postActionParameters=[{\"displayName\":\"externalId\",\"key\":\"ExternalId\"}]}";
    TypeToken<ActionEventRequest> typeToken = new TypeToken<ActionEventRequest>() {
    };
    Map<Type, Object> typeAdapters = new HashMap<>();
    typeAdapters.put(ActionEventRequest.class, new ActionEventRequestDeserializer());
    ActionEventRequest actionEventRequest = (ActionEventRequest) new MotechJsonReader().readFromString(json, typeToken.getType(), typeAdapters);
    assertNotNull(actionEventRequest);
    assertThat(actionEventRequest.getDisplayName(), is("displayName"));
    assertThat(actionEventRequest.getActionParameters().size(), is(2));
    assertThat(actionEventRequest.getActionParameters().iterator().next().getOrder(), is(3));
    assertThat(actionEventRequest.getPostActionParameters().size(), is(1));
}
Also used : MotechJsonReader(org.motechproject.commons.api.json.MotechJsonReader) ParameterType(org.motechproject.tasks.domain.enums.ParameterType) Type(java.lang.reflect.Type) HashMap(java.util.HashMap) TypeToken(com.google.gson.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 MotechJsonReader (org.motechproject.commons.api.json.MotechJsonReader)4 Type (java.lang.reflect.Type)3 HashMap (java.util.HashMap)3 TypeToken (com.google.gson.reflect.TypeToken)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 JsonObject (com.google.gson.JsonObject)1 List (java.util.List)1 ActionEventRequest (org.motechproject.tasks.contract.ActionEventRequest)1 ActionParameterRequest (org.motechproject.tasks.contract.ActionParameterRequest)1 ChannelRequest (org.motechproject.tasks.contract.ChannelRequest)1 TriggerEventRequest (org.motechproject.tasks.contract.TriggerEventRequest)1 ParameterType (org.motechproject.tasks.domain.enums.ParameterType)1