copying over updates to helper classes

This commit is contained in:
Ken Stevens 2019-01-30 17:56:47 -05:00
parent 16c44d264b
commit adb037e805
3 changed files with 57 additions and 28 deletions

View File

@ -166,4 +166,13 @@ public class ResourceModifiedMessage implements IResourceMessage {
return true;
}
@Override
public String toString() {
String resourceId = myPayloadId;
if (resourceId == null) {
resourceId = myId;
}
return "ResourceModified Message { " + myOperationType + ", " + resourceId + "}";
}
}

View File

@ -0,0 +1,28 @@
package ca.uhn.fhir.jpa.subscription.module;
import ca.uhn.fhir.jpa.subscription.module.subscriber.ResourceDeliveryJsonMessage;
import ca.uhn.fhir.jpa.subscription.module.subscriber.ResourceDeliveryMessage;
import org.hl7.fhir.instance.model.api.IBaseResource;
import java.util.function.Function;
public class FhirObjectPrinter implements Function<Object, String> {
@Override
public String apply(Object object) {
if (object instanceof IBaseResource) {
IBaseResource resource = (IBaseResource) object;
return "Resource " + resource.getIdElement().getValue();
} else if (object instanceof ResourceDeliveryMessage) {
ResourceDeliveryMessage resourceDeliveryMessage = (ResourceDeliveryMessage) object;
// TODO KHS move this to ResourceModifiedMessage.toString()
return "ResourceDelivery Message { " + resourceDeliveryMessage.getPayloadId() + "}";
} else if (object instanceof ResourceDeliveryJsonMessage) {
ResourceDeliveryJsonMessage resourceDeliveryJsonMessage = (ResourceDeliveryJsonMessage) object;
// TODO KHS move this to ResourceModifiedMessage.toString()
ResourceDeliveryMessage resourceDeliveryMessage = resourceDeliveryJsonMessage.getPayload();
return "ResourceDeliveryJsonMessage Message { " + resourceDeliveryMessage.getPayloadId() + "}";
} else {
return object.toString();
}
}
}

View File

@ -1,10 +1,8 @@
package ca.uhn.fhir.jpa.subscription.module;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.model.interceptor.api.HookParams;
import ca.uhn.fhir.jpa.model.interceptor.api.IAnonymousLambdaHook;
import ca.uhn.fhir.jpa.model.interceptor.api.Pointcut;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -13,15 +11,15 @@ import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
public class PointcutLatch implements IAnonymousLambdaHook {
private static final Logger ourLog = LoggerFactory.getLogger(PointcutLatch.class);
private static final int DEFAULT_TIMEOUT_SECONDS = 10;
private static final FhirObjectPrinter ourFhirObjectToStringMapper = new FhirObjectPrinter();
private final String name;
private CountDownLatch myCountdownLatch;
@ -36,7 +34,7 @@ public class PointcutLatch implements IAnonymousLambdaHook {
this.name = theName;
}
public void setExpectedCount(int count) throws InterruptedException {
public void setExpectedCount(int count) {
if (myCountdownLatch != null) {
throw new PointcutLatchException("setExpectedCount() called before previous awaitExpected() completed.");
}
@ -44,6 +42,7 @@ public class PointcutLatch implements IAnonymousLambdaHook {
}
private void createLatch(int count) {
ourLog.info("Creating new latch with count {}", count);
myFailure = new AtomicReference<>();
myCalledWith = new AtomicReference<>(new ArrayList<>());
myCountdownLatch = new CountDownLatch(count);
@ -61,11 +60,12 @@ public class PointcutLatch implements IAnonymousLambdaHook {
return name + " " + this.getClass().getSimpleName();
}
public void awaitExpected() throws InterruptedException {
awaitExpectedWithTimeout(DEFAULT_TIMEOUT_SECONDS);
public List<HookParams> awaitExpected() throws InterruptedException {
return awaitExpectedWithTimeout(DEFAULT_TIMEOUT_SECONDS);
}
public void awaitExpectedWithTimeout(int timeoutSecond) throws InterruptedException {
public List<HookParams> awaitExpectedWithTimeout(int timeoutSecond) throws InterruptedException {
List<HookParams> retval = myCalledWith.get();
try {
assertNotNull(getName() + " awaitExpected() called before setExpected() called.", myCountdownLatch);
assertTrue(getName() + " timed out waiting " + timeoutSecond + " seconds for latch to be triggered.", myCountdownLatch.await(timeoutSecond, TimeUnit.SECONDS));
@ -78,6 +78,8 @@ public class PointcutLatch implements IAnonymousLambdaHook {
} finally {
destroyLatch();
}
assertEquals("Concurrency error: Latch switched while waiting.", retval, myCalledWith.get());
return retval;
}
public void expectNothing() {
@ -97,37 +99,23 @@ public class PointcutLatch implements IAnonymousLambdaHook {
return "[]";
}
String retVal = "[ ";
retVal += calledWith.stream().flatMap(hookParams -> hookParams.values().stream()).map(itemToString()).collect(Collectors.joining(", "));
retVal += calledWith.stream().flatMap(hookParams -> hookParams.values().stream()).map(ourFhirObjectToStringMapper).collect(Collectors.joining(", "));
return retVal + " ]";
}
private static Function<Object, String> itemToString() {
return object -> {
if (object instanceof IBaseResource) {
IBaseResource resource = (IBaseResource) object;
return "Resource " + resource.getIdElement().getValue();
} else if (object instanceof ResourceModifiedMessage) {
ResourceModifiedMessage resourceModifiedMessage = (ResourceModifiedMessage)object;
// FIXME KHS can we get the context from the payload?
return "ResourceModified Message { " + resourceModifiedMessage.getOperationType() + ", " + resourceModifiedMessage.getNewPayload(FhirContext.forDstu3()).getIdElement().getValue() + "}";
} else {
return object.toString();
}
};
}
@Override
public void invoke(HookParams theArgs) {
if (myCountdownLatch == null) {
throw new PointcutLatchException("countdown() called before setExpectedCount() called.", theArgs);
throw new PointcutLatchException("invoke() called before setExpectedCount() called.", theArgs);
} else if (myCountdownLatch.getCount() <= 0) {
setFailure("countdown() called " + (1 - myCountdownLatch.getCount()) + " more times than expected.");
setFailure("invoke() called " + (1 - myCountdownLatch.getCount()) + " more times than expected.");
}
this.countdown();
if (myCalledWith.get() != null) {
myCalledWith.get().add(theArgs);
}
this.countdown();
}
private void countdown() {
@ -135,6 +123,10 @@ public class PointcutLatch implements IAnonymousLambdaHook {
myCountdownLatch.countDown();
}
public void call(Object arg) {
this.invoke(new HookParams(arg));
}
private class PointcutLatchException extends IllegalStateException {
public PointcutLatchException(String message, HookParams theArgs) {
super(getName() + ": " + message + " called with values: " + hookParamsToString(theArgs));
@ -146,6 +138,6 @@ public class PointcutLatch implements IAnonymousLambdaHook {
}
private static String hookParamsToString(HookParams hookParams) {
return hookParams.values().stream().map(itemToString()).collect(Collectors.joining(", "));
return hookParams.values().stream().map(ourFhirObjectToStringMapper).collect(Collectors.joining(", "));
}
}