use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class TestFrameworkTestRun method testTransactionHandlerService.
@Test
public void testTransactionHandlerService() throws Exception {
ApplicationManager applicationManager = deployApplication(testSpace, AppWithServices.class);
LOG.info("Deployed.");
ServiceManager serviceManager = applicationManager.getServiceManager(AppWithServices.TRANSACTIONS_SERVICE_NAME).start();
serviceManager.waitForStatus(true);
LOG.info("Service Started");
final URL baseUrl = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(baseUrl);
// Make a request to write in a separate thread and wait for it to return.
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Integer> requestFuture = executorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
try {
URL url = new URL(String.format("%s/write/%s/%s/%d", baseUrl, AppWithServices.DATASET_TEST_KEY, AppWithServices.DATASET_TEST_VALUE, 10000));
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = HttpRequests.execute(request);
return response.getResponseCode();
} catch (Exception e) {
LOG.error("Request thread got exception.", e);
throw Throwables.propagate(e);
}
}
});
// The dataset should not be written by the time this request is made, since the transaction to write
// has not been committed yet.
URL url = new URL(String.format("%s/read/%s", baseUrl, AppWithServices.DATASET_TEST_KEY));
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(204, response.getResponseCode());
// Wait for the transaction to commit.
Integer writeStatusCode = requestFuture.get();
Assert.assertEquals(200, writeStatusCode.intValue());
// Make the same request again. By now the transaction should've completed.
request = HttpRequest.get(url).build();
response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals(AppWithServices.DATASET_TEST_VALUE, new Gson().fromJson(response.getResponseBodyAsString(), String.class));
executorService.shutdown();
serviceManager.stop();
serviceManager.waitForStatus(false);
DataSetManager<KeyValueTable> dsManager = getDataset(testSpace.dataset(AppWithServices.TRANSACTIONS_DATASET_NAME));
String value = Bytes.toString(dsManager.get().read(AppWithServices.DESTROY_KEY));
Assert.assertEquals(AppWithServices.VALUE, value);
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class TestFrameworkTestRun method testByteCodeClassLoader.
@Category(XSlowTests.class)
@Test
public void testByteCodeClassLoader() throws Exception {
// This test verify bytecode generated classes ClassLoading
ApplicationManager appManager = deployApplication(testSpace, ClassLoaderTestApp.class);
FlowManager flowManager = appManager.getFlowManager("BasicFlow").start();
// Wait for at least 10 records being generated
RuntimeMetrics flowMetrics = flowManager.getFlowletMetrics("Sink");
flowMetrics.waitForProcessed(10, 5000, TimeUnit.MILLISECONDS);
flowManager.stop();
ServiceManager serviceManager = appManager.getServiceManager("RecordQuery").start();
URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
Assert.assertNotNull(serviceURL);
// Query record
URL url = new URL(serviceURL, "query?type=public");
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
long count = Long.parseLong(response.getResponseBodyAsString());
serviceManager.stop();
// Verify the record count with dataset
DataSetManager<KeyValueTable> recordsManager = getDataset(testSpace.dataset("records"));
KeyValueTable records = recordsManager.get();
Assert.assertTrue(count == Bytes.toLong(records.read("PUBLIC")));
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class TestAppWithCube method query.
private Collection<TimeSeries> query(URL serviceUrl, CubeQuery query) throws IOException {
URL url = new URL(serviceUrl, "query");
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<TimeSeries>>() {
}.getType());
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class TestAppWithCube method add.
private void add(URL serviceUrl, Collection<CubeFact> facts) throws IOException {
URL url = new URL(serviceUrl, "add");
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(facts)).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class DataPipelineTest method testServiceUrl.
public void testServiceUrl(Engine engine) throws Exception {
// Deploy the ServiceApp application
ApplicationManager appManager = deployApplication(ServiceApp.class);
// Start Greeting service and use it
ServiceManager serviceManager = appManager.getServiceManager(ServiceApp.Name.SERVICE_NAME).start();
// Wait service startup
serviceManager.waitForStatus(true);
URL url = new URL(serviceManager.getServiceURL(), "name");
HttpRequest httpRequest = HttpRequest.post(url).withBody("bob").build();
HttpResponse httpResponse = HttpRequests.execute(httpRequest);
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpResponse.getResponseCode());
url = new URL(serviceManager.getServiceURL(), "name/bob");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
String response;
try {
response = new String(ByteStreams.toByteArray(connection.getInputStream()), Charsets.UTF_8);
} finally {
connection.disconnect();
}
Assert.assertEquals("bob", response);
String sourceName = "ServiceUrlInput-" + engine.name();
String sinkName = "ServiceUrlOutput-" + engine.name();
/*
* source --> filter --> sink
*/
ETLBatchConfig etlConfig = ETLBatchConfig.builder("* * * * *").setEngine(engine).addStage(new ETLStage("source", MockSource.getPlugin(sourceName))).addStage(new ETLStage("filter", FilterTransform.getPlugin("name"))).addStage(new ETLStage("sink", MockSink.getPlugin(sinkName))).addConnection("source", "filter").addConnection("filter", "sink").build();
AppRequest<ETLBatchConfig> appRequest = new AppRequest<>(APP_ARTIFACT, etlConfig);
ApplicationId appId = NamespaceId.DEFAULT.app("ServiceUrl-" + engine);
appManager = deployApplication(appId, appRequest);
Schema schema = Schema.recordOf("testRecord", Schema.Field.of("name", Schema.of(Schema.Type.STRING)));
StructuredRecord recordSamuel = StructuredRecord.builder(schema).set("name", "samuel").build();
StructuredRecord recordBob = StructuredRecord.builder(schema).set("name", "bob").build();
StructuredRecord recordJane = StructuredRecord.builder(schema).set("name", "jane").build();
// write one record to each source
DataSetManager<Table> inputManager = getDataset(NamespaceId.DEFAULT.dataset(sourceName));
MockSource.writeInput(inputManager, ImmutableList.of(recordSamuel, recordBob, recordJane));
WorkflowManager workflowManager = appManager.getWorkflowManager(SmartWorkflow.NAME);
workflowManager.start();
workflowManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.MINUTES);
// check output
DataSetManager<Table> sinkManager = getDataset(sinkName);
Set<StructuredRecord> expected = ImmutableSet.of(recordBob);
Set<StructuredRecord> actual = Sets.newHashSet(MockSink.readOutput(sinkManager));
Assert.assertEquals(expected, actual);
serviceManager.stop();
serviceManager.waitForRun(ProgramRunStatus.KILLED, 180, TimeUnit.SECONDS);
}
Aggregations