use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class TestAppWithCube method searchMeasure.
private Collection<String> searchMeasure(URL serviceUrl, CubeExploreQuery query) throws IOException {
URL url = new URL(serviceUrl, "searchMeasure");
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(query)).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
return GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Collection<String>>() {
}.getType());
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class TestAppWithCube method searchDimensionValue.
private Collection<DimensionValue> searchDimensionValue(URL serviceUrl, CubeExploreQuery query) throws IOException {
URL url = new URL(serviceUrl, "searchDimensionValue");
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(query)).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
return GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Collection<DimensionValue>>() {
}.getType());
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class TestFrameworkTestRun method testAppWithServices.
@Category(SlowTests.class)
@Test
public void testAppWithServices() throws Exception {
ApplicationManager applicationManager = deployApplication(AppWithServices.class);
LOG.info("Deployed.");
ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.SERVICE_NAME).start();
serviceManager.waitForStatus(true);
LOG.info("Service Started");
URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(serviceURL);
// Call the ping endpoint
URL url = new URL(serviceURL, "ping2");
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
// Call the failure endpoint
url = new URL(serviceURL, "failure");
request = HttpRequest.get(url).build();
response = HttpRequests.execute(request);
Assert.assertEquals(500, response.getResponseCode());
Assert.assertTrue(response.getResponseBodyAsString().contains("Exception"));
// Call the verify ClassLoader endpoint
url = new URL(serviceURL, "verifyClassLoader");
request = HttpRequest.get(url).build();
response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
RuntimeMetrics serviceMetrics = serviceManager.getMetrics();
serviceMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
Assert.assertEquals(3, serviceMetrics.getInput());
Assert.assertEquals(2, serviceMetrics.getProcessed());
Assert.assertEquals(1, serviceMetrics.getException());
// in the AppWithServices the handlerName is same as the serviceName - "ServerService" handler
RuntimeMetrics handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, AppWithServices.SERVICE_NAME, AppWithServices.SERVICE_NAME);
handlerMetrics.waitForinput(3, 5, TimeUnit.SECONDS);
Assert.assertEquals(3, handlerMetrics.getInput());
Assert.assertEquals(2, handlerMetrics.getProcessed());
Assert.assertEquals(1, handlerMetrics.getException());
// we can verify metrics, by adding getServiceMetrics in MetricsManager and then disabling the system scope test in
// TestMetricsCollectionService
LOG.info("DatasetUpdateService Started");
Map<String, String> args = ImmutableMap.of(AppWithServices.WRITE_VALUE_RUN_KEY, AppWithServices.DATASET_TEST_VALUE, AppWithServices.WRITE_VALUE_STOP_KEY, AppWithServices.DATASET_TEST_VALUE_STOP);
ServiceManager datasetWorkerServiceManager = applicationManager.getServiceManager(AppWithServices.DATASET_WORKER_SERVICE_NAME).start(args);
WorkerManager datasetWorker = applicationManager.getWorkerManager(AppWithServices.DATASET_UPDATE_WORKER).start(args);
datasetWorkerServiceManager.waitForStatus(true);
ServiceManager noopManager = applicationManager.getServiceManager("NoOpService").start();
serviceManager.waitForStatus(true, 2, 1);
String result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY);
String decodedResult = new Gson().fromJson(result, String.class);
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE, decodedResult);
handlerMetrics = getMetricsManager().getServiceHandlerMetrics(NamespaceId.DEFAULT.getNamespace(), AppWithServices.APP_NAME, "NoOpService", "NoOpHandler");
handlerMetrics.waitForinput(1, 5, TimeUnit.SECONDS);
Assert.assertEquals(1, handlerMetrics.getInput());
Assert.assertEquals(1, handlerMetrics.getProcessed());
Assert.assertEquals(0, handlerMetrics.getException());
// Test that a service can discover another service
String path = String.format("discover/%s/%s", AppWithServices.APP_NAME, AppWithServices.DATASET_WORKER_SERVICE_NAME);
url = new URL(serviceURL, path);
request = HttpRequest.get(url).build();
response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
datasetWorker.stop();
datasetWorkerServiceManager.stop();
datasetWorkerServiceManager.waitForStatus(false);
LOG.info("DatasetUpdateService Stopped");
serviceManager.stop();
serviceManager.waitForStatus(false);
LOG.info("ServerService Stopped");
result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP);
decodedResult = new Gson().fromJson(result, String.class);
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP, decodedResult);
result = callServiceGet(noopManager.getServiceURL(), "ping/" + AppWithServices.DATASET_TEST_KEY_STOP_2);
decodedResult = new Gson().fromJson(result, String.class);
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE_STOP_2, decodedResult);
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class ProgramClient method getStatus.
/**
* Gets the status of multiple programs.
*
* @param namespace the namespace of the programs
* @param programs the list of programs to get status for
* @return the status of each program
*/
public List<BatchProgramStatus> getStatus(NamespaceId namespace, List<BatchProgram> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(namespace, "status");
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(programs)).build();
HttpResponse response = restClient.execute(request, config.getAccessToken());
return ObjectResponse.<List<BatchProgramStatus>>fromJsonBody(response, BATCH_STATUS_RESPONSE_TYPE, GSON).getResponseObject();
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class ProgramClient method setFlowletInstances.
/**
* Sets the number of instances that a flowlet will run on.
*
* @param flowlet the flowlet
* @param instances number of instances for the flowlet to run on
* @throws IOException if a network error occurred
* @throws NotFoundException if the application, flow, or flowlet could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void setFlowletInstances(FlowletId flowlet, int instances) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(flowlet.getParent().getNamespaceId(), String.format("apps/%s/flows/%s/flowlets/%s/instances", flowlet.getApplication(), flowlet.getFlow(), flowlet.getFlowlet()));
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(flowlet);
}
}
Aggregations