View Javadoc

1   /*
2    * Copyright 2011 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.osedu.org/licenses/ECL-2.0
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  import org.junit.After;
27  import org.junit.AfterClass;
28  import org.junit.Before;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import static org.junit.Assert.*;
32  
33  import org.kuali.student.contract.model.MessageStructure;
34  import org.kuali.student.contract.model.Service;
35  import org.kuali.student.contract.model.ServiceContractModel;
36  import org.kuali.student.contract.model.ServiceMethod;
37  import org.kuali.student.contract.model.ServiceMethodParameter;
38  import org.kuali.student.contract.model.XmlType;
39  import org.kuali.student.contract.model.util.HtmlContractServiceWriter;
40  import org.kuali.student.contract.model.util.MessageStructureHierarchyDumper;
41  import org.kuali.student.contract.model.util.ModelFinder;
42  import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
43  
44  /**
45   *
46   * @author nwright
47   */
48  public class ServiceContractModelQDoxLoaderTest {
49  
50      public ServiceContractModelQDoxLoaderTest() {
51      }
52  
53      @BeforeClass
54      public static void setUpClass() throws Exception {
55      }
56  
57      @AfterClass
58      public static void tearDownClass() throws Exception {
59      }
60  
61      @Before
62      public void setUp() {
63      }
64  
65      @After
66      public void tearDown() {
67      }
68  
69      private static final String RESOURCES_DIRECTORY = "src/test/resources";
70      private static final String TEST_SOURCE_DIRECTORY =
71              "src/test/java/org/kuali/student/contract/model/test/source";
72      private static final String ENROLL_PROJECT_SRC_MAIN = "C:/svn/ks-1.3/ks-enroll/ks-enroll-api/src/main";
73      private static final String ENROLL_PROJECT_JAVA_DIRECTORY = ENROLL_PROJECT_SRC_MAIN + "/java";
74      private static final String RICE_CORE_API_DIRECTORY = "C:/svn/rice/trunk/core/api/src/main/java";
75      private static final String RICE_KIM_API_DIRECTORY = "C:/svn/rice/trunk/kim/kim-api/src/main/java";
76      private static final String RICE_LOCATION_API_DIRECTORY = "C:/svn/rice/trunk/location/api/src/main/java";
77      private static final String RICE_KEW_API_DIRECTORY = "C:/svn/rice/trunk/kew/api/src/main/java";
78      private static final String RICE_KEN_API_DIRECTORY = "C:/svn/rice/trunk/ken/api/src/main/java";
79      private static final String RICE_KSB_API_DIRECTORY = "C:/svn/rice/trunk/ksb/api/src/main/java";
80      private static final String RICE_KRMS_API_DIRECTORY = "C:/svn/rice/trunk/krms/api/src/main/java";
81      private static ServiceContractModel model = null;
82      private ServiceContractModel getModel() {
83          if (model != null) {
84              return model;
85          }
86          List<String> srcDirs = new ArrayList();
87          System.out.println("User directory=" + System.getProperty("user.dir"));
88          System.out.println("Current directory=" + new File(".").getAbsolutePath());
89  //        srcDirs.add (ENROLL_PROJECT_JAVA_DIRECTORY);
90          srcDirs.add(TEST_SOURCE_DIRECTORY);
91  //        srcDirs.add(RICE_CORE_API_DIRECTORY); 
92  //        srcDirs.add(RICE_KIM_API_DIRECTORY); 
93  //        srcDirs.add(RICE_LOCATION_API_DIRECTORY); 
94  //        srcDirs.add(RICE_KEW_API_DIRECTORY); 
95  //        srcDirs.add(RICE_KEN_API_DIRECTORY); 
96  //        srcDirs.add(RICE_KSB_API_DIRECTORY); 
97  //        srcDirs.add(RICE_KRMS_API_DIRECTORY);     
98          boolean validateKualiStudent = false;
99          ServiceContractModel instance = new ServiceContractModelQDoxLoader(srcDirs, validateKualiStudent);
100         
101         instance = new ServiceContractModelCache(instance);
102         validate(instance);
103         model = instance;
104         return instance;
105     }
106 
107     private String dump(ServiceMethod method) {
108         StringBuilder bldr = new StringBuilder();
109         bldr.append(method.getName());
110         String comma = "";
111         bldr.append("(");
112         for (ServiceMethodParameter param : method.getParameters()) {
113             bldr.append(comma);
114             comma = ", ";
115             bldr.append(param.getType());
116             bldr.append(" ");
117             bldr.append(param.getName());
118         }
119         bldr.append(")");
120         return bldr.toString();
121     }
122 
123     private void validate(ServiceContractModel model) {
124         Collection<String> errors =
125                 new ServiceContractModelValidator(model).validate();
126         if (errors.size() > 0) {
127             StringBuilder buf = new StringBuilder();
128             buf.append(errors.size()).append(" errors found while validating the data.");
129             int cnt = 0;
130             for (String msg : errors) {
131                 cnt++;
132                 buf.append("\n");
133                 buf.append("*error*").append(cnt).append(":").append(msg);
134             }
135 
136             fail(buf.toString());
137         }
138     }
139 
140     /**
141      * Test of getServiceMethods method, of class ServiceContractModelQDoxLoader.
142      */
143     @Test
144     public void testGetServiceMethods() {
145         System.out.println("getServiceMethods");
146         ServiceContractModel model = getModel();
147         List<ServiceMethod> result = model.getServiceMethods();
148         System.out.println("Number of methods=" + result.size());
149         boolean getAtpFound = false;
150         for (ServiceMethod method : result) {
151             System.out.println(dump(method));
152             if (method.getName().equals("getAtp")) {
153                 getAtpFound = true;
154                 assertEquals ("this is an implementation note\nthis is another", method.getImplNotes());
155             }
156         }
157         assertTrue (getAtpFound);
158         if (result.size() < 10) {
159             fail("too few: " + result.size());
160         }
161         
162     }
163     
164 
165     /**
166      * Test of getSourceNames method, of class ServiceContractModelQDoxLoader.
167      */
168 //    @Test
169     public void testGetSourceNames() {
170         System.out.println("getSourceNames");
171         ServiceContractModel model = getModel();
172         List<String> expResult = new ArrayList();
173         expResult.add(TEST_SOURCE_DIRECTORY);
174         List result = model.getSourceNames();
175         assertEquals(expResult, result);
176     }
177 
178     /**
179      * Test of getServices method, of class ServiceContractModelQDoxLoader.
180      */
181 //    @Test
182     public void testGetServices() {
183         System.out.println("getServices");
184         ServiceContractModel model = getModel();
185         List<Service> result = model.getServices();
186         for (Service service : result) {
187             System.out.println(service.getKey() + " " + service.getName() + " "
188                     + service.getVersion() + " " + service.getStatus());
189         }
190         assertEquals(4, result.size());
191     }
192 
193     /**
194      * Test of getXmlTypes method, of class ServiceContractModelQDoxLoader.
195      */
196 //    @Test
197     public void testGetXmlTypes() {
198         System.out.println("getXmlTypes");
199         ServiceContractModel model = getModel();
200         List<XmlType> result = model.getXmlTypes();
201         for (XmlType xmlType : result) {
202             System.out.println("XmlType=" + xmlType.getName() + " "
203                     + xmlType.getPrimitive());
204         }
205         if (result.size() < 10) {
206             fail("too few: " + result.size());
207         }
208     }
209 
210     /**
211      * Test of getMessageStructures method, of class ServiceContractModelQDoxLoader.
212      */
213 //    @Test
214     public void testGetMessageStructures() throws FileNotFoundException {
215         System.out.println("getMessageStructures");
216         ServiceContractModel model = getModel();
217         List<MessageStructure> result = model.getMessageStructures();
218         for (MessageStructure ms : result) {
219             if (ms.getId().equalsIgnoreCase("academicCalendarInfo.typeKey")) {
220                 System.out.println("MessageStructure=" + ms.getId() + " " + ms.getType() + "required=["+ ms.getRequired() + "]");
221             }
222         }
223         if (result.size() < 10) {
224             fail("too few: " + result.size());
225         }
226         String outputFileName = "target/messageStructures.txt";
227         File file = new File(outputFileName);
228         PrintStream out = new PrintStream(file);
229         new MessageStructureHierarchyDumper(out, model).writeTabbedHeader();
230         Set<XmlType> rootTypes = HtmlContractServiceWriter.calcMainMessageStructures(
231                 model, null);
232         ModelFinder finder = new ModelFinder(model);
233         for (XmlType rootType : rootTypes) {
234             Stack<String> stack = new Stack();
235             stack.push(rootType.getName());
236             for (MessageStructure ms : finder.findMessageStructures(rootType.getName())) {
237                 new MessageStructureHierarchyDumper(out, model).writeTabbedData(ms, stack);
238             }
239         }
240     }
241 }