1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.server.mvc.dto;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.kuali.student.common.ui.client.mvc.DataModel;
28 import org.kuali.student.common.ui.client.mvc.DataModelDefinition;
29 import org.kuali.student.common.ui.client.validator.DataModelValidator;
30 import org.kuali.student.core.assembly.data.ConstraintMetadata;
31 import org.kuali.student.core.assembly.data.Data;
32 import org.kuali.student.core.assembly.data.Metadata;
33 import org.kuali.student.core.assembly.data.QueryPath;
34 import org.kuali.student.core.assembly.data.Data.DataType;
35 import org.kuali.student.core.assembly.data.Data.IntegerKey;
36 import org.kuali.student.core.validation.dto.ValidationResultInfo;
37
38
39 public class TestDataModel {
40
41 DataModelDefinition rootModelDefinition;
42 DataModelDefinition formatModelDefinition;
43
44 @Before
45 public void setup(){
46
47
48 Metadata metaId = new Metadata();
49 metaId.setDataType(DataType.STRING);
50 ConstraintMetadata constraint = new ConstraintMetadata();
51 constraint.setMinLength(10);
52 constraint.setMaxLength(10);
53 metaId.getConstraints().add(constraint);
54
55
56 Metadata metaTitle = new Metadata();
57 metaTitle.setDataType(DataType.STRING);
58 metaTitle.getConstraints().add(constraint);
59 Metadata metaType = new Metadata();
60 metaType.setDataType(DataType.STRING);
61
62
63 Map<String, Metadata> formatProperties;
64 formatProperties = new HashMap<String, Metadata>();
65 formatProperties.put("title", metaTitle);
66 formatProperties.put("type", metaType);
67
68
69 Metadata metaFormat = new Metadata();
70 metaFormat.setProperties(formatProperties);
71 metaFormat.setDataType(DataType.DATA);
72
73
74 Metadata metaFormats = new Metadata();
75 Map<String, Metadata> formatListProperties = new HashMap<String, Metadata>();
76 formatListProperties.put("*", metaFormat);
77 metaFormats.setDataType(DataType.LIST);
78 metaFormats.setProperties(formatListProperties);
79
80
81 Metadata metadataRoot = new Metadata();
82 Map<String, Metadata> rootProperties = new HashMap<String, Metadata>();
83 rootProperties.put("formats", metaFormats);
84 rootProperties.put("id", metaId);
85 metadataRoot.setProperties(rootProperties);
86 metadataRoot.setDataType(DataType.DATA);
87
88 rootModelDefinition = new DataModelDefinition();
89 rootModelDefinition.setMetadata(metadataRoot);
90
91 formatModelDefinition = new DataModelDefinition();
92 formatModelDefinition.setMetadata(metaFormat);
93 }
94
95 @Test
96 public void testQueryWildpath(){
97 DataModel dataModel = new DataModel();
98 dataModel.setRoot(new Data());
99 dataModel.setDefinition(rootModelDefinition);
100
101 dataModel.set(QueryPath.parse("formats/0/title"), "Format 1");
102 dataModel.set(QueryPath.parse("formats/1/title"), "Format 2");
103
104 Map<QueryPath, Object> values = dataModel.query("formats");
105 Map<QueryPath, Object> formatValues = dataModel.query("formats/*");
106
107 assertTrue(values.size()==1);
108 assertTrue(formatValues.size()==2);
109 }
110
111 @Test
112 public void testDataModelValidator(){
113 DataModel dataModel = new DataModel();
114 List<ValidationResultInfo> validationResults;
115 DataModelValidator validator = new CustomDataModelValidator();
116
117
118 dataModel.setRoot(new Data());
119 dataModel.setDefinition(rootModelDefinition);
120
121
122 dataModel.set(QueryPath.parse("id"), "1234");
123 validationResults = validator.validate(dataModel);
124 assertEquals(1, validationResults.size());
125 assertEquals("id", validationResults.get(0).getElement());
126
127
128 dataModel.set(QueryPath.parse("id"), "0123456890");
129 validationResults = validator.validate(dataModel);
130 assertEquals(0, validationResults.size());
131
132
133 dataModel.set(QueryPath.parse("formats/0/title"), "Format 1");
134 validationResults = validator.validate(dataModel);
135 assertEquals(1, validationResults.size());
136
137 assertEquals("formats/0/title", validationResults.get(0).getElement());
138
139
140 Data data = dataModel.getRoot();
141 data = data.get("formats");
142 data = data.get(new IntegerKey(0));
143 dataModel.setRoot(data);
144 dataModel.setDefinition(formatModelDefinition);
145 dataModel.setParentPath("formats/0");
146
147 validationResults = validator.validate(dataModel);
148 assertEquals(1, validationResults.size());
149
150 assertEquals("title", validationResults.get(0).getElement());
151 }
152
153
154
155
156
157
158
159 public class CustomDataModelValidator extends DataModelValidator {
160 @Override
161 protected String getValidationMessage(String msgKey) {
162 return msgKey;
163 }
164 }
165 }