|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-2022, University of Oslo |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * Neither the name of the HISP project nor the names of its contributors may |
| 14 | + * be used to endorse or promote products derived from this software without |
| 15 | + * specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 21 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 24 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | +package org.hisp.dhis.tracker.programrule; |
| 29 | + |
| 30 | +import static org.mockito.ArgumentMatchers.any; |
| 31 | +import static org.mockito.ArgumentMatchers.anyList; |
| 32 | +import static org.mockito.ArgumentMatchers.anySet; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.List; |
| 38 | +import org.hisp.dhis.DhisConvenienceTest; |
| 39 | +import org.hisp.dhis.common.CodeGenerator; |
| 40 | +import org.hisp.dhis.dxf2.events.event.EventService; |
| 41 | +import org.hisp.dhis.dxf2.events.event.Events; |
| 42 | +import org.hisp.dhis.program.Program; |
| 43 | +import org.hisp.dhis.program.ProgramInstance; |
| 44 | +import org.hisp.dhis.program.ProgramStageInstance; |
| 45 | +import org.hisp.dhis.program.ProgramStageInstanceService; |
| 46 | +import org.hisp.dhis.programrule.engine.ProgramRuleEngine; |
| 47 | +import org.hisp.dhis.tracker.bundle.TrackerBundle; |
| 48 | +import org.hisp.dhis.tracker.converter.AttributeValueConverterService; |
| 49 | +import org.hisp.dhis.tracker.converter.RuleEngineConverterService; |
| 50 | +import org.hisp.dhis.tracker.domain.Enrollment; |
| 51 | +import org.hisp.dhis.tracker.domain.Event; |
| 52 | +import org.hisp.dhis.tracker.domain.MetadataIdentifier; |
| 53 | +import org.hisp.dhis.tracker.preheat.TrackerPreheat; |
| 54 | +import org.junit.jupiter.api.BeforeEach; |
| 55 | +import org.junit.jupiter.api.Test; |
| 56 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 57 | +import org.mockito.Mock; |
| 58 | +import org.mockito.Mockito; |
| 59 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 60 | + |
| 61 | +@ExtendWith(MockitoExtension.class) |
| 62 | +class DefaultTrackerProgramRuleServiceTest extends DhisConvenienceTest { |
| 63 | + |
| 64 | + @Mock private ProgramRuleEngine programRuleEngine; |
| 65 | + |
| 66 | + @Mock |
| 67 | + private RuleEngineConverterService<Enrollment, ProgramInstance> enrollmentTrackerConverterService; |
| 68 | + |
| 69 | + @Mock |
| 70 | + private RuleEngineConverterService<Event, org.hisp.dhis.program.ProgramStageInstance> |
| 71 | + eventTrackerConverterService; |
| 72 | + |
| 73 | + @Mock private EventService eventService; |
| 74 | + |
| 75 | + @Mock private ProgramStageInstanceService programStageInstanceService; |
| 76 | + |
| 77 | + @Mock private TrackerBundle trackerBundle; |
| 78 | + |
| 79 | + @Mock private TrackerPreheat preheat; |
| 80 | + |
| 81 | + private DefaultTrackerProgramRuleService defaultTrackerProgramRuleService; |
| 82 | + |
| 83 | + private final Program trackerProgram = createProgram('T'); |
| 84 | + |
| 85 | + private final Program eventProgram = createProgramWithoutRegistration('E'); |
| 86 | + |
| 87 | + @BeforeEach |
| 88 | + void setUp() { |
| 89 | + defaultTrackerProgramRuleService = |
| 90 | + new DefaultTrackerProgramRuleService( |
| 91 | + programRuleEngine, |
| 92 | + enrollmentTrackerConverterService, |
| 93 | + eventTrackerConverterService, |
| 94 | + new AttributeValueConverterService(), |
| 95 | + eventService, |
| 96 | + programStageInstanceService); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void shouldInvokeSpecificMethodWhenCalculatingRuleEffectsForTrackerEvents() { |
| 101 | + String enrollmentUid = CodeGenerator.generateUid(); |
| 102 | + String eventUid = CodeGenerator.generateUid(); |
| 103 | + List<Event> events = new ArrayList<>(); |
| 104 | + Event event = |
| 105 | + Event.builder() |
| 106 | + .enrollment(enrollmentUid) |
| 107 | + .event(eventUid) |
| 108 | + .program(MetadataIdentifier.ofUid(trackerProgram.getUid())) |
| 109 | + .build(); |
| 110 | + events.add(event); |
| 111 | + ProgramInstance programInstance = new ProgramInstance(); |
| 112 | + programInstance.setUid(enrollmentUid); |
| 113 | + programInstance.setProgram(trackerProgram); |
| 114 | + List<ProgramStageInstance> programStageInstances = new ArrayList<>(); |
| 115 | + programStageInstances.add(new ProgramStageInstance()); |
| 116 | + |
| 117 | + when(preheat.getEnrollment(enrollmentUid)).thenReturn(programInstance); |
| 118 | + when(eventService.getEvents(any())).thenReturn(new Events()); |
| 119 | + when(trackerBundle.getEnrollments()).thenReturn(Collections.emptyList()); |
| 120 | + when(trackerBundle.getEvents()).thenReturn(events); |
| 121 | + when(trackerBundle.getPreheat()).thenReturn(preheat); |
| 122 | + when(eventTrackerConverterService.fromForRuleEngine(any(TrackerPreheat.class), anyList())) |
| 123 | + .thenReturn(programStageInstances); |
| 124 | + when(programRuleEngine.evaluateEnrollmentAndEvents( |
| 125 | + any(ProgramInstance.class), anySet(), anyList())) |
| 126 | + .thenReturn(Collections.emptyList()); |
| 127 | + |
| 128 | + defaultTrackerProgramRuleService.calculateRuleEffects(trackerBundle); |
| 129 | + |
| 130 | + Mockito.verify(programRuleEngine, Mockito.times(1)) |
| 131 | + .evaluateEnrollmentAndEvents(any(ProgramInstance.class), anySet(), anyList()); |
| 132 | + Mockito.verify(programRuleEngine, Mockito.times(0)) |
| 133 | + .evaluateProgramEvent(any(), any(), anyList()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void shouldInvokeSpecificMethodWhenCalculatingRuleEffectsForProgramEvents() { |
| 138 | + String enrollmentUid = CodeGenerator.generateUid(); |
| 139 | + String eventUid = CodeGenerator.generateUid(); |
| 140 | + List<Event> events = new ArrayList<>(); |
| 141 | + Event event = |
| 142 | + Event.builder() |
| 143 | + .enrollment(enrollmentUid) |
| 144 | + .event(eventUid) |
| 145 | + .program(MetadataIdentifier.ofUid(eventProgram.getUid())) |
| 146 | + .build(); |
| 147 | + events.add(event); |
| 148 | + ProgramInstance programInstance = new ProgramInstance(); |
| 149 | + programInstance.setUid(enrollmentUid); |
| 150 | + programInstance.setProgram(eventProgram); |
| 151 | + |
| 152 | + when(trackerBundle.getEnrollments()).thenReturn(Collections.emptyList()); |
| 153 | + when(trackerBundle.getEvents()).thenReturn(events); |
| 154 | + when(trackerBundle.getPreheat()).thenReturn(preheat); |
| 155 | + when(preheat.getEnrollment(enrollmentUid)).thenReturn(programInstance); |
| 156 | + when(eventTrackerConverterService.fromForRuleEngine(any(TrackerPreheat.class), anyList())) |
| 157 | + .thenReturn(Collections.emptyList()); |
| 158 | + when(programRuleEngine.evaluateProgramEvents(anySet(), any())) |
| 159 | + .thenReturn(Collections.emptyList()); |
| 160 | + |
| 161 | + defaultTrackerProgramRuleService.calculateRuleEffects(trackerBundle); |
| 162 | + |
| 163 | + Mockito.verify(programRuleEngine, Mockito.times(0)) |
| 164 | + .evaluateEnrollmentAndEvents(any(ProgramInstance.class), anySet(), anyList()); |
| 165 | + Mockito.verify(programRuleEngine, Mockito.times(1)).evaluateProgramEvents(anySet(), any()); |
| 166 | + } |
| 167 | +} |
0 commit comments