Skip to content

Commit

Permalink
fix: incremented coverage on IdempotencyService
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-deri committed Jun 24, 2024
1 parent 6328bf3 commit e35bfab
Showing 1 changed file with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import com.azure.messaging.servicebus.*;
import it.gov.pagopa.wispconverter.exception.AppErrorCodeMessageEnum;
import it.gov.pagopa.wispconverter.exception.AppException;
import it.gov.pagopa.wispconverter.repository.IdempotencyKeyRepository;
import it.gov.pagopa.wispconverter.repository.RTRequestRepository;
import it.gov.pagopa.wispconverter.repository.model.IdempotencyKeyEntity;
import it.gov.pagopa.wispconverter.repository.model.RTRequestEntity;
import it.gov.pagopa.wispconverter.repository.model.enumz.IdempotencyStatusEnum;
import it.gov.pagopa.wispconverter.repository.model.enumz.ReceiptTypeEnum;
import it.gov.pagopa.wispconverter.service.*;
import it.gov.pagopa.wispconverter.servicebus.RTConsumer;
Expand All @@ -22,6 +25,8 @@
import org.springframework.test.util.ReflectionTestUtils;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -57,8 +62,15 @@ void sendToPa_sent(String receiptType) {
ReService reService = mock(ReService.class);
doNothing().when(reService).addRe(any());

IdempotencyService idempotencyService = mock(IdempotencyService.class);
when(idempotencyService.isIdempotencyKeyProcessable(any(), any())).thenReturn(true);
IdempotencyKeyRepository idempotencyKeyRepository = mock(IdempotencyKeyRepository.class);
IdempotencyService idempotencyService = new IdempotencyService(idempotencyKeyRepository);
when(idempotencyKeyRepository.findById(any(), any())).thenReturn(Optional.of(IdempotencyKeyEntity.builder()
.status(IdempotencyStatusEnum.FAILED)
.receiptType("OK".equalsIgnoreCase(receiptType) ? ReceiptTypeEnum.OK : ReceiptTypeEnum.KO)
.lockedAt(null)
.build()));
ReflectionTestUtils.setField(idempotencyService, "lockValidityInMinutes", 10);
ReflectionTestUtils.setField(idempotencyService, "idempotencyKeyRepository", idempotencyKeyRepository);

JaxbElementUtil jaxbElementUtil = new JaxbElementUtil();

Expand Down Expand Up @@ -88,13 +100,12 @@ void sendToPa_sent(String receiptType) {

rtConsumer.processMessage(messageContext);

verify(idempotencyService, times(1)).lockIdempotencyKey(any(), any());
//verify(idempotencyService, times(1)).lockIdempotencyKey(any(), any());
verify(rtRequestRepository, times(1)).findById(any(), any());
verify(rtRequestRepository, times(1)).delete(any());
verify(rtRequestRepository, times(0)).save(any());
verify(serviceBusSenderClient, times(0)).sendMessage(any(), any());
verify(idempotencyService, times(1)).unlockIdempotencyKey(any(), any(), any());

verify(idempotencyKeyRepository, times(2)).save(any());
}

@ParameterizedTest
Expand All @@ -113,9 +124,15 @@ void sendToPa_locked(String receiptType, String rawIsCompleted) {
ReService reService = mock(ReService.class);
doNothing().when(reService).addRe(any());

IdempotencyService idempotencyService = mock(IdempotencyService.class);
when(idempotencyService.isIdempotencyKeyProcessable(any(), any())).thenReturn(false);
when(idempotencyService.isCompleted(any())).thenReturn(isCompleted);
IdempotencyKeyRepository idempotencyKeyRepository = mock(IdempotencyKeyRepository.class);
IdempotencyService idempotencyService = new IdempotencyService(idempotencyKeyRepository);
when(idempotencyKeyRepository.findById(any(), any())).thenReturn(Optional.of(IdempotencyKeyEntity.builder()
.status(isCompleted ? IdempotencyStatusEnum.SUCCESS : IdempotencyStatusEnum.LOCKED)
.receiptType("OK".equalsIgnoreCase(receiptType) ? ReceiptTypeEnum.OK : ReceiptTypeEnum.KO)
.lockedAt(Instant.now().minus(1, ChronoUnit.MINUTES))
.build()));
ReflectionTestUtils.setField(idempotencyService, "lockValidityInMinutes", 10);
ReflectionTestUtils.setField(idempotencyService, "idempotencyKeyRepository", idempotencyKeyRepository);

JaxbElementUtil jaxbElementUtil = new JaxbElementUtil();

Expand Down Expand Up @@ -145,13 +162,11 @@ void sendToPa_locked(String receiptType, String rawIsCompleted) {

rtConsumer.processMessage(messageContext);

verify(idempotencyService, times(0)).lockIdempotencyKey(any(), any());
verify(rtRequestRepository, times(1)).findById(any(), any());
verify(rtRequestRepository, times(0)).delete(any());
verify(rtRequestRepository, times(isCompleted ? 0 : 1)).save(any());
verify(serviceBusSenderClient, times(0)).sendMessage(any(), any());
verify(idempotencyService, times(0)).unlockIdempotencyKey(any(), any(), any());

verify(idempotencyKeyRepository, times(0)).save(any());
}

@ParameterizedTest
Expand Down

0 comments on commit e35bfab

Please sign in to comment.