use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.
the class SpamClassifierTest method test.
@Test
public void test() throws Exception {
// Deploy the KafkaIngestionApp application
ApplicationManager appManager = deployApplication(SpamClassifier.class);
ingestTrainingData();
publishKafkaMessages();
// start spark streaming program
SparkManager sparkManager = appManager.getSparkManager(SpamClassifierProgram.class.getSimpleName());
Map<String, String> runtimeArgs = new HashMap<>();
runtimeArgs.put("kafka.brokers", "127.0.0.1:" + kafkaPort);
runtimeArgs.put("kafka.topics", KAFKA_TOPIC);
sparkManager.start(runtimeArgs);
// Start and wait for service to start
final ServiceManager serviceManager = appManager.getServiceManager(SpamClassifier.SERVICE_HANDLER).start();
serviceManager.waitForStatus(true);
// wait for spark streaming program to write to dataset
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return testClassification(serviceManager, "1", SpamClassifier.SpamClassifierServiceHandler.SPAM) && testClassification(serviceManager, "2", SpamClassifier.SpamClassifierServiceHandler.HAM);
}
}, 60, TimeUnit.SECONDS);
// stop spark program
sparkManager.stop();
sparkManager.waitForRun(ProgramRunStatus.KILLED, 1, TimeUnit.MINUTES);
appManager.stopAll();
}
use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.
the class FileSetWordCountTest method testWordCountOnFileSet.
@Test
public void testWordCountOnFileSet() throws Exception {
// deploy the application
ApplicationManager applicationManager = deployApplication(FileSetExample.class);
final String line1 = "a b a";
final String line2 = "b a b";
// discover the file set service
ServiceManager serviceManager = applicationManager.getServiceManager("FileSetService").start();
serviceManager.waitForStatus(true);
URL serviceURL = serviceManager.getServiceURL();
// write a file to the file set using the service
HttpURLConnection connection = (HttpURLConnection) new URL(serviceURL, "lines?path=nn.1").openConnection();
try {
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.getOutputStream().write(line1.getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
} finally {
connection.disconnect();
}
// run word count over that file only
Map<String, String> runtimeArguments = Maps.newHashMap();
Map<String, String> inputArgs = Maps.newHashMap();
FileSetArguments.setInputPaths(inputArgs, "nn.1");
Map<String, String> outputArgs = Maps.newHashMap();
FileSetArguments.setOutputPath(outputArgs, "out.1");
runtimeArguments.putAll(RuntimeArguments.addScope(Scope.DATASET, "lines", inputArgs));
runtimeArguments.putAll(RuntimeArguments.addScope(Scope.DATASET, "counts", outputArgs));
MapReduceManager mapReduceManager = applicationManager.getMapReduceManager("WordCount").start(runtimeArguments);
mapReduceManager.waitForRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.MINUTES);
// retrieve the counts through the service and verify
Map<String, Integer> counts = Maps.newHashMap();
connection = (HttpURLConnection) new URL(serviceURL, "counts?path=out.1/part-r-00000").openConnection();
try {
connection.setRequestMethod("GET");
Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
readCounts(connection.getInputStream(), counts);
} finally {
connection.disconnect();
}
// "a b a" should yield "a":2, "b":1
Assert.assertEquals(2, counts.size());
Assert.assertEquals(new Integer(2), counts.get("a"));
Assert.assertEquals(new Integer(1), counts.get("b"));
// write a file to the file set using the dataset directly
DataSetManager<FileSet> linesManager = getDataset("lines");
OutputStream output = linesManager.get().getLocation("nn.2").getOutputStream();
try {
output.write(line2.getBytes(Charsets.UTF_8));
} finally {
output.close();
}
// run word count over both files
FileSetArguments.setInputPath(inputArgs, "nn.1");
FileSetArguments.addInputPath(inputArgs, "nn.2");
FileSetArguments.setOutputPath(outputArgs, "out.2");
runtimeArguments.putAll(RuntimeArguments.addScope(Scope.DATASET, "lines", inputArgs));
runtimeArguments.putAll(RuntimeArguments.addScope(Scope.DATASET, "counts", outputArgs));
mapReduceManager = applicationManager.getMapReduceManager("WordCount").start(runtimeArguments);
mapReduceManager.waitForRuns(ProgramRunStatus.COMPLETED, 2, 5, TimeUnit.MINUTES);
// retrieve the counts through the dataset API and verify
// write a file to the file set using the dataset directly
DataSetManager<FileSet> countsManager = getDataset("counts");
counts.clear();
Location resultLocation = countsManager.get().getLocation("out.2");
Assert.assertTrue(resultLocation.isDirectory());
List<String> parts = new LinkedList<>();
for (Location child : resultLocation.list()) {
if (child.getName().startsWith("part-")) {
// only read part files, no check sums or done files
parts.add(child.getName());
readCounts(child.getInputStream(), counts);
}
}
// "a b a" and "b a b" should yield "a":3, "b":3
Assert.assertEquals(2, counts.size());
Assert.assertEquals(new Integer(3), counts.get("a"));
Assert.assertEquals(new Integer(3), counts.get("b"));
// retrieve the counts through the service
counts.clear();
for (String part : parts) {
connection = (HttpURLConnection) new URL(serviceURL, "counts?path=out.2/" + part).openConnection();
try {
connection.setRequestMethod("GET");
Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
readCounts(connection.getInputStream(), counts);
} finally {
connection.disconnect();
}
}
// "a b a" and "b a b" should yield "a":3, "b":3
Assert.assertEquals(2, counts.size());
Assert.assertEquals(new Integer(3), counts.get("a"));
Assert.assertEquals(new Integer(3), counts.get("b"));
serviceManager.stop();
}
use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.
the class HelloWorldTest method test.
@Test
public void test() throws Exception {
// Deploy the HelloWorld application
ApplicationManager appManager = deployApplication(HelloWorld.class);
// Start WhoFlow
FlowManager flowManager = appManager.getFlowManager("WhoFlow").start();
Assert.assertTrue(flowManager.isRunning());
// Send stream events to the "who" Stream
StreamManager streamManager = getStreamManager("who");
streamManager.send("1");
streamManager.send("2");
streamManager.send("3");
streamManager.send("4");
streamManager.send("5");
try {
// Wait for the last Flowlet processing 5 events, or at most 5 seconds
RuntimeMetrics metrics = flowManager.getFlowletMetrics("saver");
metrics.waitForProcessed(5, 5, TimeUnit.SECONDS);
} finally {
flowManager.stop();
Assert.assertFalse(flowManager.isRunning());
}
// Start Greeting service and use it
ServiceManager serviceManager = appManager.getServiceManager(HelloWorld.Greeting.SERVICE_NAME).start();
// Wait service startup
serviceManager.waitForStatus(true);
URL url = new URL(serviceManager.getServiceURL(), "greet");
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("Hello 5!", response);
}
use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.
the class LogAnalysisAppTest method getServiceManager.
private ServiceManager getServiceManager(ApplicationManager appManager, String serviceName) throws InterruptedException {
// Start the service
ServiceManager serviceManager = appManager.getServiceManager(serviceName).start();
// Wait for service startup
serviceManager.waitForStatus(true);
return serviceManager;
}
use of co.cask.cdap.test.ServiceManager in project cdap by caskdata.
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.waitForStatus(true, 2, 1);
callServicePut(serviceManager.getServiceURL(), "key1", "value1");
String response = callServiceGet(serviceManager.getServiceURL(), "key1");
Assert.assertEquals("value1", new Gson().fromJson(response, String.class));
}
Aggregations