|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.infrastructure.documentmanagement.service; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 24 | +import static org.mockito.ArgumentMatchers.anyString; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | + |
| 29 | +import java.io.ByteArrayInputStream; |
| 30 | +import java.io.InputStream; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Optional; |
| 33 | +import org.apache.fineract.infrastructure.contentstore.data.ContentStoreType; |
| 34 | +import org.apache.fineract.infrastructure.contentstore.detector.ContentDetectorManager; |
| 35 | +import org.apache.fineract.infrastructure.contentstore.service.ContentStoreService; |
| 36 | +import org.apache.fineract.infrastructure.documentmanagement.adapter.EntityImageIdAdapter; |
| 37 | +import org.apache.fineract.infrastructure.documentmanagement.data.ImageCreateRequest; |
| 38 | +import org.apache.fineract.infrastructure.documentmanagement.domain.Image; |
| 39 | +import org.apache.fineract.infrastructure.documentmanagement.domain.ImageRepository; |
| 40 | +import org.junit.jupiter.api.BeforeEach; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 43 | +import org.mockito.ArgumentCaptor; |
| 44 | +import org.mockito.Mock; |
| 45 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 46 | + |
| 47 | +@ExtendWith(MockitoExtension.class) |
| 48 | +class ImageWritePlatformServiceImplTest { |
| 49 | + |
| 50 | + @Mock |
| 51 | + private EntityImageIdAdapter clientImageIdAdapter; |
| 52 | + @Mock |
| 53 | + private EntityImageIdAdapter staffImageIdAdapter; |
| 54 | + @Mock |
| 55 | + private ContentStoreService contentStoreService; |
| 56 | + @Mock |
| 57 | + private ImageRepository imageRepository; |
| 58 | + @Mock |
| 59 | + private ContentDetectorManager contentDetectorManager; |
| 60 | + |
| 61 | + private ImageWritePlatformServiceImpl underTest; |
| 62 | + |
| 63 | + @BeforeEach |
| 64 | + void setUp() { |
| 65 | + underTest = new ImageWritePlatformServiceImpl(List.of(clientImageIdAdapter, staffImageIdAdapter), contentStoreService, |
| 66 | + imageRepository, contentDetectorManager); |
| 67 | + |
| 68 | + when(contentStoreService.getDelimiter()).thenReturn("/"); |
| 69 | + when(contentStoreService.getType()).thenReturn(ContentStoreType.FILE_SYSTEM); |
| 70 | + when(contentStoreService.upload(anyString(), any(InputStream.class), anyString())) |
| 71 | + .thenAnswer(invocation -> invocation.getArgument(0)); |
| 72 | + when(imageRepository.save(any(Image.class))).thenAnswer(invocation -> { |
| 73 | + Image image = invocation.getArgument(0); |
| 74 | + image.setId(99L); |
| 75 | + return image; |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void createImageShouldStoreStaffImageUnderStaffDirectory() { |
| 81 | + Long entityId = 7L; |
| 82 | + when(staffImageIdAdapter.accept("staff")).thenReturn(true); |
| 83 | + when(staffImageIdAdapter.get(entityId)).thenReturn(Optional.empty()); |
| 84 | + when(staffImageIdAdapter.set(eq(entityId), anyLong())).thenReturn(Optional.empty()); |
| 85 | + when(clientImageIdAdapter.accept(anyString())).thenReturn(false); |
| 86 | + |
| 87 | + ImageCreateRequest request = ImageCreateRequest.builder().entityType("STAFF").entityId(entityId).fileName("profile.png") |
| 88 | + .type("image/png").stream(new ByteArrayInputStream(new byte[] { 1, 2, 3 })).build(); |
| 89 | + |
| 90 | + var response = underTest.createImage(request); |
| 91 | + |
| 92 | + ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class); |
| 93 | + verify(contentStoreService).upload(pathCaptor.capture(), any(InputStream.class), eq("image/png")); |
| 94 | + assertEquals("images/staff/7/profile.png", pathCaptor.getValue()); |
| 95 | + assertEquals(99L, response.getResourceId()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void createImageShouldStoreClientImageUnderClientsDirectory() { |
| 100 | + Long entityId = 7L; |
| 101 | + when(clientImageIdAdapter.accept("clients")).thenReturn(true); |
| 102 | + when(clientImageIdAdapter.get(entityId)).thenReturn(Optional.empty()); |
| 103 | + when(clientImageIdAdapter.set(eq(entityId), anyLong())).thenReturn(Optional.empty()); |
| 104 | + |
| 105 | + ImageCreateRequest request = ImageCreateRequest.builder().entityType("CLIENTS").entityId(entityId).fileName("profile.png") |
| 106 | + .type("image/png").stream(new ByteArrayInputStream(new byte[] { 1, 2, 3 })).build(); |
| 107 | + |
| 108 | + var response = underTest.createImage(request); |
| 109 | + |
| 110 | + ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class); |
| 111 | + verify(contentStoreService).upload(pathCaptor.capture(), any(InputStream.class), eq("image/png")); |
| 112 | + assertEquals("images/clients/7/profile.png", pathCaptor.getValue()); |
| 113 | + assertEquals(99L, response.getResourceId()); |
| 114 | + } |
| 115 | +} |
0 commit comments