Java Code Examples for org.codehaus.jettison.json.JSONObject#getString()

The following examples show how to use org.codehaus.jettison.json.JSONObject#getString() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bats   File: StramWebServices.java    License: Apache License 2.0 6 votes vote down vote up
@POST // not supported by WebAppProxyServlet, can only be called directly
@Path(PATH_PHYSICAL_PLAN_OPERATORS + "/{operatorId:\\d+}/properties")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject setPhysicalOperatorProperties(JSONObject request, @PathParam("operatorId") int operatorId)
{
  init();
  JSONObject response = new JSONObject();
  try {
    @SuppressWarnings("unchecked")
    Iterator<String> keys = request.keys();
    while (keys.hasNext()) {
      String key = keys.next();
      String val = request.isNull(key) ? null : request.getString(key);
      dagManager.setPhysicalOperatorProperty(operatorId, key, val);
    }
  } catch (JSONException ex) {
    LOG.warn("Got JSON Exception: ", ex);
  }
  return response;
}
 
Example 2
@Test
public void testJobsQueryFinishTimeBeginNegative() throws JSONException,
    Exception {
  WebResource r = resource();
  ClientResponse response = r.path("ws").path("v1").path("history")
      .path("mapreduce").path("jobs")
      .queryParam("finishedTimeBegin", String.valueOf(-1000))
      .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  JSONObject msg = response.getEntity(JSONObject.class);
  JSONObject exception = msg.getJSONObject("RemoteException");
  assertEquals("incorrect number of elements", 3, exception.length());
  String message = exception.getString("message");
  String type = exception.getString("exception");
  String classname = exception.getString("javaClassName");
  WebServicesTestUtils.checkStringMatch("exception message",
      "java.lang.Exception: finishedTimeBegin must be greater than 0",
      message);
  WebServicesTestUtils.checkStringMatch("exception type",
      "BadRequestException", type);
  WebServicesTestUtils.checkStringMatch("exception classname",
      "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
}
 
Example 3
@Test
public void testJobsQueryFinishTimeEndNegative() throws JSONException,
    Exception {
  WebResource r = resource();
  ClientResponse response = r.path("ws").path("v1").path("history")
      .path("mapreduce").path("jobs")
      .queryParam("finishedTimeEnd", String.valueOf(-1000))
      .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  JSONObject msg = response.getEntity(JSONObject.class);
  JSONObject exception = msg.getJSONObject("RemoteException");
  assertEquals("incorrect number of elements", 3, exception.length());
  String message = exception.getString("message");
  String type = exception.getString("exception");
  String classname = exception.getString("javaClassName");
  WebServicesTestUtils.checkStringMatch("exception message",
      "java.lang.Exception: finishedTimeEnd must be greater than 0", message);
  WebServicesTestUtils.checkStringMatch("exception type",
      "BadRequestException", type);
  WebServicesTestUtils.checkStringMatch("exception classname",
      "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
}
 
Example 4
@Test
public void testJobsQueryStartTimeEndInvalidformat() throws JSONException,
    Exception {
  WebResource r = resource();
  ClientResponse response = r.path("ws").path("v1").path("history")
      .path("mapreduce").path("jobs").queryParam("startedTimeEnd", "efsd")
      .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  JSONObject msg = response.getEntity(JSONObject.class);
  JSONObject exception = msg.getJSONObject("RemoteException");
  assertEquals("incorrect number of elements", 3, exception.length());
  String message = exception.getString("message");
  String type = exception.getString("exception");
  String classname = exception.getString("javaClassName");
  WebServicesTestUtils
      .checkStringMatch(
          "exception message",
          "java.lang.Exception: Invalid number format: For input string: \"efsd\"",
          message);
  WebServicesTestUtils.checkStringMatch("exception type",
      "BadRequestException", type);
  WebServicesTestUtils.checkStringMatch("exception classname",
      "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
}
 
Example 5
Source Project: big-c   File: TestAMWebServicesJobConf.java    License: Apache License 2.0 6 votes vote down vote up
public void verifyAMJobConf(JSONObject info, Job job) throws JSONException {

    assertEquals("incorrect number of elements", 2, info.length());

    WebServicesTestUtils.checkStringMatch("path", job.getConfFile().toString(),
        info.getString("path"));
    // just do simple verification of fields - not data is correct
    // in the fields
    JSONArray properties = info.getJSONArray("property");
    for (int i = 0; i < properties.length(); i++) {
      JSONObject prop = properties.getJSONObject(i);
      String name = prop.getString("name");
      String value = prop.getString("value");
      assertTrue("name not set", (name != null && !name.isEmpty()));
      assertTrue("value not set", (value != null && !value.isEmpty()));
    }
  }
 
Example 6
Source Project: hadoop   File: TestAMWebServicesJobs.java    License: Apache License 2.0 5 votes vote down vote up
public void verifyAMJobCounters(JSONObject info, Job job)
    throws JSONException {

  assertEquals("incorrect number of elements", 2, info.length());

  WebServicesTestUtils.checkStringMatch("id", MRApps.toString(job.getID()),
      info.getString("id"));
  // just do simple verification of fields - not data is correct
  // in the fields
  JSONArray counterGroups = info.getJSONArray("counterGroup");
  for (int i = 0; i < counterGroups.length(); i++) {
    JSONObject counterGroup = counterGroups.getJSONObject(i);
    String name = counterGroup.getString("counterGroupName");
    assertTrue("name not set", (name != null && !name.isEmpty()));
    JSONArray counters = counterGroup.getJSONArray("counter");
    for (int j = 0; j < counters.length(); j++) {
      JSONObject counter = counters.getJSONObject(j);
      String counterName = counter.getString("name");
      assertTrue("counter name not set",
          (counterName != null && !counterName.isEmpty()));

      long mapValue = counter.getLong("mapCounterValue");
      assertTrue("mapCounterValue  >= 0", mapValue >= 0);

      long reduceValue = counter.getLong("reduceCounterValue");
      assertTrue("reduceCounterValue  >= 0", reduceValue >= 0);

      long totalValue = counter.getLong("totalCounterValue");
      assertTrue("totalCounterValue  >= 0", totalValue >= 0);

    }
  }
}
 
Example 7
Source Project: big-c   File: TestAMWebServicesTasks.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskIdInvalid2() throws JSONException, Exception {
  WebResource r = resource();
  Map<JobId, Job> jobsMap = appContext.getAllJobs();
  for (JobId id : jobsMap.keySet()) {
    String jobId = MRApps.toString(id);
    String tid = "task_0_m_000000";
    try {
      r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId)
          .path("tasks").path(tid).get(JSONObject.class);
      fail("should have thrown exception on invalid uri");
    } catch (UniformInterfaceException ue) {
      ClientResponse response = ue.getResponse();
      assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
      JSONObject msg = response.getEntity(JSONObject.class);
      JSONObject exception = msg.getJSONObject("RemoteException");
      assertEquals("incorrect number of elements", 3, exception.length());
      String message = exception.getString("message");
      String type = exception.getString("exception");
      String classname = exception.getString("javaClassName");
      WebServicesTestUtils.checkStringMatch("exception message",
          "java.lang.Exception: TaskId string : "
              + "task_0_m_000000 is not properly formed", message);
      WebServicesTestUtils.checkStringMatch("exception type",
          "NotFoundException", type);
      WebServicesTestUtils.checkStringMatch("exception classname",
          "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
    }
  }
}
 
Example 8
public String state(String batchId) {
    try {
        LivyRestClient restClient = new LivyRestClient();
        String statusResult = restClient.livyGetJobStatusBatches(batchId);
        JSONObject stateJson = new JSONObject(statusResult);
        return stateJson.getString("state");
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}
 
Example 9
Source Project: hadoop   File: TestHsWebServicesTasks.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskIdBogus() throws JSONException, Exception {
  WebResource r = resource();
  Map<JobId, Job> jobsMap = appContext.getAllJobs();
  for (JobId id : jobsMap.keySet()) {
    String jobId = MRApps.toString(id);
    String tid = "bogustaskid";
    try {
      r.path("ws").path("v1").path("history").path("mapreduce").path("jobs")
          .path(jobId).path("tasks").path(tid).get(JSONObject.class);
      fail("should have thrown exception on invalid uri");
    } catch (UniformInterfaceException ue) {
      ClientResponse response = ue.getResponse();
      assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
      JSONObject msg = response.getEntity(JSONObject.class);
      JSONObject exception = msg.getJSONObject("RemoteException");
      assertEquals("incorrect number of elements", 3, exception.length());
      String message = exception.getString("message");
      String type = exception.getString("exception");
      String classname = exception.getString("javaClassName");
      WebServicesTestUtils.checkStringMatch("exception message",
          "java.lang.Exception: TaskId string : "
              + "bogustaskid is not properly formed", message);
      WebServicesTestUtils.checkStringMatch("exception type",
          "NotFoundException", type);
      WebServicesTestUtils.checkStringMatch("exception classname",
          "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
    }
  }
}
 
Example 10
Source Project: big-c   File: TestAMWebServicesTasks.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskIdInvalid() throws JSONException, Exception {
  WebResource r = resource();
  Map<JobId, Job> jobsMap = appContext.getAllJobs();
  for (JobId id : jobsMap.keySet()) {
    String jobId = MRApps.toString(id);
    String tid = "task_0_0000_d_000000";
    try {
      r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId)
          .path("tasks").path(tid).get(JSONObject.class);
      fail("should have thrown exception on invalid uri");
    } catch (UniformInterfaceException ue) {
      ClientResponse response = ue.getResponse();
      assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
      JSONObject msg = response.getEntity(JSONObject.class);
      JSONObject exception = msg.getJSONObject("RemoteException");
      assertEquals("incorrect number of elements", 3, exception.length());
      String message = exception.getString("message");
      String type = exception.getString("exception");
      String classname = exception.getString("javaClassName");
      WebServicesTestUtils.checkStringMatch("exception message",
          "java.lang.Exception: Bad TaskType identifier. TaskId string : "
              + "task_0_0000_d_000000 is not properly formed.", message);
      WebServicesTestUtils.checkStringMatch("exception type",
          "NotFoundException", type);
      WebServicesTestUtils.checkStringMatch("exception classname",
          "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
    }
  }
}
 
Example 11
@Test
public void testSingleContainerInvalid2() throws JSONException, Exception {
  WebResource r = resource();
  Application app = new MockApp(1);
  nmContext.getApplications().put(app.getAppId(), app);
  addAppContainers(app);
  Application app2 = new MockApp(2);
  nmContext.getApplications().put(app2.getAppId(), app2);
  addAppContainers(app2);
  try {
    r.path("ws").path("v1").path("node").path("containers")
        .path("container_1234_0001").accept(MediaType.APPLICATION_JSON)
        .get(JSONObject.class);
    fail("should have thrown exception on invalid user query");
  } catch (UniformInterfaceException ue) {
    ClientResponse response = ue.getResponse();
    assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
    JSONObject msg = response.getEntity(JSONObject.class);
    JSONObject exception = msg.getJSONObject("RemoteException");
    assertEquals("incorrect number of elements", 3, exception.length());
    String message = exception.getString("message");
    String type = exception.getString("exception");
    String classname = exception.getString("javaClassName");
    WebServicesTestUtils.checkStringMatch("exception message",
        "java.lang.Exception: invalid container id, container_1234_0001",
        message);
    WebServicesTestUtils.checkStringMatch("exception type",
        "BadRequestException", type);
    WebServicesTestUtils.checkStringMatch("exception classname",
        "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
  }
}
 
Example 12
Source Project: big-c   File: TestAMWebServicesTasks.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTasksQueryInvalid() throws JSONException, Exception {
  WebResource r = resource();
  Map<JobId, Job> jobsMap = appContext.getAllJobs();
  for (JobId id : jobsMap.keySet()) {
    String jobId = MRApps.toString(id);
    // tasktype must be exactly either "m" or "r"
    String tasktype = "reduce";

    try {
      r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId)
          .path("tasks").queryParam("type", tasktype)
          .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
      fail("should have thrown exception on invalid uri");
    } catch (UniformInterfaceException ue) {
      ClientResponse response = ue.getResponse();
      assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
      JSONObject msg = response.getEntity(JSONObject.class);
      JSONObject exception = msg.getJSONObject("RemoteException");
      assertEquals("incorrect number of elements", 3, exception.length());
      String message = exception.getString("message");
      String type = exception.getString("exception");
      String classname = exception.getString("javaClassName");
      WebServicesTestUtils.checkStringMatch("exception message",
          "java.lang.Exception: tasktype must be either m or r", message);
      WebServicesTestUtils.checkStringMatch("exception type",
          "BadRequestException", type);
      WebServicesTestUtils.checkStringMatch("exception classname",
          "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
    }
  }
}
 
Example 13
@Override
protected String convertMessage(String message)
{
  String data;

  try {
    JSONObject jo = new JSONObject(message);
    data = jo.getString(PubSubMessage.DATA_KEY);
  } catch (JSONException e) {
    return null;
  }

  return data;
}
 
Example 14
@Test
public void testJobsQueryFinishTimeBeginEndInvalid() throws JSONException,
    Exception {
  WebResource r = resource();
  Long now = System.currentTimeMillis();
  ClientResponse response = r.path("ws").path("v1").path("history")
      .path("mapreduce").path("jobs")
      .queryParam("finishedTimeBegin", String.valueOf(now))
      .queryParam("finishedTimeEnd", String.valueOf(40000))
      .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  JSONObject msg = response.getEntity(JSONObject.class);
  JSONObject exception = msg.getJSONObject("RemoteException");
  assertEquals("incorrect number of elements", 3, exception.length());
  String message = exception.getString("message");
  String type = exception.getString("exception");
  String classname = exception.getString("javaClassName");
  WebServicesTestUtils
      .checkStringMatch(
          "exception message",
          "java.lang.Exception: finishedTimeEnd must be greater than finishedTimeBegin",
          message);
  WebServicesTestUtils.checkStringMatch("exception type",
      "BadRequestException", type);
  WebServicesTestUtils.checkStringMatch("exception classname",
      "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
}
 
Example 15
Source Project: big-c   File: TestAMWebServicesJobs.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testJobIdInvalidBogus() throws JSONException, Exception {
  WebResource r = resource();

  try {
    r.path("ws").path("v1").path("mapreduce").path("jobs").path("bogusfoo")
        .get(JSONObject.class);
    fail("should have thrown exception on invalid uri");
  } catch (UniformInterfaceException ue) {
    ClientResponse response = ue.getResponse();
    assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
    JSONObject msg = response.getEntity(JSONObject.class);
    JSONObject exception = msg.getJSONObject("RemoteException");
    assertEquals("incorrect number of elements", 3, exception.length());
    String message = exception.getString("message");
    String type = exception.getString("exception");
    String classname = exception.getString("javaClassName");
    WebServicesTestUtils
        .checkStringMatch(
            "exception message",
            "java.lang.Exception: JobId string : bogusfoo is not properly formed",
            message);
    WebServicesTestUtils.checkStringMatch("exception type",
        "NotFoundException", type);
    WebServicesTestUtils.checkStringMatch("exception classname",
        "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
  }
}
 
Example 16
Source Project: big-c   File: TestHsWebServicesTasks.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskIdInvalid2() throws JSONException, Exception {
  WebResource r = resource();
  Map<JobId, Job> jobsMap = appContext.getAllJobs();
  for (JobId id : jobsMap.keySet()) {
    String jobId = MRApps.toString(id);
    String tid = "task_0000_m_000000";
    try {
      r.path("ws").path("v1").path("history").path("mapreduce").path("jobs")
          .path(jobId).path("tasks").path(tid).get(JSONObject.class);
      fail("should have thrown exception on invalid uri");
    } catch (UniformInterfaceException ue) {
      ClientResponse response = ue.getResponse();
      assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
      JSONObject msg = response.getEntity(JSONObject.class);
      JSONObject exception = msg.getJSONObject("RemoteException");
      assertEquals("incorrect number of elements", 3, exception.length());
      String message = exception.getString("message");
      String type = exception.getString("exception");
      String classname = exception.getString("javaClassName");
      WebServicesTestUtils.checkStringMatch("exception message",
          "java.lang.Exception: TaskId string : "
              + "task_0000_m_000000 is not properly formed", message);
      WebServicesTestUtils.checkStringMatch("exception type",
          "NotFoundException", type);
      WebServicesTestUtils.checkStringMatch("exception classname",
          "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
    }
  }
}
 
Example 17
@Test
public void testSchemaTags() throws Exception
{
  List<String> expectedTags = Lists.newArrayList("geo", "bullet");
  List<String> expectedKeyTags = Lists.newArrayList("geo.location");
  List<String> expectedValueTagsLat = Lists.newArrayList("geo.lattitude");
  List<String> expectedValueTagsLong = Lists.newArrayList("geo.longitude");

  String eventSchemaJSON = SchemaUtils.jarResourceFileToString("adsGenericEventSchemaTags.json");
  DimensionalSchema dimensional = new DimensionalSchema(
      new DimensionalConfigurationSchema(eventSchemaJSON, AggregatorRegistry.DEFAULT_AGGREGATOR_REGISTRY));

  String schemaJSON = dimensional.getSchemaJSON();

  JSONObject jo = new JSONObject(schemaJSON);
  List<String> tags = getStringList(jo.getJSONArray(FIELD_TAGS));
  Assert.assertEquals(expectedTags, tags);

  JSONArray keys = jo.getJSONArray(DimensionalConfigurationSchema.FIELD_KEYS);

  List<String> keyTags = null;

  for (int keyIndex = 0; keyIndex < keys.length(); keyIndex++) {
    JSONObject key = keys.getJSONObject(keyIndex);

    if (!key.has(FIELD_TAGS)) {
      continue;
    }

    Assert.assertEquals("location", key.get(DimensionalConfigurationSchema.FIELD_KEYS_NAME));
    keyTags = getStringList(key.getJSONArray(FIELD_TAGS));
  }

  Assert.assertTrue("No tags found for any key", keyTags != null);
  Assert.assertEquals(expectedKeyTags, keyTags);

  JSONArray values = jo.getJSONArray(DimensionalConfigurationSchema.FIELD_VALUES);

  boolean valueTagsLat = false;
  boolean valueTagsLong = false;

  for (int valueIndex = 0; valueIndex < values.length(); valueIndex++) {
    JSONObject value = values.getJSONObject(valueIndex);

    if (!value.has(FIELD_TAGS)) {
      continue;
    }

    String valueName = value.getString(DimensionalConfigurationSchema.FIELD_VALUES_NAME);
    List<String> valueTags = getStringList(value.getJSONArray(FIELD_TAGS));

    LOG.debug("value name: {}", valueName);

    if (valueName.startsWith("impressions")) {
      Assert.assertEquals(expectedValueTagsLat, valueTags);
      valueTagsLat = true;
    } else if (valueName.startsWith("clicks")) {
      Assert.assertEquals(expectedValueTagsLong, valueTags);
      valueTagsLong = true;
    } else {
      Assert.fail("There should be no tags for " + valueName);
    }
  }

  Assert.assertTrue("No tags found for impressions", valueTagsLat);
  Assert.assertTrue("No tags found for clicks", valueTagsLong);
}
 
Example 18
Source Project: Bats   File: LogicalPlanSerializer.java    License: Apache License 2.0 4 votes vote down vote up
public static PropertiesConfiguration convertToProperties(JSONObject json) throws JSONException
{
  PropertiesConfiguration props = new PropertiesConfiguration();
  JSONArray allOperators = json.getJSONArray("operators");
  JSONArray allStreams = json.getJSONArray("streams");

  for (int j = 0; j < allOperators.length(); j++) {
    JSONObject operatorDetail = allOperators.getJSONObject(j);
    String operatorName = operatorDetail.getString("name");
    String operatorKey = LogicalPlanConfiguration.OPERATOR_PREFIX + operatorName;
    props.setProperty(operatorKey + ".classname", operatorDetail.getString("class"));
    JSONObject properties = operatorDetail.optJSONObject("properties");
    if (properties != null) {
      Iterator<String> iter2 = properties.keys();
      while (iter2.hasNext()) {
        String propertyName = iter2.next();
        if (!propertyName.equals("name") && !propertyName.equals("class") && properties.opt(propertyName) != null) {
          JSONArray list = properties.optJSONArray(propertyName);
          String value = "";
          if (list != null) {
            for (int i = 0; i < list.length(); i++) {
              if (i != 0) {
                value += ",";
              }
              value += list.get(i).toString();
            }
            props.setProperty(operatorKey + "." + propertyName, value);
          } else {
            props.setProperty(operatorKey + "." + propertyName, properties.get(propertyName));
          }
        }
      }
    }
  }

  for (int j = 0; j < allStreams.length(); j++) {
    JSONObject streamDetail = allStreams.getJSONObject(j);
    String streamName = streamDetail.getString("name");
    String streamKey = LogicalPlanConfiguration.STREAM_PREFIX + streamName;
    JSONObject sourceDetail = streamDetail.getJSONObject("source");
    JSONArray sinksList = streamDetail.getJSONArray("sinks");

    props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_SOURCE, sourceDetail.getString("operatorName") + "." + sourceDetail.getString("portName"));
    String sinksValue = "";
    for (int i = 0; i < sinksList.length(); i++) {
      if (!sinksValue.isEmpty()) {
        sinksValue += ",";
      }
      sinksValue += sinksList.getJSONObject(i).getString("operatorName") + "." + sinksList.getJSONObject(i).getString("portName");
    }
    props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_SINKS, sinksValue);
    String locality = streamDetail.optString("locality", null);
    if (locality != null) {
      props.setProperty(streamKey + "." + LogicalPlanConfiguration.STREAM_LOCALITY, Locality.valueOf(locality));
    }
  }

  // TBD: Attributes

  return props;
}
 
Example 19
Source Project: batfish   File: WorkMgrServiceTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testGetAnalysisAnswersRows() throws Exception {
  initNetwork();
  initSnapshot();
  String analysisName = "analysis1";
  String questionName = "question1";
  Question questionObj = new TestQuestion();
  String questionContent = BatfishObjectMapper.writeString(questionObj);
  String columnName = "col";
  Map<String, AnswerRowsOptions> analysisAnswersOptions =
      ImmutableMap.of(
          questionName,
          new AnswerRowsOptions(
              ImmutableSet.of(columnName),
              ImmutableList.of(new ColumnFilter(columnName, "")),
              Integer.MAX_VALUE,
              0,
              ImmutableList.of(new ColumnSortOption(columnName, false)),
              false));
  String analysisAnswersOptionsStr =
      BatfishObjectMapper.writePrettyString(analysisAnswersOptions);

  int value = 5;

  String analysisJsonString = String.format("{\"%s\":%s}", questionName, questionContent);
  File analysisFile = _networksFolder.newFile(analysisName);
  FileUtils.writeStringToFile(analysisFile, analysisJsonString, StandardCharsets.UTF_8);

  _service.configureAnalysis(
      CoordConsts.DEFAULT_API_KEY,
      BatfishVersion.getVersionStatic(),
      _networkName,
      "new",
      analysisName,
      new FileInputStream(analysisFile),
      "",
      null);
  AnalysisId analysisId = idm().getAnalysisId(analysisName, _networkId).get();
  QuestionId questionId = idm().getQuestionId(questionName, _networkId, analysisId).get();
  AnswerId answerId =
      idm()
          .getBaseAnswerId(
              _networkId,
              _snapshotId,
              questionId,
              DEFAULT_QUESTION_SETTINGS_ID,
              DEFAULT_NETWORK_NODE_ROLES_ID,
              null,
              analysisId);
  Answer testAnswer = new Answer();
  testAnswer.setStatus(AnswerStatus.SUCCESS);
  testAnswer.addAnswerElement(
      new TableAnswerElement(
              new TableMetadata(
                  ImmutableList.of(new ColumnMetadata(columnName, Schema.INTEGER, "foobar")),
                  new DisplayHints().getTextDesc()))
          .addRow(Row.of(columnName, value)));
  String testAnswerStr = BatfishObjectMapper.writeString(testAnswer);
  AnswerMetadata answerMetadata =
      AnswerMetadataUtil.computeAnswerMetadata(testAnswer, Main.getLogger());
  Main.getWorkMgr().getStorage().storeAnswer(testAnswerStr, answerId);
  Main.getWorkMgr().getStorage().storeAnswerMetadata(answerMetadata, answerId);

  JSONArray answerOutput =
      _service.getAnalysisAnswersRows(
          CoordConsts.DEFAULT_API_KEY,
          BatfishVersion.getVersionStatic(),
          _networkName,
          _snapshotName,
          null,
          analysisName,
          analysisAnswersOptionsStr,
          null);

  assertThat(answerOutput.get(0), equalTo(CoordConsts.SVC_KEY_SUCCESS));

  JSONObject answerJsonObject = (JSONObject) answerOutput.get(1);
  String answersJsonString = answerJsonObject.getString(CoordConsts.SVC_KEY_ANSWERS);
  Map<String, Answer> structuredAnswers =
      BatfishObjectMapper.mapper()
          .readValue(answersJsonString, new TypeReference<Map<String, Answer>>() {});

  assertThat(structuredAnswers.keySet(), equalTo(ImmutableSet.of(questionName)));

  Answer processedAnswer = structuredAnswers.get(questionName);
  TableAnswerElement processedTable =
      (TableAnswerElement) processedAnswer.getAnswerElements().get(0);

  assertThat(processedTable.getRowsList(), equalTo(ImmutableList.of(Row.of(columnName, value))));
}
 
Example 20
Source Project: hadoop   File: TestAMWebServicesJobs.java    License: Apache License 2.0 4 votes vote down vote up
public void verifyAMJob(JSONObject info, Job job) throws JSONException {

    assertEquals("incorrect number of elements", 30, info.length());

    // everyone access fields
    verifyAMJobGeneric(job, info.getString("id"), info.getString("user"),
        info.getString("name"), info.getString("state"),
        info.getLong("startTime"), info.getLong("finishTime"),
        info.getLong("elapsedTime"), info.getInt("mapsTotal"),
        info.getInt("mapsCompleted"), info.getInt("reducesTotal"),
        info.getInt("reducesCompleted"),
        (float) info.getDouble("reduceProgress"),
        (float) info.getDouble("mapProgress"));

    String diagnostics = "";
    if (info.has("diagnostics")) {
      diagnostics = info.getString("diagnostics");
    }

    // restricted access fields - if security and acls set
    verifyAMJobGenericSecure(job, info.getInt("mapsPending"),
        info.getInt("mapsRunning"), info.getInt("reducesPending"),
        info.getInt("reducesRunning"), info.getBoolean("uberized"),
        diagnostics, info.getInt("newReduceAttempts"),
        info.getInt("runningReduceAttempts"),
        info.getInt("failedReduceAttempts"),
        info.getInt("killedReduceAttempts"),
        info.getInt("successfulReduceAttempts"), info.getInt("newMapAttempts"),
        info.getInt("runningMapAttempts"), info.getInt("failedMapAttempts"),
        info.getInt("killedMapAttempts"), info.getInt("successfulMapAttempts"));

    Map<JobACL, AccessControlList> allacls = job.getJobACLs();
    if (allacls != null) {

      for (Map.Entry<JobACL, AccessControlList> entry : allacls.entrySet()) {
        String expectName = entry.getKey().getAclName();
        String expectValue = entry.getValue().getAclString();
        Boolean found = false;
        // make sure ws includes it
        if (info.has("acls")) {
          JSONArray arr = info.getJSONArray("acls");

          for (int i = 0; i < arr.length(); i++) {
            JSONObject aclInfo = arr.getJSONObject(i);
            if (expectName.matches(aclInfo.getString("name"))) {
              found = true;
              WebServicesTestUtils.checkStringMatch("value", expectValue,
                  aclInfo.getString("value"));
            }
          }
        } else {
          fail("should have acls in the web service info");
        }
        assertTrue("acl: " + expectName + " not found in webservice output",
            found);
      }
    }

  }