001/** 002 * Copyright 2004-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.student.contract.model.impl; 017 018import java.io.File; 019import java.io.FileNotFoundException; 020import java.io.PrintStream; 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.List; 024import java.util.Set; 025import java.util.Stack; 026 027import org.junit.After; 028import org.junit.AfterClass; 029import org.junit.Before; 030import org.junit.BeforeClass; 031import org.junit.Test; 032 033import static org.junit.Assert.*; 034 035import org.kuali.student.contract.model.MessageStructure; 036import org.kuali.student.contract.model.Service; 037import org.kuali.student.contract.model.ServiceContractModel; 038import org.kuali.student.contract.model.ServiceMethod; 039import org.kuali.student.contract.model.ServiceMethodParameter; 040import org.kuali.student.contract.model.XmlType; 041import org.kuali.student.contract.model.util.HtmlContractServiceWriter; 042import org.kuali.student.contract.model.util.MessageStructureHierarchyDumper; 043import org.kuali.student.contract.model.util.ModelFinder; 044import org.kuali.student.contract.model.validation.ServiceContractModelValidator; 045import org.kuali.student.validation.decorator.mojo.ValidationDecoratorWriterForOneService; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049/** 050 * 051 * @author nwright 052 */ 053public class ServiceContractModelQDoxLoaderTest { 054 055 private static Logger log = LoggerFactory.getLogger(ServiceContractModelQDoxLoaderTest.class); 056 057 058 public ServiceContractModelQDoxLoaderTest() { 059 } 060 061 @BeforeClass 062 public static void setUpClass() throws Exception { 063 } 064 065 @AfterClass 066 public static void tearDownClass() throws Exception { 067 } 068 069 @Before 070 public void setUp() { 071 } 072 073 @After 074 public void tearDown() { 075 } 076 077 private static final String RESOURCES_DIRECTORY = "src/test/resources"; 078 private static final String TEST_SOURCE_DIRECTORY = 079 "src/test/java/org/kuali/student/contract/model/test/source"; 080 081 private static final String KSAP_API_DIRECTORY = "D:\\svn\\ks\\trunk\\ks-api\\ks-ap-api\\src\\main\\java"; 082 private static final String ENROLL_PROJECT_SRC_MAIN = "C:/svn/ks-1.3/ks-enroll/ks-enroll-api/src/main"; 083 private static final String ENROLL_PROJECT_JAVA_DIRECTORY = ENROLL_PROJECT_SRC_MAIN + "/java"; 084 private static final String RICE_CORE_API_DIRECTORY = "C:/svn/rice/trunk/core/api/src/main/java"; 085 private static final String RICE_KIM_API_DIRECTORY = "C:/svn/rice/trunk/kim/kim-api/src/main/java"; 086 private static final String RICE_LOCATION_API_DIRECTORY = "C:/svn/rice/trunk/location/api/src/main/java"; 087 private static final String RICE_KEW_API_DIRECTORY = "C:/svn/rice/trunk/kew/api/src/main/java"; 088 private static final String RICE_KEN_API_DIRECTORY = "C:/svn/rice/trunk/ken/api/src/main/java"; 089 private static final String RICE_KSB_API_DIRECTORY = "C:/svn/rice/trunk/ksb/api/src/main/java"; 090 private static final String RICE_KRMS_API_DIRECTORY = "C:/svn/rice/trunk/krms/api/src/main/java"; 091 private static ServiceContractModel model = null; 092 private ServiceContractModel getModel() { 093 if (model != null) { 094 return model; 095 } 096 List<String> srcDirs = new ArrayList(); 097 System.out.println("User directory=" + System.getProperty("user.dir")); 098 System.out.println("Current directory=" + new File(".").getAbsolutePath()); 099// srcDirs.add (ENROLL_PROJECT_JAVA_DIRECTORY); 100 srcDirs.add(TEST_SOURCE_DIRECTORY); 101// srcDirs.add(KSAP_API_DIRECTORY); 102// srcDirs.add(RICE_CORE_API_DIRECTORY); 103// srcDirs.add(RICE_KIM_API_DIRECTORY); 104// srcDirs.add(RICE_LOCATION_API_DIRECTORY); 105// srcDirs.add(RICE_KEW_API_DIRECTORY); 106// srcDirs.add(RICE_KEN_API_DIRECTORY); 107// srcDirs.add(RICE_KSB_API_DIRECTORY); 108// srcDirs.add(RICE_KRMS_API_DIRECTORY); 109 ServiceContractModel instance = new ServiceContractModelQDoxLoader(srcDirs); 110 111 instance = new ServiceContractModelCache(instance); 112 validate(instance); 113 model = instance; 114 return instance; 115 } 116 117 private String dump(ServiceMethod method) { 118 StringBuilder bldr = new StringBuilder(); 119 bldr.append(method.getName()); 120 String comma = ""; 121 bldr.append("("); 122 for (ServiceMethodParameter param : method.getParameters()) { 123 bldr.append(comma); 124 comma = ", "; 125 bldr.append(param.getType()); 126 bldr.append(" "); 127 bldr.append(param.getName()); 128 } 129 bldr.append(")"); 130 return bldr.toString(); 131 } 132 133 private void validate(ServiceContractModel model) { 134 Collection<String> errors = 135 new ServiceContractModelValidator(model).validate(); 136 if (errors.size() > 0) { 137 StringBuilder buf = new StringBuilder(); 138 buf.append(errors.size()).append(" errors found while validating the data."); 139 int cnt = 0; 140 for (String msg : errors) { 141 cnt++; 142 buf.append("\n"); 143 buf.append("*error*").append(cnt).append(":").append(msg); 144 } 145 146 fail(buf.toString()); 147 } 148 } 149 150 /** 151 * Test of getServiceMethods method, of class ServiceContractModelQDoxLoader. 152 */ 153 @Test 154 public void testGetServiceMethods() { 155 System.out.println("getServiceMethods"); 156 ServiceContractModel model = getModel(); 157 List<ServiceMethod> result = model.getServiceMethods(); 158 System.out.println("Number of methods=" + result.size()); 159 boolean getAtpFound = false; 160 for (ServiceMethod method : result) { 161 System.out.println(dump(method)); 162 if (method.getName().equals("getAtp")) { 163 getAtpFound = true; 164 assertEquals ("this is an implementation note\nthis is another", method.getImplNotes()); 165 } 166 } 167 assertTrue (getAtpFound); 168 if (result.size() < 10) { 169 fail("too few: " + result.size()); 170 } 171 172 } 173 174 175 /** 176 * Test of getSourceNames method, of class ServiceContractModelQDoxLoader. 177 */ 178// @Test 179 public void testGetSourceNames() { 180 System.out.println("getSourceNames"); 181 ServiceContractModel model = getModel(); 182 List<String> expResult = new ArrayList(); 183 expResult.add(TEST_SOURCE_DIRECTORY); 184 List result = model.getSourceNames(); 185 assertEquals(expResult, result); 186 } 187 188 /** 189 * Test of getServices method, of class ServiceContractModelQDoxLoader. 190 */ 191// @Test 192 public void testGetServices() { 193 System.out.println("getServices"); 194 ServiceContractModel model = getModel(); 195 List<Service> result = model.getServices(); 196 for (Service service : result) { 197 System.out.println(service.getKey() + " " + service.getName() + " " 198 + service.getVersion() + " " + service.getStatus()); 199 } 200 assertEquals(4, result.size()); 201 } 202 203 /** 204 * Test of getXmlTypes method, of class ServiceContractModelQDoxLoader. 205 */ 206// @Test 207 public void testGetXmlTypes() { 208 System.out.println("getXmlTypes"); 209 ServiceContractModel model = getModel(); 210 List<XmlType> result = model.getXmlTypes(); 211 for (XmlType xmlType : result) { 212 System.out.println("XmlType=" + xmlType.getName() + " " 213 + xmlType.getPrimitive()); 214 } 215 if (result.size() < 10) { 216 fail("too few: " + result.size()); 217 } 218 } 219 220 /** 221 * Test of getMessageStructures method, of class ServiceContractModelQDoxLoader. 222 */ 223// @Test 224 public void testGetMessageStructures() throws FileNotFoundException { 225 System.out.println("getMessageStructures"); 226 ServiceContractModel model = getModel(); 227 List<MessageStructure> result = model.getMessageStructures(); 228 for (MessageStructure ms : result) { 229 if (ms.getId().equalsIgnoreCase("academicCalendarInfo.typeKey")) { 230 System.out.println("MessageStructure=" + ms.getId() + " " + ms.getType() + "required=["+ ms.getRequired() + "]"); 231 } 232 } 233 if (result.size() < 10) { 234 fail("too few: " + result.size()); 235 } 236 String outputFileName = "target/messageStructures.txt"; 237 File file = new File(outputFileName); 238 PrintStream out = new PrintStream(file); 239 new MessageStructureHierarchyDumper(out, model).writeTabbedHeader(); 240 Set<XmlType> rootTypes = HtmlContractServiceWriter.calcMainMessageStructures( 241 model, null); 242 ModelFinder finder = new ModelFinder(model); 243 for (XmlType rootType : rootTypes) { 244 Stack<String> stack = new Stack(); 245 stack.push(rootType.getName()); 246 for (MessageStructure ms : finder.findMessageStructures(rootType.getName())) { 247 new MessageStructureHierarchyDumper(out, model).writeTabbedData(ms, stack); 248 } 249 } 250 } 251}