Search in sources :

Example 1 with StudyBurst

use of org.sagebionetworks.bridge.models.schedules2.StudyBurst in project BridgeServer2 by Sage-Bionetworks.

the class SchedulerTest method studyBurstsAreResolvedInTimeline.

@Test
public void studyBurstsAreResolvedInTimeline() throws Exception {
    Schedule2 schedule = createSchedule("P3D");
    StudyBurst burst1 = new StudyBurst();
    burst1.setIdentifier("burst1");
    burst1.setOriginEventId("timeline_retrieved");
    burst1.setInterval(Period.parse("P1D"));
    burst1.setOccurrences(3);
    burst1.setUpdateType(MUTABLE);
    StudyBurst burst2 = new StudyBurst();
    burst2.setIdentifier("burst2");
    burst2.setOriginEventId("timeline_retrieved");
    burst2.setInterval(Period.parse("P1D"));
    burst2.setOccurrences(1);
    burst2.setUpdateType(MUTABLE);
    schedule.setStudyBursts(ImmutableList.of(burst1, burst2));
    Session session = createOneTimeSession("P1D");
    session.setStudyBurstIds(ImmutableList.of("burst1", "burst2"));
    schedule.setSessions(ImmutableList.of(session));
    Timeline timeline = Scheduler.INSTANCE.calculateTimeline(schedule);
    assertEquals(timeline.getSchedule().size(), 5);
    // The study burst entries are in order by number of occurence, despite
    // having exactly the same day range after event
    List<String> triggerEventIds = timeline.getSchedule().stream().map(ScheduledSession::getStartEventId).collect(toList());
    assertEquals(triggerEventIds, ImmutableList.of("enrollment", "study_burst:burst1:01", "study_burst:burst1:02", "study_burst:burst1:03", "study_burst:burst2:01"));
    ScheduledSession schSession = timeline.getSchedule().get(1);
    assertEquals(schSession.getStudyBurstId(), "burst1");
    assertEquals(schSession.getStudyBurstNum(), Integer.valueOf(1));
    schSession = timeline.getSchedule().get(2);
    assertEquals(schSession.getStudyBurstId(), "burst1");
    assertEquals(schSession.getStudyBurstNum(), Integer.valueOf(2));
    schSession = timeline.getSchedule().get(3);
    assertEquals(schSession.getStudyBurstId(), "burst1");
    assertEquals(schSession.getStudyBurstNum(), Integer.valueOf(3));
    schSession = timeline.getSchedule().get(4);
    assertEquals(schSession.getStudyBurstId(), "burst2");
    assertEquals(schSession.getStudyBurstNum(), Integer.valueOf(1));
    // Let's verify that the original schedule does not contain alterations to its
    // start event IDs or study burst IDs
    Session updatedSession = schedule.getSessions().get(0);
    assertEquals(updatedSession.getStartEventIds(), ImmutableList.of("enrollment"));
    assertEquals(updatedSession.getStudyBurstIds(), ImmutableList.of("burst1", "burst2"));
}
Also used : Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) Session(org.sagebionetworks.bridge.models.schedules2.Session) Test(org.testng.annotations.Test)

Example 2 with StudyBurst

use of org.sagebionetworks.bridge.models.schedules2.StudyBurst in project BridgeServer2 by Sage-Bionetworks.

the class StudyActivityEventServiceTest method publishEvent_eventMutableStudyBurstsMutable.

@Test
public void publishEvent_eventMutableStudyBurstsMutable() {
    // event immutable, study bursts mutable. Study bursts are updated, would update
    // even if they existed.
    StudyActivityEvent event = makeBuilder().withObjectId(ENROLLMENT_FIELD).withTimestamp(ENROLLMENT_TS).withObjectType(ENROLLMENT).build();
    // This time however, the event is not updated because it exists.
    when(mockDao.getRecentStudyActivityEvent(any(), any(), eq(ENROLLMENT_FIELD))).thenReturn(PERSISTED_EVENT);
    Study study = Study.create();
    study.setAppId(TEST_APP_ID);
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    StudyBurst burst = new StudyBurst();
    burst.setOriginEventId(ENROLLMENT_FIELD);
    burst.setIdentifier("foo");
    burst.setDelay(Period.parse("P1W"));
    burst.setInterval(Period.parse("P1W"));
    burst.setOccurrences(3);
    burst.setUpdateType(MUTABLE);
    Schedule2 schedule = new Schedule2();
    schedule.setStudyBursts(ImmutableList.of(burst));
    when(mockScheduleService.getScheduleForStudy(TEST_APP_ID, study)).thenReturn(Optional.of(schedule));
    service.publishEvent(event, false, true);
    verify(mockDao, times(3)).publishEvent(eventCaptor.capture());
    StudyActivityEvent sb1 = eventCaptor.getAllValues().get(0);
    assertEquals(sb1.getEventId(), "study_burst:foo:01");
    assertEquals(sb1.getTimestamp(), ENROLLMENT_TS.plusWeeks(1));
    StudyActivityEvent sb2 = eventCaptor.getAllValues().get(1);
    assertEquals(sb2.getEventId(), "study_burst:foo:02");
    assertEquals(sb2.getTimestamp(), ENROLLMENT_TS.plusWeeks(2));
    StudyActivityEvent sb3 = eventCaptor.getAllValues().get(2);
    assertEquals(sb3.getEventId(), "study_burst:foo:03");
    assertEquals(sb3.getTimestamp(), ENROLLMENT_TS.plusWeeks(3));
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) Test(org.testng.annotations.Test)

Example 3 with StudyBurst

use of org.sagebionetworks.bridge.models.schedules2.StudyBurst in project BridgeServer2 by Sage-Bionetworks.

the class StudyActivityEventServiceTest method publishEvent_studyBurstEventThrowsError.

@Test
public void publishEvent_studyBurstEventThrowsError() {
    // This event doesn’t update unless there is no persisted event. Here
    // it does not persist.
    StudyActivityEvent event = makeBuilder().withObjectId(ENROLLMENT_FIELD).withTimestamp(ENROLLMENT_TS).withObjectType(ENROLLMENT).build();
    Study study = Study.create();
    study.setAppId(TEST_APP_ID);
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    StudyBurst burst = new StudyBurst();
    burst.setOriginEventId(ENROLLMENT_FIELD);
    burst.setIdentifier("foo");
    burst.setInterval(Period.parse("P1W"));
    burst.setOccurrences(3);
    burst.setUpdateType(IMMUTABLE);
    Schedule2 schedule = new Schedule2();
    schedule.setStudyBursts(ImmutableList.of(burst));
    when(mockScheduleService.getScheduleForStudy(TEST_APP_ID, study)).thenReturn(Optional.of(schedule));
    // The existence of the first event will be enough to throw an error
    when(mockDao.getRecentStudyActivityEvent(any(), any(), eq("study_burst:foo:01"))).thenReturn(PERSISTED_EVENT);
    try {
        service.publishEvent(event, true, true);
        fail("Should have thrown exception");
    } catch (BadRequestException e) {
        assertEquals(e.getMessage(), "Study event(s) failed to publish: study_burst:foo:01.");
    }
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) Test(org.testng.annotations.Test)

Example 4 with StudyBurst

use of org.sagebionetworks.bridge.models.schedules2.StudyBurst in project BridgeServer2 by Sage-Bionetworks.

the class StudyActivityEventServiceTest method publishEvent_eventMutableStudyBurstsImmutable.

@Test
public void publishEvent_eventMutableStudyBurstsImmutable() {
    StudyActivityEvent event = makeBuilder().withObjectId(ENROLLMENT_FIELD).withTimestamp(ENROLLMENT_TS).withObjectType(ENROLLMENT).withUpdateType(IMMUTABLE).build();
    // This time however, the event is not updated because it exists.
    when(mockDao.getRecentStudyActivityEvent(any(), any(), eq(ENROLLMENT_FIELD))).thenReturn(PERSISTED_EVENT);
    Study study = Study.create();
    study.setAppId(TEST_APP_ID);
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    StudyBurst burst = new StudyBurst();
    burst.setOriginEventId(ENROLLMENT_FIELD);
    burst.setIdentifier("foo");
    burst.setDelay(Period.parse("P1W"));
    burst.setInterval(Period.parse("P1W"));
    burst.setOccurrences(3);
    burst.setUpdateType(IMMUTABLE);
    Schedule2 schedule = new Schedule2();
    schedule.setStudyBursts(ImmutableList.of(burst));
    when(mockScheduleService.getScheduleForStudy(TEST_APP_ID, study)).thenReturn(Optional.of(schedule));
    when(mockDao.getRecentStudyActivityEvent(any(), any(), eq("study_burst:foo:01"))).thenReturn(PERSISTED_EVENT);
    when(mockDao.getRecentStudyActivityEvent(any(), any(), eq("study_burst:foo:03"))).thenReturn(PERSISTED_EVENT);
    service.publishEvent(event, false, true);
    verify(mockDao, times(1)).publishEvent(eventCaptor.capture());
    // Only one is published because it was not persisted (in this case it was probably
    // deleted). This is the effect of having mutable study bursts, and they do update after
    // the origin event updates. We may introduce behavior to break this linkage or to allow
    // the deletion of immutable events so mistakes can be recreated (BRIDGE-3120).
    StudyActivityEvent sb2 = eventCaptor.getAllValues().get(0);
    assertEquals(sb2.getEventId(), "study_burst:foo:02");
    assertEquals(sb2.getTimestamp(), ENROLLMENT_TS.plusWeeks(2));
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) Test(org.testng.annotations.Test)

Example 5 with StudyBurst

use of org.sagebionetworks.bridge.models.schedules2.StudyBurst in project BridgeServer2 by Sage-Bionetworks.

the class StudyActivityEventServiceTest method deleteEvent_deletesStudyBurstEvents.

@Test
public void deleteEvent_deletesStudyBurstEvents() {
    // Publish a deletable event.
    StudyActivityEvent event = makeBuilder().withObjectId("foo").withTimestamp(ENROLLMENT_TS).withObjectType(CUSTOM).withUpdateType(MUTABLE).build();
    Study study = Study.create();
    study.setAppId(TEST_APP_ID);
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    StudyBurst burst = new StudyBurst();
    burst.setOriginEventId("custom:foo");
    burst.setIdentifier("foo");
    burst.setInterval(Period.parse("P1W"));
    burst.setOccurrences(3);
    burst.setUpdateType(MUTABLE);
    Schedule2 schedule = new Schedule2();
    schedule.setStudyBursts(ImmutableList.of(burst));
    when(mockScheduleService.getScheduleForStudy(TEST_APP_ID, study)).thenReturn(Optional.of(schedule));
    StudyActivityEvent persistedEvent = new StudyActivityEvent();
    when(mockDao.getRecentStudyActivityEvent(TEST_USER_ID, TEST_STUDY_ID, "custom:foo")).thenReturn(persistedEvent);
    service.deleteEvent(event, false);
    verify(mockDao, times(4)).deleteEvent(eventCaptor.capture());
    StudyActivityEvent origin = eventCaptor.getAllValues().get(0);
    assertEquals(origin.getEventId(), "custom:foo");
    StudyActivityEvent sb1 = eventCaptor.getAllValues().get(1);
    assertEquals(sb1.getEventId(), "study_burst:foo:01");
    assertEquals(sb1.getAppId(), TEST_APP_ID);
    assertEquals(sb1.getStudyId(), TEST_STUDY_ID);
    assertEquals(sb1.getUserId(), TEST_USER_ID);
    StudyActivityEvent sb2 = eventCaptor.getAllValues().get(2);
    assertEquals(sb2.getEventId(), "study_burst:foo:02");
    StudyActivityEvent sb3 = eventCaptor.getAllValues().get(3);
    assertEquals(sb3.getEventId(), "study_burst:foo:03");
}
Also used : Study(org.sagebionetworks.bridge.models.studies.Study) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) Test(org.testng.annotations.Test)

Aggregations

StudyBurst (org.sagebionetworks.bridge.models.schedules2.StudyBurst)22 Schedule2 (org.sagebionetworks.bridge.models.schedules2.Schedule2)16 Test (org.testng.annotations.Test)16 StudyActivityEvent (org.sagebionetworks.bridge.models.activities.StudyActivityEvent)13 Study (org.sagebionetworks.bridge.models.studies.Study)11 Session (org.sagebionetworks.bridge.models.schedules2.Session)4 HashSet (java.util.HashSet)3 Period (org.joda.time.Period)3 AssessmentReference (org.sagebionetworks.bridge.models.schedules2.AssessmentReference)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 LocalTime (org.joda.time.LocalTime)2 RequestContext (org.sagebionetworks.bridge.RequestContext)2 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)2 SessionTest (org.sagebionetworks.bridge.models.schedules2.SessionTest)2 TimeWindow (org.sagebionetworks.bridge.models.schedules2.TimeWindow)2 Charsets (com.google.common.base.Charsets)1 Strings (com.google.common.base.Strings)1 HashMultiset (com.google.common.collect.HashMultiset)1