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());
}
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());
}
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());
}
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));
}
Aggregations