[44] | 1 | package tudelft.dialogmanager;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 |
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.util.Arrays;
|
---|
| 7 | import java.util.List;
|
---|
| 8 |
|
---|
| 9 | import org.junit.Before;
|
---|
| 10 | import org.junit.Test;
|
---|
| 11 |
|
---|
| 12 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 13 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 14 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 15 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 17 |
|
---|
| 18 | import tudelft.dialogmanager.parameters.DoubleValue;
|
---|
| 19 | import tudelft.dialogmanager.parameters.ParameterValue;
|
---|
| 20 |
|
---|
| 21 | public class UpdateFunctionTest {
|
---|
| 22 |
|
---|
| 23 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 24 | private final String serialized = "{\"AddFunction\":[[\"a\",\"b\"],[\"sum\"]]}";;
|
---|
| 25 | private final AddFunction testf = new AddFunction(
|
---|
| 26 | Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("sum")));
|
---|
| 27 |
|
---|
| 28 | @Before
|
---|
| 29 | public void before() {
|
---|
| 30 | jackson.registerSubtypes(AddFunction.class);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | @Test
|
---|
| 34 | public void testSerialize() throws JsonProcessingException {
|
---|
| 35 |
|
---|
| 36 | String text = jackson.writeValueAsString(testf);
|
---|
| 37 | System.out.println(text);
|
---|
| 38 |
|
---|
| 39 | assertEquals(serialized, text);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Test
|
---|
| 43 | public void testDeserialize()
|
---|
| 44 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 45 | UpdateFunction func = jackson.readValue(serialized,
|
---|
| 46 | UpdateFunction.class);
|
---|
| 47 | assertEquals(testf, func);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | @Test
|
---|
| 51 | public void testFunctionCall() {
|
---|
| 52 | DoubleValue a = new DoubleValue(3d);
|
---|
| 53 | DoubleValue b = new DoubleValue(4d);
|
---|
| 54 | DoubleValue sum = new DoubleValue(7d);
|
---|
| 55 | assertEquals(Arrays.asList(sum), testf.call(Arrays.asList(a, b)));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Function that computes sum of argumets
|
---|
| 62 | */
|
---|
| 63 | class AddFunction extends UpdateFunction {
|
---|
| 64 |
|
---|
| 65 | @JsonCreator
|
---|
| 66 | public AddFunction(List<List<String>> inout) {
|
---|
| 67 | super(inout);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | @Override
|
---|
| 71 | public List<ParameterValue> call(List<ParameterValue> inargs) {
|
---|
| 72 | Double a = ((DoubleValue) inargs.get(0)).getValue();
|
---|
| 73 | Double b = ((DoubleValue) inargs.get(1)).getValue();
|
---|
| 74 | return Arrays.asList(new DoubleValue(a + b));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | } |
---|