use of com.google.gson.reflect.TypeToken in project cdap by caskdata.
the class SparkTest method testSparkHttpService.
@Test
public void testSparkHttpService() throws Exception {
ApplicationManager applicationManager = deploy(TestSparkApp.class);
SparkManager sparkManager = applicationManager.getSparkManager(SparkServiceProgram.class.getSimpleName()).start();
URL url = sparkManager.getServiceURL(5, TimeUnit.MINUTES);
Assert.assertNotNull(url);
// GET request to sum n numbers.
URL sumURL = url.toURI().resolve("sum?n=" + Joiner.on("&n=").join(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)).toURL();
HttpURLConnection urlConn = (HttpURLConnection) sumURL.openConnection();
Assert.assertEquals(HttpURLConnection.HTTP_OK, urlConn.getResponseCode());
try (InputStream is = urlConn.getInputStream()) {
Assert.assertEquals(55, Integer.parseInt(new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8)));
}
URL wordcountURL = url.toURI().resolve("wordcount").toURL();
urlConn = (HttpURLConnection) wordcountURL.openConnection();
// POST lines of sentences
urlConn.setDoOutput(true);
urlConn.setChunkedStreamingMode(10);
List<String> messages = new ArrayList<>();
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8"))) {
for (int i = 0; i < 10; i++) {
writer.printf("Message number %d\n", i);
messages.add("Message number " + i);
}
}
Assert.assertEquals(200, urlConn.getResponseCode());
try (Reader reader = new InputStreamReader(urlConn.getInputStream(), "UTF-8")) {
Map<String, Integer> result = new Gson().fromJson(reader, new TypeToken<Map<String, Integer>>() {
}.getType());
// Do a wordcount locally to get the expected result
Map<String, Integer> expected = messages.stream().flatMap((Function<String, Stream<String>>) s -> Arrays.stream(s.split("\\s+"))).map(s -> Maps.immutableEntry(s, 1)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1 + v2));
Assert.assertEquals(expected, result);
}
sparkManager.stop();
}
use of com.google.gson.reflect.TypeToken in project cdap by caskdata.
the class ServiceArtifactTestRun method testServiceArtifact.
@Test
public void testServiceArtifact() throws Exception {
ApplicationManager appManager = deployWithArtifact(ServiceArtifactApp.class, artifactJar);
ServiceManager serviceManager = appManager.getServiceManager("artifact").start();
URL serviceURL = serviceManager.getServiceURL(30, TimeUnit.SECONDS);
Assert.assertNotNull(serviceURL);
URL listURL = serviceURL.toURI().resolve("list").toURL();
try (Reader reader = new InputStreamReader(listURL.openStream(), StandardCharsets.UTF_8)) {
List<ArtifactInfo> artifacts = new Gson().fromJson(reader, new TypeToken<List<ArtifactInfo>>() {
}.getType());
// It should have the test app, and two plugin artifacts
Assert.assertEquals(3, artifacts.size());
Assert.assertTrue(artifacts.stream().anyMatch(info -> info.getName().equals(ServiceArtifactApp.class.getSimpleName())));
Assert.assertTrue(artifacts.stream().anyMatch(info -> info.getName().equals("dummybase")));
Assert.assertTrue(artifacts.stream().anyMatch(info -> info.getName().equals("dummy")));
}
URL loadURL = serviceURL.toURI().resolve("load?parent=dummybase&plugin=dummy&class=" + DummyPlugin.class.getName()).toURL();
HttpURLConnection urlConn = (HttpURLConnection) loadURL.openConnection();
Assert.assertEquals(200, urlConn.getResponseCode());
try (Reader reader = new InputStreamReader(urlConn.getInputStream(), StandardCharsets.UTF_8)) {
Assert.assertEquals(DummyPlugin.class.getName(), CharStreams.toString(reader));
}
serviceManager.stop();
serviceManager.waitForStatus(false);
}
use of com.google.gson.reflect.TypeToken in project cdap by caskdata.
the class MetricsHandlerTestRun method verifySearchMetricResult.
private void verifySearchMetricResult(String url, List<String> expectedValues) throws Exception {
HttpResponse response = doPost(url, null);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
String result = EntityUtils.toString(response.getEntity());
List<String> reply = GSON.fromJson(result, new TypeToken<List<String>>() {
}.getType());
reply = removeMetricsSystemMetrics(reply);
Assert.assertEquals(expectedValues.size(), reply.size());
for (int i = 0; i < expectedValues.size(); i++) {
Assert.assertEquals(expectedValues.get(i), reply.get(i));
}
}
use of com.google.gson.reflect.TypeToken in project cdap by caskdata.
the class DecisionTreeRegressionAppTest method test.
@Test
public void test() throws Exception {
// Deploy the Application
ApplicationManager appManager = deployApplication(DecisionTreeRegressionApp.class);
// Start the Service
ServiceManager serviceManager = appManager.getServiceManager(ModelDataService.SERVICE_NAME).start();
serviceManager.waitForStatus(true, 30, 1);
URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
URL addDataURL = new URL(serviceURL, "labels");
HttpRequest request = HttpRequest.builder(HttpMethod.PUT, addDataURL).withBody(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return getClass().getClassLoader().getResourceAsStream("sample_libsvm_data.txt");
}
}).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
// Start a Spark Program
SparkManager sparkManager = appManager.getSparkManager(ModelTrainer.NAME).start();
sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
// Check that there is a new model
URL listModelsURL = new URL(serviceURL, "models");
request = HttpRequest.builder(HttpMethod.GET, listModelsURL).build();
response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
List<String> models = GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<List<String>>() {
}.getType());
Assert.assertEquals(1, models.size());
// Check that there is some model metadata
String modelId = models.get(0);
URL modelMetaURL = new URL(serviceURL, "models/" + modelId);
request = HttpRequest.builder(HttpMethod.GET, modelMetaURL).build();
response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
ModelMeta meta = GSON.fromJson(response.getResponseBodyAsString(), ModelMeta.class);
Assert.assertNotNull(meta);
Assert.assertEquals(0.7, meta.getTrainingPercentage(), 0.000001);
Assert.assertEquals(692, meta.getNumFeatures());
// Check that the corresponding model file exists
DataSetManager<FileSet> modelFiles = getDataset(DecisionTreeRegressionApp.MODEL_DATASET);
Assert.assertTrue(modelFiles.get().getBaseLocation().append(modelId).exists());
}
use of com.google.gson.reflect.TypeToken in project cdap by caskdata.
the class DatasetInstanceHandlerTest method getInstanceProperties.
private ObjectResponse<Map<String, String>> getInstanceProperties(String instanceName) throws IOException {
HttpRequest request = HttpRequest.get(getUrl("/data/datasets/" + instanceName + "/properties")).build();
HttpResponse response = HttpRequests.execute(request);
return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
}.getType());
}
Aggregations