Skip to content

Commit ab70142

Browse files
author
Rupert Westenthaler
committed
MORE-Platform#385: Fixed an issue while importing Adherence checks to a Study. Also added a unit test that validates that importing an adherence check does not override existing, but instead returns the existing
1 parent 59f5eea commit ab70142

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

studymanager-services/src/main/java/io/redlink/more/studymanager/repository/goals/GoalConfigurationRepository.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.time.LocalTime;
2323
import java.util.List;
24+
import java.util.Objects;
2425

2526
@Component
2627
public class GoalConfigurationRepository {
@@ -153,7 +154,7 @@ public GoalAdherenceCheck upsertCheck(GoalAdherenceCheck check) {
153154
@Transactional
154155
public GoalAdherenceCheck doImport(Long studyId, GoalAdherenceCheck check) {
155156
var existing = getCheckById(studyId, check.getCheckId());
156-
if(existing != null) {
157+
if(existing == null) {
157158
KeyHolder keyHolder = new GeneratedKeyHolder();
158159
namedTemplate.update(
159160
IMPORT_ADHERENCE_CHECK,
@@ -163,7 +164,7 @@ public GoalAdherenceCheck doImport(Long studyId, GoalAdherenceCheck check) {
163164
keyHolder,
164165
new String[]{"check_id"}
165166
);
166-
Integer checkId = keyHolder.getKey().intValue();
167+
Integer checkId = Objects.requireNonNull(keyHolder.getKey()).intValue();
167168
return getCheckById(studyId, checkId);
168169
} else { //do not override existing adherence check in the study
169170
return existing;

studymanager-services/src/test/java/io/redlink/more/studymanager/repository/goals/GoalConfigurationRepositoryTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,37 @@ void testDoImportAdherenceCheck() {
253253
assertThat(goalConfigurationRepository.listChecks(studyX)).isEmpty();
254254
}
255255

256+
@Test
257+
@DisplayName("GoalAdherenceCheck import does not override existing")
258+
void testDoSkipImportExistingAdherenceCheck() {
259+
Long studyX = studyRepository.insert(new Study().setContact(new Contact().setPerson("X"))).getStudyId();
260+
Long studyY = studyRepository.insert(new Study().setContact(new Contact().setPerson("Y"))).getStudyId();
261+
//add an existing adherence check for the id=1 to studyY
262+
goalConfigurationRepository.upsertCheck(new GoalAdherenceCheck()
263+
.setStudyId(studyY)
264+
.setCheckId(1) // pretend exported
265+
.setTitle("Existing Check")
266+
.setTime(LocalTime.of(14, 05)));
267+
268+
//now create a check for StudyX with the same id=1 but different title and time
269+
GoalAdherenceCheck originalStudyX = new GoalAdherenceCheck()
270+
.setStudyId(studyX)
271+
.setCheckId(1) // pretend exported
272+
.setTitle("Imported Check")
273+
.setTime(LocalTime.of(14, 15));
274+
275+
//import the check from studyX and validate that the existing one is not overridden
276+
GoalAdherenceCheck imported = goalConfigurationRepository.doImport(studyY, originalStudyX);
277+
278+
assertThat(imported.getStudyId()).isEqualTo(studyY);
279+
assertThat(imported.getCheckId()).isEqualTo(1);
280+
assertThat(imported.getTitle()).isEqualTo("Existing Check");
281+
assertThat(imported.getTime()).isEqualTo(LocalTime.of(14, 05));
282+
283+
assertThat(goalConfigurationRepository.listChecks(studyY)).hasSize(1);
284+
assertThat(goalConfigurationRepository.listChecks(studyX)).isEmpty();
285+
}
286+
256287
@Test
257288
@DisplayName("GoalTopic can be imported with forced studyId (upsert behavior)")
258289
void testDoImportGoalTopic() {

0 commit comments

Comments
 (0)