FlyteInputsUtils { static Literals.LiteralMap fillParameterInInputs(Interface.ParameterMap parameterMap, String parameter) { var literalMapBuilder = Literals.LiteralMap.newBuilder(); parameterMap .getParametersMap() .forEach( (key, value) -> { if (!key.toLowerCase().equals(PARAMETER_NAME)) { if (value.hasDefault()) { literalMapBuilder.putLiterals(key, value.getDefault()); return; } var message = "LP inputs must have default values. Missing default value for key:" + key; LOG.error(message); throw new UnsupportedOperationException(message); } if (value.getVar().getType().getSimple() != DATETIME) { var message = PARAMETER_NAME + " should be of type DATETIME"; LOG.error(message); throw new IllegalArgumentException(message); } else { var literal = buildLiteralForPartition(parameter); literalMapBuilder.putLiterals(key, literal); } }); return literalMapBuilder.build(); } private  FlyteInputsUtils();   }
@Test public void shouldOnlyFillParameterInInputs() { var inputName = "key"; var inputValue = "value"; var parameterMap = Interface.ParameterMap.newBuilder() .putParameters(inputName, Interface.Parameter.newBuilder() .setVar(Interface.Variable.newBuilder() .setType(Types.LiteralType.newBuilder(). setSimple(Types.SimpleType.STRING) .build()) .build()) .setDefault(Literals.Literal.newBuilder() .setScalar(Literals.Scalar.newBuilder() .setPrimitive(Literals.Primitive.newBuilder().setStringValue(inputValue) .build()).build()) .build()) .build()) .build(); var inputs = fillParameterInInputs(parameterMap, PARAMETER); assertNull(inputs.getLiteralsMap() .get(PARAMETER_NAME)); assertThat(inputValue, equalTo(inputs.getLiteralsMap() .get(inputName) .getScalar() .getPrimitive() .getStringValue())); }
@Test public void shouldThrowExceptionIfInputIsRequired() { var parameterMap = Interface.ParameterMap.newBuilder() .putParameters(KEY, Interface.Parameter.newBuilder() .setVar(Interface.Variable.newBuilder() .setType(Types.LiteralType.newBuilder(). setSimple(Types.SimpleType.STRING) .build()) .build()) .setRequired(true) .build()) .build(); var exception = assertThrows( UnsupportedOperationException.class, () -> fillParameterInInputs(parameterMap, PARAMETER) ); assertThat(exception.getMessage(), equalTo(EXCEPTION_MESSAGE)); }
@Test public void shouldThrowExceptionIfInputIsNotRequired() { var parameterMap = Interface.ParameterMap.newBuilder() .putParameters(KEY, Interface.Parameter.newBuilder() .setVar(Interface.Variable.newBuilder() .setType(Types.LiteralType.newBuilder(). setSimple(Types.SimpleType.STRING) .build()) .build()) .setRequired(false) .build()) .build(); var exception = assertThrows( UnsupportedOperationException.class, () -> fillParameterInInputs(parameterMap, PARAMETER) ); assertThat(exception.getMessage(), equalTo(EXCEPTION_MESSAGE)); }
@Test public void shouldThrowExceptionIfInputHasNoDefault() { var parameterMap = Interface.ParameterMap.newBuilder() .putParameters(KEY, Interface.Parameter.newBuilder() .setVar(Interface.Variable.newBuilder() .setType(Types.LiteralType.newBuilder(). setSimple(Types.SimpleType.STRING) .build()) .build()) .build()) .build(); var exception = assertThrows( UnsupportedOperationException.class, () -> fillParameterInInputs(parameterMap, PARAMETER) ); assertThat(exception.getMessage(), equalTo(EXCEPTION_MESSAGE)); }
@Test public void shouldThrowExceptionIfParameterHasWrongType() { var parameterMap = Interface.ParameterMap.newBuilder() .putParameters(PARAMETER_NAME, Interface.Parameter.newBuilder() .setVar(Interface.Variable.newBuilder() .setType(Types.LiteralType.newBuilder(). setSimple(Types.SimpleType.STRING) .build()) .build()) .setDefault(Literals.Literal.newBuilder() .setScalar(Literals.Scalar.newBuilder() .setPrimitive(Literals.Primitive.newBuilder() .setStringValue("2020-09-15").build()) .build()) .build()) .build()) .build(); var exception = assertThrows( IllegalArgumentException.class, () -> fillParameterInInputs(parameterMap, PARAMETER) ); assertThat(exception.getMessage(), equalTo(PARAMETER_NAME + " should be of type DATETIME")); }