Move class to top level, add tests

This commit is contained in:
Tadgh 2020-09-14 13:20:01 -04:00
parent c3ddf59d6f
commit 2e1f2a662a
3 changed files with 143 additions and 103 deletions

View File

@ -25,21 +25,12 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders; 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<T> implements Message<T>, IModelJson { public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@JsonProperty("headers") @JsonProperty("headers")
private HapiMessageHeaders myHeaders; private HapiMessageHeaders myHeaders;
private String RETRY_COUNT_HEADER = "retryCount";
private String FIRST_FAILURE_HEADER = "firstFailure";
private String LAST_FAILURE_HEADER = "lastFailure";
/** /**
* Constructor * Constructor
@ -48,86 +39,10 @@ public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
super(); super();
setDefaultRetryHeaders(); setDefaultRetryHeaders();
} }
public static class HapiMessageHeaders implements Map<String, Object>{
private final Map<String, Object> headers;
public HapiMessageHeaders(Map<String, Object> 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<? extends String, ?> m) {
this.headers.putAll(m);
}
@Override
public void clear() {
this.headers.clear();
}
@Override
public Set<String> keySet() {
return this.headers.keySet();
}
@Override
public Collection<Object> values() {
return this.headers.values();
}
@Override
public Set<Entry<String, Object>> entrySet() {
return this.headers.entrySet();
}
}
protected void setDefaultRetryHeaders() { protected void setDefaultRetryHeaders() {
Map<String, Object> headers = new HashMap<>(); HapiMessageHeaders messageHeaders = new HapiMessageHeaders();
headers.put(RETRY_COUNT_HEADER, 0); messageHeaders.initializeDefaultRetryValues();
headers.put(FIRST_FAILURE_HEADER, null);
headers.put(LAST_FAILURE_HEADER, null);
HapiMessageHeaders messageHeaders = new HapiMessageHeaders(headers);
setHeaders(messageHeaders); setHeaders(messageHeaders);
} }
@ -140,22 +55,6 @@ public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
return myHeaders; 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) { public void setHeaders(HapiMessageHeaders theHeaders) {
myHeaders = theHeaders; myHeaders = theHeaders;

View File

@ -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<String, Object>, 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<String, Object> headers;
public HapiMessageHeaders(Map<String, Object> 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<? extends String, ?> m) {
this.headers.putAll(m);
}
@Override
public void clear() {
this.headers.clear();
}
@Override
public Set<String> keySet() {
return this.headers.keySet();
}
@Override
public Collection<Object> values() {
return this.headers.values();
}
@Override
public Set<Entry<String, Object>> 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<String, Object> 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);
}
}

View File

@ -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)));
}
}