package tudelft.dialogmanager; import static org.junit.Assert.assertEquals; import java.io.IOException; import java.util.Arrays; import java.util.List; import org.junit.Before; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import tudelft.dialogmanager.parameters.DoubleValue; import tudelft.dialogmanager.parameters.ParameterValue; public class UpdateFunctionTest { private final ObjectMapper jackson = new ObjectMapper(); private final String serialized = "{\"AddFunction\":[[\"a\",\"b\"],[\"sum\"]]}";; private final AddFunction testf = new AddFunction( Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("sum"))); @Before public void before() { jackson.registerSubtypes(AddFunction.class); } @Test public void testSerialize() throws JsonProcessingException { String text = jackson.writeValueAsString(testf); System.out.println(text); assertEquals(serialized, text); } @Test public void testDeserialize() throws JsonParseException, JsonMappingException, IOException { UpdateFunction func = jackson.readValue(serialized, UpdateFunction.class); assertEquals(testf, func); } @Test public void testFunctionCall() { DoubleValue a = new DoubleValue(3d); DoubleValue b = new DoubleValue(4d); DoubleValue sum = new DoubleValue(7d); assertEquals(Arrays.asList(sum), testf.call(Arrays.asList(a, b))); } } /** * Function that computes sum of argumets */ class AddFunction extends UpdateFunction { @JsonCreator public AddFunction(List> inout) { super(inout); } @Override public List call(List inargs) { Double a = ((DoubleValue) inargs.get(0)).getValue(); Double b = ((DoubleValue) inargs.get(1)).getValue(); return Arrays.asList(new DoubleValue(a + b)); } }