Skip to content

Commit 85a95f8

Browse files
[chore] [mod-data-export] unit tests
1 parent 4018353 commit 85a95f8

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

src/test/java/org/folio/dataexp/service/logs/ErrorLogServiceTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
import org.mockito.Mock;
4242
import org.mockito.junit.jupiter.MockitoExtension;
4343
import org.springframework.data.domain.PageImpl;
44+
import com.fasterxml.jackson.core.JsonProcessingException;
45+
import static org.folio.dataexp.util.ErrorCode.SOME_UUIDS_NOT_FOUND;
46+
import java.util.ArrayList;
4447

4548
@ExtendWith(MockitoExtension.class)
4649
class ErrorLogServiceTest {
@@ -320,4 +323,79 @@ void saveWithAffectedRecordShouldSetEmptyInventoryLinkForDeletedRecord() {
320323
assertEquals(instanceTitle, affectedRecord.getTitle());
321324
verify(configurationService, never()).getValue(anyString());
322325
}
326+
327+
@Test
328+
@SneakyThrows
329+
void saveGeneralErrorWithMessageValuesTest() {
330+
// TestMate-8df560da94786686fc4858ef57a9e5ec
331+
// Given
332+
var jobExecutionId = UUID.fromString("a890b134-736f-4e5a-8351-9c608f3a3a58");
333+
var errorMessageCode = ErrorCode.SOME_UUIDS_NOT_FOUND.getCode();
334+
var errorMessageValues = List.of("uuid1", "uuid2");
335+
var userId = UUID.fromString("b890b134-736f-4e5a-8351-9c608f3a3a59");
336+
var jobProfileId = UUID.fromString("c890b134-736f-4e5a-8351-9c608f3a3a50");
337+
when(folioExecutionContext.getUserId()).thenReturn(userId);
338+
when(jobExecutionService.getById(jobExecutionId)).thenReturn(new JobExecution().id(jobExecutionId).jobProfileId(jobProfileId));
339+
when(jobProfileService.jobProfileExists(jobProfileId)).thenReturn(true);
340+
var errorLogCaptor = ArgumentCaptor.forClass(ErrorLog.class);
341+
when(objectMapper.writeValueAsString(errorLogCaptor.capture())).thenAnswer(invocation -> new ObjectMapper().writeValueAsString(invocation.getArgument(0)));
342+
// When
343+
errorLogService.saveGeneralErrorWithMessageValues(errorMessageCode, errorMessageValues, jobExecutionId);
344+
// Then
345+
verify(errorLogEntityCqlRepository).insertIfNotExists(
346+
isA(UUID.class),
347+
anyString(),
348+
isA(Date.class),
349+
eq(userId.toString()),
350+
eq(jobExecutionId),
351+
eq(jobProfileId));
352+
var capturedErrorLog = errorLogCaptor.getValue();
353+
assertEquals(jobExecutionId, capturedErrorLog.getJobExecutionId());
354+
assertEquals(errorMessageCode, capturedErrorLog.getErrorMessageCode());
355+
assertEquals(errorMessageValues, capturedErrorLog.getErrorMessageValues());
356+
assertEquals(ErrorLog.LogLevelEnum.ERROR, capturedErrorLog.getLogLevel());
357+
}
358+
359+
@Test
360+
@SneakyThrows
361+
void populateUuidsNotFoundErrorLogShouldCorrectlyFormatSingleUuid() {
362+
// TestMate-1da6299af83636050ba587663941e69d
363+
// Given
364+
var jobExecutionId = UUID.fromString("a890b134-736f-4e5a-8351-9c608f3a3a58");
365+
var userId = UUID.fromString("b890b134-736f-4e5a-8351-9c608f3a3a59");
366+
var jobProfileId = UUID.fromString("c890b134-736f-4e5a-8351-9c608f3a3a50");
367+
var notFoundUuid = "a1b2c3d4-e5f6-7890-1234-567890abcdef";
368+
var notFoundUuids = List.of(notFoundUuid);
369+
when(errorLogEntityCqlRepository.getByJobExecutionIdAndErrorCode(
370+
jobExecutionId, SOME_UUIDS_NOT_FOUND.getCode()))
371+
.thenReturn(new ArrayList<>());
372+
when(folioExecutionContext.getUserId()).thenReturn(userId);
373+
when(jobExecutionService.getById(jobExecutionId))
374+
.thenReturn(new JobExecution().id(jobExecutionId).jobProfileId(jobProfileId));
375+
when(jobProfileService.jobProfileExists(jobProfileId)).thenReturn(true);
376+
var errorLogCaptor = ArgumentCaptor.forClass(String.class);
377+
when(objectMapper.writeValueAsString(any(ErrorLog.class))).thenAnswer(invocation -> {
378+
var realObjectMapper = new ObjectMapper();
379+
return realObjectMapper.writeValueAsString(invocation.getArgument(0));
380+
});
381+
// When
382+
errorLogService.populateUuidsNotFoundErrorLog(jobExecutionId, notFoundUuids);
383+
// Then
384+
verify(errorLogEntityCqlRepository)
385+
.getByJobExecutionIdAndErrorCode(jobExecutionId, SOME_UUIDS_NOT_FOUND.getCode());
386+
verify(errorLogEntityCqlRepository)
387+
.insertIfNotExists(
388+
isA(UUID.class),
389+
errorLogCaptor.capture(),
390+
isA(Date.class),
391+
eq(userId.toString()),
392+
eq(jobExecutionId),
393+
eq(jobProfileId));
394+
var realObjectMapper = new ObjectMapper();
395+
var capturedErrorLog = realObjectMapper.readValue(errorLogCaptor.getValue(), ErrorLog.class);
396+
assertEquals(SOME_UUIDS_NOT_FOUND.getCode(), capturedErrorLog.getErrorMessageCode());
397+
assertEquals(jobExecutionId, capturedErrorLog.getJobExecutionId());
398+
assertEquals(1, capturedErrorLog.getErrorMessageValues().size());
399+
assertEquals(notFoundUuid, capturedErrorLog.getErrorMessageValues().get(0));
400+
}
323401
}

0 commit comments

Comments
 (0)