Search in sources :

Example 1 with APIMethodResult

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method defineModuleMethodTestRoute.

// TODO Remove when not needed anymore
@SuppressWarnings("boxing")
private void defineModuleMethodTestRoute() {
    Route route = router.route(HttpMethod.GET, "/:" + PARAM_MODULE + "/:" + PARAM_METHOD + "*").produces(APPLICATION_JSON);
    route.blockingHandler(routingContext -> {
        HttpServerRequest request = routingContext.request();
        String module = request.getParam(PARAM_MODULE);
        String method = request.getParam(PARAM_METHOD);
        Map<String, Object> params = retrieveQueryParameters(request);
        logger.logInfo("TEST http://localhost:" + String.valueOf(port) + request.uri() + " AT " + new Date().toString());
        ModuleInstance instance = null;
        try {
            instance = engine.createInstance(module, defaultConnectionConfiguration);
            APIMethodResult result = engine.callAPIOnInstance(instance.getId(), method, params);
            if (!result.wasExceptionRaised()) {
                String resultJson = Serializer.convertLegacyResultToJson(result.getResult());
                logger.logDebug("RESULT: " + String.valueOf(resultJson));
                engine.deleteInstance(instance.getId());
                respond(200, routingContext, resultJson);
            } else {
                engine.deleteInstance(instance.getId());
                logger.logDebug("RESULT error happened: " + result.getMetadata());
                if (result.getMetadata() == null) {
                    sendError(setResponse(500, routingContext), null, (result.getException().getMessage() == null) ? "Exception of type " + result.getException() + " was raised" : result.getException().getMessage());
                } else {
                    sendError(setResponse(500, routingContext), result.getMetadata().getStatusCode(), "An error occurred during call. Call metadata: " + result.getMetadata().toString());
                }
            }
        } catch (UnknownAPIException | APICallException | ServerCacheException e) {
            if (instance != null) {
                try {
                    engine.deleteInstance(instance.getId());
                } catch (ServerCacheException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            sendError(setResponse(500, routingContext), null, (e.getMessage() == null) ? "Exception of type " + e + " was raised" : e.getMessage());
        }
    });
}
Also used : UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) HttpServerRequest(io.vertx.core.http.HttpServerRequest) JsonObject(io.vertx.core.json.JsonObject) APIMethodResult(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) ModuleInstance(com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance) Route(io.vertx.ext.web.Route) Date(java.util.Date) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException)

Example 2 with APIMethodResult

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method defineRunInstanceMethodRoute.

private void defineRunInstanceMethodRoute() {
    Route route = router.route(HttpMethod.POST, "/instances/:" + PARAM_INSTANCE + "/methods/:" + PARAM_METHOD).produces(APPLICATION_JSON);
    route.blockingHandler(routingContext -> {
        HttpServerRequest request = routingContext.request();
        String instanceId = request.getParam(PARAM_INSTANCE);
        String methodId = request.getParam(PARAM_METHOD);
        Map<String, Object> methodArgs = fetchMethodArgs(routingContext.getBodyAsString());
        execute(200, routingContext, new ServerAction() {

            @Override
            public Object execute() throws Exception {
                logger.logInfo("TEST http://localhost:" + String.valueOf(port) + request.uri() + " AT " + new Date().toString());
                APIMethodResult result = engine.callAPIOnInstance(instanceId, methodId, methodArgs);
                if (!result.wasExceptionRaised()) {
                    return result.getResult();
                }
                logger.logDebug("RESULT error happened: " + result.getMetadata());
                throw new APICallException(result);
            }
        }, true);
    });
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) JsonObject(io.vertx.core.json.JsonObject) APIMethodResult(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) Route(io.vertx.ext.web.Route) UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) MissingInstanceException(com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException) Date(java.util.Date)

Example 3 with APIMethodResult

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult in project mbed-cloud-sdk-java by ARMmbed.

the class APICaller method callAPIOnModuleInstance.

@SuppressWarnings("null")
public APIMethodResult callAPIOnModuleInstance(ModuleInstance moduleInstance, String method, Map<String, Object> parameters) throws UnknownAPIException, APICallException {
    if (moduleInstance == null) {
        throwMissingModule(null);
    }
    APIModule moduleDescription = moduleInstance.getModuleDescription();
    if (moduleDescription == null) {
        throwMissingModule(moduleDescription);
    }
    if (method == null) {
        throwUnknownAPI(moduleDescription.getSimpleName(), method);
    }
    final List<APIMethod> methodObjs = moduleDescription.getMethod(method);
    if (methodObjs == null) {
        throwUnknownAPI(moduleDescription.getSimpleName(), method);
    }
    APICallException lastException = null;
    APIMethodResult result = null;
    Object instance = moduleInstance.getInstance();
    // then last exception is raised or the last result is returned if not null.
    for (final APIMethod methodObj : methodObjs) {
        try {
            result = null;
            lastException = null;
            API api = new API(moduleDescription, methodObj);
            result = api.call(instance, parameters);
            // other methods
            if (!result.wasExceptionRaised()) {
                return result;
            }
        } catch (APICallException exception) {
            lastException = exception;
        }
    }
    // If the call was successful but an exception was raised during it then the failure is returned.
    if (result != null) {
        return result;
    }
    // If the call was not successful and hence, an exception was raised, then it is thrown.
    throw lastException;
}
Also used : APIModule(com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule) APIMethod(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod) APIMethodResult(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)

Aggregations

APIMethodResult (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)3 ServerCacheException (com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException)2 UnknownAPIException (com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException)2 APICallException (com.arm.mbed.cloud.sdk.testutils.APICallException)2 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 JsonObject (io.vertx.core.json.JsonObject)2 Route (io.vertx.ext.web.Route)2 Date (java.util.Date)2 MissingInstanceException (com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException)1 APIMethod (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod)1 APIModule (com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule)1 ModuleInstance (com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance)1