Skip to content

Commit

Permalink
feat: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gb-cic committed Dec 21, 2023
1 parent 3c535f6 commit 365b989
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 48 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class JsonUtility {
*/
private JsonUtility() {}

private static ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper mapper = new ObjectMapper();

/**
* Methods that converts an Object to a JSON string.
Expand Down Expand Up @@ -59,8 +59,4 @@ public static <T> T jsonToObject(String jsonString, Class<T> clazz) {

return obj;
}

public static <T> T clone (Object object, Class<T> outputClass) {
return JsonUtility.jsonToObject(JsonUtility.objectToJson(object), outputClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package it.finanze.sanita.fse2.ms.edsclient;

import it.finanze.sanita.fse2.ms.edsclient.client.impl.EdsClient;
import it.finanze.sanita.fse2.ms.edsclient.config.Constants;
import it.finanze.sanita.fse2.ms.edsclient.dto.EdsResponseDTO;
import it.finanze.sanita.fse2.ms.edsclient.dto.request.PublicationRequestBodyDTO;
import it.finanze.sanita.fse2.ms.edsclient.repository.entity.IniEdsInvocationETY;
import it.finanze.sanita.fse2.ms.edsclient.repository.impl.EdsInvocationRepo;
import it.finanze.sanita.fse2.ms.edsclient.service.impl.ConfigSRV;
import it.finanze.sanita.fse2.ms.edsclient.service.impl.EdsInvocationSRV;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ActiveProfiles;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ComponentScan(basePackages = {Constants.ComponentScan.BASE})
@ActiveProfiles(Constants.Profile.TEST)
public class EdsInvocationSRVTest {

@Autowired
private EdsInvocationSRV edsInvocationSRV;

@MockBean
private EdsClient edsClient;

@MockBean
private EdsInvocationRepo edsInvocationRepo;

@MockBean
private ConfigSRV configSRV;

@Test
void testReplaceByWorkflowInstanceIdAndIdentifier(){
EdsResponseDTO out = new EdsResponseDTO();
IniEdsInvocationETY iniEdsInvocationETY = new IniEdsInvocationETY();
String workFlowInstanceId = "test";
String identifier = "test";
iniEdsInvocationETY.setWorkflowInstanceId("test");
iniEdsInvocationETY.setData(new Document("key", "test"));
out.setEsito(true);

when(edsClient.dispatchAndSendData(Mockito.any())).thenReturn(out);
when(edsInvocationRepo.findByWorkflowInstanceId(Mockito.anyString())).thenReturn(iniEdsInvocationETY);
when(configSRV.isRemoveMetadataEnable()).thenReturn(true);
edsInvocationSRV.replaceByWorkflowInstanceIdAndIdentifier(identifier, workFlowInstanceId);

verify(edsInvocationRepo, times(1)).removeByWorkflowInstanceId(Mockito.anyString());
}

@Test
void testPublishByWorkflowInstanceIdAndPriorityDocumentNotFound(){
EdsResponseDTO out;
String workFlowInstanceId = "test";
PublicationRequestBodyDTO request = new PublicationRequestBodyDTO();
request.setWorkflowInstanceId(workFlowInstanceId);

when(edsInvocationRepo.findByWorkflowInstanceId(Mockito.anyString())).thenReturn(null);
out = edsInvocationSRV.publishByWorkflowInstanceIdAndPriority(request);


assertEquals(out.getMessageError(), "Nessun documento trovato per il workflowInstanceId: " + request.getWorkflowInstanceId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ComponentScan(basePackages = {Constants.ComponentScan.BASE})
Expand Down Expand Up @@ -126,4 +129,10 @@ void testDocumentTypeEnum() {
Assertions.assertEquals("2.16.840.1.113883.2.9.10.1.11.1.1", tempVAC);
Assertions.assertEquals("Scheda della singola Vaccinazione", docTypeVAC);
}

@Test
@DisplayName("Get by template id return null")
void testGetByTemplateId_ReturnNull(){
assertNull(DocumentTypeEnum.getByTemplateId(""));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package it.finanze.sanita.fse2.ms.edsclient;

import it.finanze.sanita.fse2.ms.edsclient.config.Constants;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ActiveProfiles(Constants.Profile.TEST)
@AutoConfigureMockMvc
public class LivenessCheckCTLTest {

@Autowired
MockMvc mockMvc;

@Test
public void testHealthCheck() throws Exception {
mockMvc.perform(
get("/status")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
*/
package it.finanze.sanita.fse2.ms.edsclient;

import static org.junit.jupiter.api.Assertions.assertEquals;

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

import it.finanze.sanita.fse2.ms.edsclient.config.Constants;
import it.finanze.sanita.fse2.ms.edsclient.utility.RequestUtility;
import org.bson.Document;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ActiveProfiles;

import it.finanze.sanita.fse2.ms.edsclient.config.Constants;
import it.finanze.sanita.fse2.ms.edsclient.utility.RequestUtility;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ComponentScan(basePackages = {Constants.ComponentScan.BASE})
Expand All @@ -34,24 +34,61 @@ public class RequestUtilityTest {
@Test
@DisplayName("Undefined subject role test")
void testExtractSubjectRoleFromTokenUndefined() {
List<Document> listMetadata = new ArrayList<Document>();
List<Document> listMetadata = new ArrayList<>();

String undefinedSubjectRole = Constants.AppConstants.JWT_MISSING_SUBJECT_ROLE;
assertEquals(undefinedSubjectRole, RequestUtility.extractSubjectRoleFromToken(listMetadata));
}

@Test
@DisplayName("field test undefined")
void testExtractFieldFromMetadataUndefined() {
List<Document> listMetadata = new ArrayList<Document>();
List<Document> listMetadata = new ArrayList<>();
String undefinedField = Constants.AppConstants.UNKNOWN_DOCUMENT_TYPE;
assertEquals(undefinedField, RequestUtility.extractFieldFromMetadata(listMetadata, undefinedField));
}

@Test
@DisplayName("issuer test undefined")
void testExtractIssuerFromMetadataUndefined() {
List<Document> listMetadata = new ArrayList<Document>();
List<Document> listMetadata = new ArrayList<>();
String undefinedIssuer = Constants.AppConstants.UNKNOWN_ISSUER;
assertEquals(undefinedIssuer, RequestUtility.extractIssuerFromToken(listMetadata, undefinedIssuer));
}

@Test
@DisplayName("Extract subject Role success")
void testExtractSubjectRoleFromTokenSuccess(){
List<Document> listMetadata = new ArrayList<>();
Document doc = new Document("tokenEntry", new Document("payload", new Document("subject_role", "testRole")));
listMetadata.add(doc);

String expectedSubjectRole = "testRole";
String actualSubjectRole = RequestUtility.extractSubjectRoleFromToken(listMetadata);
assertEquals(expectedSubjectRole, actualSubjectRole);
}

@Test
@DisplayName("Extract issuer success")
void testExtractIssuerFromTokenSuccess(){
List<Document> listMetadata = new ArrayList<>();
Document doc = new Document("tokenEntry", new Document("payload", new Document("fieldName", "fieldName")));
listMetadata.add(doc);

String expectedIssuer = "fieldName";
String actualIssuer = RequestUtility.extractIssuerFromToken(listMetadata, "fieldName");
assertEquals(expectedIssuer, actualIssuer);
}

@Test
@DisplayName("Extract field from metadata success")
void testExtractFieldFromMetadataSuccess(){
List<Document> listMetadata = new ArrayList<>();
Document doc = new Document("documentEntry", new Document("fieldName", "fieldName"));
listMetadata.add(doc);

String expectedField = "fieldName";
String actualField = RequestUtility.extractFieldFromMetadata(listMetadata, "fieldName");
assertEquals(expectedField, actualField);
}
}

0 comments on commit 365b989

Please sign in to comment.