View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.contract.model.impl;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.PrintStream;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Set;
25  import java.util.Stack;
26  
27  import org.junit.After;
28  import org.junit.AfterClass;
29  import org.junit.Before;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  
33  import static org.junit.Assert.*;
34  
35  import org.kuali.student.contract.model.MessageStructure;
36  import org.kuali.student.contract.model.Service;
37  import org.kuali.student.contract.model.ServiceContractModel;
38  import org.kuali.student.contract.model.ServiceMethod;
39  import org.kuali.student.contract.model.ServiceMethodParameter;
40  import org.kuali.student.contract.model.XmlType;
41  import org.kuali.student.contract.model.util.HtmlContractServiceWriter;
42  import org.kuali.student.contract.model.util.MessageStructureHierarchyDumper;
43  import org.kuali.student.contract.model.util.ModelFinder;
44  import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
45  import org.kuali.student.validation.decorator.mojo.ValidationDecoratorWriterForOneService;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   *
51   * @author nwright
52   */
53  public class ServiceContractModelQDoxLoaderTest {
54      
55      private static Logger log = LoggerFactory.getLogger(ServiceContractModelQDoxLoaderTest.class);
56      
57  
58      public ServiceContractModelQDoxLoaderTest() {
59      }
60  
61      @BeforeClass
62      public static void setUpClass() throws Exception {
63      }
64  
65      @AfterClass
66      public static void tearDownClass() throws Exception {
67      }
68  
69      @Before
70      public void setUp() {
71      }
72  
73      @After
74      public void tearDown() {
75      }
76  
77      private static final String RESOURCES_DIRECTORY = "src/test/resources";
78      private static final String TEST_SOURCE_DIRECTORY =
79              "src/test/java/org/kuali/student/contract/model/test/source";
80      private static final String ENROLL_PROJECT_SRC_MAIN = "C:/svn/ks-1.3/ks-enroll/ks-enroll-api/src/main";
81      private static final String ENROLL_PROJECT_JAVA_DIRECTORY = ENROLL_PROJECT_SRC_MAIN + "/java";
82      private static final String RICE_CORE_API_DIRECTORY = "C:/svn/rice/trunk/core/api/src/main/java";
83      private static final String RICE_KIM_API_DIRECTORY = "C:/svn/rice/trunk/kim/kim-api/src/main/java";
84      private static final String RICE_LOCATION_API_DIRECTORY = "C:/svn/rice/trunk/location/api/src/main/java";
85      private static final String RICE_KEW_API_DIRECTORY = "C:/svn/rice/trunk/kew/api/src/main/java";
86      private static final String RICE_KEN_API_DIRECTORY = "C:/svn/rice/trunk/ken/api/src/main/java";
87      private static final String RICE_KSB_API_DIRECTORY = "C:/svn/rice/trunk/ksb/api/src/main/java";
88      private static final String RICE_KRMS_API_DIRECTORY = "C:/svn/rice/trunk/krms/api/src/main/java";
89      private static ServiceContractModel model = null;
90      private ServiceContractModel getModel() {
91          if (model != null) {
92              return model;
93          }
94          List<String> srcDirs = new ArrayList();
95          log.info("User directory=" + System.getProperty("user.dir"));
96          log.info("Current directory=" + new File(".").getAbsolutePath());
97  //        srcDirs.add (ENROLL_PROJECT_JAVA_DIRECTORY);
98          srcDirs.add(TEST_SOURCE_DIRECTORY);
99  //        srcDirs.add(RICE_CORE_API_DIRECTORY); 
100 //        srcDirs.add(RICE_KIM_API_DIRECTORY); 
101 //        srcDirs.add(RICE_LOCATION_API_DIRECTORY); 
102 //        srcDirs.add(RICE_KEW_API_DIRECTORY); 
103 //        srcDirs.add(RICE_KEN_API_DIRECTORY); 
104 //        srcDirs.add(RICE_KSB_API_DIRECTORY); 
105 //        srcDirs.add(RICE_KRMS_API_DIRECTORY);     
106         boolean validateKualiStudent = false;
107         ServiceContractModel instance = new ServiceContractModelQDoxLoader(srcDirs, validateKualiStudent);
108         
109         instance = new ServiceContractModelCache(instance);
110         validate(instance);
111         model = instance;
112         return instance;
113     }
114 
115     private String dump(ServiceMethod method) {
116         StringBuilder bldr = new StringBuilder();
117         bldr.append(method.getName());
118         String comma = "";
119         bldr.append("(");
120         for (ServiceMethodParameter param : method.getParameters()) {
121             bldr.append(comma);
122             comma = ", ";
123             bldr.append(param.getType());
124             bldr.append(" ");
125             bldr.append(param.getName());
126         }
127         bldr.append(")");
128         return bldr.toString();
129     }
130 
131     private void validate(ServiceContractModel model) {
132         Collection<String> errors =
133                 new ServiceContractModelValidator(model).validate();
134         if (errors.size() > 0) {
135             StringBuilder buf = new StringBuilder();
136             buf.append(errors.size()).append(" errors found while validating the data.");
137             int cnt = 0;
138             for (String msg : errors) {
139                 cnt++;
140                 buf.append("\n");
141                 buf.append("*error*").append(cnt).append(":").append(msg);
142             }
143 
144             fail(buf.toString());
145         }
146     }
147 
148     /**
149      * Test of getServiceMethods method, of class ServiceContractModelQDoxLoader.
150      */
151     @Test
152     public void testGetServiceMethods() {
153         log.info("getServiceMethods");
154         ServiceContractModel model = getModel();
155         List<ServiceMethod> result = model.getServiceMethods();
156         log.info("Number of methods=" + result.size());
157         boolean getAtpFound = false;
158         for (ServiceMethod method : result) {
159             log.info(dump(method));
160             if (method.getName().equals("getAtp")) {
161                 getAtpFound = true;
162                 assertEquals ("this is an implementation note\nthis is another", method.getImplNotes());
163             }
164         }
165         assertTrue (getAtpFound);
166         if (result.size() < 10) {
167             fail("too few: " + result.size());
168         }
169         
170     }
171     
172 
173     /**
174      * Test of getSourceNames method, of class ServiceContractModelQDoxLoader.
175      */
176 //    @Test
177     public void testGetSourceNames() {
178         log.info("getSourceNames");
179         ServiceContractModel model = getModel();
180         List<String> expResult = new ArrayList();
181         expResult.add(TEST_SOURCE_DIRECTORY);
182         List result = model.getSourceNames();
183         assertEquals(expResult, result);
184     }
185 
186     /**
187      * Test of getServices method, of class ServiceContractModelQDoxLoader.
188      */
189 //    @Test
190     public void testGetServices() {
191         log.info("getServices");
192         ServiceContractModel model = getModel();
193         List<Service> result = model.getServices();
194         for (Service service : result) {
195             log.info(service.getKey() + " " + service.getName() + " "
196                     + service.getVersion() + " " + service.getStatus());
197         }
198         assertEquals(4, result.size());
199     }
200 
201     /**
202      * Test of getXmlTypes method, of class ServiceContractModelQDoxLoader.
203      */
204 //    @Test
205     public void testGetXmlTypes() {
206         log.info("getXmlTypes");
207         ServiceContractModel model = getModel();
208         List<XmlType> result = model.getXmlTypes();
209         for (XmlType xmlType : result) {
210             log.info("XmlType=" + xmlType.getName() + " "
211                     + xmlType.getPrimitive());
212         }
213         if (result.size() < 10) {
214             fail("too few: " + result.size());
215         }
216     }
217 
218     /**
219      * Test of getMessageStructures method, of class ServiceContractModelQDoxLoader.
220      */
221 //    @Test
222     public void testGetMessageStructures() throws FileNotFoundException {
223         log.info("getMessageStructures");
224         ServiceContractModel model = getModel();
225         List<MessageStructure> result = model.getMessageStructures();
226         for (MessageStructure ms : result) {
227             if (ms.getId().equalsIgnoreCase("academicCalendarInfo.typeKey")) {
228                 log.info("MessageStructure=" + ms.getId() + " " + ms.getType() + "required=["+ ms.getRequired() + "]");
229             }
230         }
231         if (result.size() < 10) {
232             fail("too few: " + result.size());
233         }
234         String outputFileName = "target/messageStructures.txt";
235         File file = new File(outputFileName);
236         PrintStream out = new PrintStream(file);
237         new MessageStructureHierarchyDumper(out, model).writeTabbedHeader();
238         Set<XmlType> rootTypes = HtmlContractServiceWriter.calcMainMessageStructures(
239                 model, null);
240         ModelFinder finder = new ModelFinder(model);
241         for (XmlType rootType : rootTypes) {
242             Stack<String> stack = new Stack();
243             stack.push(rootType.getName());
244             for (MessageStructure ms : finder.findMessageStructures(rootType.getName())) {
245                 new MessageStructureHierarchyDumper(out, model).writeTabbedData(ms, stack);
246             }
247         }
248     }
249 }