add message key to all message types (#4101)

* add message key to all message types

* move test

Co-authored-by: Ken Stevens <ken@smilecdr.com>
This commit is contained in:
Ken Stevens 2022-10-01 11:36:04 -04:00 committed by GitHub
parent e9a855d3f6
commit 26756a40bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 5 deletions

View File

@ -17,7 +17,6 @@ import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Library;
import org.hl7.fhir.r4.model.Measure;
import org.hl7.fhir.r4.model.StructureDefinition;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@ -27,6 +26,7 @@ import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import javax.annotation.Nonnull;
import java.util.function.Predicate;
import static ca.uhn.fhir.util.ClasspathUtil.loadResource;
@ -151,7 +151,7 @@ public class JpaPersistedResourceValidationSupportFromValidationChainTest {
assertEquals(29, validationResult.getMessages().stream().filter(errorMessagePredicate()).count());
}
@NotNull
@Nonnull
private static FhirValidator getFhirValidator() {
FhirValidator validator;
final FhirInstanceValidator instanceValidator = new FhirInstanceValidator(ourCtx);
@ -162,12 +162,12 @@ public class JpaPersistedResourceValidationSupportFromValidationChainTest {
return validator;
}
@NotNull
@Nonnull
private static Bundle getBundle(String jsonFilePath) {
return ourCtx.newJsonParser().parseResource(Bundle.class, loadResource(jsonFilePath));
}
@NotNull
@Nonnull
private IValidationSupport getValidationSupportWithJpaPersistedResourceValidationSupport() {
return new ValidationSupportChain(
new DefaultProfileValidationSupport(ourCtx),
@ -175,7 +175,7 @@ public class JpaPersistedResourceValidationSupportFromValidationChainTest {
);
}
@NotNull
@Nonnull
private static Predicate<SingleValidationMessage> errorMessagePredicate() {
return message -> message.getSeverity() == ResultSeverityEnum.ERROR;
}

View File

@ -33,6 +33,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import javax.annotation.Nullable;
import java.util.List;
import static org.apache.commons.lang3.StringUtils.isBlank;
@ -201,5 +202,13 @@ public abstract class BaseResourceModifiedMessage extends BaseResourceMessage im
}
return true;
}
@Nullable
@Override
public String getMessageKeyOrNull() {
return myPayloadId;
}
}

View File

@ -24,6 +24,8 @@ import ca.uhn.fhir.rest.server.messaging.ResourceOperationMessage;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.builder.ToStringBuilder;
import javax.annotation.Nullable;
public class ResourceOperationJsonMessage extends BaseJsonMessage<ResourceOperationMessage> {
@ -66,4 +68,10 @@ public class ResourceOperationJsonMessage extends BaseJsonMessage<ResourceOperat
.append("myPayload", myPayload)
.toString();
}
@Nullable
@Override
public String getMessageKeyOrNull() {
return myPayload.getMessageKeyOrNull();
}
}

View File

@ -0,0 +1,57 @@
package ca.uhn.fhir.rest.server.messaging.json;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.subscription.model.ResourceDeliveryJsonMessage;
import ca.uhn.fhir.jpa.subscription.model.ResourceDeliveryMessage;
import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedJsonMessage;
import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedMessage;
import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.rest.server.messaging.BaseResourceMessage;
import ca.uhn.fhir.rest.server.messaging.ResourceOperationMessage;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Patient;
import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
import static org.junit.jupiter.api.Assertions.assertEquals;
class BaseJsonMessageTest {
FhirContext ourFhirContext = FhirContext.forR4Cached();
static final String RESOURCE_ID = "Patient/123";
@Test
void test_messageKeyIsResourceId_ResourceOperationJsonMessage() {
ResourceOperationJsonMessage message = new ResourceOperationJsonMessage();
IBaseResource patient = buildPatient();
ResourceOperationMessage payload = new ResourceOperationMessage(ourFhirContext, patient, ResourceOperationMessage.OperationTypeEnum.CREATE);
message.setPayload(payload);
assertEquals(RESOURCE_ID, message.getMessageKeyOrNull());
}
@Nonnull
private static IBaseResource buildPatient() {
IBaseResource patient = new Patient();
patient.setId(RESOURCE_ID);
return patient;
}
@Test
void test_messageKeyIsResourceId_ResourceDeliveryJsonMessage() {
ResourceDeliveryJsonMessage message = new ResourceDeliveryJsonMessage();
IBaseResource patient = buildPatient();
ResourceDeliveryMessage payload = new ResourceDeliveryMessage();
payload.setPayload(ourFhirContext, patient, EncodingEnum.JSON);
message.setPayload(payload);
assertEquals(RESOURCE_ID, message.getMessageKeyOrNull());
}
@Test
void test_messageKeyIsResourceId_ResourceModifiedJsonMessage() {
ResourceModifiedJsonMessage message = new ResourceModifiedJsonMessage();
IBaseResource patient = buildPatient();
ResourceModifiedMessage payload = new ResourceModifiedMessage(ourFhirContext, patient, BaseResourceMessage.OperationTypeEnum.CREATE);
message.setPayload(payload);
assertEquals(RESOURCE_ID, message.getMessageKeyOrNull());
}
}

View File

@ -26,6 +26,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.builder.ToStringBuilder;
import javax.annotation.Nullable;
public class ResourceDeliveryJsonMessage extends BaseJsonMessage<ResourceDeliveryMessage> {
private static final ObjectMapper ourObjectMapper = new ObjectMapper().registerModule(new com.fasterxml.jackson.datatype.jsr310.JavaTimeModule());
@ -55,6 +57,15 @@ public class ResourceDeliveryJsonMessage extends BaseJsonMessage<ResourceDeliver
myPayload = thePayload;
}
@Override
@Nullable
public String getMessageKeyOrNull() {
if (myPayload == null) {
return null;
}
return myPayload.getMessageKeyOrNull();
}
@Override
public String toString() {
return new ToStringBuilder(this)

View File

@ -32,6 +32,8 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import javax.annotation.Nullable;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
@SuppressWarnings("WeakerAccess")
@ -147,4 +149,10 @@ public class ResourceDeliveryMessage extends BaseResourceMessage implements IRes
}
return retVal;
}
@Nullable
@Override
public String getMessageKeyOrNull() {
return myPayloadId;
}
}