Skip to content

Commit

Permalink
#666 Test and fix reading nested objects from request and JSON with v…
Browse files Browse the repository at this point in the history
…alidation
  • Loading branch information
alessiostalla committed Oct 12, 2023
1 parent 2747546 commit eeaf6ae
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 87 deletions.
9 changes: 5 additions & 4 deletions elements/src/main/java/com/manydesigns/elements/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public interface Element extends XhtmlFragment {
public static final String copyright =
"Copyright (C) 2005-2020 ManyDesigns srl";

public void readFromRequest(HttpServletRequest req);
public boolean validate();
public void readFromObject(Object obj);
public void writeToObject(Object obj);
void readFromRequest(HttpServletRequest req);
boolean validate();
boolean isValid();
void readFromObject(Object obj);
void writeToObject(Object obj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public boolean validate() {
return result;
}


public boolean isValid() {
for (T current : this) {
if (!current.isValid()) {
return false;
}
}
return true;
}

public void readFromObject(Object obj) {
for (T current : this) {
current.readFromObject(obj);
Expand Down Expand Up @@ -118,5 +128,4 @@ public Collection<Field> fields() {
}
return fields;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,19 @@ public ObjectField(@NotNull PropertyAccessor accessor, @NotNull Mode mode) {

@Override
public boolean validate() {
errors.clear();
if (isRequired() && value == null) {
errors.add(getText("elements.error.field.required"));
return false;
}
return form.validate();
}

@Override
public boolean isValid() {
return super.isValid() && form.isValid();
}

@Override
public void writeToObject(Object obj) {
form.writeToObject(accessor.get(obj));
Expand Down Expand Up @@ -94,6 +104,7 @@ public void readFrom(KeyValueAccessor keyValueAccessor) {
return;
}
if(!keyValueAccessor.has(accessor.getName())) {
setValue(null);
return;
}
bulkChecked = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,13 @@ protected void appendToSearchString(StringBuilder sb, String name, String value,
throw new RuntimeException(e);
}
}

public boolean validate() {
return true;
}

@Override
public boolean isValid() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ public void readFromRequest(HttpServletRequest req) {
}
}

public boolean validate() {
return true;
}

public void toXhtml(@NotNull XhtmlBuffer xb) {
xb.openElement("div");
xb.addAttribute("class", "form-group boolean-search-field");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.NotNull;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;

/*
* @author Paolo Predonzani - [email protected]
Expand All @@ -54,8 +55,9 @@ public EncryptedTextSearchField(PropertyAccessor accessor, String prefix, String

try {
Class<?> clazz = Class.forName(classPath);
encrypter = (FieldEncrypter)clazz.newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
encrypter = (FieldEncrypter)clazz.getConstructor().newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
InvocationTargetException | NoSuchMethodException e) {
logger.error(e.getMessage(),e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ protected Object readValue(String parameter, Class type) {
}
}

public boolean validate() {
return true;
}

public void toSearchString(StringBuilder sb, String encoding) {
if (minStringValue != null) {
appendToSearchString(sb, minInputName, minStringValue, encoding);
Expand Down Expand Up @@ -180,7 +176,7 @@ protected boolean isGreaterThan(Object v1, Object v2) {
return false;
}
}

//**************************************************************************
// Getters/setters
//**************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ public void readFromRequest(HttpServletRequest req) {
}
}

public boolean validate() {
return true;
}

public void toXhtml(@NotNull XhtmlBuffer xb) {
xb.openElement("div");
xb.addAttribute("class", "form-group");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ public void readFromRequest(HttpServletRequest req) {
}
}

public boolean validate() {
return true;
}

public void toSearchString(StringBuilder sb, String encoding) {
if (value != null) {
appendToSearchString(sb, inputName, value, encoding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public boolean validate() {
return result;
}

@Override
public boolean isValid() {
for (Row row : rows) {
if (!row.isValid()) {
return false;
}
}
return true;
}

public void readFromObject(Object obj) {
Class clazz = obj.getClass();
if (clazz.isArray()) { // Tratta obj come un array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ public void writeToObject(Object obj) {
public void toXhtml(@NotNull XhtmlBuffer xb) {
}

@Override
public boolean isValid() {
return true;
}

public Mode getMode() {
return null;
}

public void setMode(Mode mode) {
}
public void setMode(Mode mode) {}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2013 ManyDesigns srl. All rights reserved.
* Copyright (C) 2005-2023 ManyDesigns srl. All rights reserved.
* http://www.manydesigns.com/
*
* This is free software; you can redistribute it and/or modify it
Expand All @@ -22,11 +22,14 @@

import com.manydesigns.elements.AbstractElementsTest;
import com.manydesigns.elements.Mode;
import com.manydesigns.elements.json.JsonKeyValueAccessor;
import com.manydesigns.elements.reflection.ClassAccessor;
import com.manydesigns.elements.reflection.JavaClassAccessor;
import com.manydesigns.elements.reflection.PropertyAccessor;
import com.manydesigns.elements.servlet.MutableHttpServletRequest;
import com.manydesigns.elements.util.Util;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand All @@ -42,7 +45,7 @@
@Test
public class ObjectFieldTest extends AbstractElementsTest {
public static final String copyright =
"Copyright (c) 2005-2013, ManyDesigns srl";
"Copyright (c) 2005-2023, ManyDesigns srl";

ClassAccessor classAccessor;
PropertyAccessor myPropertyAccessor;
Expand All @@ -57,10 +60,39 @@ public void setUp() throws Exception {
myPropertyAccessor = classAccessor.getProperty("nested");
}

public void testReadFromJSON() {
var field = new ObjectField(myPropertyAccessor, Mode.EDIT);

field.setRequired(true);
assertTrue(field.isRequired());

//--------------------------------------------------------------------------
// Required
//--------------------------------------------------------------------------
NestedBean n1 = new NestedBean("n1", true);
field.setValue(n1);
assertEquals(n1, field.getValue());

JSONObject jsonObject = new JSONObject();
JsonKeyValueAccessor keyValueAccessor = new JsonKeyValueAccessor(jsonObject);
field.readFrom(keyValueAccessor);
field.validate();
assertFalse(field.isValid());
assertNull(field.getValue());

jsonObject.put("nested", new JSONObject());
field.readFrom(keyValueAccessor);
field.validate();
assertTrue(field.isValid());
assertFalse(((NestedBean) field.getValue()).flag);

jsonObject.getJSONObject("nested").put("flag", true);
field.readFrom(keyValueAccessor);
field.validate();
assertTrue(field.isValid());
assertTrue(((NestedBean) field.getValue()).flag);

jsonObject.getJSONObject("nested").put("flag", false);
field.readFrom(keyValueAccessor);
assertFalse(((NestedBean) field.getValue()).flag);
}

public void testRequiredEdit() {
var field = new ObjectField(myPropertyAccessor, Mode.EDIT);
Expand Down Expand Up @@ -92,18 +124,25 @@ public void testRequiredReadFromRequest() {
field.setRequired(true);
assertTrue(field.isRequired());

MutableHttpServletRequest req = new MutableHttpServletRequest();

NestedBean n1 = new NestedBean("n1", true);
field.setValue(n1);
assertEquals(n1, field.getValue());

field.readFromRequest(req);
field.validate();
assertTrue(field.isValid());
assertTrue(((NestedBean) field.getValue()).flag);

req.setParameter("nested.flag", "");
field.readFromRequest(req);
field.validate();
assertTrue(field.isValid());
assertFalse(((NestedBean) field.getValue()).flag);

req.setParameter("nested.flag", "true");
field.readFromRequest(req);
field.validate();
assertTrue(field.isValid());
assertTrue(((NestedBean) field.getValue()).flag);

req.setParameter("nested.flag", "false");
Expand All @@ -122,64 +161,33 @@ public void testRequiredReadFromRequest() {
assertTrue(((NestedBean) field.getValue()).flag);
}

/*
//--------------------------------------------------------------------------
// Not required
//--------------------------------------------------------------------------
public void testNotRequiredEdit() {
var field = new ObjectField(myPropertyAccessor, Mode.EDIT);
assertEquals(Mode.EDIT, field.getMode());

public void testNotRequiredEdit() {
booleanField = new BooleanField(myPropertyAccessor, Mode.EDIT);
assertEquals(Mode.EDIT, booleanField.getMode());
field.setRequired(false);
assertFalse(field.isRequired());

booleanField.setRequired(false);
assertFalse(booleanField.isRequired());
assertNull(field.getValue());
String text = Util.elementToString(field);

assertNull(booleanField.getValue());
String text = Util.elementToString(booleanField);
assertEquals("<div class=\"form-group readwrite no-value\">" +
"<label for=\"myBoolean\" class=\"control-label\">My boolean</label>" +
"<div>" +
"<select id=\"myBoolean\" class=\"form-control\" name=\"myBoolean\">" +
"<option selected=\"selected\"></option>" +
"<option value=\"true\">Yes</option>" +
"<option value=\"false\">No</option>" +
"</select>" +
"</div>" +
"</div>", text);
booleanField.setValue(false);
assertFalse(booleanField.getValue());
text = Util.elementToString(booleanField);
assertEquals("<div class=\"form-group readwrite no-value\"><label for=\"nested\" class=\"control-label\">Nested</label><div><fieldset class=\"mde-columns-1\"><div class=\"row\"><div class=\"col-md-12 mde-colspan-1\"><div class=\"form-group readwrite no-value\"><label for=\"nested.name\" class=\"control-label\">Name</label><div><input id=\"nested.name\" type=\"text\" name=\"nested.name\" class=\"form-control\" /></div></div></div></div><div class=\"row\"><div class=\"col-md-12 mde-colspan-1\"><div class=\"form-group readwrite no-value required\"><label for=\"nested.flag\" class=\"control-label\">Flag</label><div><div class=\"checkbox\"><input id=\"nested.flag\" type=\"checkbox\" name=\"nested.flag\" value=\"true\" /><label for=\"nested.flag\"></label><input type=\"hidden\" name=\"__checkbox_nested.flag\" value=\"true\" /></div></div></div></div></div></fieldset></div></div>", text);

assertEquals("<div class=\"form-group readwrite\">" +
"<label for=\"myBoolean\" class=\"control-label\">My boolean</label>" +
"<div>" +
"<select id=\"myBoolean\" class=\"form-control\" name=\"myBoolean\">" +
"<option></option>" +
"<option value=\"true\">Yes</option>" +
"<option value=\"false\" selected=\"selected\">No</option>" +
"</select>" +
"</div>" +
"</div>", text);
NestedBean n1 = new NestedBean("n1", false);
field.setValue(n1);
assertEquals(n1, field.getValue());
text = Util.elementToString(field);

assertEquals("<div class=\"form-group readwrite no-value\"><label for=\"nested\" class=\"control-label\">Nested</label><div><fieldset class=\"mde-columns-1\"><div class=\"row\"><div class=\"col-md-12 mde-colspan-1\"><div class=\"form-group readwrite\"><label for=\"nested.name\" class=\"control-label\">Name</label><div><input id=\"nested.name\" type=\"text\" name=\"nested.name\" value=\"n1\" class=\"form-control\" /></div></div></div></div><div class=\"row\"><div class=\"col-md-12 mde-colspan-1\"><div class=\"form-group readwrite required\"><label for=\"nested.flag\" class=\"control-label\">Flag</label><div><div class=\"checkbox\"><input id=\"nested.flag\" type=\"checkbox\" name=\"nested.flag\" value=\"true\" /><label for=\"nested.flag\"></label><input type=\"hidden\" name=\"__checkbox_nested.flag\" value=\"true\" /></div></div></div></div></div></fieldset></div></div>", text);

booleanField.setValue(true);
assertTrue(booleanField.getValue());
text = Util.elementToString(booleanField);
assertEquals("<div class=\"form-group readwrite\">" +
"<label for=\"myBoolean\" class=\"control-label\">My boolean</label>" +
"<div>" +
"<select id=\"myBoolean\" class=\"form-control\" name=\"myBoolean\">" +
"<option></option>" +
"<option value=\"true\" selected=\"selected\">Yes</option>" +
"<option value=\"false\">No</option>" +
"</select>" +
"</div>" +
"</div>", text);
n1 = new NestedBean("n1", true);
field.setValue(n1);
assertEquals(n1, field.getValue());
text = Util.elementToString(field);

}
assertEquals("<div class=\"form-group readwrite no-value\"><label for=\"nested\" class=\"control-label\">Nested</label><div><fieldset class=\"mde-columns-1\"><div class=\"row\"><div class=\"col-md-12 mde-colspan-1\"><div class=\"form-group readwrite\"><label for=\"nested.name\" class=\"control-label\">Name</label><div><input id=\"nested.name\" type=\"text\" name=\"nested.name\" value=\"n1\" class=\"form-control\" /></div></div></div></div><div class=\"row\"><div class=\"col-md-12 mde-colspan-1\"><div class=\"form-group readwrite required\"><label for=\"nested.flag\" class=\"control-label\">Flag</label><div><div class=\"checkbox\"><input id=\"nested.flag\" type=\"checkbox\" name=\"nested.flag\" value=\"true\" checked=\"checked\" /><label for=\"nested.flag\"></label><input type=\"hidden\" name=\"__checkbox_nested.flag\" value=\"true\" /></div></div></div></div></div></fieldset></div></div>", text);
}
/*
public void testNotRequiredPreview() {
booleanField = new BooleanField(myPropertyAccessor, Mode.PREVIEW);
Expand All @@ -197,8 +205,6 @@ public void testNotRequiredReadFromRequest() {
booleanField.setRequired(false);
assertFalse(booleanField.isRequired());
MutableHttpServletRequest req = new MutableHttpServletRequest();
booleanField.setValue(true);
assertTrue(booleanField.getValue());
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<joda-time.version>2.9.1</joda-time.version>
<jsp.api.version>2.3.0</jsp.api.version>
<jsqlparser.version>0.9.7</jsqlparser.version>
<json.version>20180813</json.version>
<json.version>20230618</json.version>
<jsoup.version>1.15.3</jsoup.version>
<jxl.version>2.6.12</jxl.version>
<liquibase.version>4.8.0</liquibase.version>
Expand Down

0 comments on commit eeaf6ae

Please sign in to comment.