Skip to content

Commit

Permalink
#122 - adding expires_in as string support for cds hooks requests ...…
Browse files Browse the repository at this point in the history
… added simple test
  • Loading branch information
c-schuler committed Sep 21, 2022
1 parent 08eff0c commit 1e62825
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
Expand Down Expand Up @@ -50,6 +51,22 @@ public static class FhirAuthorization {
public String scope;
@JsonProperty(required = true)
public String subject;

@JsonAnySetter
public void setExpiresIn(Object expiresIn) throws JsonProcessingException {
if (expiresIn instanceof String) {
this.expiresIn = Integer.parseInt((String) expiresIn);
}
else if (expiresIn instanceof Integer) {
this.expiresIn = (Integer) expiresIn;
}
else {
throw new JsonProcessingException(
String.format(
"The expires_in property accepts values of type String or Integer/ Found %s",
expiresIn.getClass().getSimpleName())) {};
}
}
}

public static class Context {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.opencds.cqf.ruler.plugin.cdshooks.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.opencds.cqf.ruler.cdshooks.request.CdsHooksRequest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

class CdsHooksRequestIT {
@Test
void testFhirAuthExpiresIn() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
// expires_in as String
String requestJson = "{\n" +
" \"hookInstance\": \"6bc883b2-b795-4dcb-b661-34884a31d472\",\n" +
" \"fhirServer\": \"https://cloud.alphora.com/sandbox/r4/cds/fhir\",\n" +
" \"hook\": \"patient-view\",\n" +
" \"fhirAuthorization\": {\n" +
" \"access_token\": \"12345\",\n" +
" \"token_type\": \"Bearer\",\n" +
" \"expires_in\": \"3600\",\n" +
" \"scope\": \"\",\n" +
" \"subject\": \"\"\n" +
" },\n" +
" \"context\": {\n" +
" \"userId\": \"Practitioner/example\",\n" +
" \"patientId\": \"Patient/example\"\n" +
" }\n" +
"}";
CdsHooksRequest cdsHooksRequest = mapper.readValue(requestJson, CdsHooksRequest.class);
assertNotNull(cdsHooksRequest.fhirAuthorization);
assertEquals(3600, cdsHooksRequest.fhirAuthorization.expiresIn);

// expires_in as int
requestJson = "{\n" +
" \"hookInstance\": \"6bc883b2-b795-4dcb-b661-34884a31d472\",\n" +
" \"fhirServer\": \"https://cloud.alphora.com/sandbox/r4/cds/fhir\",\n" +
" \"hook\": \"patient-view\",\n" +
" \"fhirAuthorization\": {\n" +
" \"access_token\": \"12345\",\n" +
" \"token_type\": \"Bearer\",\n" +
" \"expires_in\": 3600,\n" +
" \"scope\": \"\",\n" +
" \"subject\": \"\"\n" +
" },\n" +
" \"context\": {\n" +
" \"userId\": \"Practitioner/example\",\n" +
" \"patientId\": \"Patient/example\"\n" +
" }\n" +
"}";

cdsHooksRequest = mapper.readValue(requestJson, CdsHooksRequest.class);
assertNotNull(cdsHooksRequest.fhirAuthorization);
assertEquals(3600, cdsHooksRequest.fhirAuthorization.expiresIn);

// expires_in as invalid type (double)
requestJson = "{\n" +
" \"hookInstance\": \"6bc883b2-b795-4dcb-b661-34884a31d472\",\n" +
" \"fhirServer\": \"https://cloud.alphora.com/sandbox/r4/cds/fhir\",\n" +
" \"hook\": \"patient-view\",\n" +
" \"fhirAuthorization\": {\n" +
" \"access_token\": \"12345\",\n" +
" \"token_type\": \"Bearer\",\n" +
" \"expires_in\": 3600.45,\n" +
" \"scope\": \"\",\n" +
" \"subject\": \"\"\n" +
" },\n" +
" \"context\": {\n" +
" \"userId\": \"Practitioner/example\",\n" +
" \"patientId\": \"Patient/example\"\n" +
" }\n" +
"}";

try {
cdsHooksRequest = mapper.readValue(requestJson, CdsHooksRequest.class);
fail();
} catch (JsonProcessingException JPE) {
// pass
}
}
}

0 comments on commit 1e62825

Please sign in to comment.