Search in sources :

Example 6 with ActionEventRequest

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

the class ActionEventRequestDeserializer method deserialize.

@Override
public ActionEventRequest deserialize(JsonElement element, Type type, JsonDeserializationContext context) {
    ActionEventRequest actionEvent = null;
    if (element.isJsonObject()) {
        JsonObject jsonObject = element.getAsJsonObject();
        actionEvent = new ActionEventRequestBuilder().setDisplayName(getValue(jsonObject, DISPLAY_NAME_FIELD)).setSubject(getValue(jsonObject, SUBJECT_FIELD)).setDescription(getValue(jsonObject, DESCRIPTION_FIELD)).setServiceInterface(getValue(jsonObject, SERVICE_INTERFACE_FIELD)).setServiceMethod(getValue(jsonObject, SERVICE_METHOD_FIELD)).setServiceMethodCallManner(getValue(jsonObject, SERVICE_METHOD_CALL_MANNER_FIELD)).setName(getValue(jsonObject, NAME_FIELD)).createActionEventRequest();
        if (jsonObject.has(ACTION_PARAMETERS_FIELD)) {
            JsonArray jsonArray = jsonObject.getAsJsonArray(ACTION_PARAMETERS_FIELD);
            for (int i = 0; i < jsonArray.size(); ++i) {
                ActionParameterRequest parameter = context.deserialize(jsonArray.get(i), ActionParameterRequest.class);
                boolean changeOrder = parameter.getOrder() == null;
                actionEvent.addParameter(parameter, changeOrder);
            }
        }
        if (jsonObject.has(POST_ACTION_PARAMETERS_FIELD)) {
            JsonArray jsonArray = jsonObject.getAsJsonArray(POST_ACTION_PARAMETERS_FIELD);
            for (int i = 0; i < jsonArray.size(); ++i) {
                ActionParameterRequest parameter = context.deserialize(jsonArray.get(i), ActionParameterRequest.class);
                boolean changeOrder = parameter.getOrder() == null;
                actionEvent.addPostActionParameter(parameter, changeOrder);
            }
        }
        if (!actionEvent.isValid()) {
            throw new JsonParseException("Channel action must contain subject and/or serviceInterface and serviceMethod");
        }
    }
    return actionEvent;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest) JsonParseException(com.google.gson.JsonParseException) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) ActionEventRequestBuilder(org.motechproject.tasks.contract.builder.ActionEventRequestBuilder)

Example 7 with ActionEventRequest

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

the class ActionEventDeserializerTest method shouldDeserializeJsonWithSubject.

@Test
public void shouldDeserializeJsonWithSubject() {
    ActionEventRequest expected = getExpected(true, false);
    JsonObject object = createJsonObject(expected);
    ActionEventRequest actual = deserializer.deserialize(object, null, null);
    assertEquals(expected, actual);
}
Also used : JsonObject(com.google.gson.JsonObject) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) Test(org.junit.Test)

Example 8 with ActionEventRequest

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

the class ActionEventDeserializerTest method shouldNotSetEmptyActionParameterSet.

@Test
public void shouldNotSetEmptyActionParameterSet() {
    ActionEventRequest expected = getExpected(false, true);
    JsonObject object = createJsonObject(expected);
    object.add(ACTION_PARAMETERS_FIELD, new JsonArray());
    ActionEventRequest actual = deserializer.deserialize(object, null, null);
    assertEquals(expected, actual);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) Test(org.junit.Test)

Example 9 with ActionEventRequest

use of org.motechproject.tasks.contract.ActionEventRequest 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 10 with ActionEventRequest

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

the class ActionEventDeserializerTest method shouldDeserializeJsonWithServiceInfo.

@Test
public void shouldDeserializeJsonWithServiceInfo() {
    ActionEventRequest expected = getExpected(false, true);
    JsonObject object = createJsonObject(expected);
    ActionEventRequest actual = deserializer.deserialize(object, null, null);
    assertEquals(expected, actual);
}
Also used : JsonObject(com.google.gson.JsonObject) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) Test(org.junit.Test)

Aggregations

ActionEventRequest (org.motechproject.tasks.contract.ActionEventRequest)14 Test (org.junit.Test)10 JsonObject (com.google.gson.JsonObject)6 ActionParameterRequest (org.motechproject.tasks.contract.ActionParameterRequest)5 TestActionEventRequestBuilder (org.motechproject.tasks.contract.builder.TestActionEventRequestBuilder)4 TreeSet (java.util.TreeSet)3 ActionParameterRequestBuilder (org.motechproject.tasks.contract.builder.ActionParameterRequestBuilder)3 JsonArray (com.google.gson.JsonArray)2 ArrayList (java.util.ArrayList)2 ChannelRequest (org.motechproject.tasks.contract.ChannelRequest)2 TriggerEventRequest (org.motechproject.tasks.contract.TriggerEventRequest)2 ActionEvent (org.motechproject.tasks.domain.mds.channel.ActionEvent)2 TriggerEvent (org.motechproject.tasks.domain.mds.channel.TriggerEvent)2 JsonParseException (com.google.gson.JsonParseException)1 TypeToken (com.google.gson.reflect.TypeToken)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 MotechJsonReader (org.motechproject.commons.api.json.MotechJsonReader)1 EventParameterRequest (org.motechproject.tasks.contract.EventParameterRequest)1 ActionEventRequestBuilder (org.motechproject.tasks.contract.builder.ActionEventRequestBuilder)1