use of org.batfish.common.BatfishException in project batfish by batfish.
the class Client method loadQuestionFromText.
/**
* Loads question from a JSON
*
* @param questionText Question JSON Text
* @param questionSource JSON key of question or file path of JSON
* @return question loaded as a {@link JSONObject}
* @throws BatfishException if question does not have instanceName or question cannot be parsed
*/
static JSONObject loadQuestionFromText(String questionText, String questionSource) {
try {
JSONObject questionObj = new JSONObject(questionText);
if (questionObj.has(BfConsts.PROP_INSTANCE) && !questionObj.isNull(BfConsts.PROP_INSTANCE)) {
JSONObject instanceDataObj = questionObj.getJSONObject(BfConsts.PROP_INSTANCE);
String instanceDataStr = instanceDataObj.toString();
InstanceData instanceData = BatfishObjectMapper.mapper().readValue(instanceDataStr, new TypeReference<InstanceData>() {
});
validateInstanceData(instanceData);
return questionObj;
} else {
throw new BatfishException(String.format("Question in %s has no instance data", questionSource));
}
} catch (JSONException | IOException e) {
throw new BatfishException("Failed to process question", e);
}
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class Client method validateInstanceData.
private static void validateInstanceData(InstanceData instanceData) {
String description = instanceData.getDescription();
String q = "Question: '" + instanceData.getInstanceName() + "'";
if (description == null || description.length() == 0) {
throw new BatfishException(q + " is missing question description");
}
for (Entry<String, Variable> e : instanceData.getVariables().entrySet()) {
String variableName = e.getKey();
Variable variable = e.getValue();
String v = "Variable: '" + variableName + "' in " + q;
String variableDescription = variable.getDescription();
if (variableDescription == null || variableDescription.length() == 0) {
throw new BatfishException(v + " is missing description");
}
}
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class QuestionHelper method getQuestion.
public static Question getQuestion(String questionTypeStr, Map<String, Supplier<Question>> questions) {
Supplier<Question> supplier = questions.get(questionTypeStr);
if (supplier == null) {
throw new BatfishException("No question found of type: " + questionTypeStr + '.');
}
Question question = supplier.get();
return question;
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class CommonUtil method checkJsonEqual.
public static boolean checkJsonEqual(Object a, Object b) {
try {
String aString = BatfishObjectMapper.writePrettyString(a);
String bString = BatfishObjectMapper.writePrettyString(b);
JSONAssert.assertEquals(aString, bString, false);
return true;
} catch (Exception e) {
throw new BatfishException("JSON equality check failed", e);
} catch (AssertionError err) {
return false;
}
}
use of org.batfish.common.BatfishException in project batfish by batfish.
the class CommonUtil method getConfig.
public static org.apache.commons.configuration2.Configuration getConfig(String overridePropertyName, String defaultPropertyFilename, Class<?> defaultPropertyLocatorClass) {
String overriddenPath = System.getProperty(overridePropertyName);
URL propertiesUrl;
if (overriddenPath != null) {
// The user provided an override, so look up that configuration instead.
try {
propertiesUrl = new URL(new URL("file://"), overriddenPath);
} catch (MalformedURLException e) {
throw new BatfishException("Error treating " + overriddenPath + " as a path to a properties file", e);
}
} else {
// Find the default properties file.
propertiesUrl = defaultPropertyLocatorClass.getClassLoader().getResource(defaultPropertyFilename);
}
try {
return new Configurations().properties(propertiesUrl);
} catch (Exception e) {
throw new BatfishException("Error loading configuration from " + overriddenPath, e);
}
}
Aggregations