Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for expires_in as string #614

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}