66import static org .assertj .core .api .Assertions .assertThat ;
77import static org .mockito .ArgumentMatchers .any ;
88import static org .mockito .ArgumentMatchers .eq ;
9- import static org .mockito .Mockito .mock ;
10- import static org .mockito .Mockito .verify ;
11- import static org .mockito .Mockito .verifyNoInteractions ;
12- import static org .mockito .Mockito .when ;
9+ import static org .mockito .Mockito .*;
1310
1411import com .sap .cds .feature .attachments .generated .cds4j .sap .attachments .Attachments ;
1512import com .sap .cds .feature .attachments .generated .test .cds4j .unit .test .testservice .Attachment ;
@@ -70,6 +67,24 @@ void whereConditionIncludedNothingHappens() {
7067 verifyNoInteractions (attachmentsReader , deleteContentAttachmentEvent );
7168 }
7269
70+ @ Test
71+ void entityHasNoAttachmentsAndIsNotAttachmentEntityNothingHappens () {
72+ // Test the case where isAttachmentEntity and hasAttachmentAssociations both return false
73+ CdsEntity mockEntity = mock (CdsEntity .class );
74+ // Entity has no elements with name "attachment"
75+ when (mockEntity .getQualifiedName ())
76+ .thenReturn ("TestService.RegularEntity" ); // No "Attachment" in name
77+ when (eventContext .getTarget ()).thenReturn (mockEntity );
78+
79+ CqnDelete mockDelete = mock (CqnDelete .class );
80+ when (mockDelete .where ()).thenReturn (Optional .empty ());
81+ when (eventContext .getCqn ()).thenReturn (mockDelete );
82+
83+ cut .processBeforeDraftCancel (eventContext );
84+
85+ verifyNoInteractions (attachmentsReader );
86+ }
87+
7388 @ Test
7489 void nothingSelectedNothingToDo () {
7590 getEntityAndMockContext (RootTable_ .CDS_NAME );
@@ -84,6 +99,33 @@ void nothingSelectedNothingToDo() {
8499
85100 @ Test
86101 void attachmentReaderCorrectCalled () {
102+ getEntityAndMockContext (Attachment_ .CDS_NAME );
103+ CqnDelete delete = Delete .from (Attachment_ .class );
104+ when (eventContext .getCqn ()).thenReturn (delete );
105+ when (eventContext .getModel ()).thenReturn (runtime .getCdsModel ());
106+
107+ cut .processBeforeDraftCancel (eventContext );
108+
109+ CdsEntity target = eventContext .getTarget ();
110+ verify (attachmentsReader )
111+ .readAttachments (eq (runtime .getCdsModel ()), eq (target ), deleteArgumentCaptor .capture ());
112+ // Check if the modified CqnDelete that is passed to readAttachments looks correct
113+ CqnDelete modifiedCQN = deleteArgumentCaptor .getValue ();
114+ assertThat (modifiedCQN .toJson ())
115+ .isEqualTo (
116+ "{\" DELETE\" :{\" from\" :{\" ref\" :[{\" id\" :\" unit.test.TestService.Attachment\" ,\" where\" :[{\" ref\" :[\" IsActiveEntity\" ]},\" =\" ,{\" val\" :true}]}]}}}" );
117+
118+ deleteArgumentCaptor = ArgumentCaptor .forClass (CqnDelete .class );
119+ CdsEntity siblingTarget = target .getTargetOf (Drafts .SIBLING_ENTITY );
120+ verify (attachmentsReader )
121+ .readAttachments (
122+ eq (runtime .getCdsModel ()), eq (siblingTarget ), deleteArgumentCaptor .capture ());
123+ CqnDelete siblingDelete = deleteArgumentCaptor .getValue ();
124+ assertThat (siblingDelete .toJson ()).isNotEqualTo (delete .toJson ());
125+ }
126+
127+ @ Test
128+ void attachmentReaderCorrectCalledForEntityWithAttachmentAssociations () {
87129 getEntityAndMockContext (RootTable_ .CDS_NAME );
88130 CqnDelete delete = Delete .from (RootTable_ .class );
89131 when (eventContext .getCqn ()).thenReturn (delete );
@@ -94,8 +136,11 @@ void attachmentReaderCorrectCalled() {
94136 CdsEntity target = eventContext .getTarget ();
95137 verify (attachmentsReader )
96138 .readAttachments (eq (runtime .getCdsModel ()), eq (target ), deleteArgumentCaptor .capture ());
97- CqnDelete originDelete = deleteArgumentCaptor .getValue ();
98- assertThat (originDelete .toJson ()).isEqualTo (delete .toJson ());
139+ // Check if the modified CqnDelete that is passed to readAttachments looks correct
140+ CqnDelete modifiedCQN = deleteArgumentCaptor .getValue ();
141+ assertThat (modifiedCQN .toJson ())
142+ .isEqualTo (
143+ "{\" DELETE\" :{\" from\" :{\" ref\" :[{\" id\" :\" unit.test.TestService.RootTable\" ,\" where\" :[{\" ref\" :[\" IsActiveEntity\" ]},\" =\" ,{\" val\" :true}]}]}}}" );
99144
100145 deleteArgumentCaptor = ArgumentCaptor .forClass (CqnDelete .class );
101146 CdsEntity siblingTarget = target .getTargetOf (Drafts .SIBLING_ENTITY );
@@ -108,8 +153,8 @@ void attachmentReaderCorrectCalled() {
108153
109154 @ Test
110155 void modifierCalledWithCorrectEntitiesIfDraftIsInContext () {
111- getEntityAndMockContext (RootTable_ .CDS_NAME + DraftUtils .DRAFT_TABLE_POSTFIX );
112- CqnDelete delete = Delete .from (RootTable_ .class );
156+ getEntityAndMockContext (Attachment_ .CDS_NAME + DraftUtils .DRAFT_TABLE_POSTFIX );
157+ CqnDelete delete = Delete .from (Attachment_ .class );
113158 when (eventContext .getCqn ()).thenReturn (delete );
114159 when (eventContext .getModel ()).thenReturn (runtime .getCdsModel ());
115160
@@ -123,7 +168,9 @@ void modifierCalledWithCorrectEntitiesIfDraftIsInContext() {
123168 .readAttachments (
124169 eq (runtime .getCdsModel ()), eq (siblingTarget ), deleteArgumentCaptor .capture ());
125170 CqnDelete siblingDelete = deleteArgumentCaptor .getValue ();
126- assertThat (siblingDelete .toJson ()).isEqualTo (delete .toJson ());
171+ assertThat (siblingDelete .toJson ())
172+ .isEqualTo (
173+ "{\" DELETE\" :{\" from\" :{\" ref\" :[{\" id\" :\" unit.test.TestService.Attachment\" ,\" where\" :[{\" ref\" :[\" IsActiveEntity\" ]},\" =\" ,{\" val\" :true}]}]}}}" );
127174 }
128175
129176 @ Test
0 commit comments