-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [PRMT-4850] Added `ConditionExpression` to `UpdateItemRequest`. Signed-off-by: martin-nhs <[email protected]> * [PRMT-4850] Started implementing unit tests. Signed-off-by: martin-nhs <[email protected]> * [PRMT-4850] Added additional unit test. Signed-off-by: martin-nhs <[email protected]> * [PRMT-4850] Enhanced unit tests. Signed-off-by: martin-nhs <[email protected]> * [PRMT-4850] Update methods to not use legacy code. Signed-off-by: martin-nhs <[email protected]> * [PRMT-4850] Updated tests to include more test cases. Signed-off-by: martin-nhs <[email protected]> * [PRMT-4850] Remove commented out test. Signed-off-by: martin-nhs <[email protected]> * [PRMT-4850] Enhance test assertions. Signed-off-by: martin-nhs <[email protected]> --------- Signed-off-by: martin-nhs <[email protected]>
- Loading branch information
1 parent
6ae6954
commit 4dc6193
Showing
3 changed files
with
163 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/test/java/uk/nhs/prm/repo/ehrtransferservice/database/TransferRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package uk.nhs.prm.repo.ehrtransferservice.database; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient; | ||
import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException; | ||
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest; | ||
import uk.nhs.prm.repo.ehrtransferservice.config.AppConfig; | ||
import uk.nhs.prm.repo.ehrtransferservice.database.enumeration.ConversationTransferStatus; | ||
import uk.nhs.prm.repo.ehrtransferservice.exceptions.database.ConversationUpdateException; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.*; | ||
import static uk.nhs.prm.repo.ehrtransferservice.database.enumeration.TransferTableAttribute.INBOUND_CONVERSATION_ID; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class TransferRepositoryTest { | ||
@Mock | ||
private AppConfig appConfig; | ||
|
||
@Mock | ||
private DynamoDbClient dynamoDbClient; | ||
|
||
@InjectMocks | ||
private TransferRepository transferRepository; | ||
|
||
private static final String TABLE_NAME = "ehr-transfer-tracker"; | ||
private static final String CREATED_AT_CONDITION_EXPRESSION = "attribute_exists(CreatedAt)"; | ||
private static final String UPDATE_STATUS_CODE_EXPRESSION = "SET #TransferStatus = :tsValue, #UpdatedAt = :uaValue"; | ||
private static final String UPDATE_WITH_FAILURE_CODE_EXPRESSION = "SET #TransferStatus = :tsValue, #FailureCode = :fcValue, #UpdatedAt = :uaValue"; | ||
private static final ArgumentCaptor<UpdateItemRequest> updateItemRequestCaptor = ArgumentCaptor.forClass(UpdateItemRequest.class); | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
doReturn(TABLE_NAME) | ||
.when(appConfig) | ||
.transferTrackerDbTableName(); | ||
} | ||
|
||
@Test | ||
void updateConversationStatus_ValidInboundConversationIdAndStatus_ShouldCallUpdateItem() { | ||
// given | ||
final String inboundConversationId = "F68FE779-5A2A-490B-81FE-6976543EFC06"; | ||
final ConversationTransferStatus status = ConversationTransferStatus.INBOUND_COMPLETE; | ||
|
||
// when | ||
transferRepository.updateConversationStatus(UUID.fromString(inboundConversationId), status); | ||
|
||
// then | ||
verify(appConfig).transferTrackerDbTableName(); | ||
verify(dynamoDbClient).updateItem(updateItemRequestCaptor.capture()); | ||
assertEquals(inboundConversationId, updateItemRequestCaptor.getValue().key().get(INBOUND_CONVERSATION_ID.name).s()); | ||
assertEquals(status.name(), updateItemRequestCaptor.getValue().expressionAttributeValues().get(":tsValue").s()); | ||
assertEquals(CREATED_AT_CONDITION_EXPRESSION, updateItemRequestCaptor.getValue().conditionExpression()); | ||
assertEquals(UPDATE_STATUS_CODE_EXPRESSION, updateItemRequestCaptor.getValue().updateExpression()); | ||
} | ||
|
||
@Test | ||
void updateConversationStatus_ValidInboundConversationIdAndStatusWithNoCreatedAtDate_ShouldThrowConversationUpdateException() { | ||
// given | ||
final String inboundConversationId = "C68082F9-EFB9-4144-BAA0-3A2F2E2A88B9"; | ||
final ConversationTransferStatus status = ConversationTransferStatus.INBOUND_COMPLETE; | ||
|
||
// when | ||
doThrow(ConditionalCheckFailedException.class) | ||
.when(dynamoDbClient) | ||
.updateItem(any(UpdateItemRequest.class)); | ||
|
||
// then | ||
assertThrows( | ||
ConversationUpdateException.class, | ||
() -> transferRepository.updateConversationStatus(UUID.fromString(inboundConversationId), status) | ||
); | ||
|
||
verify(dynamoDbClient).updateItem(updateItemRequestCaptor.capture()); | ||
assertEquals(inboundConversationId, updateItemRequestCaptor.getValue().key().get(INBOUND_CONVERSATION_ID.name).s()); | ||
} | ||
|
||
@Test | ||
void updateConversationStatusWithFailure_ValidInboundConversationIdAndFailureCode_ShouldCallUpdateItem() { | ||
// given | ||
final String inboundConversationId = "0A1B2C3D-4E5F-6789-ABCD-EF0123456789"; | ||
final String failureCode = "19"; | ||
|
||
// when | ||
transferRepository.updateConversationStatusWithFailure(UUID.fromString(inboundConversationId), failureCode); | ||
|
||
// then | ||
verify(appConfig).transferTrackerDbTableName(); | ||
verify(dynamoDbClient).updateItem(updateItemRequestCaptor.capture()); | ||
assertEquals(inboundConversationId, updateItemRequestCaptor.getValue().key().get(INBOUND_CONVERSATION_ID.name).s()); | ||
assertEquals(failureCode, updateItemRequestCaptor.getValue().expressionAttributeValues().get(":fcValue").s()); | ||
assertEquals(UPDATE_WITH_FAILURE_CODE_EXPRESSION, updateItemRequestCaptor.getValue().updateExpression()); | ||
assertEquals(CREATED_AT_CONDITION_EXPRESSION, updateItemRequestCaptor.getValue().conditionExpression()); | ||
} | ||
|
||
@Test | ||
void updateConversationStatusWithFailure_ValidInboundConversationIdAndFailureCodeWithNoCreatedAtDate_ShouldThrowConversationUpdateException() { | ||
// given | ||
final String inboundConversationId = "8A9B0CAD-2E3F-4567-ABCD-EF8901234567"; | ||
final String failureCode = "19"; | ||
|
||
// when | ||
doThrow(ConditionalCheckFailedException.class) | ||
.when(dynamoDbClient) | ||
.updateItem(any(UpdateItemRequest.class)); | ||
|
||
// then | ||
assertThrows( | ||
ConversationUpdateException.class, | ||
() -> transferRepository.updateConversationStatusWithFailure(UUID.fromString(inboundConversationId), failureCode) | ||
); | ||
|
||
verify(dynamoDbClient).updateItem(updateItemRequestCaptor.capture()); | ||
assertEquals(inboundConversationId, updateItemRequestCaptor.getValue().key().get(INBOUND_CONVERSATION_ID.name).s()); | ||
} | ||
} |