View Javadoc

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  	@Before
45  	public void setup(){
46  		
47  		//Create course properties
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  		//Create format fields				
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  		//Create format field properties
63  		Map<String, Metadata> formatProperties;
64  		formatProperties = new HashMap<String, Metadata>();					
65  		formatProperties.put("title", metaTitle);		
66  		formatProperties.put("type", metaType);
67  		
68  		//Create format metadata
69  		Metadata metaFormat = new Metadata();
70  		metaFormat.setProperties(formatProperties);
71  		metaFormat.setDataType(DataType.DATA);
72  		
73  		//Create format list metadata
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  		//Create root metadata structure
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 		//Validation test for full Data Model
118 		dataModel.setRoot(new Data());
119 		dataModel.setDefinition(rootModelDefinition);
120 	
121 		//Test invalid id
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 		//Test valid id
128 		dataModel.set(QueryPath.parse("id"), "0123456890");
129 		validationResults = validator.validate(dataModel);		
130 		assertEquals(0, validationResults.size());
131 		
132 		//Test invalid format title
133 		dataModel.set(QueryPath.parse("formats/0/title"), "Format 1");
134 		validationResults = validator.validate(dataModel);		
135 		assertEquals(1, validationResults.size());
136 		//validation path should begin from full data model
137 		assertEquals("formats/0/title", validationResults.get(0).getElement());		
138 		
139 		//Validation test for partial data model (i.e. child data element)
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 		//validation path should be relative to the child element 
150 		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 		@Override
161 		protected String getValidationMessage(String msgKey) {
162 			return msgKey;
163 		}		
164 	}
165 }