Skip to content

Commit

Permalink
OZ-689 - Switched to properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
wluyima committed Sep 10, 2024
1 parent 1d688bc commit e21a51a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ An OpenMRS module that intercepts and redirects fetch requests for `ChargeItemDe
resources to an external API, with support for basic authentication.

## Configuration
The module reads the configuration from a json file named `config.json` in a directory named `fhirproxy` which is
located in the application data directory, below is an example configuration file.
The module reads the configuration from a properties file named `config.properties` in a directory named `fhirproxy`
which is located in the application data directory, below is an example configuration file.
```
{
"externalApiEnabled" : true,
"baseUrl" : "http://your-fhirserver.com/fhir/R4",
"username" : "test-user",
"password" : "test-password"
}
external.api.enabled=true
base.url=http://your-fhirserver/fhir/R4
username=test-user
password=test-password
```
#### Configuration Properties
`externalApiEnabled`: When set to true, the module will delegate `GET` FHIR requests for `ChargeItemDefinition` and
`external.api.enabled`: When set to true, the module will delegate `GET` FHIR requests for `ChargeItemDefinition` and
`InventoryItem` to the configured external API.

`baseUrl`: The root URL for the external FHIR API
`base.url`: The root URL for the external FHIR API

`username`: The username to authenticate to the external FHIR API

Expand Down
7 changes: 7 additions & 0 deletions api/src/main/java/org/openmrs/module/fhirproxy/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public class Config {
@Getter
private String password;

public Config(boolean externalApiEnabled, String baseUrl, String username, String password) {
this.externalApiEnabled = externalApiEnabled;
this.baseUrl = baseUrl;
this.username = username;
this.password = password;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class Constants {

public static final String MODULE_ID = "fhirproxy";

public static final String CONFIG_FILE = "config.json";
public static final String CONFIG_FILE = "config.properties";

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void started() {

if (cfg.isExternalApiEnabled()) {
if (StringUtils.isBlank(cfg.getBaseUrl())) {
throw new ModuleException("Fhir Proxy module requires baseUrl when external FHIR API is enabled");
throw new ModuleException("Fhir Proxy module requires base.url when external FHIR API is enabled");
} else if (StringUtils.isBlank(cfg.getUsername())) {
throw new ModuleException("Fhir Proxy module requires username when external FHIR API is enabled");
} else if (StringUtils.isBlank(cfg.getPassword())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.openmrs.module.fhirproxy;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.openmrs.util.OpenmrsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Provides utility methods for the module.
*/
Expand All @@ -29,8 +29,13 @@ public static final Config getConfig() throws IOException {
LOG.info("Loading config for FHIR proxy module");

File configDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Constants.MODULE_ID);
File configFile = new File(configDir, Constants.CONFIG_FILE);
config = new ObjectMapper().readValue(configFile, Config.class);
Properties props = new Properties();
props.load(new FileInputStream(new File(configDir, Constants.CONFIG_FILE)));
final boolean enabled = Boolean.valueOf(props.getProperty("external.api.enabled", "false"));
final String baseUrl = props.getProperty("base.url");
final String username = props.getProperty("username");
final String password = props.getProperty("password");
config = new Config(enabled, baseUrl, username, password);
}

return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void started_shouldFailIfBaseUrlIsMissing() {

Exception ex = Assert.assertThrows(ModuleException.class, () -> activator.started());

assertEquals("Fhir Proxy module requires baseUrl when external FHIR API is enabled", ex.getMessage());
assertEquals("Fhir Proxy module requires base.url when external FHIR API is enabled", ex.getMessage());
}

@Test
Expand Down
6 changes: 0 additions & 6 deletions api/src/test/resources/config.json

This file was deleted.

4 changes: 4 additions & 0 deletions api/src/test/resources/config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
external.api.enabled=true
base.url=http://test.test
username=test-user
password=test-password

0 comments on commit e21a51a

Please sign in to comment.