diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/AbstractConfig.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/AbstractConfig.java new file mode 100644 index 00000000..3863a980 --- /dev/null +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/AbstractConfig.java @@ -0,0 +1,97 @@ +package it.finanze.sanita.fse2.ms.gtw.dispatcher; + +import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IConfigClient; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.impl.ConfigSRV; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.ProfileUtility; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.GENERIC; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; + +public abstract class AbstractConfig { + + @SpyBean + protected ConfigSRV config; + @MockBean + private IConfigClient client; + @SpyBean + private ProfileUtility profiles; + + public abstract List> defaults(); + + protected void testCacheProps(Pair prop, Runnable fn) { + // Set default props + setup(prop); + // Check it returns the cached-value + fn.run(); + // Verify client has not been invoked + verify(client, never()).getProps(eq(prop.getKey()), any(), any()); + } + + protected void testRefreshProps(Pair prop, String newValue, Runnable fn) { + // Set default props + setup(prop); + // Mock new answer + when(client.getProps(eq(prop.getKey()), any(), any())).thenReturn(newValue); + // Force refresh + doReturn(0L).when(config).getRefreshRate(); + // Check it returns the new-value + fn.run(); + // Verify client has been invoked + verify(client, times(1)).getProps(eq(prop.getKey()), any(), any()); + } + + protected void testIntegrityCheck() { + // Forget one props + List> broken = new ArrayList<>(defaults()); + broken.remove(0); + // Expect broken + assertThrows(IllegalStateException.class, () -> setup(broken)); + } + + @SafeVarargs + private final void setup(Pair... keys) { + setup(defaults(), keys); + } + + @SafeVarargs + private final void setup(List> defaultProps, Pair... keys) { + ConfigItemDTO req = request(); + Map generics = req.getConfigurationItems().get(0).getItems(); + // Load generics + for (Pair def : defaultProps) { + generics.put(def.getKey(), def.getValue()); + } + // Apply specific + for (Pair props : keys) { + generics.put(props.getKey(), props.getValue()); + } + when(client.getConfigurationItems(any())).thenReturn(req); + doReturn(false).when(profiles).isTestProfile(); + config.postConstruct(); + } + + private ConfigItemDTO request() { + ConfigItemDTO items = new ConfigItemDTO(); + List values = new ArrayList<>(); + + ConfigItemDTO.ConfigDataItemDTO generic = new ConfigItemDTO.ConfigDataItemDTO(); + generic.setKey(GENERIC.name()); + generic.setItems(new HashMap<>()); + + values.add(generic); + items.setConfigurationItems(values); + + return items; + } + +} diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/ConfigClientTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/ConfigClientTest.java new file mode 100644 index 00000000..2de2cb64 --- /dev/null +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/ConfigClientTest.java @@ -0,0 +1,105 @@ +package it.finanze.sanita.fse2.ms.gtw.dispatcher; + +import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IConfigClient; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.ConfigClientRoutes; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.DisplayName; +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.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.web.client.RestTemplate; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants.Profile.TEST; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.GENERIC; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@Slf4j +@SpringBootTest(webEnvironment = RANDOM_PORT) +@ActiveProfiles(TEST) +class ConfigClientTest { + + private static final ConfigItemTypeEnum specific = ConfigItemTypeEnum.DISPATCHER; + + @Autowired + private IConfigClient config; + + @MockBean + private RestTemplate client; + + @Autowired + private ConfigClientRoutes routes; + + @Test + @DisplayName("Get prop test with prop value different from previous") + void getPropTest(){ + // Mock the it-gtw-config status + when(client.getForEntity(Mockito.anyString(), Mockito.eq(String.class))).thenReturn(ResponseEntity.ok().build()); + + String prop_name = "prop_name"; + String expected = "true"; + when(client.getForObject(routes.getConfigItem(specific, prop_name), String.class)).thenReturn(expected); + + String actual = config.getProps(prop_name, "false", specific); + assertEquals(expected, actual); + } + + @Test + @DisplayName("Get prop name with SPECIFIC prop not found") + void getPropTestWithSpecificPropNotFound(){ + // Mock the it-gtw-config status + when(client.getForEntity(Mockito.anyString(), Mockito.eq(String.class))).thenReturn(ResponseEntity.ok().build()); + + String prop_name = "prop_name"; + String expected = "true"; + when(client.getForObject(routes.getConfigItem(specific, prop_name), String.class)).thenReturn(null); + when(client.getForObject(routes.getConfigItem(GENERIC, prop_name), String.class)).thenReturn(expected); + + + String actual = config.getProps(prop_name, "false", specific); + assertEquals(expected, actual); + } + + @Test + void getAllPropsTest(){ + ConfigItemDTO expected = request(); + + when(client.getForObject(Mockito.anyString(), Mockito.eq(ConfigItemDTO.class))).thenReturn(expected); + + ConfigItemDTO actual = config.getConfigurationItems(GENERIC); + + assertEquals(expected.getTraceId(), actual.getTraceId()); + assertEquals(expected.getSpanId(), actual.getSpanId()); + assertEquals(expected.getConfigurationItems().size(), actual.getConfigurationItems().size()); + assertEquals(expected.getConfigurationItems().get(0).getKey(), actual.getConfigurationItems().get(0).getKey()); + } + + private static ConfigItemDTO request() { + ConfigItemDTO.ConfigDataItemDTO dataItem = new ConfigItemDTO.ConfigDataItemDTO(); + ConfigItemDTO expected = new ConfigItemDTO(); + // Valorizzo le props di tipo FHIR-MAPPING-ENGINE + Map map = new HashMap(); + map.put("cfg-items-retention-day", "true"); + dataItem.setKey(GENERIC.name()); + dataItem.setItems(map); + // Creo la variabile di ritorno + List props = new ArrayList<>(); + props.add(dataItem); + expected.setTraceId("traceID"); + expected.setSpanId("spanId"); + expected.setConfigurationItems(props); + return expected; + } +} diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/ConfigTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/ConfigTest.java new file mode 100644 index 00000000..b48b2abc --- /dev/null +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/ConfigTest.java @@ -0,0 +1,70 @@ +package it.finanze.sanita.fse2.ms.gtw.dispatcher; + +import it.finanze.sanita.fse2.ms.gtw.dispatcher.adapter.CustomResponseBodyAdviceAdapter; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.WebCFG; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; + +import java.util.Arrays; +import java.util.List; + +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.*; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants.Profile.TEST; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@Slf4j +@SpringBootTest(webEnvironment = RANDOM_PORT) +@ActiveProfiles(TEST) +@TestInstance(PER_CLASS) +class ConfigTest extends AbstractConfig { + + // Exclude from running by mocking + @MockBean + private WebCFG web; + @MockBean + private CustomResponseBodyAdviceAdapter adapter; + + private static final List> DEFAULT_PROPS = Arrays.asList( + Pair.of(PROPS_NAME_REMOVE_EDS_ENABLE, "false"), + Pair.of(PROPS_NAME_AUDIT_ENABLED, "false"), + Pair.of(PROPS_NAME_CONTROL_LOG_ENABLED, "false"), + Pair.of(PROPS_NAME_SUBJECT, "false"), + Pair.of(PROPS_NAME_ISSUER_CF, "false") + ); + + @Test + void testCacheProps() { + testCacheProps(DEFAULT_PROPS.get(0), () -> assertFalse(config.isRemoveEds())); + testCacheProps(DEFAULT_PROPS.get(1), () -> assertFalse(config.isAuditEnable())); + testCacheProps(DEFAULT_PROPS.get(2), () -> assertFalse(config.isControlLogPersistenceEnable())); + testCacheProps(DEFAULT_PROPS.get(3), () -> assertFalse(config.isSubjectNotAllowed())); + testCacheProps(DEFAULT_PROPS.get(4), () -> assertFalse(config.isCfOnIssuerNotAllowed())); + } + + @Test + void testRefreshProps() { + testRefreshProps(DEFAULT_PROPS.get(0), "true", () -> assertTrue(config.isRemoveEds())); + testRefreshProps(DEFAULT_PROPS.get(1), "true", () -> assertTrue(config.isAuditEnable())); + testRefreshProps(DEFAULT_PROPS.get(2), "true", () -> assertTrue(config.isControlLogPersistenceEnable())); + testRefreshProps(DEFAULT_PROPS.get(3), "true", () -> assertTrue(config.isSubjectNotAllowed())); + testRefreshProps(DEFAULT_PROPS.get(4), "true", () -> assertTrue(config.isCfOnIssuerNotAllowed())); + } + + @Test + void testIntegrityProps() { + testIntegrityCheck(); + } + + @Override + public List> defaults() { + return DEFAULT_PROPS; + } +} diff --git a/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/EngineSRVTest.java b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/EngineSRVTest.java index 7d426d2f..a28bed84 100644 --- a/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/EngineSRVTest.java +++ b/src/test/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/EngineSRVTest.java @@ -1,41 +1,98 @@ -package it.finanze.sanita.fse2.ms.gtw.dispatcher; - -import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.BusinessException; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.entity.engine.EngineETY; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.entity.engine.sub.EngineMap; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.mongo.impl.EngineRepo; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.impl.EngineSRV; -import org.apache.commons.lang3.tuple.Pair; -import org.junit.jupiter.api.Test; -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.test.context.ActiveProfiles; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@ActiveProfiles(Constants.Profile.TEST) -public class EngineSRVTest { - - @Autowired - EngineSRV engineSRV; - - @MockBean - EngineRepo engineRepo; - - @Test - void testGetStructureObjectID_Exception(){ - when(engineRepo.getLatestEngine()).thenReturn(null); - - assertThrows( - BusinessException.class, - () -> engineSRV.getStructureObjectID("test") - ); - } - -} +package it.finanze.sanita.fse2.ms.gtw.dispatcher; + +import it.finanze.sanita.fse2.ms.gtw.dispatcher.config.Constants; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.BusinessException; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.entity.engine.EngineETY; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.entity.engine.sub.EngineMap; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.mongo.impl.EngineRepo; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.impl.EngineSRV; +import org.apache.commons.lang3.tuple.Pair; +import org.bson.types.ObjectId; +import org.junit.jupiter.api.Test; +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.test.context.ActiveProfiles; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles(Constants.Profile.TEST) +class EngineSRVTest { + + @Autowired + EngineSRV engineSRV; + + @MockBean + EngineRepo engineRepo; + + @Test + void testGetStructureObjectID_Exception(){ + when(engineRepo.getLatestEngine()).thenReturn(null); + + assertThrows( + BusinessException.class, + () -> engineSRV.getStructureObjectID("test") + ); + } + + @Test + void testGetStructureObjectID_NotMapException(){ + when(engineRepo.getLatestEngine()).thenReturn(new EngineETY()); + + assertThrows( + BusinessException.class, + () -> engineSRV.getStructureObjectID("test") + ); + } + + @Test + void testGetStructureObjectID_Success(){ + Pair expectedValue; + EngineETY engine = new EngineETY(); + engine.setId("656f557479464f1e20f869a7"); + EngineMap engineMap = new EngineMap(); + engineMap.setOid("6537915bd1efbd30cc7d62bf"); + engineMap.setRoot(Arrays.asList("2.16.840.1.113883.2.9.10.1.8.1", "2.16.840.1.113883.2.9.10.1.5")); + engineMap.setUri("http://salute.gov.it/ig/cda-fhir-maps/StructureMap/RefertodiAnatomiaPatologica"); + engineMap.setVersion("1.0"); + List enginesMap = new ArrayList<>(); + enginesMap.add(engineMap); + engine.setRoots(enginesMap); + engine.setAvailable(true); + engine.setLastSync(new Date()); + engine.setFiles(new ArrayList()); + + expectedValue = Pair.of(engine.getId(), engineMap.getOid()); + Pair actualValue; + when(engineRepo.getLatestEngine()).thenReturn(engine); + actualValue = engineSRV.getStructureObjectID("2.16.840.1.113883.2.9.10.1.8.1"); + + assertEquals(expectedValue, actualValue); + } + + @Test + void testGetStructureObjectID_NoMapException(){ + Pair expectedValue; + EngineETY engine = new EngineETY(); + engine.setId("656f557479464f1e20f869a7"); + + Pair actualValue; + when(engineRepo.getLatestEngine()).thenReturn(engine); + assertThrows( + BusinessException.class, + () -> engineSRV.getStructureObjectID("test"), + String.format("Nessuna mappa con id %s รจ stata trovata nell'engine %s", engine.getId(), "test") + ); + + } + +}