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