Search in sources :

Example 1 with BridgeObjectMapper

use of org.sagebionetworks.bridge.json.BridgeObjectMapper in project BridgeServer2 by Sage-Bionetworks.

the class ActivityTest method olderPublishedActivitiesCanBeDeserialized.

@Test
public void olderPublishedActivitiesCanBeDeserialized() throws Exception {
    String oldJson = "{\"label\":\"Personal Health Survey\",\"ref\":\"https://webservices-staging.sagebridge.org/api/v2/surveys/ac1e57fd-5e8e-473f-b82f-bac7547b6783/revisions/published\",\"activityType\":\"survey\",\"survey\":{\"guid\":\"ac1e57fd-5e8e-473f-b82f-bac7547b6783\",\"identifier\":\"identifier\",\"type\":\"GuidCreatedOnVersionHolder\"},\"type\":\"Activity\"}";
    BridgeObjectMapper mapper = BridgeObjectMapper.get();
    Activity activity = mapper.readValue(oldJson, Activity.class);
    assertEquals(activity.getLabel(), "Personal Health Survey");
    assertEquals(activity.getActivityType(), ActivityType.SURVEY);
    SurveyReference ref = activity.getSurvey();
    assertEquals(ref.getIdentifier(), "identifier");
    assertNull(ref.getCreatedOn(), "createdOn null");
    assertEquals(ref.getGuid(), "ac1e57fd-5e8e-473f-b82f-bac7547b6783", "guid set");
    assertTrue(ref.getHref().matches("http[s]?://.*/v3/surveys/ac1e57fd-5e8e-473f-b82f-bac7547b6783/revisions/published"));
}
Also used : BridgeObjectMapper(org.sagebionetworks.bridge.json.BridgeObjectMapper) Test(org.testng.annotations.Test)

Example 2 with BridgeObjectMapper

use of org.sagebionetworks.bridge.json.BridgeObjectMapper in project BridgeServer2 by Sage-Bionetworks.

the class CriteriaScheduleStrategyTest method canSerialize.

@Test
public void canSerialize() throws Exception {
    BridgeObjectMapper mapper = BridgeObjectMapper.get();
    setUpStrategyWithAppVersions();
    setUpStrategyWithRequiredAndProhibitedSets();
    String json = mapper.writeValueAsString(strategy);
    JsonNode node = mapper.readTree(json);
    assertEquals(node.get("type").asText(), "CriteriaScheduleStrategy");
    assertNotNull(node.get("scheduleCriteria"));
    ArrayNode array = (ArrayNode) node.get("scheduleCriteria");
    JsonNode schCriteria1 = array.get(0);
    assertEquals(schCriteria1.get("type").asText(), "ScheduleCriteria");
    JsonNode criteriaNode = schCriteria1.get("criteria");
    JsonNode minVersionsNode = criteriaNode.get("minAppVersions");
    assertEquals(minVersionsNode.get(OperatingSystem.IOS).asInt(), 4);
    JsonNode maxVersionsNode = criteriaNode.get("maxAppVersions");
    assertEquals(maxVersionsNode.get(OperatingSystem.IOS).asInt(), 12);
    assertNotNull(criteriaNode.get("allOfGroups"));
    assertNotNull(criteriaNode.get("noneOfGroups"));
    assertNotNull(schCriteria1.get("schedule"));
    JsonNode schCriteria2 = array.get(1);
    JsonNode criteriaNode2 = schCriteria2.get("criteria");
    Set<String> allOfGroups = arrayToSet(criteriaNode2.get("allOfGroups"));
    assertTrue(allOfGroups.contains("req1"));
    assertTrue(allOfGroups.contains("req2"));
    Set<String> noneOfGroups = arrayToSet(criteriaNode2.get("noneOfGroups"));
    assertTrue(noneOfGroups.contains("proh1"));
    assertTrue(noneOfGroups.contains("proh2"));
    Set<String> allOfStudyIds = arrayToSet(criteriaNode2.get("allOfStudyIds"));
    assertTrue(allOfStudyIds.contains("studyA"));
    assertTrue(allOfStudyIds.contains("studyB"));
    Set<String> noneOfStudyIds = arrayToSet(criteriaNode2.get("noneOfStudyIds"));
    assertTrue(noneOfStudyIds.contains("studyC"));
    // But mostly, if this isn't all serialized, and then deserialized, these won't be equal
    CriteriaScheduleStrategy newStrategy = mapper.readValue(json, CriteriaScheduleStrategy.class);
    assertEquals(newStrategy, strategy);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) BridgeObjectMapper(org.sagebionetworks.bridge.json.BridgeObjectMapper) Test(org.testng.annotations.Test)

Example 3 with BridgeObjectMapper

use of org.sagebionetworks.bridge.json.BridgeObjectMapper in project BridgeServer2 by Sage-Bionetworks.

the class ScheduleTest method canRountripSerialize.

@Test
public void canRountripSerialize() throws Exception {
    Activity activity = new Activity.Builder().withLabel("label").withTask("ref").build();
    Schedule schedule = new Schedule();
    schedule.getActivities().add(activity);
    schedule.setCronTrigger("0 0 8 ? * TUE *");
    schedule.setDelay(Period.parse("P1D"));
    schedule.setExpires(Period.parse("P2D"));
    schedule.setStartsOn(DateTime.parse("2015-02-02T10:10:10.000Z"));
    schedule.setEndsOn(DateTime.parse("2015-01-01T10:10:10.000Z"));
    schedule.setEventId(Schedule.EVENT_ID_PROPERTY);
    schedule.setInterval(Period.parse("P3D"));
    schedule.setSequencePeriod(Period.parse("P3W"));
    schedule.setLabel(Schedule.LABEL_PROPERTY);
    schedule.setScheduleType(ScheduleType.RECURRING);
    schedule.setTimes(Lists.newArrayList(LocalTime.parse("10:10"), LocalTime.parse("14:00")));
    BridgeObjectMapper mapper = BridgeObjectMapper.get();
    String string = mapper.writeValueAsString(schedule);
    JsonNode node = mapper.readTree(string);
    assertEquals(node.get(Schedule.LABEL_PROPERTY).textValue(), "label");
    assertEquals(node.get(Schedule.SCHEDULE_TYPE_PROPERTY).textValue(), "recurring");
    assertEquals(node.get(Schedule.EVENT_ID_PROPERTY).textValue(), "eventId");
    assertEquals(node.get(Schedule.CRON_TRIGGER_PROPERTY).textValue(), "0 0 8 ? * TUE *");
    assertEquals(node.get(Schedule.DELAY_PROPERTY).textValue(), "P1D");
    assertEquals(node.get(Schedule.INTERVAL_PROPERTY).textValue(), "P3D");
    assertEquals(node.get(Schedule.EXPIRES_PROPERTY).textValue(), "P2D");
    assertEquals(node.get(Schedule.SEQUENCE_PERIOD_PROPERTY).textValue(), "P3W");
    assertEquals(node.get(Schedule.STARTS_ON_PROPERTY).textValue(), "2015-02-02T10:10:10.000Z");
    assertEquals(node.get(Schedule.ENDS_ON_PROPERTY).textValue(), "2015-01-01T10:10:10.000Z");
    assertFalse(node.get(Schedule.PERSISTENT_PROPERTY).booleanValue());
    assertEquals(node.get("type").textValue(), "Schedule");
    ArrayNode times = (ArrayNode) node.get("times");
    assertEquals(times.get(0).textValue(), "10:10:00.000");
    assertEquals(times.get(1).textValue(), "14:00:00.000");
    JsonNode actNode = node.get("activities").get(0);
    assertEquals(actNode.get("label").textValue(), "label");
    assertEquals(actNode.get("activityType").textValue(), "task");
    assertEquals(actNode.get("type").textValue(), "Activity");
    JsonNode taskNode = actNode.get("task");
    assertEquals(taskNode.get("identifier").textValue(), "ref");
    assertEquals(taskNode.get("type").textValue(), "TaskReference");
    schedule = mapper.readValue(string, Schedule.class);
    assertEquals(schedule.getCronTrigger(), "0 0 8 ? * TUE *");
    assertEquals(schedule.getDelay().toString(), "P1D");
    assertEquals(schedule.getExpires().toString(), "P2D");
    assertEquals(schedule.getEventId(), "eventId");
    assertEquals(schedule.getLabel(), "label");
    assertEquals(schedule.getInterval().toString(), "P3D");
    assertEquals(schedule.getScheduleType(), ScheduleType.RECURRING);
    assertEquals(schedule.getStartsOn().toString(), "2015-02-02T10:10:10.000Z");
    assertEquals(schedule.getEndsOn().toString(), "2015-01-01T10:10:10.000Z");
    assertEquals(schedule.getTimes().get(0).toString(), "10:10:00.000");
    assertEquals(schedule.getTimes().get(1).toString(), "14:00:00.000");
    assertEquals(schedule.getSequencePeriod().toString(), "P3W");
    activity = schedule.getActivities().get(0);
    assertEquals(activity.getLabel(), "label");
    assertEquals(activity.getTask().getIdentifier(), "ref");
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) BridgeObjectMapper(org.sagebionetworks.bridge.json.BridgeObjectMapper) Test(org.testng.annotations.Test)

Example 4 with BridgeObjectMapper

use of org.sagebionetworks.bridge.json.BridgeObjectMapper in project BridgeServer2 by Sage-Bionetworks.

the class DynamoScheduledActivityTest method canRoundtripSerialize.

@Test
public void canRoundtripSerialize() throws Exception {
    LocalDateTime scheduledOn = LocalDateTime.now().plusWeeks(1);
    LocalDateTime expiresOn = LocalDateTime.now().plusWeeks(1);
    String scheduledOnString = scheduledOn.toDateTime(DateTimeZone.UTC).toString();
    String expiresOnString = expiresOn.toDateTime(DateTimeZone.UTC).toString();
    DynamoScheduledActivity schActivity = new DynamoScheduledActivity();
    schActivity.setTimeZone(DateTimeZone.UTC);
    schActivity.setActivity(TestUtils.getActivity3());
    schActivity.setLocalScheduledOn(scheduledOn);
    schActivity.setLocalExpiresOn(expiresOn);
    schActivity.setGuid("AAA-BBB-CCC");
    schActivity.setHealthCode("FFF-GGG-HHH");
    schActivity.setPersistent(true);
    schActivity.setReferentGuid("referentGuid");
    schActivity.setClientData(TestUtils.getClientData());
    BridgeObjectMapper mapper = BridgeObjectMapper.get();
    String output = ScheduledActivity.SCHEDULED_ACTIVITY_WRITER.writeValueAsString(schActivity);
    JsonNode node = mapper.readTree(output);
    assertEquals(node.get("guid").asText(), "AAA-BBB-CCC");
    assertEquals(node.get("scheduledOn").asText(), scheduledOnString);
    assertEquals(node.get("expiresOn").asText(), expiresOnString);
    assertEquals(node.get("status").asText(), "available");
    assertEquals(node.get("type").asText(), "ScheduledActivity");
    assertTrue(node.get("persistent").asBoolean());
    assertNull(node.get("schedule"));
    assertNull(node.get("referentGuid"));
    assertEquals(node.size(), 8);
    assertEquals(node.get("clientData"), TestUtils.getClientData());
    JsonNode activityNode = node.get("activity");
    assertEquals(activityNode.get("label").asText(), "Activity3");
    assertEquals(activityNode.get("task").get("identifier").asText(), "tapTest");
    assertEquals(activityNode.get("activityType").asText(), "task");
    assertEquals(activityNode.get("type").asText(), "Activity");
    // zero out the health code field, because that will not be serialized
    schActivity.setHealthCode(null);
    DynamoScheduledActivity newActivity = mapper.readValue(output, DynamoScheduledActivity.class);
    // The local schedule values are not serialized and the calculated values aren't deserialized,
    // but they are verified above.
    newActivity.setTimeZone(DateTimeZone.UTC);
    newActivity.setLocalScheduledOn(scheduledOn);
    newActivity.setLocalExpiresOn(expiresOn);
    newActivity.setReferentGuid("referentGuid");
    // Also works without having to reset the timezone.
    assertEquals(newActivity, schActivity);
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) JsonNode(com.fasterxml.jackson.databind.JsonNode) BridgeObjectMapper(org.sagebionetworks.bridge.json.BridgeObjectMapper) Test(org.testng.annotations.Test)

Example 5 with BridgeObjectMapper

use of org.sagebionetworks.bridge.json.BridgeObjectMapper in project BridgeServer2 by Sage-Bionetworks.

the class ApplicationControllerTest method appleAppLinks.

@Test
public void appleAppLinks() throws Exception {
    DynamoApp app2 = new DynamoApp();
    app2.setIdentifier(TEST_APP_ID);
    app2.setSupportEmail("support@email.com");
    app2.setPasswordPolicy(PasswordPolicy.DEFAULT_PASSWORD_POLICY);
    doReturn(ImmutableList.of(app, app2)).when(appService).getApps();
    app.getAppleAppLinks().add(TestConstants.APPLE_APP_LINK);
    app.getAppleAppLinks().add(TestConstants.APPLE_APP_LINK_2);
    app2.getAppleAppLinks().add(TestConstants.APPLE_APP_LINK_3);
    app2.getAppleAppLinks().add(TestConstants.APPLE_APP_LINK_4);
    String json = controller.appleAppLinks();
    JsonNode node = BridgeObjectMapper.get().readTree(json);
    JsonNode applinks = node.get("applinks");
    JsonNode details = applinks.get("details");
    BridgeObjectMapper mapper = BridgeObjectMapper.get();
    AppleAppLink link0 = mapper.readValue(details.get(0).toString(), AppleAppLink.class);
    AppleAppLink link1 = mapper.readValue(details.get(1).toString(), AppleAppLink.class);
    AppleAppLink link2 = mapper.readValue(details.get(2).toString(), AppleAppLink.class);
    AppleAppLink link3 = mapper.readValue(details.get(3).toString(), AppleAppLink.class);
    assertEquals(TestConstants.APPLE_APP_LINK, link0);
    assertEquals(TestConstants.APPLE_APP_LINK_2, link1);
    assertEquals(TestConstants.APPLE_APP_LINK_3, link2);
    assertEquals(TestConstants.APPLE_APP_LINK_4, link3);
}
Also used : AppleAppLink(org.sagebionetworks.bridge.models.apps.AppleAppLink) DynamoApp(org.sagebionetworks.bridge.dynamodb.DynamoApp) JsonNode(com.fasterxml.jackson.databind.JsonNode) BridgeObjectMapper(org.sagebionetworks.bridge.json.BridgeObjectMapper) Test(org.testng.annotations.Test)

Aggregations

BridgeObjectMapper (org.sagebionetworks.bridge.json.BridgeObjectMapper)10 Test (org.testng.annotations.Test)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 LocalDateTime (org.joda.time.LocalDateTime)1 DynamoApp (org.sagebionetworks.bridge.dynamodb.DynamoApp)1 AppleAppLink (org.sagebionetworks.bridge.models.apps.AppleAppLink)1