Skip to content

Commit

Permalink
Merge branch 'develop' into OP-1305
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudFonzam committed Oct 9, 2024
2 parents 8fb48ee + bf3add9 commit 1aac4ad
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 36 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-jupiter</artifactId>
<version>5.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/org/isf/generaldata/GeneralData.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,7 @@
package org.isf.generaldata;

/**
* ------------------------------------------
* General Data
* <p>
* 12/2007 - isf bari - added resource bundle for internationalization
* 19/06/2008 - isf bari - added patientsheet jasper report name
* 20/12/2008 - isf bari - added patientextended
* 01/01/2009 - Fabrizio - added OPDEXTENDED
* 20/01/2009 - Chiara - added attribute MATERNITYRESTARTINJUNE to reset progressive number of maternity ward
* 25/02/2011 - Claudia - added attribute MAINMENUALWAYSONTOP to handle main menu always on Top
* 01/05/2011 - Vito - added attribute VIDEOMODULEENABLED to enable/disable video module
* 10/08/2011 - Claudia - added PATIENTVACCINEEXTENDED to show patient on Patient Vaccine
* 19/10/2011 - Mwithi - GeneralData 2.0: catching exception on single property and assign DEFAULT value
* 29/12/2011 - Nicola - added XMPPMODULEENABLED to enable/disable communication module
* 06/07/2022 - Nicole - added USERSLISTLOGIN to login by typing the username in a textbox (no) or selecting the user from a list (yes)
* -------------------------------------------
*/
public final class GeneralData extends ConfigurationProperties {

Expand Down Expand Up @@ -247,6 +233,8 @@ private GeneralData(String fileProperties) {
PATIENTPHOTOSTORAGE = myGetProperty("PATIENTPHOTOSTORAGE", DEFAULT_PATIENTPHOTOSTORAGE);
SESSIONTIMEOUT = myGetProperty("SESSIONTIMEOUT", DEFAULT_SESSIONTIMEOUT);
PARAMSURL = myGetProperty("PARAMSURL", DEFAULT_PARAMSURL);

ParamsData.getInstance();
}

public static GeneralData getGeneralData() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/isf/generaldata/ParamsData.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public static synchronized ParamsData getInstance() {
}

private ParamsData() {
GeneralData.getGeneralData();
this.configProvider = ConfigProviderFactory.createConfigProvider();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@

class ApiConfigProvider implements ConfigProvider {

private static final String VERSION = Version.getVersion().toString();

private static final Logger LOGGER = LoggerFactory.getLogger(ApiConfigProvider.class);

private final String version = Version.getVersion().toString();
private final ParamsApi api;
private final Map<String, Object> configData;

Expand Down Expand Up @@ -97,7 +96,7 @@ private Map<String, Object> fetchData() {
try {
Map<String, Object> response = api.getData();
if (response != null) {
Object versionData = response.getOrDefault(VERSION, response.get("default"));
Object versionData = response.getOrDefault(version, response.get("default"));
if (versionData instanceof Map) {
return (Map<String, Object>) versionData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

class JsonFileConfigProvider implements ConfigProvider {

Expand Down Expand Up @@ -65,17 +66,23 @@ private Map<String, Object> fetchDataFile() {

// Parse the JSON string into a Map<String, String> using Gson
Gson gson = new Gson();
Map<String, Object> resultMap = gson.fromJson(response.toString(), Map.class);

// Check if 'version' is present, otherwise use "default"
return resultMap.containsKey(VERSION) ? (Map<String, Object>) resultMap.get(VERSION)
: (Map<String, Object>) resultMap.get("default");
Map<String, Object> resultMap;
try {
resultMap = gson.fromJson(response.toString(), Map.class);
LOGGER.info("Configuration fetched.");

// Check if 'version' is present, otherwise use "default"
return resultMap.containsKey(VERSION) ? (Map<String, Object>) resultMap.get(VERSION)
: (Map<String, Object>) resultMap.get("default");
} catch (JsonSyntaxException e) {
LOGGER.error("Failed to parse the configuration json.");
}
}
} else {
LOGGER.error("Failed to fetch configuration URL. HTTP response code: {}", responseCode);
}
} catch (IOException e) {
LOGGER.error("Error during HTTP request: {}", e.getMessage());
LOGGER.error("Error during configuration HTTP request: {}", e.getMessage());
}

return Collections.emptyMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,131 @@
package org.isf.generaldata.configProvider;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import org.isf.generaldata.GeneralData;
import org.isf.generaldata.Version;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.MediaType;

public class TestApiConfigProvider {

private static final String CONFIG_JSON = "{\n" +
" \"1.15.0\": {\n" +
" \"parameter\": \"https://version-url.com\"\n" +
" },\n" +
" \"default\": {\n" +
" \"parameter\": \"https://default-url.com\"\n" +
" }\n" +
"}";

private ClientAndServer mockServer;

@BeforeEach
public void startServer() {
mockServer = startClientAndServer(1080); // Start the MockServer
}

@AfterEach
public void stopServer() {
mockServer.stop(); // Stop the MockServer
}

@Test
void testGetTestParamsData() {
// NOTE: this uses a remote json file so things are bound to fail
GeneralData.initialize();
ApiConfigProvider apiConfigProvider = new ApiConfigProvider();
void testGetTestParamsDataDefaultVersion() throws Exception {
// Set up the mock server to respond with the desired JSON
mockServer.when(request().withMethod("GET").withPath("/test")).respond(response().withStatusCode(200)
.withContentType(MediaType.APPLICATION_JSON)
.withBody(CONFIG_JSON));

// Use MockedStatic to mock GeneralData
try (MockedStatic<GeneralData> mockedGeneralData = mockStatic(GeneralData.class);
MockedStatic<Version> mockedVersion = Mockito.mockStatic(Version.class)) {

assertThat(apiConfigProvider.getConfigData()).isNotNull();
// Mock the Version.getVersion() method to return a specific version
Version mockVersion = mock(Version.class);
mockedVersion.when(Version::getVersion).thenReturn(mockVersion);
when(mockVersion.toString()).thenReturn("");

assertThat(apiConfigProvider.get("someParam")).isNull();
// Mock the initialization to set PARAMSURL to the mock server URL
mockedGeneralData.when(GeneralData::initialize).thenAnswer(invocation -> {
GeneralData.PARAMSURL = "http://localhost:1080/test"; // Use the mock server's URL
return null;
});

// void method
apiConfigProvider.close();
// Initialize GeneralData with the mocked PARAMSURL
GeneralData.initialize();

ApiConfigProvider apiConfigProvider = new ApiConfigProvider();

assertThat(apiConfigProvider.get("parameter")).isEqualTo("https://default-url.com");

apiConfigProvider.close();
}
}

@Test
void testGetTestParamsDataWithVersion() throws Exception {
// Set up the mock server to respond with the desired JSON
mockServer.when(request().withMethod("GET").withPath("/test")).respond(response().withStatusCode(200)
.withContentType(MediaType.APPLICATION_JSON)
.withBody(CONFIG_JSON));

// Use MockedStatic to mock GeneralData
try (MockedStatic<GeneralData> mockedGeneralData = mockStatic(GeneralData.class);
MockedStatic<Version> mockedVersion = Mockito.mockStatic(Version.class)) {

// Mock the Version.getVersion() method to return a specific version
Version mockVersion = mock(Version.class);
mockedVersion.when(Version::getVersion).thenReturn(mockVersion);
when(mockVersion.toString()).thenReturn("1.15.0");

// Mock the initialization to set PARAMSURL to the mock server URL
mockedGeneralData.when(GeneralData::initialize).thenAnswer(invocation -> {
GeneralData.PARAMSURL = "http://localhost:1080/test"; // Use the mock server's URL
return null;
});

// Initialize GeneralData with the mocked PARAMSURL
GeneralData.initialize();

ApiConfigProvider apiConfigProvider = new ApiConfigProvider();

assertThat(apiConfigProvider.get("parameter")).isEqualTo("https://version-url.com");

apiConfigProvider.close();
}
}

@Test
void testGetTestParamsDataEmptyParmsUrl() {
GeneralData.initialize();
GeneralData.PARAMSURL = "";
ApiConfigProvider apiConfigProvider = new ApiConfigProvider();

assertThat(apiConfigProvider.getConfigData()).isNull();
try (MockedStatic<GeneralData> mockedGeneralData = mockStatic(GeneralData.class)) {

// Mock the initialization to set PARAMSURL to the mock server URL
mockedGeneralData.when(GeneralData::initialize).thenAnswer(invocation -> {
GeneralData.PARAMSURL = ""; // Use the mock server's URL
return null;
});

// Initialize GeneralData with the mocked PARAMSURL
GeneralData.initialize();

ApiConfigProvider apiConfigProvider = new ApiConfigProvider();

assertThat(apiConfigProvider.getConfigData()).isNull();

apiConfigProvider.close();
}
}
}

0 comments on commit 1aac4ad

Please sign in to comment.