use of org.apache.asterix.api.http.server.ConnectorApiServlet 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.asterix.api.http.server.ConnectorApiServlet in project asterixdb by apache.
the class ConnectorApiServletTest method testFormResponseObject.
@Test
public void testFormResponseObject() throws Exception {
ConnectorApiServlet let = new ConnectorApiServlet(new ConcurrentHashMap<>(), new String[] { "/" }, (ICcApplicationContext) ExecutionTestUtil.integrationUtil.cc.getApplicationContext());
ObjectMapper om = new ObjectMapper();
ObjectNode actualResponse = om.createObjectNode();
FileSplit[] splits = new FileSplit[2];
splits[0] = new ManagedFileSplit("asterix_nc1", "foo1");
splits[1] = new ManagedFileSplit("asterix_nc2", "foo2");
Map<String, NodeControllerInfo> nodeMap = new HashMap<>();
NodeControllerInfo mockInfo1 = mock(NodeControllerInfo.class);
NodeControllerInfo mockInfo2 = mock(NodeControllerInfo.class);
// Sets up mock returns.
when(mockInfo1.getNetworkAddress()).thenReturn(new NetworkAddress("127.0.0.1", 3099));
when(mockInfo2.getNetworkAddress()).thenReturn(new NetworkAddress("127.0.0.2", 3099));
String[] fieldNames = new String[] { "a1", "a2" };
IAType[] fieldTypes = new IAType[] { BuiltinType.ABOOLEAN, BuiltinType.ADAYTIMEDURATION };
ARecordType recordType = new ARecordType("record", fieldNames, fieldTypes, true);
String primaryKey = "a1";
// Calls ConnectorAPIServlet.formResponseObject.
nodeMap.put("asterix_nc1", mockInfo1);
nodeMap.put("asterix_nc2", mockInfo2);
PA.invokeMethod(let, "formResponseObject(" + ObjectNode.class.getName() + ", " + FileSplit.class.getName() + "[], " + ARecordType.class.getName() + ", " + String.class.getName() + ", boolean, " + Map.class.getName() + ")", actualResponse, splits, recordType, primaryKey, true, nodeMap);
// Constructs expected response.
ObjectNode expectedResponse = om.createObjectNode();
expectedResponse.put("temp", true);
expectedResponse.put("keys", primaryKey);
expectedResponse.set("type", recordType.toJSON());
ArrayNode splitsArray = om.createArrayNode();
ObjectNode element1 = om.createObjectNode();
element1.put("ip", "127.0.0.1");
element1.put("path", splits[0].getPath());
ObjectNode element2 = om.createObjectNode();
element2.put("ip", "127.0.0.2");
element2.put("path", splits[1].getPath());
splitsArray.add(element1);
splitsArray.add(element2);
expectedResponse.set("splits", splitsArray);
// Checks results.
Assert.assertEquals(actualResponse.toString(), expectedResponse.toString());
}
Aggregations