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  package org.kuali.student.lum.ui;
16  
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.fail;
19  
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.OutputStream;
24  import java.io.PrintStream;
25  import java.util.ArrayList;
26  import java.util.Date;
27  import java.util.HashSet;
28  import java.util.LinkedHashMap;
29  import java.util.LinkedHashSet;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  
34  import org.junit.Test;
35  import org.kuali.student.r1.common.assembly.data.Metadata;
36  import org.kuali.student.r1.common.assembly.dictionary.MetadataFormatter;
37  import org.kuali.student.r1.common.assembly.dictionary.MetadataServiceImpl;
38  import org.kuali.student.r1.common.dictionary.service.DictionaryService;
39  import org.kuali.student.r1.common.dictionary.service.impl.DictionaryServiceImpl;
40  import org.kuali.student.r1.core.proposal.dto.ProposalInfo;
41  import org.kuali.student.r2.core.statement.dto.ReqCompFieldInfo;
42  import org.kuali.student.r2.core.statement.dto.ReqComponentInfo;
43  import org.kuali.student.r2.core.statement.dto.StatementInfo;
44  import org.kuali.student.r2.lum.course.dto.CourseInfo;
45  import org.kuali.student.r2.lum.program.dto.MajorDisciplineInfo;
46  import org.kuali.student.r2.lum.program.dto.ProgramRequirementInfo;
47  
48  public class TestMetadataServiceDictionary {
49  
50  	@Test
51  	public void testMetadataService() {
52  		Set<String> startingClasses = new LinkedHashSet<String>();
53  		Map<String, Set<String>> types = new LinkedHashMap<String, Set<String>>();
54  		startingClasses.add(CourseInfo.class.getName());
55  		startingClasses.add(MajorDisciplineInfo.class.getName());
56  		startingClasses.add(ProgramRequirementInfo.class.getName());
57  		startingClasses.add(ProposalInfo.class.getName());
58  		startingClasses.add(StatementInfo.class.getName());
59  		startingClasses.add(ReqComponentInfo.class.getName());
60  		startingClasses.add(ReqCompFieldInfo.class.getName());
61  		startingClasses.add("cluset");
62  		startingClasses.add("courseSet");
63  		startingClasses.add("programSet");
64  		startingClasses.add("testSet");
65  		startingClasses.add("search");
66  		startingClasses.add("browse");
67  
68  		// startingClasses.add (StatementTreeViewInfo.class);
69  
70  		Set<String> typesForClass = new LinkedHashSet<String>();
71  		types.put(ReqCompFieldInfo.class.getName(), typesForClass);
72  		typesForClass.add("kuali.reqComponent.field.type.gpa");
73  		typesForClass.add("kuali.reqComponent.field.type.operator");
74  		typesForClass.add("kuali.reqComponent.field.type.clu.id");
75  		typesForClass.add("kuali.reqComponent.field.type.course.clu.id");
76  		typesForClass.add("kuali.reqComponent.field.type.program.clu.id");
77  		typesForClass.add("kuali.reqComponent.field.type.test.clu.id");
78  		typesForClass.add("kuali.reqComponent.field.type.test.score");
79  		typesForClass.add("kuali.reqComponent.field.type.cluSet.id");
80  		typesForClass.add("kuali.reqComponent.field.type.course.cluSet.id");
81  		typesForClass.add("kuali.reqComponent.field.type.program.cluSet.id");
82  		typesForClass.add("kuali.reqComponent.field.type.test.cluSet.id");
83  		typesForClass.add("kuali.reqComponent.field.type.person.id");
84  		typesForClass.add("kuali.reqComponent.field.type.org.id");
85  		typesForClass
86  				.add("kuali.reqComponent.field.type.value.positive.integer");
87  		typesForClass.add("kuali.reqComponent.field.type.gradeType.id");
88  		typesForClass.add("kuali.reqComponent.field.type.grade.id");
89  		typesForClass.add("kuali.reqComponent.field.type.durationType.id");
90  		typesForClass.add("kuali.reqComponent.field.type.duration");
91  
92  		DictionaryService courseDictService = new DictionaryServiceImpl(
93  				"classpath:ks-courseInfo-dictionary-context.xml");
94  		DictionaryService progDictService = new DictionaryServiceImpl(
95  				"classpath:ks-programInfo-dictionary-context.xml");
96  		DictionaryService cluSetDictService = new DictionaryServiceImpl(
97  				"classpath:ks-cluSetInfo-dictionary-context.xml");
98  		
99  		for (String objType : cluSetDictService.getObjectTypes()) {
100 			System.out.println("Cluset has object type=" + objType);
101 		}
102 		
103 		DictionaryService proposalDictService = new DictionaryServiceImpl(
104 				"classpath:ks-proposalInfo-dictionary-context.xml");
105 		DictionaryService statementDictService = new DictionaryServiceImpl(
106 				"classpath:ks-statement-dictionary-context.xml");
107 		MetadataServiceImpl metadataService = new MetadataServiceImpl(
108 				courseDictService, progDictService, cluSetDictService,
109 				proposalDictService, statementDictService);
110 		metadataService
111 				.setUiLookupContext("classpath:lum-ui-lookup-context.xml");
112 		
113 		List<String> errors = new ArrayList<String>();
114 		for (String className : startingClasses) {
115 			String outFile = "target/metadata-for-" + className + ".txt";
116 			File file = new File(outFile);
117 			file.getParentFile().mkdirs();
118 			OutputStream outputStream = null;
119 			try {
120 				outputStream = new FileOutputStream(file, false);
121 			} catch (FileNotFoundException ex) {
122 				throw new RuntimeException(ex);
123 			}
124 			PrintStream out = new PrintStream(outputStream);
125 			out.println("(!) This page was automatically generated on "	+ new Date());
126 			out.println("DO NOT UPDATE MANUALLY!");
127 			out.println("");
128 			out.println("This page represents a formatted view of the lum ui dictionary for " + className);
129 			out.println("");
130 			out.println("----");
131 			out.println("{toc}");
132 			out.println("----");
133 			// out.println ("getting meta data for " + className);
134 			Metadata metadata = metadataService.getMetadata(className);
135 			assertNotNull(metadata);
136 			errors.addAll(this.validateMetadata(metadata, className, null));
137 			MetadataFormatter formatter = new MetadataFormatter(className,
138 					metadata, null, null, new HashSet<Metadata>(), 1);
139 			out.println(formatter.formatForWiki());
140 			if (types.get(className) == null) {
141 				continue;
142 			}
143 			for (String type : types.get(className)) {
144 				System.out.println("*** Generating formatted version for " + type);
145 				metadata = metadataService.getMetadata(className, type,
146 						(String) null);
147 				assertNotNull(metadata);
148 				errors.addAll(this.validateMetadata(metadata, className, type));
149 				formatter = new MetadataFormatter(className, metadata, type,
150 						null, new HashSet<Metadata>(), 1);
151 				out.println(formatter.formatForWiki());
152 			}
153 			out.close();
154 		}
155 		if (errors.size() > 0) {
156 			for (String error : errors) {
157 				System.out.println("error: " + error);
158 			}
159 			System.out.println(errors.size() + " errors found when validating metadata");
160 
161 			// These first 6 are because the recursion stops but the final field still is flagged as DATA even though it cannot have sub fields
162 
163 			//error: org.kuali.student.r2.lum.course.dto.CourseInfo.courseSpecificLOs.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
164 			//error: org.kuali.student.lum.program.dto.MajorDisciplineInfo.variations.*.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
165 			//error: org.kuali.student.lum.program.dto.MajorDisciplineInfo.orgCoreProgram.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
166 			//error: org.kuali.student.lum.program.dto.MajorDisciplineInfo.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
167 			//error: org.kuali.student.lum.program.dto.ProgramRequirementInfo.statement.statements.*.statements.*.statements.* is of type DATA but it has no properties
168 			//error: org.kuali.student.lum.program.dto.ProgramRequirementInfo.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
169 			
170 			// 33 errors found when validating metadata
171 			if (errors.size() != 33) {
172 				fail(errors.size() + " errors found when validating metadata");
173 			}
174 		}
175 	}
176 
177 	private MetadataServiceDictionaryValidator validator = null;
178 
179 	private MetadataServiceDictionaryValidator getValidator() {
180 		if (validator == null) {
181 			validator = new MetadataServiceDictionaryValidator();
182 		}
183 		return validator;
184 	}
185 
186 	private List<String> validateMetadata(Metadata md, String name, String type) {
187 		return getValidator().validateMetadata(md, name, type);
188 	}
189 }