Add a test

This commit is contained in:
jamesagnew 2019-11-02 18:47:02 -04:00
parent 5ad57d9ea9
commit f7ad82d7a8
2 changed files with 168 additions and 1 deletions

View File

@ -25,6 +25,7 @@ import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.Interceptor;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.jpa.api.IDaoRegistry;
import ca.uhn.fhir.jpa.config.BaseConfig;
import ca.uhn.fhir.jpa.dao.DaoConfig;
import ca.uhn.fhir.jpa.dao.DaoRegistry;
@ -162,6 +163,7 @@ public class SubscriptionActivatingInterceptor {
}
}
@SuppressWarnings("unchecked")
private boolean activateSubscription(String theActiveStatus, final IBaseResource theSubscription, String theRequestedStatus) {
IFhirResourceDao subscriptionDao = myDaoRegistry.getSubscriptionDao();
IBaseResource subscription = subscriptionDao.read(theSubscription.getIdElement());
@ -215,7 +217,14 @@ public class SubscriptionActivatingInterceptor {
}
}
private void validateSubmittedSubscription(IBaseResource theSubscription) {
@VisibleForTesting
@SuppressWarnings("WeakerAccess")
public void setSubscriptionStrategyEvaluatorForUnitTest(SubscriptionStrategyEvaluator theSubscriptionStrategyEvaluator) {
mySubscriptionStrategyEvaluator = theSubscriptionStrategyEvaluator;
}
@SuppressWarnings("WeakerAccess")
public void validateSubmittedSubscription(IBaseResource theSubscription) {
CanonicalSubscription subscription = mySubscriptionCanonicalizer.canonicalize(theSubscription);
boolean finished = false;
@ -328,6 +337,18 @@ public class SubscriptionActivatingInterceptor {
});
}
@SuppressWarnings("WeakerAccess")
@VisibleForTesting
public void setSubscriptionCanonicalizerForUnitTest(SubscriptionCanonicalizer theSubscriptionCanonicalizer) {
mySubscriptionCanonicalizer = theSubscriptionCanonicalizer;
}
@SuppressWarnings("WeakerAccess")
@VisibleForTesting
public void setDaoRegistryForUnitTest(DaoRegistry theDaoRegistry) {
myDaoRegistry = theDaoRegistry;
}
@VisibleForTesting
public static void setWaitForSubscriptionActivationSynchronouslyForUnitTest(boolean theWaitForSubscriptionActivationSynchronouslyForUnitTest) {

View File

@ -0,0 +1,146 @@
package ca.uhn.fhir.jpa.subscription;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.dao.DaoRegistry;
import ca.uhn.fhir.jpa.subscription.module.cache.SubscriptionCanonicalizer;
import ca.uhn.fhir.jpa.subscription.module.matcher.SubscriptionStrategyEvaluator;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import org.hl7.fhir.r4.model.Subscription;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class SubscriptionActivatingInterceptorTest {
@Mock
public DaoRegistry myDaoRegistry;
private SubscriptionActivatingInterceptor mySvc;
private FhirContext myCtx = FhirContext.forR4();
@Mock
private SubscriptionStrategyEvaluator mySubscriptionStrategyEvaluator;
@Before
public void before() {
mySvc = new SubscriptionActivatingInterceptor();
mySvc.setSubscriptionCanonicalizerForUnitTest(new SubscriptionCanonicalizer(myCtx));
mySvc.setDaoRegistryForUnitTest(myDaoRegistry);
mySvc.setSubscriptionStrategyEvaluatorForUnitTest(mySubscriptionStrategyEvaluator);
}
@Test
public void testValidate_Empty() {
Subscription subscription = new Subscription();
try {
mySvc.validateSubmittedSubscription(subscription);
fail();
} catch (UnprocessableEntityException e) {
assertThat(e.getMessage(), containsString("Subscription.status must be populated on this server"));
}
}
@Test
public void testValidate_RestHook_Populated() {
when(myDaoRegistry.isResourceTypeSupported(eq("Patient"))).thenReturn(true);
Subscription subscription = new Subscription();
subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE);
subscription.setCriteria("Patient?identifier=foo");
subscription.getChannel().setType(Subscription.SubscriptionChannelType.RESTHOOK);
subscription.getChannel().setPayload("application/fhir+json");
subscription.getChannel().setEndpoint("http://foo");
mySvc.validateSubmittedSubscription(subscription);
}
@Test
public void testValidate_RestHook_ResourceTypeNotSupported() {
when(myDaoRegistry.isResourceTypeSupported(eq("Patient"))).thenReturn(false);
Subscription subscription = new Subscription();
subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE);
subscription.setCriteria("Patient?identifier=foo");
subscription.getChannel().setType(Subscription.SubscriptionChannelType.RESTHOOK);
subscription.getChannel().setPayload("application/fhir+json");
subscription.getChannel().setEndpoint("http://foo");
try {
mySvc.validateSubmittedSubscription(subscription);
fail();
} catch (UnprocessableEntityException e) {
assertThat(e.getMessage(), containsString("Subscription.criteria contains invalid/unsupported resource type: Patient"));
}
}
@Test
public void testValidate_RestHook_NoEndpoint() {
Subscription subscription = new Subscription();
subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE);
subscription.setCriteria("Patient?identifier=foo");
subscription.getChannel().setType(Subscription.SubscriptionChannelType.RESTHOOK);
subscription.getChannel().setPayload("application/fhir+json");
try {
mySvc.validateSubmittedSubscription(subscription);
fail();
} catch (UnprocessableEntityException e) {
assertThat(e.getMessage(), containsString("Rest-hook subscriptions must have Subscription.channel.endpoint defined"));
}
}
@Test
public void testValidate_RestHook_NoType() {
Subscription subscription = new Subscription();
subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE);
subscription.setCriteria("Patient?identifier=foo");
subscription.getChannel().setPayload("application/fhir+json");
subscription.getChannel().setEndpoint("http://foo");
try {
mySvc.validateSubmittedSubscription(subscription);
fail();
} catch (UnprocessableEntityException e) {
assertThat(e.getMessage(), containsString("Subscription.channel.type must be populated"));
}
}
@Test
public void testValidate_RestHook_NoPayload() {
when(myDaoRegistry.isResourceTypeSupported(eq("Patient"))).thenReturn(true);
Subscription subscription = new Subscription();
subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE);
subscription.setCriteria("Patient?identifier=foo");
subscription.getChannel().setType(Subscription.SubscriptionChannelType.RESTHOOK);
subscription.getChannel().setEndpoint("http://foo");
mySvc.validateSubmittedSubscription(subscription);
}
@Test
public void testValidate_RestHook_NoCriteria() {
Subscription subscription = new Subscription();
subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE);
subscription.getChannel().setType(Subscription.SubscriptionChannelType.RESTHOOK);
subscription.getChannel().setPayload("application/fhir+json");
subscription.getChannel().setEndpoint("http://foo");
try {
mySvc.validateSubmittedSubscription(subscription);
fail();
} catch (UnprocessableEntityException e) {
assertThat(e.getMessage(), containsString("Subscription.criteria must be populated"));
}
}
}