Search in sources :

Example 76 with NamespaceId

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

the class MetadataHttpHandlerTestRun method testCrossNamespaceSearchMetadata.

@Test
public void testCrossNamespaceSearchMetadata() throws Exception {
    NamespaceId namespace1 = new NamespaceId("ns1");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace1).build());
    NamespaceId namespace2 = new NamespaceId("ns2");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace2).build());
    try {
        appClient.deploy(namespace1, createAppJarFile(AllProgramsApp.class));
        appClient.deploy(namespace2, createAppJarFile(AllProgramsApp.class));
        // Add metadata to app
        Map<String, String> props = ImmutableMap.of("key1", "value1");
        Metadata meta = new Metadata(props, Collections.emptySet());
        ApplicationId app1Id = namespace1.app(AllProgramsApp.NAME);
        addProperties(app1Id, props);
        ApplicationId app2Id = namespace2.app(AllProgramsApp.NAME);
        addProperties(app2Id, props);
        MetadataSearchResponse results = super.searchMetadata(ImmutableList.of(), "value*", Collections.emptySet(), null, 0, 10, 0, null, false);
        Map<MetadataEntity, Metadata> expected = new HashMap<>();
        expected.put(app1Id.toMetadataEntity(), meta);
        expected.put(app2Id.toMetadataEntity(), meta);
        Map<MetadataEntity, Metadata> actual = new HashMap<>();
        for (MetadataSearchResultRecord record : results.getResults()) {
            actual.put(record.getMetadataEntity(), record.getMetadata().get(MetadataScope.USER));
        }
        Assert.assertEquals(expected, actual);
    } finally {
        namespaceClient.delete(namespace1);
        namespaceClient.delete(namespace2);
    }
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) HashMap(java.util.HashMap) Metadata(io.cdap.cdap.api.metadata.Metadata) MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) AllProgramsApp(io.cdap.cdap.client.app.AllProgramsApp) MetadataSearchResultRecord(io.cdap.cdap.proto.metadata.MetadataSearchResultRecord) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 77 with NamespaceId

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

the class ProgramClientTestRun method testBatchProgramCalls.

@Test
public void testBatchProgramCalls() throws Exception {
    final NamespaceId namespace = NamespaceId.DEFAULT;
    final ApplicationId appId = namespace.app(FakeApp.NAME);
    BatchProgram pingService = new BatchProgram(FakeApp.NAME, ProgramType.SERVICE, PingService.NAME);
    BatchProgram writerService = new BatchProgram(FakeApp.NAME, ProgramType.SERVICE, DatasetWriterService.NAME);
    BatchProgram missing = new BatchProgram(FakeApp.NAME, ProgramType.SERVICE, "not" + PingService.NAME);
    appClient.deploy(namespace, createAppJarFile(FakeApp.class));
    try {
        // make a batch call to start multiple programs, one of which does not exist
        List<BatchProgramStart> programStarts = ImmutableList.of(new BatchProgramStart(pingService), new BatchProgramStart(writerService), new BatchProgramStart(missing));
        List<BatchProgramResult> results = programClient.start(namespace, programStarts);
        // check that we got a 200 for programs that exist, and a 404 for the one that doesn't
        for (BatchProgramResult result : results) {
            if (missing.getProgramId().equals(result.getProgramId())) {
                Assert.assertEquals(404, result.getStatusCode());
            } else {
                Assert.assertEquals(200, result.getStatusCode());
            }
        }
        // wait for all programs to be in RUNNING status
        assertProgramRuns(programClient, namespace.app(pingService.getAppId()).service(pingService.getProgramId()), ProgramRunStatus.RUNNING, 1, 10);
        assertProgramRuns(programClient, namespace.app(writerService.getAppId()).service(writerService.getProgramId()), ProgramRunStatus.RUNNING, 1, 10);
        // make a batch call for status of programs, one of which does not exist
        List<BatchProgram> programs = ImmutableList.of(pingService, writerService, missing);
        List<BatchProgramStatus> statusList = programClient.getStatus(namespace, programs);
        // check status is running for programs that exist, and that we get a 404 for the one that doesn't
        for (BatchProgramStatus status : statusList) {
            if (missing.getProgramId().equals(status.getProgramId())) {
                Assert.assertEquals(404, status.getStatusCode());
            } else {
                Assert.assertEquals(200, status.getStatusCode());
                Assert.assertEquals("RUNNING", status.getStatus());
            }
        }
        // make a batch call to stop programs, one of which does not exist
        results = programClient.stop(namespace, programs);
        // check that we got a 200 for programs that exist, and a 404 for the one that doesn't
        for (BatchProgramResult result : results) {
            if (missing.getProgramId().equals(result.getProgramId())) {
                Assert.assertEquals(404, result.getStatusCode());
            } else {
                Assert.assertEquals(200, result.getStatusCode());
            }
        }
        // check programs are in stopped state
        final List<BatchProgram> stoppedPrograms = ImmutableList.of(pingService, writerService);
        Tasks.waitFor(true, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                List<BatchProgramStatus> statusList = programClient.getStatus(namespace, stoppedPrograms);
                for (BatchProgramStatus status : statusList) {
                    if (status.getStatusCode() != 200) {
                        return false;
                    }
                    if (status.getStatus().equals("RUNNING")) {
                        return false;
                    }
                }
                return true;
            }
        }, 10, TimeUnit.SECONDS);
    } finally {
        try {
            appClient.delete(appId);
        } catch (Exception e) {
            LOG.error("Error deleting app {} during test cleanup.", appId, e);
        }
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) BatchProgramStart(io.cdap.cdap.proto.BatchProgramStart) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) BatchProgram(io.cdap.cdap.proto.BatchProgram) Test(org.junit.Test)

Example 78 with NamespaceId

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

the class ApplicationClientTestRun method testDeleteAll.

@Test
public void testDeleteAll() throws Exception {
    NamespaceId namespace = NamespaceId.DEFAULT;
    ApplicationId app = namespace.app(FakeApp.NAME);
    ApplicationId app2 = namespace.app(AppReturnsArgs.NAME);
    Assert.assertEquals(0, appClient.list(namespace).size());
    try {
        // deploy first app
        LOG.info("Deploying first app");
        appClient.deploy(namespace, createAppJarFile(FakeApp.class));
        appClient.waitForDeployed(app, 30, TimeUnit.SECONDS);
        Assert.assertEquals(1, appClient.list(namespace).size());
        // deploy second app
        LOG.info("Deploying second app");
        appClient.deploy(namespace, createAppJarFile(AppReturnsArgs.class));
        appClient.waitForDeployed(app2, 30, TimeUnit.SECONDS);
        Assert.assertEquals(2, appClient.list(namespace).size());
    } finally {
        appClient.deleteAll(namespace);
        appClient.waitForDeleted(app, 30, TimeUnit.SECONDS);
        appClient.waitForDeleted(app2, 30, TimeUnit.SECONDS);
        Assert.assertEquals(0, appClient.list(namespace).size());
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) AppReturnsArgs(io.cdap.cdap.client.app.AppReturnsArgs) Test(org.junit.Test)

Example 79 with NamespaceId

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

the class MetadataDatasetTest method testCrossNamespaceSearchPagination.

@Test
public void testCrossNamespaceSearchPagination() throws Exception {
    ApplicationId ns1app1 = new NamespaceId("ns1").app("a1");
    ApplicationId ns1app2 = new NamespaceId("ns1").app("a2");
    ApplicationId ns1app3 = new NamespaceId("ns1").app("a3");
    ApplicationId ns2app1 = new NamespaceId("ns2").app("a1");
    ApplicationId ns2app2 = new NamespaceId("ns2").app("a2");
    String key = MetadataConstants.ENTITY_NAME_KEY;
    txnl.execute(() -> {
        dataset.addProperty(ns1app1.toMetadataEntity(), key, ns1app1.getApplication());
        dataset.addProperty(ns1app2.toMetadataEntity(), key, ns1app2.getApplication());
        dataset.addProperty(ns1app3.toMetadataEntity(), key, ns1app3.getApplication());
        dataset.addProperty(ns2app1.toMetadataEntity(), key, ns2app1.getApplication());
        dataset.addProperty(ns2app2.toMetadataEntity(), key, ns2app2.getApplication());
    });
    MetadataEntry ns1app1Entry = new MetadataEntry(ns1app1.toMetadataEntity(), key, ns1app1.getApplication());
    MetadataEntry ns1app2Entry = new MetadataEntry(ns1app2.toMetadataEntity(), key, ns1app2.getApplication());
    MetadataEntry ns1app3Entry = new MetadataEntry(ns1app3.toMetadataEntity(), key, ns1app3.getApplication());
    MetadataEntry ns2app1Entry = new MetadataEntry(ns2app1.toMetadataEntity(), key, ns2app1.getApplication());
    MetadataEntry ns2app2Entry = new MetadataEntry(ns2app2.toMetadataEntity(), key, ns2app2.getApplication());
    SortInfo nameAsc = new SortInfo(MetadataConstants.ENTITY_NAME_KEY, SortInfo.SortOrder.ASC);
    // first, get the full ordered list in one page
    SearchRequest request1 = new SearchRequest(null, "*", ALL_TYPES, nameAsc, 0, 10, 1, null, false, EnumSet.allOf(EntityScope.class));
    List<MetadataEntry> actual = txnl.execute(() -> dataset.search(request1).getResults());
    List<MetadataEntry> expected = new ArrayList<>();
    // sorted by name asc
    expected.add(ns1app1Entry);
    expected.add(ns2app1Entry);
    expected.add(ns1app2Entry);
    expected.add(ns2app2Entry);
    expected.add(ns1app3Entry);
    Assert.assertEquals(expected, actual);
    // now search with 3 pages, 2 results per page
    SearchRequest request2 = new SearchRequest(null, "*", ALL_TYPES, nameAsc, 0, 2, 3, null, false, EnumSet.allOf(EntityScope.class));
    SearchResults results = txnl.execute(() -> dataset.search(request2));
    // dataset returns all pages, so results should be in same order
    Assert.assertEquals(expected, results.getResults());
    // check the cursors
    List<String> expectedCursors = new ArrayList<>();
    expectedCursors.add(ns1app2Entry.getValue());
    expectedCursors.add(ns1app3Entry.getValue());
    Assert.assertEquals(expectedCursors, results.getCursors());
    // now search for for 2nd and 3rd pages using the cursor
    SearchRequest request3 = new SearchRequest(null, "*", ALL_TYPES, nameAsc, 0, 2, 3, results.getCursors().get(0), false, EnumSet.allOf(EntityScope.class));
    results = txnl.execute(() -> dataset.search(request3));
    expected.clear();
    expected.add(ns1app2Entry);
    expected.add(ns2app2Entry);
    expected.add(ns1app3Entry);
    Assert.assertEquals(expected, results.getResults());
    Assert.assertEquals(Collections.singletonList(ns1app3Entry.getValue()), results.getCursors());
}
Also used : ArrayList(java.util.ArrayList) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) EntityScope(io.cdap.cdap.proto.EntityScope) Test(org.junit.Test)

Example 80 with NamespaceId

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

the class PreferencesHttpHandlerInternal method getNamespacePreferences.

/**
 * Get namespace level preferences.
 *
 * Note that if the given namespace doesn't exist, the return {@link PreferencesDetail} will be empty
 * (i.e. {@link PreferencesDetail#properties} will be an empty map). In the case of requesting resolved preferences,
 * the returned {@link PreferencesDetail} will include preferences from ancestor (i.e. preferences at instance level)
 *
 * @param request {@link HttpRequest}
 * @param responder the responder used for sending response back to client
 * @param namespace the namespace to get preferences for
 * @param resolved whether to return resolved preferences or not
 */
@Path("/namespaces/{namespace-id}/preferences")
@GET
public void getNamespacePreferences(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @QueryParam("resolved") boolean resolved) {
    NamespaceId namespaceId = new NamespaceId(namespace);
    // No need to check if namespace exists. PreferencesService returns an empty PreferencesDetail when that happens.
    PreferencesDetail detail;
    if (resolved) {
        detail = preferencesService.getResolvedPreferences(namespaceId);
    } else {
        detail = preferencesService.getPreferences(namespaceId);
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(detail, PreferencesDetail.class));
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

NamespaceId (io.cdap.cdap.proto.id.NamespaceId)648 Test (org.junit.Test)292 Path (javax.ws.rs.Path)136 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)124 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)108 IOException (java.io.IOException)102 ProgramId (io.cdap.cdap.proto.id.ProgramId)86 GET (javax.ws.rs.GET)74 DatasetId (io.cdap.cdap.proto.id.DatasetId)68 ArrayList (java.util.ArrayList)64 BadRequestException (io.cdap.cdap.common.BadRequestException)60 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)58 Principal (io.cdap.cdap.proto.security.Principal)56 Set (java.util.Set)52 Id (io.cdap.cdap.common.id.Id)50 File (java.io.File)50 HashSet (java.util.HashSet)50 NotFoundException (io.cdap.cdap.common.NotFoundException)48 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)46 HashMap (java.util.HashMap)46