|
| 1 | +/* |
| 2 | + * © 2024-2025 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. |
| 3 | + */ |
| 4 | +package com.sap.cds.feature.attachments.handler.draftservice; |
| 5 | + |
| 6 | +import static com.sap.cds.services.draft.Drafts.IS_ACTIVE_ENTITY; |
| 7 | +import static org.assertj.core.api.Assertions.assertThat; |
| 8 | + |
| 9 | +import com.sap.cds.feature.attachments.generated.test.cds4j.unit.test.testservice.RootTable_; |
| 10 | +import com.sap.cds.ql.CQL; |
| 11 | +import com.sap.cds.ql.Select; |
| 12 | +import com.sap.cds.ql.cqn.CqnSelect; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +class ActiveEntityModifierFlattenerTest { |
| 16 | + |
| 17 | + private static final String TEST_DRAFT_SERVICE_BOOKS = "test.DraftService.Books"; |
| 18 | + |
| 19 | + @Test |
| 20 | + void activeEntityReplacedToFalse() { |
| 21 | + var select = Select.from(RootTable_.class).where(root -> root.IsActiveEntity().eq(true)); |
| 22 | + |
| 23 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(false, RootTable_.CDS_NAME)); |
| 24 | + |
| 25 | + assertThat(result.toString()) |
| 26 | + .contains("{\"ref\":[\"IsActiveEntity\"]},\"=\",{\"val\":false}]}"); |
| 27 | + assertThat(result.toString()).doesNotContain("true"); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void activeEntityReplacedToTrue() { |
| 32 | + var select = Select.from(RootTable_.class).where(root -> root.IsActiveEntity().eq(false)); |
| 33 | + |
| 34 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(true, RootTable_.CDS_NAME)); |
| 35 | + |
| 36 | + assertThat(result.toString()).contains("{\"ref\":[\"IsActiveEntity\"]},\"=\",{\"val\":true}]}"); |
| 37 | + assertThat(result.toString()).doesNotContain("false"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void entityNameReplaced() { |
| 42 | + var select = Select.from(RootTable_.class).where(root -> root.IsActiveEntity().eq(true)); |
| 43 | + |
| 44 | + var result = |
| 45 | + CQL.copy(select, new ActiveEntityModifierFlattener(true, RootTable_.CDS_NAME + "_draft")); |
| 46 | + |
| 47 | + assertThat(result.toString()).contains("{\"ref\":[\"unit.test.TestService.RootTable_draft\"]}"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void nothingReplaced() { |
| 52 | + var select = Select.from(RootTable_.class).where(root -> root.IsActiveEntity().eq(true)); |
| 53 | + |
| 54 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(true, RootTable_.CDS_NAME)); |
| 55 | + |
| 56 | + assertThat(result).hasToString(select.toString()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void selectWithFilterReplace() { |
| 61 | + CqnSelect select = |
| 62 | + Select.from( |
| 63 | + TEST_DRAFT_SERVICE_BOOKS, |
| 64 | + c -> |
| 65 | + c.filter(e -> e.get(IS_ACTIVE_ENTITY).eq(false)) |
| 66 | + .to("relatedMovies") |
| 67 | + .filter(e -> e.get(IS_ACTIVE_ENTITY).eq(false)) |
| 68 | + .to("relatedBook")); |
| 69 | + |
| 70 | + var result = |
| 71 | + CQL.copy(select, new ActiveEntityModifierFlattener(true, TEST_DRAFT_SERVICE_BOOKS)); |
| 72 | + |
| 73 | + assertThat(result.toString()).contains("{\"ref\":[\"IsActiveEntity\"]},\"=\",{\"val\":true}]}"); |
| 74 | + assertThat(result.toString()).doesNotContain("false"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void onlyRefActiveEntityIsReplaced() { |
| 79 | + var select = |
| 80 | + Select.from(RootTable_.class) |
| 81 | + .where( |
| 82 | + root -> |
| 83 | + root.IsActiveEntity() |
| 84 | + .eq(true) |
| 85 | + .and( |
| 86 | + root.HasActiveEntity() |
| 87 | + .eq(true) |
| 88 | + .and( |
| 89 | + CQL.constant(true) |
| 90 | + .eq(root.IsActiveEntity()) |
| 91 | + .and(CQL.constant(true).eq(root.HasActiveEntity()))))); |
| 92 | + |
| 93 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(false, RootTable_.CDS_NAME)); |
| 94 | + |
| 95 | + assertThat(result.toString()).contains("{\"ref\":[\"IsActiveEntity\"]},\"=\",{\"val\":false}"); |
| 96 | + assertThat(result.toString()).contains("{\"ref\":[\"HasActiveEntity\"]},\"=\",{\"val\":true}"); |
| 97 | + assertThat(result.toString()).contains("{\"val\":false},\"=\",{\"ref\":[\"IsActiveEntity\"]}"); |
| 98 | + assertThat(result.toString()).contains("{\"val\":true},\"=\",{\"ref\":[\"HasActiveEntity\"]}"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void nestedReferenceWithFilterIsFlattened() { |
| 103 | + // Create a nested reference with a filter on the last segment: AdminService.Books -> covers |
| 104 | + // (with filter) |
| 105 | + CqnSelect select = |
| 106 | + Select.from( |
| 107 | + "AdminService.Books", |
| 108 | + c -> c.to("covers").filter(e -> e.get(IS_ACTIVE_ENTITY).eq(false))); |
| 109 | + |
| 110 | + // The target entity name should be the flattened version |
| 111 | + String targetEntityName = "AdminService.Books.covers"; |
| 112 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(true, targetEntityName)); |
| 113 | + |
| 114 | + // The result should have the flattened entity name |
| 115 | + assertThat(result.toString()).contains(targetEntityName); |
| 116 | + // The filter should be modified to use the new IsActiveEntity value |
| 117 | + assertThat(result.toString()).contains("{\"ref\":[\"IsActiveEntity\"]},\"=\",{\"val\":true}]}"); |
| 118 | + // Should not contain the original nested structure |
| 119 | + assertThat(result.toString()).doesNotContain("\"to\":"); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void nestedReferenceWithoutFilterIsFlattened() { |
| 124 | + // Create a nested reference with a filter on the last segment: AdminService.Books -> covers |
| 125 | + // (with filter) |
| 126 | + CqnSelect select = Select.from("AdminService.Books", c -> c.to("covers")); |
| 127 | + |
| 128 | + // The target entity name should be the flattened version |
| 129 | + String targetEntityName = "AdminService.Books.covers"; |
| 130 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(true, targetEntityName)); |
| 131 | + |
| 132 | + // The result should have the flattened entity name |
| 133 | + assertThat(result.toString()).contains(targetEntityName); |
| 134 | + // Should not contain the original nested structure |
| 135 | + assertThat(result.toString()).doesNotContain("\"to\":"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void nestedReferenceWithNonMatchingRootIsNotFlattened() { |
| 140 | + // Create a nested reference where root doesn't match: OtherService.Books -> covers |
| 141 | + CqnSelect select = Select.from("OtherService.Books", c -> c.to("covers")); |
| 142 | + |
| 143 | + // Target entity is AdminService.Books_covers - root doesn't match |
| 144 | + String targetEntityName = "AdminService.Books.covers"; |
| 145 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(true, targetEntityName)); |
| 146 | + |
| 147 | + // Since the root "OtherService.Books" doesn't match "AdminService.Books" (from |
| 148 | + // targetEntityName), |
| 149 | + // it should follow the normal path and just replace the root entity name |
| 150 | + assertThat(result.toString()).contains(targetEntityName); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void singleSegmentReferenceIsNotFlattened() { |
| 155 | + // Test with single segment (no nesting) to ensure else branch is covered |
| 156 | + CqnSelect select = Select.from("AdminService.Books"); |
| 157 | + |
| 158 | + String targetEntityName = "AdminService.Books_drafts"; |
| 159 | + var result = CQL.copy(select, new ActiveEntityModifierFlattener(false, targetEntityName)); |
| 160 | + |
| 161 | + // Should follow the normal path (else branch) since there's only one segment |
| 162 | + assertThat(result.toString()).contains(targetEntityName); |
| 163 | + } |
| 164 | +} |
0 commit comments