|
4 | 4 | package com.sap.cds.feature.attachments.service.handler; |
5 | 5 |
|
6 | 6 | import static org.assertj.core.api.Assertions.assertThat; |
| 7 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
7 | 9 | import static org.mockito.Mockito.mock; |
8 | 10 | import static org.mockito.Mockito.verify; |
9 | 11 | import static org.mockito.Mockito.when; |
|
19 | 21 | import com.sap.cds.reflect.CdsEntity; |
20 | 22 | import com.sap.cds.services.changeset.ChangeSetListener; |
21 | 23 | import com.sap.cds.services.impl.changeset.ChangeSetContextImpl; |
| 24 | +import java.util.Collections; |
22 | 25 | import java.util.Map; |
23 | 26 | import java.util.Objects; |
24 | 27 | import org.junit.jupiter.api.AfterEach; |
@@ -103,6 +106,38 @@ void malwareScannerRegisteredForEndOfTransaction() { |
103 | 106 | verify(malwareScanProvider).getChangeSetListener(entity, "contentId"); |
104 | 107 | } |
105 | 108 |
|
| 109 | + @Test |
| 110 | + void createAttachment_emptyAttachmentIds_handlesGracefully() { |
| 111 | + var createContext = AttachmentCreateEventContext.create(); |
| 112 | + createContext.setAttachmentIds(Collections.emptyMap()); |
| 113 | + createContext.setData(MediaData.create()); |
| 114 | + createContext.setAttachmentEntity(mock(CdsEntity.class)); |
| 115 | + ChangeSetContextImpl.open(false); |
| 116 | + |
| 117 | + cut.createAttachment(createContext); |
| 118 | + |
| 119 | + assertThat(createContext.getContentId()) |
| 120 | + .as("contentId should be null when Attachments.ID key is missing from attachmentIds") |
| 121 | + .isNull(); |
| 122 | + assertThat(createContext.isCompleted()).isTrue(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void afterCreateAttachment_noChangeSetContext_throws() { |
| 127 | + var entity = mock(CdsEntity.class); |
| 128 | + when(malwareScanProvider.getChangeSetListener(any(), any())) |
| 129 | + .thenReturn(mock(ChangeSetListener.class)); |
| 130 | + var createContext = AttachmentCreateEventContext.create(); |
| 131 | + createContext.setAttachmentIds(Map.of(Attachments.ID, "some-id")); |
| 132 | + createContext.setData(MediaData.create()); |
| 133 | + createContext.setAttachmentEntity(entity); |
| 134 | + |
| 135 | + cut.createAttachment(createContext); |
| 136 | + |
| 137 | + assertThatThrownBy(() -> cut.afterCreateAttachment(createContext)) |
| 138 | + .isInstanceOf(NullPointerException.class); |
| 139 | + } |
| 140 | + |
106 | 141 | private void closeChangeSetContext() throws Exception { |
107 | 142 | var context = ChangeSetContextImpl.getCurrent(); |
108 | 143 | if (Objects.nonNull(context)) { |
|
0 commit comments