use of org.webpieces.microsvc.impl.TestCaseRecorder.RECORDER_KEY in project webpieces by deanhiller.
the class HttpsJsonClientInvokeHandler method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
TestCaseRecorder recorder = (TestCaseRecorder) Context.get(RECORDER_KEY);
EndpointInfo recordingInfo = null;
if (recorder != null) {
Map<String, Object> ctxSnapshot = Context.copyContext();
recordingInfo = new EndpointInfo(method, args, ctxSnapshot);
recorder.addEndpointInfo(recordingInfo);
}
clientAssertions.throwIfCannotGoRemote();
Path annotation = method.getAnnotation(Path.class);
if (annotation == null) {
throw new IllegalArgumentException("The @Path annotation is missing from method=" + method + " clazz=" + method.getDeclaringClass());
}
String path = getPath(method);
HttpMethod httpMethod = getHttpMethod(method);
Class<?> clazz = method.getReturnType();
if (!(CompletableFuture.class.isAssignableFrom(clazz))) {
throw new IllegalStateException("All api methods must return a XFuture");
}
ParameterizedType t = (ParameterizedType) method.getGenericReturnType();
Class retType = (Class) t.getActualTypeArguments()[0];
Map<String, Object> context = Context.getContext();
if (context == null) {
throw new IllegalStateException("Please call Current.setContext with the request context or create your own context");
}
Object header = context.get(Context.HEADERS);
if (header == null) {
throw new IllegalStateException("Context.HEADERS is missing from the Context. please set that up first");
} else if (!(header instanceof Map)) {
throw new IllegalStateException("Context.HEADERS is not a Map<String, String> and is setup incorrectly");
}
log.info("Sending http request to: " + addr.getHostName() + ":" + addr.getPort() + path);
Object body = args[0];
if (hasUrlParams) {
path = transformPath(path, method, args);
body = findBody(method, args);
}
Endpoint endpoint = new Endpoint(addr, httpMethod.getCode(), path);
XFuture<Object> xFuture = clientHelper.sendHttpRequest(method, body, endpoint, retType).thenApply(retVal -> {
// Only needed by APIs/methods that return CompletableFuture :( not XFuture
Context.restoreContext(context);
return retVal;
});
if (recorder == null) {
return xFuture;
}
EndpointInfo finalInfo = recordingInfo;
return xFuture.handle((resp, exc1) -> addTestRecordingInfo(finalInfo, resp, exc1)).thenCompose(Function.identity());
}
Aggregations