Clover Coverage Report - KS Common 1.1 (Aggregated)
Coverage timestamp: Sun Mar 6 2011 20:59:55 EST
68   165   4   17
0   104   0.06   2
4     1  
2    
 
  TestDataModel       Line # 39 67 0% 3 0 100% 1.0
  TestDataModel.CustomDataModelValidator       Line # 159 1 0% 1 0 100% 1.0
 
  (2)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
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  2 toggle @Before
45    public void setup(){
46   
47    //Create course properties
48  2 Metadata metaId = new Metadata();
49  2 metaId.setDataType(DataType.STRING);
50  2 ConstraintMetadata constraint = new ConstraintMetadata();
51  2 constraint.setMinLength(10);
52  2 constraint.setMaxLength(10);
53  2 metaId.getConstraints().add(constraint);
54   
55    //Create format fields
56  2 Metadata metaTitle = new Metadata();
57  2 metaTitle.setDataType(DataType.STRING);
58  2 metaTitle.getConstraints().add(constraint);
59  2 Metadata metaType = new Metadata();
60  2 metaType.setDataType(DataType.STRING);
61   
62    //Create format field properties
63  2 Map<String, Metadata> formatProperties;
64  2 formatProperties = new HashMap<String, Metadata>();
65  2 formatProperties.put("title", metaTitle);
66  2 formatProperties.put("type", metaType);
67   
68    //Create format metadata
69  2 Metadata metaFormat = new Metadata();
70  2 metaFormat.setProperties(formatProperties);
71  2 metaFormat.setDataType(DataType.DATA);
72   
73    //Create format list metadata
74  2 Metadata metaFormats = new Metadata();
75  2 Map<String, Metadata> formatListProperties = new HashMap<String, Metadata>();
76  2 formatListProperties.put("*", metaFormat);
77  2 metaFormats.setDataType(DataType.LIST);
78  2 metaFormats.setProperties(formatListProperties);
79   
80    //Create root metadata structure
81  2 Metadata metadataRoot = new Metadata();
82  2 Map<String, Metadata> rootProperties = new HashMap<String, Metadata>();
83  2 rootProperties.put("formats", metaFormats);
84  2 rootProperties.put("id", metaId);
85  2 metadataRoot.setProperties(rootProperties);
86  2 metadataRoot.setDataType(DataType.DATA);
87   
88  2 rootModelDefinition = new DataModelDefinition();
89  2 rootModelDefinition.setMetadata(metadataRoot);
90   
91  2 formatModelDefinition = new DataModelDefinition();
92  2 formatModelDefinition.setMetadata(metaFormat);
93    }
94   
 
95  1 toggle @Test
96    public void testQueryWildpath(){
97  1 DataModel dataModel = new DataModel();
98  1 dataModel.setRoot(new Data());
99  1 dataModel.setDefinition(rootModelDefinition);
100   
101  1 dataModel.set(QueryPath.parse("formats/0/title"), "Format 1");
102  1 dataModel.set(QueryPath.parse("formats/1/title"), "Format 2");
103   
104  1 Map<QueryPath, Object> values = dataModel.query("formats");
105  1 Map<QueryPath, Object> formatValues = dataModel.query("formats/*");
106   
107  1 assertTrue(values.size()==1);
108  1 assertTrue(formatValues.size()==2);
109    }
110   
 
111  1 toggle @Test
112    public void testDataModelValidator(){
113  1 DataModel dataModel = new DataModel();
114  1 List<ValidationResultInfo> validationResults;
115  1 DataModelValidator validator = new CustomDataModelValidator();
116   
117    //Validation test for full Data Model
118  1 dataModel.setRoot(new Data());
119  1 dataModel.setDefinition(rootModelDefinition);
120   
121    //Test invalid id
122  1 dataModel.set(QueryPath.parse("id"), "1234");
123  1 validationResults = validator.validate(dataModel);
124  1 assertEquals(1, validationResults.size());
125  1 assertEquals("id", validationResults.get(0).getElement());
126   
127    //Test valid id
128  1 dataModel.set(QueryPath.parse("id"), "0123456890");
129  1 validationResults = validator.validate(dataModel);
130  1 assertEquals(0, validationResults.size());
131   
132    //Test invalid format title
133  1 dataModel.set(QueryPath.parse("formats/0/title"), "Format 1");
134  1 validationResults = validator.validate(dataModel);
135  1 assertEquals(1, validationResults.size());
136    //validation path should begin from full data model
137  1 assertEquals("formats/0/title", validationResults.get(0).getElement());
138   
139    //Validation test for partial data model (i.e. child data element)
140  1 Data data = dataModel.getRoot();
141  1 data = data.get("formats");
142  1 data = data.get(new IntegerKey(0));
143  1 dataModel.setRoot(data);
144  1 dataModel.setDefinition(formatModelDefinition);
145  1 dataModel.setParentPath("formats/0");
146   
147  1 validationResults = validator.validate(dataModel);
148  1 assertEquals(1, validationResults.size());
149    //validation path should be relative to the child element
150  1 assertEquals("title", validationResults.get(0).getElement());
151    }
152   
153    /**
154    * Extending DataModelValidator to override call to Application.getApplicationContext()
155    * for messages, since ApplicationContext does a GWT.create() which can't be called from
156    * JUnit tests.
157    *
158    */
 
159    public class CustomDataModelValidator extends DataModelValidator {
 
160  3 toggle @Override
161    protected String getValidationMessage(String msgKey) {
162  3 return msgKey;
163    }
164    }
165    }