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.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  		//Create course properties
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  		//Create format fields				
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  		//Create format field properties
64  		Map<String, Metadata> formatProperties;
65  		formatProperties = new HashMap<String, Metadata>();					
66  		formatProperties.put("title", metaTitle);		
67  		formatProperties.put("type", metaType);
68  		
69  		//Create format metadata
70  		Metadata metaFormat = new Metadata();
71  		metaFormat.setProperties(formatProperties);
72  		metaFormat.setDataType(DataType.DATA);
73  		
74  		//Create format list metadata
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  		//Create root metadata structure
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 		//Validation test for full Data Model
119 		dataModel.setRoot(new Data());
120 		dataModel.setDefinition(rootModelDefinition);
121 	
122 		//Test invalid id
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 		//Test valid id
129 		dataModel.set(QueryPath.parse("id"), "0123456890");
130 		validationResults = validator.validate(dataModel);		
131 		assertEquals(0, validationResults.size());
132 		
133 		//Test invalid format title
134 		dataModel.set(QueryPath.parse("formats/0/title"), "Format 1");
135 		validationResults = validator.validate(dataModel);		
136 		assertEquals(1, validationResults.size());
137 		//validation path should begin from full data model
138 		assertEquals("formats/0/title", validationResults.get(0).getElement());		
139 		
140 		//Validation test for partial data model (i.e. child data element)
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 		//validation path should be relative to the child element 
151 		assertEquals("title", validationResults.get(0).getElement());		
152 	}
153 
154 	/**
155 	 * Extending DataModelValidator to override call to Application.getApplicationContext()
156 	 * for messages, since ApplicationContext does a GWT.create() which can't be called from
157 	 * JUnit tests.
158 	 *
159 	 */
160 	public class CustomDataModelValidator extends DataModelValidator {
161 		@Override
162 		protected String getValidationMessage(String msgKey) {
163 			return msgKey;
164 		}		
165 	}
166 }