use of org.apache.hyracks.http.api.IServletResponse in project asterixdb by apache.
the class ConnectorApiServletTest method testGet.
@Test
public void testGet() throws Exception {
// Starts test asterixdb cluster.
SqlppExecutionTest.setUp();
// Configures a test connector api servlet.
ConnectorApiServlet let = new ConnectorApiServlet(new ConcurrentHashMap<>(), new String[] { "/" }, (ICcApplicationContext) ExecutionTestUtil.integrationUtil.cc.getApplicationContext());
Map<String, NodeControllerInfo> nodeMap = new HashMap<>();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintWriter outputWriter = new PrintWriter(outputStream);
// Creates mocks.
IHyracksClientConnection mockHcc = mock(IHyracksClientConnection.class);
NodeControllerInfo mockInfo1 = mock(NodeControllerInfo.class);
NodeControllerInfo mockInfo2 = mock(NodeControllerInfo.class);
IServletRequest mockRequest = mock(IServletRequest.class);
IServletResponse mockResponse = mock(IServletResponse.class);
FullHttpRequest mockHttpRequest = mock(FullHttpRequest.class);
// Put stuff in let map
let.ctx().put(ServletConstants.HYRACKS_CONNECTION_ATTR, mockHcc);
// Sets up mock returns.
when(mockRequest.getHttpRequest()).thenReturn(mockHttpRequest);
when(mockHttpRequest.method()).thenReturn(HttpMethod.GET);
when(mockRequest.getParameter("dataverseName")).thenReturn("Metadata");
when(mockRequest.getParameter("datasetName")).thenReturn("Dataset");
when(mockResponse.writer()).thenReturn(outputWriter);
when(mockHcc.getNodeControllerInfos()).thenReturn(nodeMap);
when(mockInfo1.getNetworkAddress()).thenReturn(new NetworkAddress("127.0.0.1", 3099));
when(mockInfo2.getNetworkAddress()).thenReturn(new NetworkAddress("127.0.0.2", 3099));
// Calls ConnectorAPIServlet.formResponseObject.
nodeMap.put("asterix_nc1", mockInfo1);
nodeMap.put("asterix_nc2", mockInfo2);
let.handle(mockRequest, mockResponse);
// Constructs the actual response.
ObjectMapper om = new ObjectMapper();
ObjectNode actualResponse = (ObjectNode) om.readTree(outputStream.toString());
// Checks the temp-or-not, primary key, data type of the dataset.
boolean temp = actualResponse.get("temp").asBoolean();
Assert.assertFalse(temp);
String primaryKey = actualResponse.get("keys").asText();
Assert.assertEquals("DataverseName,DatasetName", primaryKey);
ARecordType recordType = (ARecordType) JSONDeserializerForTypes.convertFromJSON(actualResponse.get("type"));
Assert.assertEquals(getMetadataRecordType("Metadata", "Dataset"), recordType);
// Checks the correctness of results.
ArrayNode splits = (ArrayNode) actualResponse.get("splits");
String path = (splits.get(0)).get("path").asText();
Assert.assertTrue(path.endsWith("Metadata/Dataset_idx_Dataset"));
// Tears down the asterixdb cluster.
SqlppExecutionTest.tearDown();
}
use of org.apache.hyracks.http.api.IServletResponse in project asterixdb by apache.
the class QueryCancellationServletTest method testDelete.
@Test
public void testDelete() throws Exception {
// Creates a query cancellation servlet.
QueryCancellationServlet cancellationServlet = new QueryCancellationServlet(new ConcurrentHashMap<>(), new String[] { "/" });
// Adds mocked Hyracks client connection into the servlet context.
IHyracksClientConnection mockHcc = mock(IHyracksClientConnection.class);
cancellationServlet.ctx().put(ServletConstants.HYRACKS_CONNECTION_ATTR, mockHcc);
// Adds a query context into the servlet context.
IStatementExecutorContext queryCtx = new StatementExecutorContext();
cancellationServlet.ctx().put(ServletConstants.RUNNING_QUERIES_ATTR, queryCtx);
// Tests the case that query is not in the map.
IServletRequest mockRequest = mockRequest("1");
IServletResponse mockResponse = mock(IServletResponse.class);
cancellationServlet.handle(mockRequest, mockResponse);
verify(mockResponse, times(1)).setStatus(HttpResponseStatus.NOT_FOUND);
// Tests the case that query is in the map.
queryCtx.put("1", new JobId(1));
cancellationServlet.handle(mockRequest, mockResponse);
verify(mockResponse, times(1)).setStatus(HttpResponseStatus.OK);
// Tests the case the client_context_id is not provided.
mockRequest = mockRequest(null);
cancellationServlet.handle(mockRequest, mockResponse);
verify(mockResponse, times(1)).setStatus(HttpResponseStatus.BAD_REQUEST);
// Tests the case that the job cancellation hit some exception from Hyracks.
queryCtx.put("2", new JobId(2));
Mockito.doThrow(new Exception()).when(mockHcc).cancelJob(any());
mockRequest = mockRequest("2");
cancellationServlet.handle(mockRequest, mockResponse);
verify(mockResponse, times(1)).setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
use of org.apache.hyracks.http.api.IServletResponse in project asterixdb by apache.
the class RestApiServlet method initResponse.
/**
* Initialize the Content-Type of the response, and construct a
* SessionConfig with the appropriate output writer and output-format
* based on the Accept: header and other servlet parameters.
*/
static SessionOutput initResponse(IServletRequest request, IServletResponse response) throws IOException {
HttpUtil.setContentType(response, HttpUtil.ContentType.TEXT_PLAIN, HttpUtil.Encoding.UTF8);
// CLEAN_JSON output is the default; most generally useful for a
// programmatic HTTP API
OutputFormat format = OutputFormat.CLEAN_JSON;
// First check the "output" servlet parameter.
String output = request.getParameter("output");
String accept = request.getHeader("Accept", "");
if (output != null) {
if ("CSV".equals(output)) {
format = OutputFormat.CSV;
} else if ("ADM".equals(output)) {
format = OutputFormat.ADM;
}
} else {
// Second check the Accept: HTTP header.
if (accept.contains("application/x-adm")) {
format = OutputFormat.ADM;
} else if (accept.contains("text/csv")) {
format = OutputFormat.CSV;
}
}
if (format == OutputFormat.CLEAN_JSON && ("true".equals(request.getParameter("lossless")) || accept.contains("lossless=true"))) {
format = OutputFormat.LOSSLESS_JSON;
}
SessionOutput.ResultAppender appendHandle = (app, handle) -> app.append("{ \"").append("handle").append("\":" + " \"").append(handle).append("\" }");
SessionConfig sessionConfig = new SessionConfig(format);
// If it's JSON or ADM, check for the "wrapper-array" flag. Default is
// "true" for JSON and "false" for ADM. (Not applicable for CSV.)
boolean wrapperArray = format == OutputFormat.CLEAN_JSON || format == OutputFormat.LOSSLESS_JSON;
String wrapperParam = request.getParameter("wrapper-array");
if (wrapperParam != null) {
wrapperArray = Boolean.valueOf(wrapperParam);
} else if (accept.contains("wrap-array=true")) {
wrapperArray = true;
} else if (accept.contains("wrap-array=false")) {
wrapperArray = false;
}
sessionConfig.set(SessionConfig.FORMAT_WRAPPER_ARRAY, wrapperArray);
// Now that format is set, output the content-type
switch(format) {
case ADM:
HttpUtil.setContentType(response, "application/x-adm");
break;
case CLEAN_JSON:
// No need to reflect "clean-ness" in output type; fall through
case LOSSLESS_JSON:
HttpUtil.setContentType(response, "application/json");
break;
case CSV:
// Check for header parameter or in Accept:.
if ("present".equals(request.getParameter("header")) || accept.contains("header=present")) {
HttpUtil.setContentType(response, "text/csv; header=present");
sessionConfig.set(SessionConfig.FORMAT_CSV_HEADER, true);
} else {
HttpUtil.setContentType(response, "text/csv; header=absent");
}
break;
default:
throw new IOException("Unknown format " + format);
}
return new SessionOutput(sessionConfig, response.writer(), null, null, appendHandle, null);
}
use of org.apache.hyracks.http.api.IServletResponse in project asterixdb by apache.
the class VersionApiServletTest method testGet.
@Test
public void testGet() throws Exception {
// Configures a test version api servlet.
VersionApiServlet servlet = new VersionApiServlet(new ConcurrentHashMap<>(), new String[] { "/" });
Map<String, String> propMap = new HashMap<>();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintWriter outputWriter = new PrintWriter(outputStream);
// Creates mocks.
CcApplicationContext mockCtx = mock(CcApplicationContext.class);
IServletRequest mockRequest = mock(IServletRequest.class);
IHyracksClientConnection mockHcc = mock(IHyracksClientConnection.class);
IServletResponse mockResponse = mock(IServletResponse.class);
BuildProperties mockProperties = mock(BuildProperties.class);
FullHttpRequest mockHttpRequest = mock(FullHttpRequest.class);
// Put stuff in let map
servlet.ctx().put(HYRACKS_CONNECTION_ATTR, mockHcc);
servlet.ctx().put(ASTERIX_APP_CONTEXT_INFO_ATTR, mockCtx);
// Sets up mock returns.
when(mockResponse.writer()).thenReturn(outputWriter);
when(mockRequest.getHttpRequest()).thenReturn(mockHttpRequest);
when(mockHttpRequest.method()).thenReturn(HttpMethod.GET);
when(mockCtx.getBuildProperties()).thenReturn(mockProperties);
when(mockProperties.getAllProps()).thenReturn(propMap);
propMap.put("git.build.user.email", "foo@bar.baz");
propMap.put("git.build.host", "fulliautomatix");
propMap.put("git.dirty", "true");
propMap.put("git.remote.origin.url", "git@github.com:apache/incubator-asterixdb.git");
propMap.put("git.closest.tag.name", "asterix-0.8.7-incubating");
propMap.put("git.commit.id.describe-short", "asterix-0.8.7-incubating-19-dirty");
propMap.put("git.commit.user.email", "foo@bar.baz");
propMap.put("git.commit.time", "21.10.2015 @ 23:36:41 PDT");
propMap.put("git.commit.message.full", "ASTERIXDB-1045: fix log file reading during recovery\n\nChange-Id: Ic83ee1dd2d7ba88180c25f4ec6c7aa8d0a5a7162\nReviewed-on: https://asterix-gerrit.ics.uci.edu/465\nTested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>");
propMap.put("git.build.version", "0.8.8-SNAPSHOT");
propMap.put("git.commit.message.short", "ASTERIXDB-1045: fix log file reading during recovery");
propMap.put("git.commit.id.abbrev", "e1dad19");
propMap.put("git.branch", "foo/bar");
propMap.put("git.build.user.name", "Asterix");
propMap.put("git.closest.tag.commit.count", "19");
propMap.put("git.commit.id.describe", "asterix-0.8.7-incubating-19-ge1dad19-dirty");
propMap.put("git.commit.id", "e1dad1984640517366a7e73e323c9de27b0676f7");
propMap.put("git.tags", "");
propMap.put("git.build.time", "22.10.2015 @ 17:11:07 PDT");
propMap.put("git.commit.user.name", "Obelix");
// Calls VersionAPIServlet.formResponseObject.
servlet.handle(mockRequest, mockResponse);
// Constructs the actual response.
ObjectMapper om = new ObjectMapper();
ObjectNode actualResponse = (ObjectNode) om.readTree(outputStream.toByteArray());
ObjectNode expectedResponse = om.createObjectNode();
for (Map.Entry<String, String> e : propMap.entrySet()) {
expectedResponse.put(e.getKey(), e.getValue());
}
// Checks the response contains all the expected keys.
Assert.assertEquals(actualResponse.toString(), expectedResponse.toString());
}
Aggregations