2525import com .sap .cds .feature .attachments .service .handler .DefaultAttachmentsServiceHandler ;
2626import com .sap .cds .services .Service ;
2727import com .sap .cds .services .ServiceCatalog ;
28+ import com .sap .cds .services .cds .ApplicationService ;
2829import com .sap .cds .services .draft .DraftService ;
2930import com .sap .cds .services .environment .CdsEnvironment ;
3031import com .sap .cds .services .handler .EventHandler ;
@@ -42,6 +43,7 @@ class RegistrationTest {
4243 private AttachmentService attachmentService ;
4344 private OutboxService outboxService ;
4445 private DraftService draftService ;
46+ private ApplicationService applicationService ;
4547 private ArgumentCaptor <Service > serviceArgumentCaptor ;
4648 private ArgumentCaptor <EventHandler > handlerArgumentCaptor ;
4749
@@ -61,6 +63,7 @@ void setup() {
6163 attachmentService = mock (AttachmentService .class );
6264 outboxService = mock (OutboxService .class );
6365 draftService = mock (DraftService .class );
66+ applicationService = mock (ApplicationService .class );
6467 serviceArgumentCaptor = ArgumentCaptor .forClass (Service .class );
6568 handlerArgumentCaptor = ArgumentCaptor .forClass (EventHandler .class );
6669 }
@@ -87,6 +90,7 @@ void handlersAreRegistered() {
8790 when (serviceCatalog .getService (OutboxService .class , OutboxService .PERSISTENT_UNORDERED_NAME )).thenReturn (
8891 outboxService );
8992 when (serviceCatalog .getServices (DraftService .class )).thenReturn (Stream .of (draftService ));
93+ when (serviceCatalog .getServices (ApplicationService .class )).thenReturn (Stream .of (applicationService ));
9094
9195 cut .eventHandlers (configurer );
9296
@@ -104,9 +108,40 @@ void handlersAreRegistered() {
104108 isHandlerForClassIncluded (handlers , DraftActiveAttachmentsHandler .class );
105109 }
106110
111+ @ Test
112+ void lessHandlersAreRegistered () {
113+ when (serviceCatalog .getService (PersistenceService .class , PersistenceService .DEFAULT_NAME )).thenReturn (
114+ persistenceService );
115+ when (serviceCatalog .getService (AttachmentService .class , AttachmentService .DEFAULT_NAME )).thenReturn (
116+ attachmentService );
117+ when (serviceCatalog .getService (OutboxService .class , OutboxService .PERSISTENT_UNORDERED_NAME )).thenReturn (
118+ outboxService );
119+
120+ cut .eventHandlers (configurer );
121+
122+ var handlerSize = 1 ;
123+ verify (configurer , times (handlerSize )).eventHandler (handlerArgumentCaptor .capture ());
124+ var handlers = handlerArgumentCaptor .getAllValues ();
125+ assertThat (handlers ).hasSize (handlerSize );
126+ isHandlerForClassIncluded (handlers , DefaultAttachmentsServiceHandler .class );
127+ // event handlers for application services are not registered
128+ isHandlerForClassMissing (handlers , CreateAttachmentsHandler .class );
129+ isHandlerForClassMissing (handlers , UpdateAttachmentsHandler .class );
130+ isHandlerForClassMissing (handlers , DeleteAttachmentsHandler .class );
131+ isHandlerForClassMissing (handlers , ReadAttachmentsHandler .class );
132+ // event handlers for draft services are not registered
133+ isHandlerForClassMissing (handlers , DraftPatchAttachmentsHandler .class );
134+ isHandlerForClassMissing (handlers , DraftCancelAttachmentsHandler .class );
135+ isHandlerForClassMissing (handlers , DraftActiveAttachmentsHandler .class );
136+ }
137+
107138 private void isHandlerForClassIncluded (List <EventHandler > handlers , Class <? extends EventHandler > includedClass ) {
108139 var isHandlerIncluded = handlers .stream ().anyMatch (handler -> handler .getClass () == includedClass );
109140 assertThat (isHandlerIncluded ).isTrue ();
110141 }
142+ private void isHandlerForClassMissing (List <EventHandler > handlers , Class <? extends EventHandler > includedClass ) {
143+ var isHandlerIncluded = handlers .stream ().anyMatch (handler -> handler .getClass () == includedClass );
144+ assertThat (isHandlerIncluded ).isFalse ();
145+ }
111146
112147}
0 commit comments