Search in sources :

Example 1 with ServiceEnabledCommand

use of org.locationtech.geowave.core.cli.api.ServiceEnabledCommand in project geowave by locationtech.

the class GeoWaveOperationServiceWrapperTest method postMethodReturnsSuccessStatus.

@Test
public void postMethodReturnsSuccessStatus() throws Exception {
    // Rarely used Teapot Code to check.
    final Boolean successStatusIs200 = false;
    final ServiceEnabledCommand operation = mockedOperation(HttpMethod.POST, successStatusIs200);
    classUnderTest = new GeoWaveOperationServiceWrapper(operation, null);
    classUnderTest.setResponse(new Response(null));
    classUnderTest.restPost(mockedRequest(MediaType.APPLICATION_JSON));
    Assert.assertEquals(successStatusIs200, classUnderTest.getResponse().getStatus().equals(Status.SUCCESS_OK));
}
Also used : Response(org.restlet.Response) ServiceEnabledCommand(org.locationtech.geowave.core.cli.api.ServiceEnabledCommand) Test(org.junit.Test)

Example 2 with ServiceEnabledCommand

use of org.locationtech.geowave.core.cli.api.ServiceEnabledCommand in project geowave by locationtech.

the class GeoWaveOperationServiceWrapperTest method mockedOperation.

private ServiceEnabledCommand mockedOperation(final HttpMethod method, final Boolean successStatusIs200, final boolean isAsync) throws Exception {
    final ServiceEnabledCommand operation = Mockito.mock(ServiceEnabledCommand.class);
    Mockito.when(operation.getMethod()).thenReturn(method);
    Mockito.when(operation.runAsync()).thenReturn(isAsync);
    Mockito.when(operation.successStatusIs200()).thenReturn(successStatusIs200);
    Mockito.when(operation.computeResults(Matchers.any())).thenReturn(null);
    return operation;
}
Also used : ServiceEnabledCommand(org.locationtech.geowave.core.cli.api.ServiceEnabledCommand)

Example 3 with ServiceEnabledCommand

use of org.locationtech.geowave.core.cli.api.ServiceEnabledCommand in project geowave by locationtech.

the class GeoWaveOperationServiceWrapperTest method asyncMethodReturnsSuccessStatus.

@Test
@Ignore
public void asyncMethodReturnsSuccessStatus() throws Exception {
    // Rarely used Teapot Code to check.
    final Boolean successStatusIs200 = true;
    final ServiceEnabledCommand operation = mockedOperation(HttpMethod.POST, successStatusIs200, true);
    classUnderTest = new GeoWaveOperationServiceWrapper(operation, null);
    classUnderTest.setResponse(new Response(null));
    classUnderTest.restPost(null);
    // TODO: Returns 500. Error Caught at
    // "final Context appContext = Application.getCurrent().getContext();"
    Assert.assertEquals(successStatusIs200, classUnderTest.getResponse().getStatus().equals(Status.SUCCESS_OK));
}
Also used : Response(org.restlet.Response) ServiceEnabledCommand(org.locationtech.geowave.core.cli.api.ServiceEnabledCommand) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with ServiceEnabledCommand

use of org.locationtech.geowave.core.cli.api.ServiceEnabledCommand in project geowave by locationtech.

the class GeoWaveOperationServiceWrapperTest method getMethodReturnsSuccessStatus.

@Test
public void getMethodReturnsSuccessStatus() throws Exception {
    // Rarely used Teapot Code to check.
    final Boolean successStatusIs200 = true;
    final ServiceEnabledCommand operation = mockedOperation(HttpMethod.GET, successStatusIs200);
    classUnderTest = new GeoWaveOperationServiceWrapper(operation, null);
    classUnderTest.setResponse(new Response(null));
    classUnderTest.setRequest(new Request(Method.GET, "foo.bar"));
    classUnderTest.restGet();
    Assert.assertEquals(successStatusIs200, classUnderTest.getResponse().getStatus().equals(Status.SUCCESS_OK));
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) ServiceEnabledCommand(org.locationtech.geowave.core.cli.api.ServiceEnabledCommand) Test(org.junit.Test)

Example 5 with ServiceEnabledCommand

use of org.locationtech.geowave.core.cli.api.ServiceEnabledCommand in project geowave by locationtech.

the class GeowaveOperationGrpcGenerator method parseOperationsForApiRoutes.

/**
 * This method parses all the Geowave Operation classes and creates the info to generate a gRPC
 * based on the operation.
 *
 * @throws SecurityException
 * @throws NoSuchMethodException
 */
public void parseOperationsForApiRoutes() throws NoSuchMethodException, SecurityException {
    final HashMap<String, ArrayList<String>> rpcs = new HashMap<>();
    final HashMap<String, ArrayList<String>> rpcInputMessages = new HashMap<>();
    final HashMap<String, String> retMessages = new HashMap<>();
    Set<Class<? extends ServiceEnabledCommand>> t = null;
    try {
        t = new Reflections("org.locationtech.geowave").getSubTypesOf(ServiceEnabledCommand.class);
    } catch (final Exception e) {
        LOGGER.debug(e.getMessage());
    }
    if (t == null) {
        LOGGER.debug("No operations found");
        return;
    }
    for (final Class<? extends ServiceEnabledCommand> operation : t) {
        if (!Modifier.isAbstract(operation.getModifiers())) {
            // Tokenize the package name so we can store the operations
            // according to their original package names
            final String packageName = operation.getPackage().getName();
            final String[] packageToks = packageName.split("\\.");
            String serviceName = "";
            for (int i = 0; i < packageToks.length; i++) {
                if (packageToks[i].equalsIgnoreCase("geowave")) {
                    // reason)
                    if (packageToks[i + 2].equalsIgnoreCase("operations")) {
                        serviceName = "Core" + WordUtils.capitalize(packageToks[i + 1]);
                    } else {
                        serviceName = WordUtils.capitalize(packageToks[i + 1]) + WordUtils.capitalize(packageToks[i + 2]);
                    }
                    if (!rpcs.containsKey(serviceName)) {
                        rpcs.put(serviceName, new ArrayList<String>());
                        rpcInputMessages.put(serviceName, new ArrayList<String>());
                        break;
                    }
                }
            }
            LOGGER.info("Parsing operation: " + operation.getName());
            // tokenize the operation name so we can generate a name for
            // the RPC
            final String[] rpcNameToks = operation.getName().split("\\.");
            final String rpcName = rpcNameToks[rpcNameToks.length - 1];
            // get the return type for this command
            String responseName = "";
            Class<?> parentClass = operation;
            boolean success = false;
            Type paramType = null;
            while (parentClass != null) {
                try {
                    paramType = ((ParameterizedType) parentClass.getGenericSuperclass()).getActualTypeArguments()[0];
                    success = true;
                } catch (final Exception e) {
                    continue;
                } finally {
                    if (success) {
                        break;
                    }
                    parentClass = parentClass.getSuperclass();
                }
            }
            if (success) {
                String retType = GeoWaveGrpcOperationParser.getGrpcReturnType(paramType.getTypeName());
                responseName = retType.replaceAll("(<)|(>)|(,)", " ");
                responseName = WordUtils.capitalize(responseName);
                responseName = responseName.replaceAll(" ", "") + "ResponseProtos";
                // empty message
                if (retType.equalsIgnoreCase("void")) {
                    retType = "\nmessage " + responseName + " { }";
                } else {
                    retType = "\nmessage " + responseName + " { " + retType + " responseValue = 1; }";
                }
                retMessages.put(retType, retType);
            }
            final String rpc = "\t rpc " + rpcName + "(" + rpcName + "ParametersProtos) returns (" + responseName + ") {} \n";
            rpcs.get(serviceName).add(rpc);
            final ProcessOperationResult pr = new ProcessOperationResult();
            pr.message = "\nmessage " + rpcName + "ParametersProtos {";
            pr.currFieldPosition = 1;
            Class<?> opClass = operation;
            try {
                while (opClass.getSuperclass() != null) {
                    processOperation(opClass, pr);
                    opClass = opClass.getSuperclass();
                }
            } catch (final IOException e) {
                LOGGER.error("Exception encountered processing operations", e);
            }
            pr.message += "\n}\n";
            rpcInputMessages.get(serviceName).add(pr.message);
        }
    }
    // write out all the service files
    Iterator it = rpcs.entrySet().iterator();
    while (it.hasNext()) {
        final HashMap.Entry pair = (HashMap.Entry) it.next();
        final String currServiceName = (String) pair.getKey();
        final ArrayList<String> rpcList = (ArrayList<String>) pair.getValue();
        final ArrayList<String> rpcInputMessageList = rpcInputMessages.get(currServiceName);
        final String serviceFilename = outputBasePath + "/src/main/protobuf/GeoWave" + pair.getKey() + ".proto";
        Writer serviceWriter = null;
        try {
            serviceWriter = new OutputStreamWriter(new FileOutputStream(serviceFilename), "UTF-8");
        } catch (final IOException e) {
            LOGGER.error("Exception encountered opening file stream", e);
        }
        // first write header
        final String serviceHeader = header + "import \"GeoWaveReturnTypesProtos.proto\";\n" + options.replace("&OUTER_CLASSNAME&", currServiceName + "ServiceProtos");
        try {
            if (serviceWriter != null) {
                serviceWriter.write(serviceHeader + "\n");
                // write out service definition
                serviceWriter.write("service " + currServiceName + " { \n");
                // write out rpcs for this service
                for (int i = 0; i < rpcList.size(); i++) {
                    serviceWriter.write(rpcList.get(i));
                }
                // end service definition
                serviceWriter.write("}\n");
                for (int i = 0; i < rpcInputMessageList.size(); i++) {
                    serviceWriter.write(rpcInputMessageList.get(i));
                }
            }
        } catch (final IOException e) {
            LOGGER.error("Exception encountered writing proto file", e);
        } finally {
            safeClose(serviceWriter);
        }
    }
    final String serviceReturnFilename = outputBasePath + "/src/main/protobuf/GeoWaveReturnTypesProtos.proto";
    Writer serviceReturnWriter = null;
    try {
        serviceReturnWriter = new OutputStreamWriter(new FileOutputStream(serviceReturnFilename), "UTF-8");
    } catch (final IOException e) {
        LOGGER.error("Exception encountered opening file stream", e);
    }
    try {
        // files
        if (serviceReturnWriter != null) {
            serviceReturnWriter.write(header + protobufPackage);
            it = retMessages.entrySet().iterator();
            while (it.hasNext()) {
                final HashMap.Entry pair = (HashMap.Entry) it.next();
                serviceReturnWriter.write((String) pair.getValue());
            }
        }
    } catch (final IOException e) {
        LOGGER.error("Exception encountered writing proto file", e);
    } finally {
        safeClose(serviceReturnWriter);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceEnabledCommand(org.locationtech.geowave.core.cli.api.ServiceEnabledCommand) ParameterizedType(java.lang.reflect.ParameterizedType) Iterator(java.util.Iterator) Reflections(org.reflections.Reflections) IOException(java.io.IOException) IOException(java.io.IOException) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

ServiceEnabledCommand (org.locationtech.geowave.core.cli.api.ServiceEnabledCommand)5 Test (org.junit.Test)3 Response (org.restlet.Response)3 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Ignore (org.junit.Ignore)1 Reflections (org.reflections.Reflections)1 Request (org.restlet.Request)1