View Javadoc
1   /**
2    * Copyright 2004-2014 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      
81      private static final String KSAP_API_DIRECTORY = "D:\\svn\\ks\\trunk\\ks-api\\ks-ap-api\\src\\main\\java";
82      private static final String ENROLL_PROJECT_SRC_MAIN = "C:/svn/ks-1.3/ks-enroll/ks-enroll-api/src/main";
83      private static final String ENROLL_PROJECT_JAVA_DIRECTORY = ENROLL_PROJECT_SRC_MAIN + "/java";
84      private static final String RICE_CORE_API_DIRECTORY = "C:/svn/rice/trunk/core/api/src/main/java";
85      private static final String RICE_KIM_API_DIRECTORY = "C:/svn/rice/trunk/kim/kim-api/src/main/java";
86      private static final String RICE_LOCATION_API_DIRECTORY = "C:/svn/rice/trunk/location/api/src/main/java";
87      private static final String RICE_KEW_API_DIRECTORY = "C:/svn/rice/trunk/kew/api/src/main/java";
88      private static final String RICE_KEN_API_DIRECTORY = "C:/svn/rice/trunk/ken/api/src/main/java";
89      private static final String RICE_KSB_API_DIRECTORY = "C:/svn/rice/trunk/ksb/api/src/main/java";
90      private static final String RICE_KRMS_API_DIRECTORY = "C:/svn/rice/trunk/krms/api/src/main/java";
91      private static ServiceContractModel model = null;
92      private ServiceContractModel getModel() {
93          if (model != null) {
94              return model;
95          }
96          List<String> srcDirs = new ArrayList();
97          System.out.println("User directory=" + System.getProperty("user.dir"));
98          System.out.println("Current directory=" + new File(".").getAbsolutePath());
99  //        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 }