From 2e1f2a662a44944b68677f5b4d4f6ce3dfc72e9d Mon Sep 17 00:00:00 2001 From: Tadgh Date: Mon, 14 Sep 2020 13:20:01 -0400 Subject: [PATCH] Move class to top level, add tests --- .../messaging/json/BaseJsonMessage.java | 105 +---------------- .../messaging/json/HapiMessageHeaders.java | 108 ++++++++++++++++++ .../BaseResourceModifiedMessageTest.java | 33 ++++++ 3 files changed, 143 insertions(+), 103 deletions(-) create mode 100644 hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/HapiMessageHeaders.java create mode 100644 hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/messaging/BaseResourceModifiedMessageTest.java diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/BaseJsonMessage.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/BaseJsonMessage.java index ae9cb3ced38..bb422fc25c2 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/BaseJsonMessage.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/BaseJsonMessage.java @@ -25,21 +25,12 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHeaders; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - public abstract class BaseJsonMessage implements Message, IModelJson { private static final long serialVersionUID = 1L; @JsonProperty("headers") private HapiMessageHeaders myHeaders; - private String RETRY_COUNT_HEADER = "retryCount"; - private String FIRST_FAILURE_HEADER = "firstFailure"; - private String LAST_FAILURE_HEADER = "lastFailure"; /** * Constructor @@ -48,86 +39,10 @@ public abstract class BaseJsonMessage implements Message, IModelJson { super(); setDefaultRetryHeaders(); } - public static class HapiMessageHeaders implements Map{ - - private final Map headers; - - public HapiMessageHeaders(Map theHeaders) { - headers = theHeaders; - } - - public HapiMessageHeaders() { - headers = new HashMap<>(); - } - - - @Override - public int size() { - return this.headers.size(); - } - - @Override - public boolean isEmpty() { - return this.headers.isEmpty(); - } - - @Override - public boolean containsKey(Object key) { - return this.headers.containsKey(key); - } - - @Override - public boolean containsValue(Object value) { - return this.headers.containsValue(value); - } - - @Override - public Object get(Object key) { - return this.headers.get(key); - } - - @Override - public Object put(String key, Object value) { - return this.headers.put(key, value); - } - - @Override - public Object remove(Object key) { - return this.headers.remove(key); - } - - @Override - public void putAll(Map m) { - this.headers.putAll(m); - } - - @Override - public void clear() { - this.headers.clear(); - } - - @Override - public Set keySet() { - return this.headers.keySet(); - } - - @Override - public Collection values() { - return this.headers.values(); - } - - @Override - public Set> entrySet() { - return this.headers.entrySet(); - } - } protected void setDefaultRetryHeaders() { - Map headers = new HashMap<>(); - headers.put(RETRY_COUNT_HEADER, 0); - headers.put(FIRST_FAILURE_HEADER, null); - headers.put(LAST_FAILURE_HEADER, null); - HapiMessageHeaders messageHeaders = new HapiMessageHeaders(headers); + HapiMessageHeaders messageHeaders = new HapiMessageHeaders(); + messageHeaders.initializeDefaultRetryValues(); setHeaders(messageHeaders); } @@ -140,22 +55,6 @@ public abstract class BaseJsonMessage implements Message, IModelJson { return myHeaders; } - public final Integer getRetryCount() { - //TODO GGG this is not NPE-safe - return (Integer)this.getHeaders().get(RETRY_COUNT_HEADER); - } - - public final Date getFirstFailureDate() { - //TODO GGG this is not NPE-safe - return (Date)this.getHeaders().get(FIRST_FAILURE_HEADER); - - } - - public final Date getLastFailureDate() { - //TODO GGG this is not NPE-safe - return (Date)this.getHeaders().get(LAST_FAILURE_HEADER); - - } public void setHeaders(HapiMessageHeaders theHeaders) { myHeaders = theHeaders; diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/HapiMessageHeaders.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/HapiMessageHeaders.java new file mode 100644 index 00000000000..17bf3488270 --- /dev/null +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/messaging/json/HapiMessageHeaders.java @@ -0,0 +1,108 @@ +package ca.uhn.fhir.rest.server.messaging.json; + +import ca.uhn.fhir.model.api.IModelJson; + +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class HapiMessageHeaders implements Map, IModelJson { + public static String RETRY_COUNT_HEADER = "retryCount"; + public static String FIRST_FAILURE_HEADER = "firstFailure"; + public static String LAST_FAILURE_HEADER = "lastFailure"; + + private final Map headers; + + public HapiMessageHeaders(Map theHeaders) { + headers = theHeaders; + } + + public HapiMessageHeaders() { + headers = new HashMap<>(); + } + + + @Override + public int size() { + return this.headers.size(); + } + + @Override + public boolean isEmpty() { + return this.headers.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return this.headers.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return this.headers.containsValue(value); + } + + @Override + public Object get(Object key) { + return this.headers.get(key); + } + + @Override + public Object put(String key, Object value) { + return this.headers.put(key, value); + } + + @Override + public Object remove(Object key) { + return this.headers.remove(key); + } + + @Override + public void putAll(Map m) { + this.headers.putAll(m); + } + + @Override + public void clear() { + this.headers.clear(); + } + + @Override + public Set keySet() { + return this.headers.keySet(); + } + + @Override + public Collection values() { + return this.headers.values(); + } + + @Override + public Set> entrySet() { + return this.headers.entrySet(); + } + + public Integer getRetryCount() { + return (Integer)this.getHeaders().get(RETRY_COUNT_HEADER); + } + + public Date getFirstFailureDate() { + return (Date)this.getHeaders().get(FIRST_FAILURE_HEADER); + + } + + public Date getLastFailureDate() { + return (Date)this.getHeaders().get(LAST_FAILURE_HEADER); + + } + public Map getHeaders() { + return this.headers; + } + public void initializeDefaultRetryValues() { + headers.put(RETRY_COUNT_HEADER, 0); + headers.put(FIRST_FAILURE_HEADER, null); + headers.put(LAST_FAILURE_HEADER, null); + } +} diff --git a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/messaging/BaseResourceModifiedMessageTest.java b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/messaging/BaseResourceModifiedMessageTest.java new file mode 100644 index 00000000000..f001236e0e1 --- /dev/null +++ b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/messaging/BaseResourceModifiedMessageTest.java @@ -0,0 +1,33 @@ +package ca.uhn.fhir.rest.server.messaging; + +import ca.uhn.fhir.rest.server.messaging.json.BaseResourceModifiedJsonMessage; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static ca.uhn.fhir.rest.server.messaging.json.HapiMessageHeaders.FIRST_FAILURE_HEADER; +import static ca.uhn.fhir.rest.server.messaging.json.HapiMessageHeaders.LAST_FAILURE_HEADER; +import static ca.uhn.fhir.rest.server.messaging.json.HapiMessageHeaders.RETRY_COUNT_HEADER; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class BaseResourceModifiedMessageTest { + + @Test + public void testSerializationAndDeserializationOfResourceModifiedMessage() throws JsonProcessingException { + BaseResourceModifiedJsonMessage jsonMessage = new BaseResourceModifiedJsonMessage(); + jsonMessage.setPayload(new BaseResourceModifiedMessage()); + ObjectMapper mapper = new ObjectMapper(); + String serialized = mapper.writeValueAsString(jsonMessage); + jsonMessage = mapper.readValue(serialized, BaseResourceModifiedJsonMessage.class); + + assertThat(jsonMessage.getHapiHeaders().getRetryCount(), is(equalTo(0))); + assertThat(jsonMessage.getHapiHeaders().getFirstFailureDate(), is(equalTo(null))); + assertThat(jsonMessage.getHapiHeaders().getLastFailureDate(), is(equalTo(null))); + + assertThat(jsonMessage.getHeaders().get(RETRY_COUNT_HEADER), is(equalTo(0))); + assertThat(jsonMessage.getHeaders().get(FIRST_FAILURE_HEADER), is(equalTo(null))); + assertThat(jsonMessage.getHeaders().get(LAST_FAILURE_HEADER), is(equalTo(null))); + } +}