Search in sources :

Example 21 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.

the class TestFrameworkTestRun method testMetadataAccessInService.

@Category(SlowTests.class)
@Test
public void testMetadataAccessInService() throws Exception {
    ApplicationManager applicationManager = deployApplication(AppWithMetadataPrograms.class);
    LOG.info("Deployed.");
    ServiceManager serviceManager = applicationManager.getServiceManager(AppWithMetadataPrograms.METADATA_SERVICE_NAME).start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    LOG.info("Service Started");
    URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
    Assert.assertNotNull(serviceURL);
    // add some tags
    callServicePut(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/tags", "tag1");
    callServicePut(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/tags", "tag2");
    // add some properties
    Map<String, String> propertiesToAdd = ImmutableMap.of("k1", "v1", "k2", "v2");
    callServicePut(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/properties", GSON.toJson(propertiesToAdd));
    // test service is able to read metadata
    String result = callServiceGet(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET);
    Map<MetadataScope, Metadata> scopeMetadata = GSON.fromJson(result, MAP_METADATASCOPE_METADATA_TYPE);
    // verify system metadata
    Assert.assertTrue(scopeMetadata.containsKey(MetadataScope.SYSTEM));
    Assert.assertTrue(scopeMetadata.containsKey(MetadataScope.USER));
    Assert.assertFalse(scopeMetadata.get(MetadataScope.SYSTEM).getProperties().isEmpty());
    Assert.assertFalse(scopeMetadata.get(MetadataScope.SYSTEM).getTags().isEmpty());
    Assert.assertTrue(scopeMetadata.get(MetadataScope.SYSTEM).getProperties().containsKey("entity-name"));
    Assert.assertEquals(AppWithMetadataPrograms.METADATA_SERVICE_DATASET, scopeMetadata.get(MetadataScope.SYSTEM).getProperties().get("entity-name"));
    Assert.assertTrue(scopeMetadata.get(MetadataScope.SYSTEM).getTags().containsAll(Arrays.asList("explore", "batch")));
    // verify user metadata
    Assert.assertFalse(scopeMetadata.get(MetadataScope.USER).getProperties().isEmpty());
    Assert.assertFalse(scopeMetadata.get(MetadataScope.USER).getTags().isEmpty());
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getTags().containsAll(Arrays.asList("tag1", "tag2")));
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getProperties().containsKey("k1"));
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getProperties().containsKey("k2"));
    Assert.assertEquals("v1", scopeMetadata.get(MetadataScope.USER).getProperties().get("k1"));
    Assert.assertEquals("v2", scopeMetadata.get(MetadataScope.USER).getProperties().get("k2"));
    // delete a tag
    callServiceDelete(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/tags/" + "tag1");
    // delete a property
    callServiceDelete(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/properties/" + "k1");
    // get metadata and verify
    result = callServiceGet(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET);
    scopeMetadata = GSON.fromJson(result, MAP_METADATASCOPE_METADATA_TYPE);
    Assert.assertEquals(1, scopeMetadata.get(MetadataScope.USER).getTags().size());
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getTags().contains("tag2"));
    Assert.assertEquals(1, scopeMetadata.get(MetadataScope.USER).getProperties().size());
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getProperties().containsKey("k2"));
    Assert.assertEquals("v2", scopeMetadata.get(MetadataScope.USER).getProperties().get("k2"));
    // delete all tags
    callServiceDelete(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/tags");
    // get metadata and verify
    result = callServiceGet(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET);
    scopeMetadata = GSON.fromJson(result, MAP_METADATASCOPE_METADATA_TYPE);
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getTags().isEmpty());
    Assert.assertFalse(scopeMetadata.get(MetadataScope.USER).getProperties().isEmpty());
    // delete all properties
    callServiceDelete(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/properties");
    // get metadata and verify
    result = callServiceGet(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET);
    scopeMetadata = GSON.fromJson(result, MAP_METADATASCOPE_METADATA_TYPE);
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getProperties().isEmpty());
    // add some tag and property again
    callServicePut(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/tags", "tag1");
    callServicePut(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET + "/properties", GSON.toJson(propertiesToAdd));
    // get metadata and verify
    result = callServiceGet(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET);
    scopeMetadata = GSON.fromJson(result, MAP_METADATASCOPE_METADATA_TYPE);
    Assert.assertFalse(scopeMetadata.get(MetadataScope.USER).getTags().isEmpty());
    Assert.assertFalse(scopeMetadata.get(MetadataScope.USER).getProperties().isEmpty());
    // delete all metadata
    callServiceDelete(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET);
    // get metadata and verify
    result = callServiceGet(serviceManager.getServiceURL(), "metadata/" + AppWithMetadataPrograms.METADATA_SERVICE_DATASET);
    scopeMetadata = GSON.fromJson(result, MAP_METADATASCOPE_METADATA_TYPE);
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getTags().isEmpty());
    Assert.assertTrue(scopeMetadata.get(MetadataScope.USER).getProperties().isEmpty());
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) ServiceManager(io.cdap.cdap.test.ServiceManager) Metadata(io.cdap.cdap.api.metadata.Metadata) URL(java.net.URL) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 22 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.

the class TestFrameworkTestRun method testCustomActionDatasetAccess.

@Category(SlowTests.class)
@Test
public void testCustomActionDatasetAccess() throws Exception {
    addDatasetInstance("keyValueTable", DatasetWithCustomActionApp.CUSTOM_TABLE);
    addDatasetInstance("fileSet", DatasetWithCustomActionApp.CUSTOM_FILESET);
    ApplicationManager appManager = deployApplication(DatasetWithCustomActionApp.class);
    ServiceManager serviceManager = appManager.getServiceManager(DatasetWithCustomActionApp.CUSTOM_SERVICE).start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    WorkflowManager workflowManager = appManager.getWorkflowManager(DatasetWithCustomActionApp.CUSTOM_WORKFLOW).start();
    workflowManager.waitForRun(ProgramRunStatus.COMPLETED, 2, TimeUnit.MINUTES);
    appManager.stopAll();
    DataSetManager<KeyValueTable> outTableManager = getDataset(DatasetWithCustomActionApp.CUSTOM_TABLE);
    KeyValueTable outputTable = outTableManager.get();
    Assert.assertEquals("world", Bytes.toString(outputTable.read("hello")));
    Assert.assertEquals("service", Bytes.toString(outputTable.read("hi")));
    Assert.assertEquals("another.world", Bytes.toString(outputTable.read("another.hello")));
    DataSetManager<FileSet> outFileSetManager = getDataset(DatasetWithCustomActionApp.CUSTOM_FILESET);
    FileSet fs = outFileSetManager.get();
    try (InputStream in = fs.getLocation("test").getInputStream()) {
        Assert.assertEquals(42, in.read());
    }
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) FileSet(io.cdap.cdap.api.dataset.lib.FileSet) ServiceManager(io.cdap.cdap.test.ServiceManager) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) InputStream(java.io.InputStream) WorkflowManager(io.cdap.cdap.test.WorkflowManager) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 23 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.

the class TestFrameworkTestRun method testAppVersionsCreation.

@Test
public void testAppVersionsCreation() throws Exception {
    ArtifactId artifactId = new ArtifactId(NamespaceId.DEFAULT.getNamespace(), "cfg-app", "1.0.0-SNAPSHOT");
    addAppArtifact(artifactId, ConfigTestApp.class);
    ApplicationId appId = new ApplicationId(NamespaceId.DEFAULT.getNamespace(), "AppV1", "version1");
    AppRequest<ConfigTestApp.ConfigClass> createRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), new ConfigTestApp.ConfigClass("tD1", "tV1"));
    ApplicationManager appManager = deployApplication(appId, createRequest);
    ServiceManager serviceManager = appManager.getServiceManager(ConfigTestApp.SERVICE_NAME);
    serviceManager.start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    URL serviceURL = serviceManager.getServiceURL();
    Gson gson = new Gson();
    Assert.assertEquals("tV1", gson.fromJson(callServiceGet(serviceURL, "ping"), String.class));
    serviceManager.stop();
    serviceManager.waitForStopped(10, TimeUnit.SECONDS);
    appId = new ApplicationId(NamespaceId.DEFAULT.getNamespace(), "AppV1", "version2");
    createRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), new ConfigTestApp.ConfigClass("tD2", "tV2"));
    appManager = deployApplication(appId, createRequest);
    serviceManager = appManager.getServiceManager(ConfigTestApp.SERVICE_NAME);
    serviceManager.start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    serviceURL = serviceManager.getServiceURL();
    Assert.assertEquals("tV2", gson.fromJson(callServiceGet(serviceURL, "ping"), String.class));
    serviceManager.stop();
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) Gson(com.google.gson.Gson) URL(java.net.URL) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ServiceManager(io.cdap.cdap.test.ServiceManager) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ConfigTestApp(io.cdap.cdap.ConfigTestApp) Test(org.junit.Test)

Example 24 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.

the class TestFrameworkTestRun method testAppWithDataset.

private void testAppWithDataset(Class<? extends Application> app, String serviceName) throws Exception {
    ApplicationManager applicationManager = deployApplication(app);
    // Query the result
    ServiceManager serviceManager = applicationManager.getServiceManager(serviceName).start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    callServicePut(serviceManager.getServiceURL(), "key1", "value1");
    String response = callServiceGet(serviceManager.getServiceURL(), "key1");
    Assert.assertEquals("value1", new Gson().fromJson(response, String.class));
    serviceManager.stop();
    serviceManager.waitForRun(ProgramRunStatus.KILLED, 10, TimeUnit.SECONDS);
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) ServiceManager(io.cdap.cdap.test.ServiceManager) Gson(com.google.gson.Gson)

Example 25 with ServiceManager

use of io.cdap.cdap.test.ServiceManager in project cdap by cdapio.

the class TestFrameworkTestRun method testGetServiceURL.

@Category(SlowTests.class)
@Test
public void testGetServiceURL() throws Exception {
    ApplicationManager applicationManager = deployApplication(AppUsingGetServiceURL.class);
    ServiceManager centralServiceManager = applicationManager.getServiceManager(AppUsingGetServiceURL.CENTRAL_SERVICE).start();
    centralServiceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    WorkerManager pingingWorker = applicationManager.getWorkerManager(AppUsingGetServiceURL.PINGING_WORKER).start();
    // Test service's getServiceURL
    ServiceManager serviceManager = applicationManager.getServiceManager(AppUsingGetServiceURL.FORWARDING).start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    String result = callServiceGet(serviceManager.getServiceURL(), "ping");
    String decodedResult = new Gson().fromJson(result, String.class);
    // Verify that the service was able to hit the CentralService and retrieve the answer.
    Assert.assertEquals(AppUsingGetServiceURL.ANSWER, decodedResult);
    // Wait for the worker completed to make sure a value has been written to the dataset
    pingingWorker.waitForRun(ProgramRunStatus.COMPLETED, 30, TimeUnit.SECONDS);
    // Validate the value in the dataset by reading it via the service
    result = callServiceGet(serviceManager.getServiceURL(), "read/" + AppUsingGetServiceURL.DATASET_KEY);
    decodedResult = new Gson().fromJson(result, String.class);
    Assert.assertEquals(AppUsingGetServiceURL.ANSWER, decodedResult);
    serviceManager.stop();
    centralServiceManager.stop();
    serviceManager.waitForStopped(10, TimeUnit.SECONDS);
    centralServiceManager.waitForStopped(10, TimeUnit.SECONDS);
}
Also used : WorkerManager(io.cdap.cdap.test.WorkerManager) ApplicationManager(io.cdap.cdap.test.ApplicationManager) ServiceManager(io.cdap.cdap.test.ServiceManager) Gson(com.google.gson.Gson) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

ServiceManager (io.cdap.cdap.test.ServiceManager)52 ApplicationManager (io.cdap.cdap.test.ApplicationManager)48 Test (org.junit.Test)44 URL (java.net.URL)28 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)22 Gson (com.google.gson.Gson)16 Category (org.junit.experimental.categories.Category)16 HttpResponse (io.cdap.common.http.HttpResponse)14 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)12 PartitionedFileSet (io.cdap.cdap.api.dataset.lib.PartitionedFileSet)10 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)10 WorkflowManager (io.cdap.cdap.test.WorkflowManager)10 HttpRequest (io.cdap.common.http.HttpRequest)10 IOException (java.io.IOException)10 FileSet (io.cdap.cdap.api.dataset.lib.FileSet)8 WorkerManager (io.cdap.cdap.test.WorkerManager)8 Set (java.util.Set)8 Table (io.cdap.cdap.api.dataset.table.Table)7 ImmutableSet (com.google.common.collect.ImmutableSet)6 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)6