Search in sources :

Example 16 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by cdapio.

the class QueryClientTest method testAll.

@Test
public void testAll() throws Exception {
    NamespaceId namespace = new NamespaceId("queryClientTestNamespace");
    NamespaceId otherNamespace = new NamespaceId("queryClientOtherNamespace");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
    ApplicationId app = namespace.app(FakeApp.NAME);
    ServiceId service = app.service(DatasetWriterService.NAME);
    DatasetId dataset = namespace.dataset(FakeApp.DS_NAME);
    appClient.deploy(namespace, createAppJarFile(FakeApp.class));
    try {
        programClient.start(service);
        assertProgramRunning(programClient, service);
        // Write some data through the service
        Map<String, String> data = new HashMap<>();
        data.put("bob", "123");
        data.put("joe", "321");
        URL writeURL = new URL(serviceClient.getServiceURL(service), "write");
        HttpResponse response = HttpRequests.execute(HttpRequest.post(writeURL).withBody(new Gson().toJson(data)).build(), new DefaultHttpRequestConfig(false));
        Assert.assertEquals(200, response.getResponseCode());
        executeBasicQuery(namespace, FakeApp.DS_NAME);
        exploreClient.disableExploreDataset(dataset).get();
        try {
            queryClient.execute(namespace, "select * from " + FakeApp.DS_NAME).get();
            Assert.fail("Explore Query should have thrown an ExecutionException since explore is disabled");
        } catch (ExecutionException e) {
        // ignored
        }
        exploreClient.enableExploreDataset(dataset).get();
        executeBasicQuery(namespace, FakeApp.DS_NAME);
        try {
            queryClient.execute(otherNamespace, "show tables").get();
            Assert.fail("Explore Query should have thrown an ExecutionException since the database should not exist");
        } catch (ExecutionException e) {
        // expected
        }
    } finally {
        programClient.stop(service);
        assertProgramStopped(programClient, service);
        try {
            appClient.delete(app);
        } catch (Exception e) {
            LOG.error("Error deleting app {} during test cleanup.", e);
        }
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) HashMap(java.util.HashMap) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) Gson(com.google.gson.Gson) URL(java.net.URL) ExecutionException(java.util.concurrent.ExecutionException) ServiceId(io.cdap.cdap.proto.id.ServiceId) DatasetId(io.cdap.cdap.proto.id.DatasetId) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 17 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.

the class HttpEndpointPrefixCompleter method complete.

@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
    Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN);
    ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(SERVICE_ID));
    if (programIdArgument != null) {
        ServiceId service;
        if (arguments.get(APP_VERSION) == null) {
            service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId()).service(programIdArgument.getProgramId());
        } else {
            service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId(), arguments.get(APP_VERSION)).service(programIdArgument.getProgramId());
        }
        completer.setEndpoints(getEndpoints(service, arguments.get(METHOD)));
    } else {
        completer.setEndpoints(Collections.<String>emptyList());
    }
    return super.complete(buffer, cursor, candidates);
}
Also used : ProgramIdArgument(io.cdap.cdap.cli.ProgramIdArgument) ServiceId(io.cdap.cdap.proto.id.ServiceId)

Example 18 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.

the class HttpMethodPrefixCompleter method complete.

@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
    Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN);
    ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(SERVICE_ID));
    if (programIdArgument != null) {
        ServiceId service;
        if (arguments.get(APP_VERSION) == null) {
            service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId()).service(programIdArgument.getProgramId());
        } else {
            service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId(), arguments.get(APP_VERSION)).service(programIdArgument.getProgramId());
        }
        completer.setEndpoints(getMethods(service));
    } else {
        completer.setEndpoints(Collections.<String>emptyList());
    }
    return super.complete(buffer, cursor, candidates);
}
Also used : ProgramIdArgument(io.cdap.cdap.cli.ProgramIdArgument) ServiceId(io.cdap.cdap.proto.id.ServiceId)

Example 19 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.

the class PreferencesClientTestRun method testProgramAPI.

@Test
public void testProgramAPI() throws Exception {
    Map<String, String> propMap = Maps.newHashMap();
    propMap.put("key", "instance");
    File jarFile = createAppJarFile(AppReturnsArgs.class);
    appClient.deploy(NamespaceId.DEFAULT, jarFile);
    ApplicationId app = NamespaceId.DEFAULT.app(AppReturnsArgs.NAME);
    ServiceId service = app.service(AppReturnsArgs.SERVICE);
    try {
        client.setInstancePreferences(propMap);
        Map<String, String> setMap = Maps.newHashMap();
        setMap.put("saved", "args");
        programClient.setRuntimeArgs(service, setMap);
        assertEquals(setMap, programClient.getRuntimeArgs(service));
        propMap.put("run", "value");
        propMap.put("logical.start.time", "1234567890000");
        propMap.putAll(setMap);
        programClient.start(service, false, propMap);
        assertProgramRunning(programClient, service);
        URL serviceURL = new URL(serviceClient.getServiceURL(service), AppReturnsArgs.ENDPOINT);
        HttpResponse response = getServiceResponse(serviceURL);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        Map<String, String> responseMap = GSON.fromJson(response.getResponseBodyAsString(), STRING_MAP_TYPE);
        assertEquals(propMap, responseMap);
        programClient.stop(service);
        assertProgramStopped(programClient, service);
        assertProgramRuns(programClient, service, ProgramRunStatus.KILLED, 1, 10);
        long minStartTime = System.currentTimeMillis();
        client.deleteInstancePreferences();
        programClient.start(service);
        assertProgramRunning(programClient, service);
        propMap.remove("key");
        propMap.remove("run");
        propMap.remove("logical.start.time");
        serviceURL = new URL(serviceClient.getServiceURL(service), AppReturnsArgs.ENDPOINT);
        response = getServiceResponse(serviceURL);
        responseMap = GSON.fromJson(response.getResponseBodyAsString(), STRING_MAP_TYPE);
        long actualStartTime = Long.parseLong(responseMap.remove("logical.start.time"));
        Assert.assertTrue(actualStartTime >= minStartTime);
        assertEquals(propMap, responseMap);
        programClient.stop(service);
        assertProgramStopped(programClient, service);
        assertProgramRuns(programClient, service, ProgramRunStatus.KILLED, 2, 10);
        propMap.clear();
        minStartTime = System.currentTimeMillis();
        programClient.setRuntimeArgs(service, propMap);
        programClient.start(service);
        assertProgramRunning(programClient, service);
        serviceURL = new URL(serviceClient.getServiceURL(service), AppReturnsArgs.ENDPOINT);
        response = getServiceResponse(serviceURL);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        responseMap = GSON.fromJson(response.getResponseBodyAsString(), STRING_MAP_TYPE);
        actualStartTime = Long.parseLong(responseMap.remove("logical.start.time"));
        Assert.assertTrue(actualStartTime >= minStartTime);
        assertEquals(propMap, responseMap);
    } finally {
        programClient.stop(service);
        assertProgramStopped(programClient, service);
        appClient.delete(app);
    }
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) File(java.io.File) URL(java.net.URL) ServiceId(io.cdap.cdap.proto.id.ServiceId) Test(org.junit.Test)

Example 20 with ServiceId

use of io.cdap.cdap.proto.id.ServiceId in project cdap by caskdata.

the class QueryClientTest method testAll.

@Test
public void testAll() throws Exception {
    NamespaceId namespace = new NamespaceId("queryClientTestNamespace");
    NamespaceId otherNamespace = new NamespaceId("queryClientOtherNamespace");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
    ApplicationId app = namespace.app(FakeApp.NAME);
    ServiceId service = app.service(DatasetWriterService.NAME);
    DatasetId dataset = namespace.dataset(FakeApp.DS_NAME);
    appClient.deploy(namespace, createAppJarFile(FakeApp.class));
    try {
        programClient.start(service);
        assertProgramRunning(programClient, service);
        // Write some data through the service
        Map<String, String> data = new HashMap<>();
        data.put("bob", "123");
        data.put("joe", "321");
        URL writeURL = new URL(serviceClient.getServiceURL(service), "write");
        HttpResponse response = HttpRequests.execute(HttpRequest.post(writeURL).withBody(new Gson().toJson(data)).build(), new DefaultHttpRequestConfig(false));
        Assert.assertEquals(200, response.getResponseCode());
        executeBasicQuery(namespace, FakeApp.DS_NAME);
        exploreClient.disableExploreDataset(dataset).get();
        try {
            queryClient.execute(namespace, "select * from " + FakeApp.DS_NAME).get();
            Assert.fail("Explore Query should have thrown an ExecutionException since explore is disabled");
        } catch (ExecutionException e) {
        // ignored
        }
        exploreClient.enableExploreDataset(dataset).get();
        executeBasicQuery(namespace, FakeApp.DS_NAME);
        try {
            queryClient.execute(otherNamespace, "show tables").get();
            Assert.fail("Explore Query should have thrown an ExecutionException since the database should not exist");
        } catch (ExecutionException e) {
        // expected
        }
    } finally {
        programClient.stop(service);
        assertProgramStopped(programClient, service);
        try {
            appClient.delete(app);
        } catch (Exception e) {
            LOG.error("Error deleting app {} during test cleanup.", e);
        }
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) HashMap(java.util.HashMap) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) Gson(com.google.gson.Gson) URL(java.net.URL) ExecutionException(java.util.concurrent.ExecutionException) ServiceId(io.cdap.cdap.proto.id.ServiceId) DatasetId(io.cdap.cdap.proto.id.DatasetId) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ServiceId (io.cdap.cdap.proto.id.ServiceId)32 Test (org.junit.Test)20 HttpResponse (io.cdap.common.http.HttpResponse)14 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)10 URL (java.net.URL)8 Gson (com.google.gson.Gson)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 JsonObject (com.google.gson.JsonObject)4 ProgramIdArgument (io.cdap.cdap.cli.ProgramIdArgument)4 CommandInputError (io.cdap.cdap.cli.exception.CommandInputError)4 FakeApp (io.cdap.cdap.client.app.FakeApp)4 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)4 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)4 ProgramId (io.cdap.cdap.proto.id.ProgramId)4 File (java.io.File)4 TypeToken (com.google.common.reflect.TypeToken)2 ServiceHttpEndpoint (io.cdap.cdap.api.service.http.ServiceHttpEndpoint)2