Skip to content

Commit

Permalink
Merge pull request #270 from shiva-rakshith/config-map
Browse files Browse the repository at this point in the history
feat: update hcx integrator variables configuration
  • Loading branch information
maheshkumargangula authored Oct 17, 2022
2 parents 56f305c + 60b7bf2 commit d94910a
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,64 @@
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import java.util.Map;

/**
* The methods and variables to access the configuration. Enumeration of error codes and operations.
*/
public class HCXIntegrator {

private static HCXIntegrator hcxIntegrator = null;

private static Config config = null;

private HCXIntegrator() {
}

public static HCXIntegrator getInstance() {
public static HCXIntegrator getInstance() throws Exception {
if(config == null)
throw new Exception("Please initialize the configuration variables, in order to initialize the SDK");
if (hcxIntegrator == null)
hcxIntegrator = new HCXIntegrator();
return hcxIntegrator;
}

Config config = ConfigFactory.load();
// To initialize config factory by passing the configuration as Map
public static void init(Map<String,Object> configMap) {
config = ConfigFactory.parseMap(configMap);
}

// To initialize config factory by passing the configuration as JSON String
public static void init(String configStr) {
config = ConfigFactory.parseString(configStr);
}

public String getHCXProtocolBasePath() {
return config.getString("hcx.protocolBasePath");
return config.getString("protocolBasePath");
}

public String getParticipantCode() {
return config.getString("hcx.participantCode");
return config.getString("participantCode");
}

public String getAuthBasePath() {
return config.getString("hcx.authBasePath");
return config.getString("authBasePath");
}

public String getUsername() {
return config.getString("hcx.username");
return config.getString("username");
}

public String getPassword() {
return config.getString("hcx.password");
return config.getString("password");
}

public String getPrivateKey() {
return config.getString("hcx.encryptionPrivateKey");
return config.getString("encryptionPrivateKey");
}

public String getIGUrl() {
return config.getString("hcx.igUrl");
return config.getString("igUrl");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class HCXIncomingRequest extends FhirPayload implements IncomingRequest {

private final HCXIntegrator hcxIntegrator = HCXIntegrator.getInstance();

public HCXIncomingRequest() throws Exception {
}

@Override
public boolean process(String jwePayload, Operations operation, Map<String, Object> output) {
Map<String, Object> error = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class HCXOutgoingRequest extends FhirPayload implements OutgoingRequest {

private final HCXIntegrator hcxIntegrator = HCXIntegrator.getInstance();

public HCXOutgoingRequest() throws Exception {
}

@Override
public boolean generate(String fhirPayload, Operations operation, String recipientCode, Map<String,Object> output){
return process(fhirPayload, operation, recipientCode, "", "", output);
Expand Down Expand Up @@ -81,8 +84,8 @@ private boolean process(String fhirPayload, Operations operation, String recipie
output.putAll(response);
}
return result;
} catch (JsonProcessingException ex) {
// TODO: JsonProcessingException is handled as domain processing error, we will be enhancing in next version.
} catch (Exception ex) {
// TODO: Exception is handled as domain processing error, we will be enhancing in next version.
output.put(ErrorCodes.ERR_DOMAIN_PROCESSING.toString(), ex.getMessage());
return result;
}
Expand Down Expand Up @@ -133,9 +136,9 @@ public boolean encryptPayload(Map<String,Object> headers, String fhirPayload, Ma
}
}

// we are handling the JsonProcessingException in processFunction method
// we are handling the Exception in processFunction method
@Override
public boolean initializeHCXCall(String jwePayload, Operations operation, Map<String,Object> response) throws JsonProcessingException {
public boolean initializeHCXCall(String jwePayload, Operations operation, Map<String,Object> response) throws Exception {
Map<String,String> headers = new HashMap<>();
headers.put(Constants.AUTHORIZATION, "Bearer " + Utils.generateToken());
HttpResponse hcxResponse = HttpUtils.post(hcxIntegrator.getHCXProtocolBasePath() + operation.getOperation(), headers, jwePayload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,6 @@ public interface OutgoingRequest {
*
* @throws JsonProcessingException The exception throws when it is having issues in parsing the JSON object.
*/
boolean initializeHCXCall(String jwePayload, Operations operation, Map<String,Object> response) throws JsonProcessingException;
boolean initializeHCXCall(String jwePayload, Operations operation, Map<String,Object> response) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class Utils {

// TODO: In the initial version we are not handling the token caching, it will be handled in the next version
public static String generateToken() throws JsonProcessingException {
public static String generateToken() throws Exception {
Map<String,String> headers = new HashMap<>();
headers.put("content-type", "application/x-www-form-urlencoded");
Map<String,Object> fields = new HashMap<>();
Expand All @@ -34,7 +34,7 @@ public static String generateToken() throws JsonProcessingException {
return responseBody.get("access_token");
}

public static Map<String,Object> searchRegistry(Object participantCode) throws ServerException, JsonProcessingException {
public static Map<String,Object> searchRegistry(Object participantCode) throws Exception {
String filter = "{\"filters\":{\"participant_code\":{\"eq\":\"" + participantCode + "\"}}}";
Map<String,String> headers = new HashMap<>();
headers.put(Constants.AUTHORIZATION, "Bearer " + generateToken());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.hcxprotocol.dto;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class HCXIntegratorTest {

@Test
public void testInitializeConfigMap() throws Exception {
Map<String,Object> configMap = new HashMap<>();
configMap.put("protocolBasePath", "http://localhost:8095");
configMap.put("participantCode", "participant@01");
configMap.put("authBasePath", "http://localhost:8080");
configMap.put("username", "participant@gmail.com");
configMap.put("password", "12345");
configMap.put("encryptionPrivateKey", "Mz-VPPyU4RlcuYv1IwIvzw");
configMap.put("igUrl", "http://localhost:8090");

HCXIntegrator.init(configMap);
HCXIntegrator hcxIntegrator = HCXIntegrator.getInstance();

assertEquals("http://localhost:8095", hcxIntegrator.getHCXProtocolBasePath());
assertEquals("participant@01", hcxIntegrator.getParticipantCode());
assertEquals("http://localhost:8080", hcxIntegrator.getAuthBasePath());
assertEquals("participant@gmail.com", hcxIntegrator.getUsername());
assertEquals("12345", hcxIntegrator.getPassword());
assertEquals("Mz-VPPyU4RlcuYv1IwIvzw", hcxIntegrator.getPrivateKey());
assertEquals("http://localhost:8090", hcxIntegrator.getIGUrl());

configMap.put("password", "67890");
HCXIntegrator.init(configMap);

assertEquals("67890", hcxIntegrator.getPassword());
}

@Test
public void testInitializeConfigString() throws Exception {
String configStr = "{\"password\":\"12345\",\"protocolBasePath\":\"http://localhost:8095\",\"igUrl\":\"http://localhost:8090\",\"authBasePath\":\"http://localhost:8080\",\"encryptionPrivateKey\":\"Mz-VPPyU4RlcuYv1IwIvzw\",\"participantCode\":\"participant@01\",\"username\":\"participant@gmail.com\"}";

HCXIntegrator.init(configStr);
HCXIntegrator hcxIntegrator = HCXIntegrator.getInstance();

assertEquals("http://localhost:8095", hcxIntegrator.getHCXProtocolBasePath());
assertEquals("participant@01", hcxIntegrator.getParticipantCode());
assertEquals("http://localhost:8080", hcxIntegrator.getAuthBasePath());
assertEquals("participant@gmail.com", hcxIntegrator.getUsername());
assertEquals("12345", hcxIntegrator.getPassword());
assertEquals("Mz-VPPyU4RlcuYv1IwIvzw", hcxIntegrator.getPrivateKey());
assertEquals("http://localhost:8090", hcxIntegrator.getIGUrl());
}

@Test(expected = Exception.class)
public void testWithoutConfigVariablesInitialization() throws Exception {
HCXIntegrator hcxIntegrator = HCXIntegrator.getInstance();
}
}

0 comments on commit d94910a

Please sign in to comment.