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);
}
}
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);
}
}
}
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());
}
}
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());
}
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));
}
Aggregations