Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
22 kB
2
Indexable
Never
package br.com.senior.erpxfnd.pessoa.service;

import br.com.senior.erpxfnd.pessoa.PessoaConstants;
import br.com.senior.erpxfnd.pessoa.RequestUploadInput;
import br.com.senior.erpxfnd.pessoa.RequestUploadOutput;
import br.com.senior.erpxfnd.pessoa.factory.StubFactory;
import br.com.senior.erpxfnd.pessoa.mapper.FileIdToBlobRequestMapper;
import br.com.senior.messaging.context.functional.TenantSupplier;
import br.com.senior.platform.blobservice.BlobDetails;
import br.com.senior.platform.blobservice.BlobRequest;
import br.com.senior.platform.blobservice.BlobServiceStub;
import br.com.senior.platform.blobservice.CommitFileInput;
import br.com.senior.platform.blobservice.CreateAreaInput;
import br.com.senior.platform.blobservice.DeleteFilesInput;
import br.com.senior.platform.blobservice.DeleteFilesOutput;
import br.com.senior.platform.blobservice.Location;
import br.com.senior.platform.blobservice.RequestDownloadsInput;
import br.com.senior.platform.blobservice.RequestDownloadsOutput;
import br.com.senior.platform.blobservice.Requirement;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

class BlobServiceTest {
    
    private static final long TIMEOUT = 20_000;

    @Nested
    @FieldDefaults(level = AccessLevel.PRIVATE)
    class RequestUploadTest {

        @InjectMocks
        BlobService blobService;

        @Mock
        TenantSupplier tenantSupplier;
        @Mock
        StubFactory stubFactory;

        @Mock
        BlobServiceStub blobServiceStub;

        @Captor
        ArgumentCaptor<CreateAreaInput> createAreaInputArgumentCaptor;
        @Captor
        ArgumentCaptor<BlobRequest> blobRequestArgumentCaptor;

        @BeforeEach
        void beforeEach() {
            initMocks(this);
            blobService.postConstruct();
        }

        @Test
        void shouldCreateAreaAndRequestUpload() {
            final String tenant = RandomStringUtils.randomAlphanumeric(32);
            final RequestUploadInput input = new RequestUploadInput();
            input.fileName = RandomStringUtils.randomAlphanumeric(32);
            input.thumbnail = false;

            final BlobDetails blobDetails = new BlobDetails();
            blobDetails.fileName = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.domainName = "erpx_fnd";
            blobDetails.serviceName = "pessoa";
            blobDetails.location = new Location();
            blobDetails.location.uri = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.targetObjectId = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.token = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.version = RandomStringUtils.randomAlphanumeric(32);

            when(stubFactory.getBlobServiceStub()).thenReturn(blobServiceStub);
            when(tenantSupplier.get()).thenReturn(tenant);

            when(blobServiceStub.requestUpload(any(), eq(20_000L))).thenReturn(blobDetails);

            final RequestUploadOutput result = blobService.requestUpload(input);

            final InOrder inOrder = inOrder(blobServiceStub);
            inOrder.verify(blobServiceStub).createArea(createAreaInputArgumentCaptor.capture(), eq(20_000L));
            inOrder.verify(blobServiceStub).requestUpload(blobRequestArgumentCaptor.capture(), eq(20_000L));
            inOrder.verifyNoMoreInteractions();

            final CreateAreaInput createAreaInput = createAreaInputArgumentCaptor.getValue();
            final BlobRequest blobRequest = blobRequestArgumentCaptor.getValue();

            assertThat(createAreaInput.areaSecret).isEqualTo("V12IPNX0C9OILUSCJTT2UZO491HEQWQM");
            assertThat(createAreaInput.domainName).isEqualTo("erpx_fnd");
            assertThat(createAreaInput.serviceName).isEqualTo("pessoa");

            assertThat(blobRequest.areaSecret).isEqualTo("V12IPNX0C9OILUSCJTT2UZO491HEQWQM");
            assertThat(blobRequest.domainName).isEqualTo("erpx_fnd");
            assertThat(blobRequest.serviceName).isEqualTo("pessoa");
            assertThat(blobRequest.fileName).isEqualTo(input.fileName);
            assertThat(blobRequest.requirements).containsOnly(Requirement.External);
            assertThat(blobRequest.style).isNull();
            assertThat(blobRequest.supportedProtocols).isNullOrEmpty();
            assertThat(blobRequest.targetObjectId).isNotNull();
            assertThat(blobRequest.token).isNull();
            assertThat(blobRequest.ttl).isEqualTo(86400L);
            assertThat(blobRequest.version).isNull();

            assertThat(result.fileId).isEqualTo("%s/%s", blobRequest.targetObjectId, blobDetails.fileName);
            assertThat(result.locationURI).isEqualTo(blobDetails.location.uri);
            assertThat(result.version).isEqualTo(blobDetails.version);
        }

        @Test
        void shouldCreateAreaAndRequestUploadForThumbnail() {
            final String tenant = RandomStringUtils.randomAlphanumeric(32);
            final RequestUploadInput input = new RequestUploadInput();
            input.fileName = RandomStringUtils.randomAlphanumeric(32);
            input.thumbnail = true;

            final BlobDetails blobDetails = new BlobDetails();
            blobDetails.fileName = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.domainName = "erpx_fnd";
            blobDetails.serviceName = "pessoa";
            blobDetails.location = new Location();
            blobDetails.location.uri = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.targetObjectId = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.token = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.version = RandomStringUtils.randomAlphanumeric(32);

            when(stubFactory.getBlobServiceStub()).thenReturn(blobServiceStub);
            when(tenantSupplier.get()).thenReturn(tenant);

            when(blobServiceStub.requestUpload(any(), eq(20_000L))).thenReturn(blobDetails);

            final RequestUploadOutput result = blobService.requestUpload(input);

            final InOrder inOrder = inOrder(blobServiceStub);
            inOrder.verify(blobServiceStub).createArea(createAreaInputArgumentCaptor.capture(), eq(20_000L));
            inOrder.verify(blobServiceStub).requestUpload(blobRequestArgumentCaptor.capture(), eq(20_000L));
            inOrder.verifyNoMoreInteractions();

            final CreateAreaInput createAreaInput = createAreaInputArgumentCaptor.getValue();
            final BlobRequest blobRequest = blobRequestArgumentCaptor.getValue();

            assertThat(createAreaInput.areaSecret).isEqualTo("V12IPNX0C9OILUSCJTT2UZO491HEQWQM");
            assertThat(createAreaInput.domainName).isEqualTo("erpx_fnd");
            assertThat(createAreaInput.serviceName).isEqualTo("pessoa");

            assertThat(blobRequest.areaSecret).isEqualTo("V12IPNX0C9OILUSCJTT2UZO491HEQWQM");
            assertThat(blobRequest.domainName).isEqualTo("erpx_fnd");
            assertThat(blobRequest.serviceName).isEqualTo("pessoa");
            assertThat(blobRequest.fileName).isEqualTo(input.fileName);
            assertThat(blobRequest.requirements).containsOnly(Requirement.External);
            assertThat(blobRequest.style).isEqualTo("128x128>");
            assertThat(blobRequest.supportedProtocols).isNullOrEmpty();
            assertThat(blobRequest.targetObjectId).isNotNull();
            assertThat(blobRequest.token).isNull();
            assertThat(blobRequest.ttl).isEqualTo(86400L);
            assertThat(blobRequest.version).isNull();

            assertThat(result.fileId).isEqualTo("%s/%s", blobRequest.targetObjectId, blobDetails.fileName);
            assertThat(result.locationURI).isEqualTo(blobDetails.location.uri);
            assertThat(result.version).isEqualTo(blobDetails.version);
        }

        @Test
        void shouldCreateAreaOnlyOncePerTenantAndRequestUpload() {
            final String tenant1 = RandomStringUtils.randomAlphanumeric(32);
            final String tenant2 = RandomStringUtils.randomAlphanumeric(32);
            final RequestUploadInput input = new RequestUploadInput();
            input.fileName = RandomStringUtils.randomAlphanumeric(32);
            input.thumbnail = false;

            final BlobDetails blobDetails = new BlobDetails();
            blobDetails.fileName = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.domainName = "erpx_fnd";
            blobDetails.serviceName = "pessoa";
            blobDetails.location = new Location();
            blobDetails.location.uri = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.targetObjectId = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.token = RandomStringUtils.randomAlphanumeric(32);
            blobDetails.version = RandomStringUtils.randomAlphanumeric(32);

            when(stubFactory.getBlobServiceStub()).thenReturn(blobServiceStub);
            when(tenantSupplier.get()).thenReturn(tenant1);

            when(blobServiceStub.requestUpload(any(), eq(20_000L))).thenReturn(blobDetails);

            assertThat(blobService.requestUpload(input)).isNotNull();
            verify(blobServiceStub).createArea(any(), eq(20_000L));
            verify(blobServiceStub).requestUpload(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);

            clearInvocations(blobServiceStub);

            assertThat(blobService.requestUpload(input)).isNotNull();
            verify(blobServiceStub).requestUpload(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);

            clearInvocations(blobServiceStub);

            when(tenantSupplier.get()).thenReturn(tenant2);

            assertThat(blobService.requestUpload(input)).isNotNull();
            verify(blobServiceStub).createArea(any(), eq(20_000L));
            verify(blobServiceStub).requestUpload(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);

            clearInvocations(blobServiceStub);

            assertThat(blobService.requestUpload(input)).isNotNull();
            verify(blobServiceStub).requestUpload(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);
        }

    }

    @Nested
    @FieldDefaults(level = AccessLevel.PRIVATE)
    class CommitThumbnailTest {

        @InjectMocks
        BlobService blobService;

        @Mock
        TenantSupplier tenantSupplier;
        @Mock
        StubFactory stubFactory;

        @Mock
        BlobServiceStub blobServiceStub;

        @Captor
        ArgumentCaptor<CreateAreaInput> createAreaInputArgumentCaptor;
        @Captor
        ArgumentCaptor<CommitFileInput> commitFileInputArgumentCaptor;

        @BeforeEach
        void beforeEach() {
            initMocks(this);
            blobService.postConstruct();
        }

        @Test
        void shouldCreateAreaAndCommitThumbnail() {
            final String targetObject = RandomStringUtils.randomAlphanumeric(32);
            final String fileName = RandomStringUtils.randomAlphanumeric(32);
            final String fileId = String.format("%s/%s", targetObject, fileName);
            final String version = RandomStringUtils.randomAlphanumeric(32);
            final String tenant = RandomStringUtils.randomAlphanumeric(32);

            when(stubFactory.getBlobServiceStub()).thenReturn(blobServiceStub);
            when(tenantSupplier.get()).thenReturn(tenant);

            blobService.commitThumbnail(fileId, version);

            final InOrder inOrder = inOrder(blobServiceStub);
            inOrder.verify(blobServiceStub).createArea(createAreaInputArgumentCaptor.capture(), eq(20_000L));
            inOrder.verify(blobServiceStub).commitFile(commitFileInputArgumentCaptor.capture(), eq(20_000L));
            inOrder.verifyNoMoreInteractions();

            final CreateAreaInput createAreaInput = createAreaInputArgumentCaptor.getValue();
            final CommitFileInput commitFileInput = commitFileInputArgumentCaptor.getValue();

            assertThat(createAreaInput.areaSecret).isEqualTo("V12IPNX0C9OILUSCJTT2UZO491HEQWQM");
            assertThat(createAreaInput.domainName).isEqualTo("erpx_fnd");
            assertThat(createAreaInput.serviceName).isEqualTo("pessoa");

            assertThat(commitFileInput.areaSecret).isEqualTo("V12IPNX0C9OILUSCJTT2UZO491HEQWQM");
            assertThat(commitFileInput.domainName).isEqualTo("erpx_fnd");
            assertThat(commitFileInput.serviceName).isEqualTo("pessoa");
            assertThat(commitFileInput.fileName).isEqualTo(fileName);
            assertThat(commitFileInput.targetObject).isEqualTo(targetObject);
            assertThat(commitFileInput.styles).containsOnly("128x128>");
            assertThat(commitFileInput.release).isTrue();
            assertThat(commitFileInput.version).isEqualTo(version);
        }

        @Test
        void shouldCreateAreaOnlyOncePerTenantAndCommitThumbnail() {
            final String targetObject = RandomStringUtils.randomAlphanumeric(32);
            final String fileName = RandomStringUtils.randomAlphanumeric(32);
            final String fileId = String.format("%s/%s", targetObject, fileName);
            final String version = RandomStringUtils.randomAlphanumeric(32);
            final String tenant1 = RandomStringUtils.randomAlphanumeric(32);
            final String tenant2 = RandomStringUtils.randomAlphanumeric(32);

            when(stubFactory.getBlobServiceStub()).thenReturn(blobServiceStub);
            when(tenantSupplier.get()).thenReturn(tenant1);

            blobService.commitThumbnail(fileId, version);

            verify(blobServiceStub).createArea(any(), eq(20_000L));
            verify(blobServiceStub).commitFile(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);

            clearInvocations(blobServiceStub);

            blobService.commitThumbnail(fileId, version);
            verify(blobServiceStub).commitFile(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);

            clearInvocations(blobServiceStub);

            when(tenantSupplier.get()).thenReturn(tenant2);

            blobService.commitThumbnail(fileId, version);
            verify(blobServiceStub).createArea(any(), eq(20_000L));
            verify(blobServiceStub).commitFile(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);

            clearInvocations(blobServiceStub);

            blobService.commitThumbnail(fileId, version);
            verify(blobServiceStub).commitFile(any(), eq(20_000L));
            verifyNoMoreInteractions(blobServiceStub);
        }

    }

    @Nested
    @FieldDefaults(level = AccessLevel.PRIVATE)
    class GetImagesUrlByFilesIdTest {

        @InjectMocks
        BlobService blobService;

        @Mock
        StubFactory stubFactory;

        @Mock
        FileIdToBlobRequestMapper fileIdToBlobRequestMapper;

        @Mock
        BlobServiceStub blobServiceStub;

        @BeforeEach
        void beforeEach() {
            initMocks(this);
            blobService.postConstruct();
        }

        @Test
        void verifyMethodCall() {
            // Given
            final HashSet<String> filesIds = new HashSet<>(List.of("id1", "id2", "id3", "id4", "id4"));
            final List<BlobRequest> blobRequests = List.of(new BlobRequest(), new BlobRequest(), new BlobRequest(), new BlobRequest(), new BlobRequest());
            final RequestDownloadsInput requestDownloadsInput = new RequestDownloadsInput(blobRequests);
            final RequestDownloadsOutput requestDownloadsOutput = new RequestDownloadsOutput();
            final ArrayList<String> employeesIds = new ArrayList<>(filesIds);

            when(fileIdToBlobRequestMapper.mapList(employeesIds)).thenReturn(blobRequests);
            when(stubFactory.getBlobServiceStub()).thenReturn(blobServiceStub);
            when(blobServiceStub.requestDownloads(requestDownloadsInput, 20_000)).thenReturn(requestDownloadsOutput);

            // When
            final List<BlobDetails> imagesUrlByFilesId = blobService.getImagesUrlByFilesId(filesIds);

            // Then
            assertThat(imagesUrlByFilesId).isSameAs(requestDownloadsOutput.blobDetails);
            verify(fileIdToBlobRequestMapper).mapList(employeesIds);
            verify(stubFactory).getBlobServiceStub();
            verify(blobServiceStub).requestDownloads(requestDownloadsInput, 20_000);
            verifyNoMoreInteractions(fileIdToBlobRequestMapper, stubFactory, blobServiceStub);
        }
    }
    
    @Nested
    @FieldDefaults(level = AccessLevel.PRIVATE)
    class GetImagesByFileIdTrustedTest {

        @InjectMocks
        BlobService blobService;

        @Mock
        StubFactory stubFactory;

        @Mock
        FileIdToBlobRequestMapper fileIdToBlobRequestMapper;

        @Mock
        BlobServiceStub blobServiceStub;

        @BeforeEach
        void beforeEach() {
            initMocks(this);
            blobService.postConstruct();
        }

        @Test
        void verifyMethodCall() {
            // Given
            final HashSet<String> filesIds = new HashSet<>(List.of("id1", "id2", "id3", "id4", "id4"));
            final List<BlobRequest> blobRequests = List.of(new BlobRequest(), new BlobRequest(), new BlobRequest(), new BlobRequest(), new BlobRequest());
            final RequestDownloadsInput requestDownloadsInput = new RequestDownloadsInput(blobRequests);
            final RequestDownloadsOutput requestDownloadsOutput = new RequestDownloadsOutput();
            final ArrayList<String> employeesIds = new ArrayList<>(filesIds);

            when(fileIdToBlobRequestMapper.mapList(employeesIds)).thenReturn(blobRequests);
            when(stubFactory.getBlobServiceTrustedStub()).thenReturn(blobServiceStub);
            when(blobServiceStub.requestDownloads(requestDownloadsInput, 20_000)).thenReturn(requestDownloadsOutput);

            // When
            final List<BlobDetails> imagesUrlByFilesId = blobService.getImagesByFileIdTrusted(filesIds);

            // Then
            assertThat(imagesUrlByFilesId).isSameAs(requestDownloadsOutput.blobDetails);
            verify(fileIdToBlobRequestMapper).mapList(employeesIds);
            verify(stubFactory).getBlobServiceTrustedStub();
            verify(blobServiceStub).requestDownloads(requestDownloadsInput, 20_000);
            verifyNoMoreInteractions(fileIdToBlobRequestMapper, stubFactory, blobServiceStub);
        }
    }
    
    @Nested
    @FieldDefaults(level = AccessLevel.PRIVATE)
    class DeleteFilesTest {
        
        @InjectMocks BlobService blobService;
        @Captor ArgumentCaptor<DeleteFilesInput> deleteFilesInput;
        @Mock StubFactory stubFactory;
        @Mock BlobServiceStub blobServiceStub;
        
        @BeforeEach
        void beforeEach() {
            initMocks(this);
            blobService.postConstruct();
            when(stubFactory.getBlobServiceTrustedStub()).thenReturn(blobServiceStub);
        }
        
        @Test
        public void shouldDeleteFiles() {
            Location location = new Location(null, "uri");
            BlobDetails blobDetails = new BlobDetails(PessoaConstants.DOMAIN, PessoaConstants.SERVICE, "objectId", location);
            blobDetails.fileName = "fileName";
            var result = new DeleteFilesOutput(true);
            when(blobServiceStub.deleteFiles(deleteFilesInput.capture(), eq(TIMEOUT))).thenReturn(result);
            
            assertTrue(blobService.deleteFiles(List.of(blobDetails)));
            verify(blobServiceStub).deleteFiles(deleteFilesInput.getValue(), TIMEOUT);
            assertThat(deleteFilesInput.getValue().deleteFileInput)
                .isNotEmpty()
                .first()
                .hasFieldOrPropertyWithValue("domainName", PessoaConstants.DOMAIN)
                .hasFieldOrPropertyWithValue("serviceName", PessoaConstants.SERVICE)
                .hasFieldOrPropertyWithValue("targetObject", "objectId")
                .hasFieldOrPropertyWithValue("areaSecret", "V12IPNX0C9OILUSCJTT2UZO491HEQWQM")
                .hasFieldOrPropertyWithValue("fileName", "fileName")
                .hasFieldOrPropertyWithValue("failIfNotExists", true);
        }
    }
}