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.annotation.JsonProperty; 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.Parameters; import tudelft.dialogmanager.updatefunctions.UpdateFunction; public class UpdateFunctionTest { private final ObjectMapper jackson = new ObjectMapper(); private final String serialized = "{\"AddFunction\":[\"a\",\"b\",\"sum\"]}"; private final AddFunction testf = new AddFunction("a", "b", "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() { Parameters params = new Parameters(); params = params.with("a", new DoubleValue(3d)); params = params.with("b", new DoubleValue(4d)); assertEquals(new DoubleValue(7d), testf.call(params).get("sum")); } } /** * Function that sets result= arg1+arg2 == arg3 (a boolean value). We register * this function with the deserializer (see main class above) so that the json * file can use "EqualsFunction" to refer to this. */ class AddFunction implements UpdateFunction { private final String param1; private final String param2; private final String sumparam; @JsonCreator public AddFunction(@JsonProperty("param1") String param1, @JsonProperty("param2") String param2, @JsonProperty("sumparam") String sumparam) { this.param1 = param1; this.param2 = param2; this.sumparam = sumparam; } @Override public Parameters call(Parameters inparams) { return inparams.with(sumparam, new DoubleValue( inparams.getDouble(param1) + inparams.getDouble(param2))); } @Override public List getAssignedParameters() { return Arrays.asList(sumparam); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((param1 == null) ? 0 : param1.hashCode()); result = prime * result + ((param2 == null) ? 0 : param2.hashCode()); result = prime * result + ((sumparam == null) ? 0 : sumparam.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; AddFunction other = (AddFunction) obj; if (param1 == null) { if (other.param1 != null) return false; } else if (!param1.equals(other.param1)) return false; if (param2 == null) { if (other.param2 != null) return false; } else if (!param2.equals(other.param2)) return false; if (sumparam == null) { if (other.sumparam != null) return false; } else if (!sumparam.equals(other.sumparam)) return false; return true; } }