Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ private List<Result> validate(String correlationId, String facilityId, String pa
categorizationService.categorize(results);
validationMetrics.recordCategorizationDuration(timer.getMilliseconds(), attributes);
}
resultRepository.saveAll(results);
List<Result> submittedResults = results.stream()
.filter(result -> result.getCategories() != null
&& result.getCategories().stream().anyMatch(Category::isSubmit))
.toList();
if (!submittedResults.isEmpty()) {
resultRepository.saveAll(submittedResults);
}
return results;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,16 @@ void initializeGivesEveryShippedCategoryARule() throws IOException {
void reimportUpdatesFieldsOnAnAlreadyPersistedCategory() {
String id = "unknown_code_system";
CategorySnapshot original = snapshot(id, "Original title", "Original guidance", CategorySeverity.WARNING);
original.setSubmit(false);
categorizationService.saveCategorySnapshot(original);
testEntityManager.flush();
testEntityManager.clear();

Category initiallyPersisted = categoryRepository.findById(id).orElseThrow();
assertFalse(initiallyPersisted.isSubmit());

CategorySnapshot revised = snapshot(id, "Revised title", "Revised guidance", CategorySeverity.ERROR);
revised.setSubmit(true);
categorizationService.saveCategorySnapshot(revised);
testEntityManager.flush();
testEntityManager.clear();
Expand All @@ -203,6 +208,7 @@ void reimportUpdatesFieldsOnAnAlreadyPersistedCategory() {
assertEquals("Revised guidance", persisted.getGuidance());
assertEquals(CategorySeverity.ERROR, persisted.getSeverity());
assertTrue(persisted.isAcceptable());
assertTrue(persisted.isSubmit());
assertEquals(2, persisted.getRules().size(), "each import should append a rule");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ private Category categoryWithAcceptable(boolean acceptable) {
return category;
}

private Category categoryWithSubmit(boolean submit) {
Category category = new Category();
category.setId(submit ? "submitted-cat" : "non-submitted-cat");
category.setSubmit(submit);
return category;
}

// -------------------------------------------------------------------------
// Bundle retrieval: Blob Storage
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -265,8 +272,21 @@ void process_callsCategorizationServiceWithResults() throws Exception {
}

@Test
void process_savesAllResultsToRepository() throws Exception {
Result result = resultWithCategories(Collections.emptyList());
void process_savesOnlyResultsWithSubmitEnabledCategories() throws Exception {
Result submittedResult = resultWithCategories(List.of(categoryWithSubmit(true)));
Result nonSubmittedResult = resultWithCategories(List.of(categoryWithSubmit(false)));
Result uncategorizedResult = resultWithCategories(Collections.emptyList());
stubRestRetrieval();
when(validationService.validate(bundle)).thenReturn(List.of(submittedResult, nonSubmittedResult, uncategorizedResult));

consumer.process(buildRecord(null));

verify(resultRepository).saveAll(List.of(submittedResult));
}

@Test
void process_multiCategoryResultWithAnySubmitEnabledCategory_savesResult() throws Exception {
Result result = resultWithCategories(List.of(categoryWithSubmit(false), categoryWithSubmit(true)));
stubRestRetrieval();
when(validationService.validate(bundle)).thenReturn(List.of(result));

Expand All @@ -275,6 +295,18 @@ void process_savesAllResultsToRepository() throws Exception {
verify(resultRepository).saveAll(List.of(result));
}

@Test
void process_noSubmitEnabledCategories_doesNotSaveResults() throws Exception {
Result nonSubmittedResult = resultWithCategories(List.of(categoryWithSubmit(false)));
Result uncategorizedResult = resultWithCategories(Collections.emptyList());
stubRestRetrieval();
when(validationService.validate(bundle)).thenReturn(List.of(nonSubmittedResult, uncategorizedResult));

consumer.process(buildRecord(null));

verify(resultRepository, never()).saveAll(anyList());
}

@Test
void process_inactiveCodeResult_isCategorizedAsInactiveCodeAndPersisted() throws Exception {
// Wire a real CategorizationService backed by the shipped categories.json so this exercises the
Expand Down
Loading