source: dialogmanager/src/test/java/tudelft/dialogmanager/UpdateFunctionTest.java@ 568

Last change on this file since 568 was 116, checked in by wouter, 4 years ago

#49 fixed compile issue and all tests. NOTICE the dialogdemos still need fixing.

File size: 3.4 KB
RevLine 
[44]1package tudelft.dialogmanager;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.util.Arrays;
7import java.util.List;
8
9import org.junit.Before;
10import org.junit.Test;
11
12import com.fasterxml.jackson.annotation.JsonCreator;
[116]13import com.fasterxml.jackson.annotation.JsonProperty;
[44]14import com.fasterxml.jackson.core.JsonParseException;
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.databind.JsonMappingException;
17import com.fasterxml.jackson.databind.ObjectMapper;
18
19import tudelft.dialogmanager.parameters.DoubleValue;
[116]20import tudelft.dialogmanager.parameters.Parameters;
21import tudelft.dialogmanager.updatefunctions.UpdateFunction;
[44]22
23public 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]66class 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}
Note: See TracBrowser for help on using the repository browser.