Skip to content

Commit a12bc05

Browse files
add & update tests
1 parent 8218f99 commit a12bc05

11 files changed

Lines changed: 770 additions & 396 deletions

File tree

cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/common/ApplicationHandlerHelper.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,12 @@ public final class ApplicationHandlerHelper {
3737
private static final String ANNOTATION_IS_MEDIA_DATA = "_is_media_data";
3838
private static final String ANNOTATION_CORE_MEDIA_TYPE = "Core.MediaType";
3939
private static final ObjectMapper objectMapper = new ObjectMapper();
40+
private static final TypeReference<List<String>> STRING_LIST_TYPE_REF = new TypeReference<>() {};
4041

4142
/** Filter to support extraction of file name for attachment validation */
4243
public static final Filter FILE_NAME_FILTER =
4344
(path, element, type) -> element.getName().contentEquals("fileName");
4445

45-
/**
46-
* A filter for acceptable media types. The filter checks if the entity is a media entity and if
47-
* the element has the annotation "Core.AcceptableMediaTypes".
48-
*/
49-
public static final Filter ACCEPTABLE_MEDIA_TYPE_FILTER =
50-
(path, element, type) ->
51-
element.getName().contentEquals("content")
52-
&& element.findAnnotation("Core.AcceptableMediaTypes").isPresent();
53-
5446
/**
5547
* A filter for media content fields. The filter checks if the entity is a media entity and if the
5648
* element has the annotation "Core.MediaType".
@@ -161,20 +153,19 @@ public static void validateAcceptableMediaTypes(
161153
if (serviceEntity == null || !isMediaEntity(serviceEntity)) {
162154
return;
163155
}
164-
List<String> allowedTypes = getEntityAcceptableMediaTypes(serviceEntity, cdsModel);
156+
List<String> allowedTypes = getEntityAcceptableMediaTypes(serviceEntity);
165157
String fileName = extractFileName(entity, data);
166158
AttachmentValidationHelper.validateMediaTypeForAttachment(fileName, allowedTypes);
167159
}
168160

169-
protected static List<String> getEntityAcceptableMediaTypes(CdsEntity entity, CdsModel cdsModel) {
161+
protected static List<String> getEntityAcceptableMediaTypes(CdsEntity entity) {
170162
Optional<CdsAnnotation<Object>> flatMap =
171163
entity.getElement("content").findAnnotation("Core.AcceptableMediaTypes");
172164
List<String> result =
173165
flatMap
174166
.map(
175167
annotation ->
176-
objectMapper.convertValue(
177-
annotation.getValue(), new TypeReference<List<String>>() {}))
168+
objectMapper.convertValue(annotation.getValue(), STRING_LIST_TYPE_REF))
178169
.orElse(List.of("*/*"));
179170
return result;
180171
}

cds-feature-attachments/src/test/java/com/sap/cds/feature/attachments/handler/applicationservice/CreateAttachmentsHandlerTest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ static void classSetup() {
8080
void setup() {
8181
eventFactory = mock(ModifyAttachmentEventFactory.class);
8282
storageReader = mock(ThreadDataStorageReader.class);
83-
cut = new CreateAttachmentsHandler(
84-
eventFactory, storageReader, ModifyApplicationHandlerHelper.DEFAULT_SIZE_WITH_SCANNER);
83+
cut =
84+
new CreateAttachmentsHandler(
85+
eventFactory,
86+
storageReader,
87+
ModifyApplicationHandlerHelper.DEFAULT_SIZE_WITH_SCANNER,
88+
runtime);
8589

8690
createContext = mock(CdsCreateEventContext.class);
8791
event = mock(ModifyAttachmentEvent.class);
@@ -335,7 +339,8 @@ void restoreError_proceedsSuccessfully_noException() {
335339
@Test
336340
void restoreError_contentTooLargeWithMaxSize_throwsWithMaxSize() {
337341
var context = mock(EventContext.class);
338-
var originalException = new ServiceException(ExtendedErrorStatuses.CONTENT_TOO_LARGE, "original message");
342+
var originalException =
343+
new ServiceException(ExtendedErrorStatuses.CONTENT_TOO_LARGE, "original message");
339344
doThrow(originalException).when(context).proceed();
340345
when(context.get("attachment.MaxSize")).thenReturn("10MB");
341346

@@ -349,7 +354,8 @@ void restoreError_contentTooLargeWithMaxSize_throwsWithMaxSize() {
349354
@Test
350355
void restoreError_contentTooLargeWithoutMaxSize_throwsWithoutMaxSize() {
351356
var context = mock(EventContext.class);
352-
var originalException = new ServiceException(ExtendedErrorStatuses.CONTENT_TOO_LARGE, "original message");
357+
var originalException =
358+
new ServiceException(ExtendedErrorStatuses.CONTENT_TOO_LARGE, "original message");
353359
doThrow(originalException).when(context).proceed();
354360
when(context.get("attachment.MaxSize")).thenReturn(null);
355361

@@ -386,14 +392,15 @@ void restoreError_methodHasCorrectAnnotations() throws NoSuchMethodException {
386392

387393
@Test
388394
void processBeforeForMetadata_methodHasCorrectAnnotations() throws NoSuchMethodException {
389-
Method method = cut.getClass().getDeclaredMethod("processBeforeForMetadata", EventContext.class, List.class);
395+
Method method =
396+
cut.getClass()
397+
.getDeclaredMethod("processBeforeForMetadata", EventContext.class, List.class);
390398

391399
Before beforeAnnotation = method.getAnnotation(Before.class);
392400
HandlerOrder handlerOrderAnnotation = method.getAnnotation(HandlerOrder.class);
393401

394402
assertThat(beforeAnnotation.event())
395-
.containsExactlyInAnyOrder(
396-
CqnService.EVENT_CREATE, DraftService.EVENT_DRAFT_NEW);
403+
.containsExactlyInAnyOrder(CqnService.EVENT_CREATE, DraftService.EVENT_DRAFT_NEW);
397404
assertThat(handlerOrderAnnotation.value()).isEqualTo(HandlerOrder.BEFORE);
398405
}
399406

@@ -404,13 +411,17 @@ void processBeforeForMetadata_executesValidation() {
404411
List<CdsData> data = List.of(mock(CdsData.class));
405412
when(context.getTarget()).thenReturn(entity);
406413

407-
try (MockedStatic<ApplicationHandlerHelper> helper = mockStatic(ApplicationHandlerHelper.class)) {
408-
helper.when(() -> ApplicationHandlerHelper.validateAcceptableMediaTypes(entity, data))
414+
try (MockedStatic<ApplicationHandlerHelper> helper =
415+
mockStatic(ApplicationHandlerHelper.class)) {
416+
helper
417+
.when(() -> ApplicationHandlerHelper.validateAcceptableMediaTypes(entity, data, runtime))
409418
.thenAnswer(invocation -> null);
410419
// when
411-
new CreateAttachmentsHandler(eventFactory, storageReader, "400MB").processBeforeForMetadata(context, data);
420+
new CreateAttachmentsHandler(eventFactory, storageReader, "400MB", runtime)
421+
.processBeforeForMetadata(context, data);
412422
// then
413-
helper.verify(() -> ApplicationHandlerHelper.validateAcceptableMediaTypes(entity, data));
423+
helper.verify(
424+
() -> ApplicationHandlerHelper.validateAcceptableMediaTypes(entity, data, runtime));
414425
}
415426
}
416427

0 commit comments

Comments
 (0)