-
Notifications
You must be signed in to change notification settings - Fork 7
Add dedicated unit tests for ReadonlyDataContextEnhancer #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
825a235
Add dedicated unit tests for ReadonlyDataContextEnhancer
Schmarvinius 5423b51
Apply spotless formatting to ReadonlyDataContextEnhancerTest
Schmarvinius a2b8f01
Address review: add edge case tests and negative assertions
Schmarvinius ca0a05f
Merge branch 'main' into unit1-readonly-data-context-enhancer-test
Schmarvinius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
...eature/attachments/handler/applicationservice/helper/ReadonlyDataContextEnhancerTest.java
This file contains hidden or 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,142 @@ | ||
| /* | ||
| * Β© 2024-2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.applicationservice.helper; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.sap.cds.CdsData; | ||
| import com.sap.cds.feature.attachments.generated.cds4j.sap.attachments.Attachments; | ||
| import com.sap.cds.feature.attachments.generated.test.cds4j.unit.test.Events_; | ||
| import com.sap.cds.feature.attachments.generated.test.cds4j.unit.test.testservice.Attachment_; | ||
| import com.sap.cds.feature.attachments.handler.helper.RuntimeHelper; | ||
| import com.sap.cds.reflect.CdsEntity; | ||
| import com.sap.cds.services.runtime.CdsRuntime; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ReadonlyDataContextEnhancerTest { | ||
|
|
||
| private static final String DRAFT_READONLY_CONTEXT = "DRAFT_READONLY_CONTEXT"; | ||
|
|
||
| private static CdsRuntime runtime; | ||
|
|
||
| @BeforeAll | ||
| static void classSetup() { | ||
| runtime = RuntimeHelper.runtime; | ||
| } | ||
|
|
||
| @Test | ||
| void preserveReadonlyFields_isDraft_backupCreated() { | ||
| CdsEntity entity = runtime.getCdsModel().findEntity(Attachment_.CDS_NAME).orElseThrow(); | ||
|
|
||
| var attachment = Attachments.create(); | ||
| attachment.setContentId("doc-123"); | ||
| attachment.setStatus("Clean"); | ||
| Instant scannedAt = Instant.parse("2024-06-01T12:00:00Z"); | ||
| attachment.setScannedAt(scannedAt); | ||
| attachment.setContent(null); | ||
|
|
||
| ReadonlyDataContextEnhancer.preserveReadonlyFields(entity, List.of(attachment), true); | ||
|
|
||
| assertThat(attachment.get(DRAFT_READONLY_CONTEXT)).isNotNull(); | ||
| var backup = (CdsData) attachment.get(DRAFT_READONLY_CONTEXT); | ||
| assertThat(backup) | ||
| .containsEntry(Attachments.CONTENT_ID, "doc-123") | ||
| .containsEntry(Attachments.STATUS, "Clean") | ||
| .containsEntry(Attachments.SCANNED_AT, scannedAt) | ||
| .doesNotContainKey(Attachments.CONTENT); | ||
| } | ||
|
|
||
| @Test | ||
| void preserveReadonlyFields_isNotDraft_backupRemoved() { | ||
| CdsEntity entity = runtime.getCdsModel().findEntity(Attachment_.CDS_NAME).orElseThrow(); | ||
|
|
||
| var attachment = Attachments.create(); | ||
| attachment.setContentId("doc-456"); | ||
| attachment.setContent(null); | ||
| var existingBackup = CdsData.create(); | ||
| existingBackup.put(Attachments.CONTENT_ID, "old-id"); | ||
| existingBackup.put(Attachments.STATUS, "old-status"); | ||
| existingBackup.put(Attachments.SCANNED_AT, Instant.EPOCH); | ||
| attachment.put(DRAFT_READONLY_CONTEXT, existingBackup); | ||
|
|
||
| ReadonlyDataContextEnhancer.preserveReadonlyFields(entity, List.of(attachment), false); | ||
|
|
||
| assertThat(attachment.get(DRAFT_READONLY_CONTEXT)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void preserveReadonlyFields_isDraft_noAttachmentEntity_nothingHappens() { | ||
| CdsEntity entity = runtime.getCdsModel().findEntity(Events_.CDS_NAME).orElseThrow(); | ||
|
|
||
| var data = CdsData.create(); | ||
| data.put("content", "some text"); | ||
|
|
||
| ReadonlyDataContextEnhancer.preserveReadonlyFields(entity, List.of(data), true); | ||
|
|
||
| assertThat(data.get(DRAFT_READONLY_CONTEXT)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void restoreReadonlyFields_withBackup_fieldsRestoredAndBackupRemoved() { | ||
| var data = CdsData.create(); | ||
| var backup = CdsData.create(); | ||
| backup.put(Attachments.CONTENT_ID, "restored-id"); | ||
| backup.put(Attachments.STATUS, "Infected"); | ||
| Instant scannedAt = Instant.parse("2025-01-15T08:30:00Z"); | ||
| backup.put(Attachments.SCANNED_AT, scannedAt); | ||
| data.put(DRAFT_READONLY_CONTEXT, backup); | ||
|
|
||
| ReadonlyDataContextEnhancer.restoreReadonlyFields(data); | ||
|
|
||
| assertThat(data.get(Attachments.CONTENT_ID)).isEqualTo("restored-id"); | ||
| assertThat(data.get(Attachments.STATUS)).isEqualTo("Infected"); | ||
| assertThat(data.get(Attachments.SCANNED_AT)).isEqualTo(scannedAt); | ||
| assertThat(data.get(DRAFT_READONLY_CONTEXT)).isNull(); | ||
| } | ||
|
Schmarvinius marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| void restoreReadonlyFields_withoutBackup_noOp() { | ||
| var data = CdsData.create(); | ||
| data.put("someKey", "someValue"); | ||
|
|
||
| ReadonlyDataContextEnhancer.restoreReadonlyFields(data); | ||
|
|
||
| assertThat(data).containsEntry("someKey", "someValue"); | ||
| assertThat(data).doesNotContainKey(DRAFT_READONLY_CONTEXT); | ||
| assertThat(data).doesNotContainKey(Attachments.CONTENT_ID); | ||
| assertThat(data).doesNotContainKey(Attachments.STATUS); | ||
| assertThat(data).doesNotContainKey(Attachments.SCANNED_AT); | ||
| } | ||
|
|
||
| @Test | ||
| void restoreReadonlyFields_withPartialBackup_nullsOverwriteExistingValues() { | ||
| var data = CdsData.create(); | ||
| data.put(Attachments.STATUS, "Clean"); | ||
| var backup = CdsData.create(); | ||
| backup.put(Attachments.CONTENT_ID, "restored-id"); | ||
| // STATUS and SCANNED_AT intentionally absent from backup | ||
| data.put(DRAFT_READONLY_CONTEXT, backup); | ||
|
|
||
| ReadonlyDataContextEnhancer.restoreReadonlyFields(data); | ||
|
|
||
| assertThat(data.get(Attachments.CONTENT_ID)).isEqualTo("restored-id"); | ||
| assertThat(data.get(Attachments.STATUS)).isNull(); | ||
| assertThat(data.get(Attachments.SCANNED_AT)).isNull(); | ||
| assertThat(data.get(DRAFT_READONLY_CONTEXT)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void preserveReadonlyFields_isNotDraft_noExistingBackup_nothingHappens() { | ||
| CdsEntity entity = runtime.getCdsModel().findEntity(Attachment_.CDS_NAME).orElseThrow(); | ||
| var attachment = Attachments.create(); | ||
| attachment.setContent(null); | ||
|
|
||
| ReadonlyDataContextEnhancer.preserveReadonlyFields(entity, List.of(attachment), false); | ||
|
|
||
| assertThat(attachment.get(DRAFT_READONLY_CONTEXT)).isNull(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.