Search in sources :

Example 1 with ActionParameterRequest

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

the class ActionEventDeserializerParametrizedTest method getPillReminderEvents.

private static List<ActionEventRequest> getPillReminderEvents() {
    SortedSet<ActionParameterRequest> parameters = new TreeSet<>();
    parameters.add(new ActionParameterRequestBuilder().setKey("DosageID").setDisplayName("pillreminder.dossageID").setOrder(0).createActionParameterRequest());
    parameters.add(new ActionParameterRequestBuilder().setKey("ExternalID").setDisplayName("pillreminder.externalID").setOrder(1).createActionParameterRequest());
    parameters.add(new ActionParameterRequestBuilder().setKey("times-reminders-sent").setDisplayName("pillreminder.times.sent").setOrder(2).setType("INTEGER").createActionParameterRequest());
    parameters.add(new ActionParameterRequestBuilder().setKey("times-reminders-to-be-sent").setDisplayName("pillreminder.total.times.sent").setOrder(3).setType("INTEGER").createActionParameterRequest());
    parameters.add(new ActionParameterRequestBuilder().setKey("retry-interval").setDisplayName("pillreminder.retry.interval").setOrder(4).setType("INTEGER").createActionParameterRequest());
    ActionEventRequest event = new TestActionEventRequestBuilder().setDisplayName("pillreminder.event.subject.scheduler").setSubject("org.motechproject.server.pillreminder.scheduler-reminder").setDescription("description").setServiceInterface(null).setServiceMethod(null).setActionParameters(parameters).createActionEventRequest();
    List<ActionEventRequest> events = new ArrayList<>();
    events.add(event);
    return events;
}
Also used : TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) ActionParameterRequestBuilder(org.motechproject.tasks.contract.builder.ActionParameterRequestBuilder) TestActionEventRequestBuilder(org.motechproject.tasks.contract.builder.TestActionEventRequestBuilder)

Example 2 with ActionParameterRequest

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

the class ActionEventDeserializerParametrizedTest method getMessageCampaignEvents.

private static List<ActionEventRequest> getMessageCampaignEvents() {
    SortedSet<ActionParameterRequest> parameters1 = new TreeSet<>();
    parameters1.add(new ActionParameterRequestBuilder().setKey("CampaignName").setDisplayName("msgCampaign.campaign.name").setOrder(0).createActionParameterRequest());
    parameters1.add(new ActionParameterRequestBuilder().setKey("ExternalID").setDisplayName("msgCampaign.externalID").setOrder(1).createActionParameterRequest());
    parameters1.add(new ActionParameterRequestBuilder().setKey("MessageKey").setDisplayName("msgCampaign.message.key").setOrder(2).createActionParameterRequest());
    ActionEventRequest event1 = new TestActionEventRequestBuilder().setDisplayName("msgCampaign.send.message").setSubject("org.motechproject.messagecampaign.fired-campaign-message").setDescription("description").setServiceInterface(null).setServiceMethod(null).setActionParameters(parameters1).createActionEventRequest();
    SortedSet<ActionParameterRequest> parameters2 = new TreeSet<>();
    parameters2.add(new ActionParameterRequestBuilder().setKey("ExternalID").setDisplayName("msgCampaign.externalID").setOrder(0).createActionParameterRequest());
    parameters2.add(new ActionParameterRequestBuilder().setKey("CampaignName").setDisplayName("msgCampaign.campaign.name").setOrder(1).createActionParameterRequest());
    ActionEventRequest event2 = new TestActionEventRequestBuilder().setDisplayName("msgCampaign.campaign.completed").setSubject("org.motechproject.messagecampaign.campaign-completed").setDescription("description").setServiceInterface("org.motechproject.messagecampaign.service.MessageCampaignService").setServiceMethod("campaignCompleted").setActionParameters(parameters2).createActionEventRequest();
    return asList(event1, event2);
}
Also used : TreeSet(java.util.TreeSet) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) ActionParameterRequestBuilder(org.motechproject.tasks.contract.builder.ActionParameterRequestBuilder) TestActionEventRequestBuilder(org.motechproject.tasks.contract.builder.TestActionEventRequestBuilder)

Example 3 with ActionParameterRequest

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

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

the class ActionEventDeserializerTest method createJsonObject.

private JsonObject createJsonObject(ActionEventRequest actionEventRequest) {
    JsonObject object = new JsonObject();
    object.addProperty(DESCRIPTION_FIELD, actionEventRequest.getDescription());
    object.addProperty(DISPLAY_NAME_FIELD, actionEventRequest.getDisplayName());
    if (actionEventRequest.hasSubject()) {
        object.addProperty(SUBJECT_FIELD, actionEventRequest.getSubject());
    }
    if (actionEventRequest.hasService()) {
        object.addProperty(SERVICE_INTERFACE_FIELD, actionEventRequest.getServiceInterface());
        object.addProperty(SERVICE_METHOD_FIELD, actionEventRequest.getServiceMethod());
    }
    SortedSet<ActionParameterRequest> parameters = actionEventRequest.getActionParameters();
    if (parameters != null && !parameters.isEmpty()) {
        JsonArray array = new JsonArray();
        for (ActionParameterRequest parameter : parameters) {
            array.add(createParameter(parameter));
        }
        object.add(ACTION_PARAMETERS_FIELD, array);
    }
    return object;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest)

Example 5 with ActionParameterRequest

use of org.motechproject.tasks.contract.ActionParameterRequest 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)

Aggregations

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