[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;
|
---|
[116] | 13 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
[44] | 14 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 15 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 16 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 17 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 18 |
|
---|
| 19 | import tudelft.dialogmanager.parameters.DoubleValue;
|
---|
[116] | 20 | import tudelft.dialogmanager.parameters.Parameters;
|
---|
| 21 | import tudelft.dialogmanager.updatefunctions.UpdateFunction;
|
---|
[44] | 22 |
|
---|
| 23 | public class UpdateFunctionTest {
|
---|
| 24 |
|
---|
| 25 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
[116] | 26 | private final String serialized = "{\"AddFunction\":[\"a\",\"b\",\"sum\"]}";
|
---|
| 27 | private final AddFunction testf = new AddFunction("a", "b", "sum");
|
---|
[44] | 28 |
|
---|
| 29 | @Before
|
---|
| 30 | public void before() {
|
---|
| 31 | jackson.registerSubtypes(AddFunction.class);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | @Test
|
---|
| 35 | public void testSerialize() throws JsonProcessingException {
|
---|
| 36 |
|
---|
| 37 | String text = jackson.writeValueAsString(testf);
|
---|
| 38 | System.out.println(text);
|
---|
| 39 |
|
---|
| 40 | assertEquals(serialized, text);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Test
|
---|
| 44 | public void testDeserialize()
|
---|
| 45 | throws JsonParseException, JsonMappingException, IOException {
|
---|
| 46 | UpdateFunction func = jackson.readValue(serialized,
|
---|
| 47 | UpdateFunction.class);
|
---|
| 48 | assertEquals(testf, func);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | @Test
|
---|
| 52 | public void testFunctionCall() {
|
---|
[116] | 53 | Parameters params = new Parameters();
|
---|
| 54 | params = params.with("a", new DoubleValue(3d));
|
---|
| 55 | params = params.with("b", new DoubleValue(4d));
|
---|
| 56 | assertEquals(new DoubleValue(7d), testf.call(params).get("sum"));
|
---|
[44] | 57 | }
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
[116] | 62 | * Function that sets result= arg1+arg2 == arg3 (a boolean value). We register
|
---|
| 63 | * this function with the deserializer (see main class above) so that the json
|
---|
| 64 | * file can use "EqualsFunction" to refer to this.
|
---|
[44] | 65 | */
|
---|
[116] | 66 | class AddFunction implements UpdateFunction {
|
---|
[44] | 67 |
|
---|
[116] | 68 | private final String param1;
|
---|
| 69 | private final String param2;
|
---|
| 70 | private final String sumparam;
|
---|
| 71 |
|
---|
[44] | 72 | @JsonCreator
|
---|
[116] | 73 | public AddFunction(@JsonProperty("param1") String param1,
|
---|
| 74 | @JsonProperty("param2") String param2,
|
---|
| 75 | @JsonProperty("sumparam") String sumparam) {
|
---|
| 76 | this.param1 = param1;
|
---|
| 77 | this.param2 = param2;
|
---|
| 78 | this.sumparam = sumparam;
|
---|
[44] | 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
[116] | 82 | public Parameters call(Parameters inparams) {
|
---|
| 83 | return inparams.with(sumparam, new DoubleValue(
|
---|
| 84 | inparams.getDouble(param1) + inparams.getDouble(param2)));
|
---|
[44] | 85 | }
|
---|
| 86 |
|
---|
[116] | 87 | @Override
|
---|
| 88 | public List<String> getAssignedParameters() {
|
---|
| 89 | return Arrays.asList(sumparam);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | @Override
|
---|
| 93 | public int hashCode() {
|
---|
| 94 | final int prime = 31;
|
---|
| 95 | int result = 1;
|
---|
| 96 | result = prime * result + ((param1 == null) ? 0 : param1.hashCode());
|
---|
| 97 | result = prime * result + ((param2 == null) ? 0 : param2.hashCode());
|
---|
| 98 | result = prime * result
|
---|
| 99 | + ((sumparam == null) ? 0 : sumparam.hashCode());
|
---|
| 100 | return result;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | @Override
|
---|
| 104 | public boolean equals(Object obj) {
|
---|
| 105 | if (this == obj)
|
---|
| 106 | return true;
|
---|
| 107 | if (obj == null)
|
---|
| 108 | return false;
|
---|
| 109 | if (getClass() != obj.getClass())
|
---|
| 110 | return false;
|
---|
| 111 | AddFunction other = (AddFunction) obj;
|
---|
| 112 | if (param1 == null) {
|
---|
| 113 | if (other.param1 != null)
|
---|
| 114 | return false;
|
---|
| 115 | } else if (!param1.equals(other.param1))
|
---|
| 116 | return false;
|
---|
| 117 | if (param2 == null) {
|
---|
| 118 | if (other.param2 != null)
|
---|
| 119 | return false;
|
---|
| 120 | } else if (!param2.equals(other.param2))
|
---|
| 121 | return false;
|
---|
| 122 | if (sumparam == null) {
|
---|
| 123 | if (other.sumparam != null)
|
---|
| 124 | return false;
|
---|
| 125 | } else if (!sumparam.equals(other.sumparam))
|
---|
| 126 | return false;
|
---|
| 127 | return true;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | }
|
---|