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