Skip to content

Commit

Permalink
Merge branch 'sprint4_unit_test' into Main_sprint4_integration
Browse files Browse the repository at this point in the history
  • Loading branch information
asioleLab committed Nov 10, 2023
2 parents af9c676 + 86437bc commit 9fafed2
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 27 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<surefire-plugin.version>3.0.0</surefire-plugin.version>
<mockito.core.version>5.4.0</mockito.core.version>
<google.guava.version>32.1.3-jre</google.guava.version>
<easy.random.version>5.0.0</easy.random.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -181,6 +182,12 @@
<artifactId>guava</artifactId>
<version>${google.guava.version}</version>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-random-core</artifactId>
<version>${easy.random.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package it.gov.pagopa.atmlayer.service.model.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.UUID;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BranchConfigs {
//TODO: Validatore custom
private String branchId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package it.gov.pagopa.atmlayer.service.model.dto;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.UUID;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TerminalConfigs {
@NotNull
private UUID templateId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package it.gov.pagopa.atmlayer.service.model.resource;

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.mutiny.Uni;
import it.gov.pagopa.atmlayer.service.model.dto.BpmnAssociationDto;
import it.gov.pagopa.atmlayer.service.model.dto.BpmnCreationDto;
import it.gov.pagopa.atmlayer.service.model.dto.BranchConfigs;
import it.gov.pagopa.atmlayer.service.model.dto.TerminalConfigs;
import it.gov.pagopa.atmlayer.service.model.entity.BpmnBankConfig;
import it.gov.pagopa.atmlayer.service.model.entity.BpmnVersion;
import it.gov.pagopa.atmlayer.service.model.entity.BpmnVersionPK;
import it.gov.pagopa.atmlayer.service.model.enumeration.FunctionTypeEnum;
import it.gov.pagopa.atmlayer.service.model.mapper.BpmnConfigMapperImpl;
import it.gov.pagopa.atmlayer.service.model.mapper.BpmnVersionMapper;
import it.gov.pagopa.atmlayer.service.model.model.BpmnBankConfigDTO;
import it.gov.pagopa.atmlayer.service.model.model.BpmnDTO;
import it.gov.pagopa.atmlayer.service.model.service.BpmnVersionService;
import it.gov.pagopa.atmlayer.service.model.validators.BpmnEntityValidator;
import jakarta.ws.rs.core.MediaType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@QuarkusTest
class BpmnResourceTest {
@InjectMock
BpmnVersionService bpmnVersionService;
@InjectMock
BpmnVersionMapper bpmnVersionMapper;
@InjectMock
BpmnEntityValidator bpmnEntityValidator;
@InjectMock
BpmnConfigMapperImpl bpmnConfigMapper;

@Test
void testGetAllBpmnOK() {
List<BpmnVersion> bpmnList = new ArrayList<>();
BpmnVersion bpmnVersion = new BpmnVersion();
bpmnList.add(bpmnVersion);
List<BpmnDTO> dtoList = new ArrayList<>();
BpmnDTO bpmnDTO = new BpmnDTO();
dtoList.add(bpmnDTO);
when(bpmnVersionService.getAll()).thenReturn(Uni.createFrom().item(bpmnList));
when(bpmnVersionMapper.toDTOList(any(ArrayList.class))).thenReturn(dtoList);
ArrayList result = given()
.when().get("/api/v1/model/bpmn")
.then()
.statusCode(200)
.extract()
.body()
.as(ArrayList.class);
Assertions.assertEquals(1, result.size());
verify(bpmnVersionService, times(1)).getAll();
verify(bpmnVersionMapper, times(1)).toDTOList(bpmnList);
}

@Test
void testGetAllBpmnEmptyList() {
List<BpmnVersion> bpmnList = new ArrayList<>();
BpmnVersion bpmnVersion = new BpmnVersion();
bpmnList.add(bpmnVersion);
List<BpmnDTO> dtoList = new ArrayList<>();
when(bpmnVersionService.getAll()).thenReturn(Uni.createFrom().item(bpmnList));
when(bpmnVersionMapper.toDTOList(any(ArrayList.class))).thenReturn(dtoList);
ArrayList result = given()
.when().get("/api/v1/model/bpmn")
.then()
.statusCode(200)
.extract()
.body()
.as(ArrayList.class);
Assertions.assertTrue(result.isEmpty());
verify(bpmnVersionService, times(1)).getAll();
verify(bpmnVersionMapper, times(1)).toDTOList(bpmnList);
}

@Test
void testGetEncodedFileOK() {
BpmnVersion bpmnVersion = new BpmnVersion();
UUID uuid = UUID.randomUUID();
Long version = 1L;
bpmnVersion.setBpmnId(uuid);
bpmnVersion.setModelVersion(version);
BpmnDTO bpmnDTO = new BpmnDTO();
bpmnDTO.setDeployedFileName("testDTO");
when(bpmnVersionService.findByPk(new BpmnVersionPK(uuid, version))).thenReturn(Uni.createFrom().item(Optional.ofNullable(bpmnVersion)));
when(bpmnVersionMapper.toDTO(bpmnVersion)).thenReturn(bpmnDTO);
given()
.pathParam("bpmnId", uuid)
.pathParam("version", version)
.when().get("/api/v1/model/bpmn/{bpmnId}/version/{version}")
.then()
.statusCode(200)
.body("deployedFileName", is("testDTO"));
}

@Test
void testGetEncodedFileFileNotFound() {
BpmnVersion bpmnVersion = new BpmnVersion();
UUID uuid = UUID.randomUUID();
Long version = 1L;
bpmnVersion.setBpmnId(uuid);
bpmnVersion.setModelVersion(version);
BpmnDTO bpmnDTO = new BpmnDTO();
bpmnDTO.setDeployedFileName("testDTO");
when(bpmnVersionService.findByPk(new BpmnVersionPK(uuid, version))).thenReturn(Uni.createFrom().item(Optional.ofNullable(null)));
when(bpmnVersionMapper.toDTO(bpmnVersion)).thenReturn(bpmnDTO);
given()
.pathParam("bpmnId", uuid)
.pathParam("version", version)
.when().get("/api/v1/model/bpmn/{bpmnId}/version/{version}")
.then()
.statusCode(404);
}

@Test
void testAssociateBPMNOK() {
BpmnAssociationDto bpmnAssociationDto = getBpmnAssociationDtoInstance();
List<BpmnBankConfig> bankConfigList = new ArrayList<>();
BpmnBankConfig bpmnBankConfig = new BpmnBankConfig();
bankConfigList.add(bpmnBankConfig);
Collection<BpmnBankConfigDTO> dtoList = new HashSet<>();
BpmnBankConfigDTO bpmnBankConfigDTO = new BpmnBankConfigDTO();
dtoList.add(bpmnBankConfigDTO);
String acquirerId = "testAcq";
FunctionTypeEnum functionTypeEnum = FunctionTypeEnum.MENU;
when(bpmnEntityValidator.validateExistenceStatusAndFunctionType(any(), any())).thenReturn(Uni.createFrom().nullItem());
when(bpmnVersionService.putAssociations(any(), any(), any())).thenReturn(Uni.createFrom().item(bankConfigList));
when(bpmnConfigMapper.toDTOList(bankConfigList)).thenReturn(dtoList);
given()
.pathParam("acquirerId", acquirerId)
.pathParam("functionType", functionTypeEnum)
.contentType(MediaType.APPLICATION_JSON)
.body(bpmnAssociationDto)
.when().put("api/v1/model/bpmn/bank/{acquirerId}/associations/function/{functionType}")
.then()
.statusCode(200);
}

@Test
void createBPMNOK() throws NoSuchAlgorithmException, IOException {
BpmnVersion bpmnVersion = new BpmnVersion();
BpmnDTO bpmnDTO = new BpmnDTO();
when(bpmnVersionMapper.toEntityCreation(any(BpmnCreationDto.class))).thenReturn(bpmnVersion);
when(bpmnVersionService.createBPMN(any(BpmnVersion.class), any(File.class), any(String.class))).thenReturn(Uni.createFrom().item(bpmnVersion));
when(bpmnVersionMapper.toDTO(bpmnVersion)).thenReturn(bpmnDTO);
BpmnDTO result = given()
.contentType(MediaType.MULTIPART_FORM_DATA)
.multiPart("file", new File("src/test/resources/TestMalformed.bpmn"))
.formParam("filename", "testFileName")
.formParam("functionType", FunctionTypeEnum.MENU)
.when().post("/api/v1/model/bpmn")
.then()
.statusCode(200)
.extract().as(BpmnDTO.class);
Assertions.assertEquals(bpmnDTO, result);
}

@Test
void deleteBpmn() {
}

@Test
void deployBPMN() {
}

@Test
void downloadBpmn() {
}

@Test
void findBPMNByTriad() {
}

@Test
void upgradeBPMN() {
}

@Test
void getAssociations() {
}

private static BpmnAssociationDto getBpmnAssociationDtoInstance() {
List<String> terminalList = new ArrayList<>();
terminalList.add("testTerminal");
TerminalConfigs terminalConfigs = new TerminalConfigs(UUID.randomUUID(), 1L, terminalList);
List<TerminalConfigs> terminals = new ArrayList<>();
terminals.add(terminalConfigs);
BranchConfigs branchConfigs = new BranchConfigs("testBranch", UUID.randomUUID(), 1L, terminals);
List<BranchConfigs> branchesConfigs = new ArrayList<>();
branchesConfigs.add(branchConfigs);
return new BpmnAssociationDto(UUID.randomUUID(), 1L, branchesConfigs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package it.gov.pagopa.atmlayer.service.model.service.impl;

import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber;
import it.gov.pagopa.atmlayer.service.model.utils.FileStorageS3Utils;
import org.jeasy.random.EasyRandom;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import software.amazon.awssdk.awscore.presigner.PresignedRequest;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;

import java.net.URL;
import java.time.Duration;

import static org.mockito.Mockito.when;


@QuarkusTest
public class S3ObjectStoreServiceImplTest {

@Mock
S3AsyncClient s3;

@Mock
FileStorageS3Utils fileStorageS3Utils;

@Mock
S3Presigner presigner;

@InjectMocks
S3ObjectStoreServiceImpl s3ObjectStoreService;

EasyRandom easyRandom;

@BeforeEach
public void setup() {

MockitoAnnotations.initMocks(this);
easyRandom = new EasyRandom();
}


// @Test
// void testGeneratePresignedEmptyUrl() {
// String objectKey = "";
// String expectedURL="";
//
// GetObjectRequest getObjectRequest = fileStorageS3Utils.buildGetRequest(objectKey);
// GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
// .signatureDuration(Duration.ofMinutes(10)) // The URL will expire in 10 minutes.
// .getObjectRequest(getObjectRequest)
// .build();
//
//
// when(presigner.presignGetObject(getObjectPresignRequest)).thenReturn(PresignedRequest.url(expectedURL).build());
//
// Uni<URL> result= s3ObjectStoreService.generatePresignedUrl(objectKey);
//
// result.subscribe().withSubscriber(UniAssertSubscriber.create()).assertCompleted().assertItem(expectedURL);
// }


}
Loading

0 comments on commit 9fafed2

Please sign in to comment.