@Test public void testProvideOptionalParameterWithValue() throws IOException { Map<String, Variable> variables = new HashMap<>(); Variable integerVariable = new Variable(); integerVariable.setOptional(true); integerVariable.setValue(_mapper.readTree("3")); variables.put("integer", integerVariable); Client.checkVariableState(variables); }
"No variable named: '" + parameterName + "' in supplied question template"); if (variable.getMinElements() != null) { if (!value.isArray() || value.size() < variable.getMinElements()) { throw new BatfishException( String.format( + "Expecting a JSON array of at least %d " + "elements", parameterName, value, variable.getMinElements())); variable.setValue(value);
/** * Verify that every non-optional variable has value assigned to it. * * @throws BatfishException when there exists a missing parameter: it is not optional in {@code * variable}, but the user failed to provide it. */ static void checkVariableState(Map<String, Variable> variables) throws BatfishException { for (Entry<String, Variable> e : variables.entrySet()) { String variableName = e.getKey(); Variable variable = e.getValue(); if (!variable.getOptional() && variable.getValue() == null) { throw new BatfishException(String.format("Missing parameter: %s", variableName)); } } }
@Test public void testSatisfiedMinLengthValue() throws IOException { String longString = "\"long enough\""; Variable variable = new Variable(); variable.setMinLength(8); variable.setType(STRING); Client.validateType(_mapper.readTree(longString), variable); }
@Test public void testEquals() throws IOException { Variable variable = new Variable(); variable.setType(Type.INTEGER); Variable initialInstance = clone(variable); EqualsTester equalsTester = new EqualsTester(); equalsTester.addEqualityGroup(initialInstance, initialInstance).addEqualityGroup(new Object()); variable.setDescription("description"); equalsTester.addEqualityGroup(clone(variable)); variable.setDisplayName("display name"); equalsTester.addEqualityGroup(clone(variable)); variable.setFields(ImmutableMap.of("f", new Field())); equalsTester.addEqualityGroup(clone(variable)); variable.setLongDescription("long description"); equalsTester.addEqualityGroup(clone(variable)); variable.setMinElements(1); equalsTester.addEqualityGroup(clone(variable)); variable.setMinLength(1); equalsTester.addEqualityGroup(clone(variable)); variable.setOptional(true); equalsTester.addEqualityGroup(clone(variable)); variable.setType(Type.BOOLEAN); equalsTester.addEqualityGroup(clone(variable)); variable.setValue(BooleanNode.TRUE); equalsTester.addEqualityGroup(clone(variable)); variable.setValues(ImmutableList.of()); equalsTester.addEqualityGroup(clone(variable)); equalsTester.testEquals(); }
String varName = e.getKey(); Variable variable = e.getValue(); JsonNode value = variable.getValue(); if (variable.getOptional()) { recursivelyRemoveOptionalVar(jobj, varName); if (variable.getType() == Variable.Type.QUESTION) { if (variable.getMinElements() != null) { if (!value.isArray()) { throw new IllegalArgumentException("Expecting JSON array for array type"); String varName = e.getKey(); Variable variable = e.getValue(); JsonNode value = variable.getValue(); String valueJsonString = new ObjectMapper().writeValueAsString(value); boolean stringType = variable.getType().getStringType(); boolean setType = variable.getMinElements() != null; if (value != null) { String topLevelVarNameRegex = Pattern.quote("\"${" + varName + "}\"");
@Test public void testValidateValidNode() throws IOException { String parameterName = "boolean"; JsonNode invalidNode = _mapper.readTree("false"); Variable variable = new Variable(); variable.setType(BOOLEAN); List<AllowedValue> allowedValues = ImmutableList.of(new AllowedValue("false", "description")); variable.setValues(allowedValues); Client.validateNode(invalidNode, variable, parameterName); }
@Test public void testSatisfiedMinElementInput() throws IOException { Map<String, JsonNode> parameters = new HashMap<>(); Map<String, Variable> variables = new HashMap<>(); String jsonArray = "[\"action1\", \"action2\", \"action3\", " + "\"action4\", \"action5\", \"action6\"]"; parameters.put("actions", _mapper.readTree(jsonArray)); Variable actionsVariable = new Variable(); actionsVariable.setType(STRING); actionsVariable.setMinElements(5); variables.put("actions", actionsVariable); Client.validateAndSet(parameters, variables); }
@Test public void testProvideNonOptionalParameterWithValue() throws IOException { Map<String, Variable> variables = new HashMap<>(); Variable integerVariable = new Variable(); integerVariable.setValue(_mapper.readTree("3")); variables.put("integer", integerVariable); Client.checkVariableState(variables); }
@Test public void testMissingOptionalParameterNoValue() { Map<String, Variable> variables = new HashMap<>(); Variable integerVariable = new Variable(); integerVariable.setOptional(true); Client.checkVariableState(variables); }
@Test public void testUnsatisfiedMinLengthValue() throws IOException { String shortString = "\"short\""; Variable variable = new Variable(); variable.setMinLength(8); _thrown.expect(BatfishException.class); _thrown.expectMessage(equalTo("Must be at least 8 characters in length")); Client.validateType(_mapper.readTree(shortString), variable); }
@Test public void testSerializationFields() throws JsonParseException, JsonMappingException, JsonProcessingException, IOException { Variable variable = new Variable(); String fieldName = "f1"; Field field = new Field(); field.setOptional(true); variable.setFields(ImmutableMap.of(fieldName, field)); // fields survive serialization cyle intact assertThat( BatfishObjectMapper.mapper() .readValue(BatfishObjectMapper.writeString(variable), Variable.class) .getFields() .get(fieldName) .getOptional(), equalTo(true)); }
int minLength = variable.getMinLength() == null ? 0 : variable.getMinLength(); if (value.isTextual() && value.textValue().length() < minLength) { throw new BatfishException( String.format("Must be at least %s characters in length", minLength)); Variable.Type expectedType = variable.getType(); switch (expectedType) { case ADDRESS_BOOK:
@Test public void testMissingNonOptionalParameterNoValue() { Map<String, Variable> variables = new HashMap<>(); Variable integerVariable = new Variable(); variables.put("integer", integerVariable); _thrown.expect(BatfishException.class); String errorMessage = "Missing parameter: integer"; _thrown.expectMessage(equalTo(errorMessage)); Client.checkVariableState(variables); }
@Test public void testValidateNodeNotAllowedValue() throws IOException { String parameterName = "boolean"; JsonNode invalidNode = _mapper.readTree("false"); Variable variable = new Variable(); variable.setType(BOOLEAN); List<AllowedValue> allowedValues = ImmutableList.of(new AllowedValue("true", "description")); variable.setValues(allowedValues); _thrown.expect(BatfishException.class); _thrown.expectMessage( String.format("Invalid value: false, allowed values are: %s", allowedValues)); Client.validateNode(invalidNode, variable, parameterName); }
@Test public void testUnsatisfiedMinElementInput() throws IOException { Map<String, JsonNode> parameters = new HashMap<>(); Map<String, Variable> variables = new HashMap<>(); JsonNode jsonArray = _mapper.readTree("[\"action1\", \"action2\"]"); parameters.put("actions", jsonArray); Variable actionsVariable = new Variable(); actionsVariable.setType(STRING); actionsVariable.setMinElements(5); variables.put("actions", actionsVariable); _thrown.expect(BatfishException.class); String errorMessage = String.format( "Invalid value for parameter actions: %s. Expecting a " + "JSON array of at least 5 elements", jsonArray); _thrown.expectMessage(equalTo(errorMessage)); Client.validateAndSet(parameters, variables); }
/** * Test that translating an InstanceData object to JSON and using that JSON to create a new * InstanceData object produces an object equal to the original instance */ @Test public void testJsonSerialization() throws IOException { InstanceData instanceData = new InstanceData(); instanceData.setInstanceName("instanceName"); instanceData.setDescription("The description"); instanceData.setLongDescription("The long description"); instanceData.setOrderedVariableNames(ImmutableList.of("b", "a")); instanceData.setTags(new TreeSet<>(Arrays.asList("tag1", "tag2"))); instanceData.setVariables(ImmutableSortedMap.of("v", new Variable())); assertThat(clone(instanceData), equalTo(instanceData)); } }