-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/test/java/it/finanze/sanita/fse2/ms/gtw/garbage/ConfigClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package it.finanze.sanita.fse2.ms.gtw.garbage; | ||
|
||
import it.finanze.sanita.fse2.ms.gtw.garbage.client.IConfigClient; | ||
import it.finanze.sanita.fse2.ms.gtw.garbage.client.routes.ConfigClientRoutes; | ||
import it.finanze.sanita.fse2.ms.gtw.garbage.dto.ConfigItemDTO; | ||
import it.finanze.sanita.fse2.ms.gtw.garbage.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.garbage.config.Constants.Profile.TEST; | ||
import static it.finanze.sanita.fse2.ms.gtw.garbage.enums.ConfigItemTypeEnum.*; | ||
import static it.finanze.sanita.fse2.ms.gtw.garbage.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 = GARBAGE; | ||
|
||
@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<String, String> map = new HashMap<String, String>(); | ||
map.put("cfg-items-retention-day", "true"); | ||
dataItem.setKey(GENERIC.name()); | ||
dataItem.setItems(map); | ||
// Creo la variabile di ritorno | ||
List<ConfigItemDTO.ConfigDataItemDTO> props = new ArrayList<>(); | ||
props.add(dataItem); | ||
expected.setTraceId("traceID"); | ||
expected.setSpanId("spanId"); | ||
expected.setConfigurationItems(props); | ||
return expected; | ||
} | ||
} |