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