|
| 1 | +package com.consentframework.consenthistory.api.infrastructure.mappers; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | + |
| 7 | +import com.consentframework.consenthistory.api.infrastructure.entities.DynamoDbServiceUserConsentHistoryRecord; |
| 8 | +import com.consentframework.consenthistory.api.models.Consent; |
| 9 | +import com.consentframework.consenthistory.api.models.ConsentChangeEvent; |
| 10 | +import com.consentframework.consenthistory.api.models.ConsentEventType; |
| 11 | +import com.consentframework.consenthistory.api.testcommon.constants.TestConstants; |
| 12 | +import com.consentframework.consenthistory.api.testcommon.utils.DynamoDbServiceUserConsentHistoryRecordGenerator; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 15 | + |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +class DynamoDbConsentChangeEventMapperTest { |
| 19 | + @Test |
| 20 | + void toConsentChangeEventWhenNull() { |
| 21 | + final ConsentChangeEvent result = DynamoDbConsentChangeEventMapper.toConsentChangeEvent(null); |
| 22 | + assertNull(result); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void toConsentWhenNoImages() { |
| 27 | + final DynamoDbServiceUserConsentHistoryRecord ddbRecord = DynamoDbServiceUserConsentHistoryRecordGenerator.generate( |
| 28 | + null, null, ConsentEventType.MODIFY); |
| 29 | + validateConvertedConsentChangeEvent(ddbRecord); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void toConsentForConsentInsertEvent() { |
| 34 | + final Map<String, AttributeValue> ddbOldImage = null; |
| 35 | + final Map<String, AttributeValue> ddbNewImage = DynamoDbServiceUserConsentHistoryRecordGenerator.generateDdbConsentImage("1"); |
| 36 | + final DynamoDbServiceUserConsentHistoryRecord ddbRecord = DynamoDbServiceUserConsentHistoryRecordGenerator.generate( |
| 37 | + ddbOldImage, ddbNewImage, ConsentEventType.INSERT); |
| 38 | + validateConvertedConsentChangeEvent(ddbRecord); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void toConsentForConsentRemoveEvent() { |
| 43 | + final Map<String, AttributeValue> ddbOldImage = DynamoDbServiceUserConsentHistoryRecordGenerator.generateDdbConsentImage("1"); |
| 44 | + final Map<String, AttributeValue> ddbNewImage = null; |
| 45 | + final DynamoDbServiceUserConsentHistoryRecord ddbRecord = DynamoDbServiceUserConsentHistoryRecordGenerator.generate( |
| 46 | + ddbOldImage, ddbNewImage, ConsentEventType.REMOVE); |
| 47 | + validateConvertedConsentChangeEvent(ddbRecord); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void toConsentForConsentModifyEvent() { |
| 52 | + final Map<String, AttributeValue> ddbOldImage = DynamoDbServiceUserConsentHistoryRecordGenerator.generateDdbConsentImage("1"); |
| 53 | + final Map<String, AttributeValue> ddbNewImage = DynamoDbServiceUserConsentHistoryRecordGenerator.generateDdbConsentImage("2"); |
| 54 | + final DynamoDbServiceUserConsentHistoryRecord ddbRecord = DynamoDbServiceUserConsentHistoryRecordGenerator.generate( |
| 55 | + ddbOldImage, ddbNewImage, ConsentEventType.MODIFY); |
| 56 | + validateConvertedConsentChangeEvent(ddbRecord); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void toConsentForConsentUpdateEventDroppingAttributes() { |
| 61 | + final Map<String, AttributeValue> ddbOldImage = DynamoDbServiceUserConsentHistoryRecordGenerator.generateDdbConsentImage("1"); |
| 62 | + final Map<String, AttributeValue> ddbNewImage = DynamoDbServiceUserConsentHistoryRecordGenerator.generateDdbConsentImage( |
| 63 | + "2", null, null); |
| 64 | + final DynamoDbServiceUserConsentHistoryRecord ddbRecord = DynamoDbServiceUserConsentHistoryRecordGenerator.generate( |
| 65 | + ddbOldImage, ddbNewImage, ConsentEventType.MODIFY); |
| 66 | + |
| 67 | + final ConsentChangeEvent result = DynamoDbConsentChangeEventMapper.toConsentChangeEvent(ddbRecord); |
| 68 | + assertNotNull(result); |
| 69 | + assertEquals(ddbRecord.id(), result.getConsentId()); |
| 70 | + assertEquals(ddbRecord.eventId(), result.getEventId()); |
| 71 | + assertEquals(ddbRecord.eventTime(), result.getEventTime().toString()); |
| 72 | + assertEquals(ddbRecord.eventType(), result.getEventType().name()); |
| 73 | + validateConsentImage(ddbRecord.oldImage(), result.getOldImage(), TestConstants.TEST_CONSENT_DATA, TestConstants.TEST_EVENT_TIME); |
| 74 | + validateConsentImage(ddbRecord.newImage(), result.getNewImage(), null, null); |
| 75 | + } |
| 76 | + |
| 77 | + private void validateConvertedConsentChangeEvent(final DynamoDbServiceUserConsentHistoryRecord ddbRecord) { |
| 78 | + final ConsentChangeEvent result = DynamoDbConsentChangeEventMapper.toConsentChangeEvent(ddbRecord); |
| 79 | + assertNotNull(result); |
| 80 | + assertEquals(ddbRecord.id(), result.getConsentId()); |
| 81 | + assertEquals(ddbRecord.eventId(), result.getEventId()); |
| 82 | + assertEquals(ddbRecord.eventTime(), result.getEventTime().toString()); |
| 83 | + assertEquals(ddbRecord.eventType(), result.getEventType().name()); |
| 84 | + validateConsentImage(ddbRecord.oldImage(), result.getOldImage(), TestConstants.TEST_CONSENT_DATA, TestConstants.TEST_EVENT_TIME); |
| 85 | + validateConsentImage(ddbRecord.newImage(), result.getNewImage(), TestConstants.TEST_CONSENT_DATA, TestConstants.TEST_EVENT_TIME); |
| 86 | + } |
| 87 | + |
| 88 | + private void validateConsentImage(final Map<String, AttributeValue> ddbConsentImage, final Consent consent, |
| 89 | + final Map<String, String> expectedConsentData, final String expectedExpiryTime) { |
| 90 | + if (ddbConsentImage == null) { |
| 91 | + assertNull(consent); |
| 92 | + return; |
| 93 | + } |
| 94 | + assertNotNull(consent); |
| 95 | + assertEquals(TestConstants.TEST_SERVICE_ID, consent.getServiceId()); |
| 96 | + assertEquals(TestConstants.TEST_USER_ID, consent.getUserId()); |
| 97 | + assertEquals(TestConstants.TEST_CONSENT_ID, consent.getConsentId()); |
| 98 | + assertEquals(Integer.valueOf(ddbConsentImage.get(Consent.JSON_PROPERTY_CONSENT_VERSION).n()), consent.getConsentVersion()); |
| 99 | + assertEquals(ddbConsentImage.get(Consent.JSON_PROPERTY_STATUS).s(), consent.getStatus().name()); |
| 100 | + assertEquals(ddbConsentImage.get(Consent.JSON_PROPERTY_CONSENT_TYPE).s(), consent.getConsentType()); |
| 101 | + assertEquals(expectedConsentData, consent.getConsentData()); |
| 102 | + assertEquals(expectedExpiryTime, expectedExpiryTime == null ? null : consent.getExpiryTime().toString()); |
| 103 | + } |
| 104 | +} |
0 commit comments