### Question:
QueryTranslator { public QueryExecPlan translate(ParseBaseNode rootNode) { QueryExecPlan execPlan = new QueryExecPlan(); rewriteParseTree(rootNode, null); rewriteProjection(rootNode); nodeIndex = 0; rootAction = translateParseTree(rootNode, execPlan); checkTooManyCollectionsQuery(execPlan); rewriteRootAction(execPlan); if (hasEmbed) { constructEmbedContainer(); } execPlan.setRootAction(rootAction); execPlan.setRootNode(rootNode); if (execPlan.getAggAction() != null) { if (rootAction.getChildrenActions().isEmpty()) { AggregateAction aggAction = (AggregateAction)execPlan.getAggAction(); aggAction.setOneTableAggregation(true); } if (queryContext.isCountOnly()) { throw new QueryParseException(QueryErrCodeEnum.AGG_COUNT_NOT_SUPPORT, "Aggregation query does not support count mode!"); } } return execPlan; } QueryTranslator(QueryContext context); QueryExecPlan translate(ParseBaseNode rootNode);  }### Answer:
@Test public void testReverseQuery() { String queryStr = "Environment{*}.applications{*}.appService!ServiceInstance[@name=~\"srp-app:Raptor.*\"].runsOn[exists @name]"; QueryExecPlan plan = getTranslator(raptorContext).translate(raptorParser.parse(queryStr)); }
@Test public void testFailure() throws Exception { String query = null; try { query = "A.b"; getTranslator(deployContext).translate(deployParser.parse(query)); Assert.assertFalse(true); } catch (Exception qte) { } try { query = "Manifest.ddd"; getTranslator(deployContext).translate(deployParser.parse(query)); Assert.assertFalse(true); } catch (Exception qte) { } }
@Test public void testSetQueryPlanUnion() { String query = "ApplicationService.(services[@name=~\"srp-app.*\"] || updateStrategies{*})"; QueryExecPlan plan = getTranslator(raptorContext).translate(raptorParser.parse(query)); }
@Test public void testSubQuery() { String query = "ApplicationService[@name =~\"srp.*\" " + "and " + "(@services =& ServiceInstance[@_oid=\"4fbb314fc681caf13e283a7c\" " + "and " + "@runsOn =& Compute[@name=\"compute-00002\"]]" + ") " + "]"; QueryExecPlan plan = getTranslator(raptorContext).translate(raptorParser.parse(query)); Assert.assertFalse(plan.getSubPlans().isEmpty()); Assert.assertEquals(1, plan.getSubPlans().size()); Assert.assertFalse(plan.getSubPlans().isEmpty()); Assert.assertEquals(1, plan.getSubPlans().size()); }
@Test public void testSubQueryProjectValidation() { String query = "ApplicationService[@name =~\"srp.*\" " + "and " + "(@services =& ServiceInstance[@_oid=\"4fbb314fc681caf13e283a7c\" " + "and " + "@runsOn =& Compute[@name=\"compute-00002\"]]{@_oid, @name}" + ") " + "]"; try { getTranslator(raptorContext).translate(raptorParser.parse(query)); Assert.fail(); } catch (QueryParseException e) { Assert.assertEquals(QueryErrCodeEnum.IILEGAL_PROJECTION.getErrorCode(), e.getErrorCode()); } }