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  /*
17   * To change this template, choose Tools | Templates
18   * and open the template in the editor.
19   */
20  package org.kuali.student.contract.model.impl;
21  
22  import org.kuali.student.contract.model.MessageStructure;
23  import org.kuali.student.contract.model.Service;
24  import org.kuali.student.contract.model.ServiceContractModel;
25  import org.kuali.student.contract.model.ServiceMethod;
26  import org.kuali.student.contract.model.ServiceMethodParameter;
27  import org.kuali.student.contract.model.XmlType;
28  import org.kuali.student.contract.model.util.MessageStructureDumper;
29  import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
30  import org.kuali.student.validation.decorator.mojo.ValidationDecoratorWriterForOneService;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import java.util.ArrayList;
35  import java.util.Collection;
36  import java.util.List;
37  
38  import org.junit.After;
39  import org.junit.AfterClass;
40  import org.junit.Before;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  
44  import static org.junit.Assert.*;
45  
46  /**
47   *
48   * @author nwright
49   */
50  public class ServiceContractModelPescXsdLoaderTest {
51      
52      private static Logger log = LoggerFactory.getLogger(ServiceContractModelPescXsdLoaderTest.class);
53      
54  
55      public ServiceContractModelPescXsdLoaderTest() {
56      }
57  
58      @BeforeClass
59      public static void setUpClass() throws Exception {
60      }
61  
62      @AfterClass
63      public static void tearDownClass() throws Exception {
64      }
65  
66      @Before
67      public void setUp() {
68      }
69  
70      @After
71      public void tearDown() {
72      }
73      private static final String RESOURCES_DIRECTORY =
74              //                             "C:/svn/student/ks-core/ks-core-api/src/main/java";
75              "src/main/resources";
76      private static final String PESC_DIRECTORY =
77              RESOURCES_DIRECTORY + "/pesc";
78      private static final String PESC_CORE_MAIN = PESC_DIRECTORY + "/CoreMain.xsd";
79      private static final String PESC_ACAD_REC = PESC_DIRECTORY + "/AcademicRecord_v1.5.0.xsd";
80      private static final String PESC_COLL_TRANS = PESC_DIRECTORY + "/CollegeTranscript_v1.2.0.xsd";
81  
82      private ServiceContractModel getModel() {
83          List<String> xsdFileNames = new ArrayList();
84  //        xsdFileNames.add(PESC_CORE_MAIN);
85  //        xsdFileNames.add(PESC_ACAD_REC);
86          xsdFileNames.add(PESC_COLL_TRANS);
87          ServiceContractModel instance = new ServiceContractModelPescXsdLoader(xsdFileNames);
88          instance = new ServiceContractModelCache(instance);
89          validate(instance);
90          return instance;
91      }
92  
93      private String dump(ServiceMethod method) {
94          StringBuilder bldr = new StringBuilder();
95          bldr.append(method.getName());
96          String comma = "";
97          bldr.append("(");
98          for (ServiceMethodParameter param : method.getParameters()) {
99              bldr.append(comma);
100             comma = ", ";
101             bldr.append(param.getType());
102             bldr.append(" ");
103             bldr.append(param.getName());
104         }
105         bldr.append(")");
106         return bldr.toString();
107     }
108 
109     private void validate(ServiceContractModel model) {
110         Collection<String> errors =
111                 new ServiceContractModelValidator(model).validate();
112         if (errors.size() > 0) {
113             StringBuilder buf = new StringBuilder();
114             buf.append(errors.size()).append(" errors found while validating the data.");
115             int cnt = 0;
116             for (String msg : errors) {
117                 cnt++;
118                 buf.append("\n");
119                 buf.append("*error*").append(cnt).append(":").append(msg);
120             }
121 
122             fail(buf.toString());
123         }
124     }
125 
126     /**
127      * Test of getServiceMethods method, of class ServiceContractModelQDoxLoader.
128      */
129     @Test
130     public void testGetServiceMethods() {
131         log.info("getServiceMethods");
132         ServiceContractModel model = getModel();
133         List<ServiceMethod> result = model.getServiceMethods();
134         log.info("Number of methods=" + result.size());
135         for (ServiceMethod method : result) {
136             log.info(dump(method));
137         }
138         if (result.size() < 1) {
139             fail("too few: " + result.size());
140         }
141     }
142 
143     /**
144      * Test of getSourceNames method, of class ServiceContractModelQDoxLoader.
145      */
146     @Test
147     public void testGetSourceNames() {
148         log.info("getSourceNames");
149         ServiceContractModel model = getModel();
150         List<String> expResult = new ArrayList();
151         expResult.add(PESC_COLL_TRANS);
152         List result = model.getSourceNames();
153         assertEquals(expResult, result);
154     }
155 
156     /**
157      * Test of getServices method, of class ServiceContractModelQDoxLoader.
158      */
159     @Test
160     public void testGetServices() {
161         log.info("getServices");
162         ServiceContractModel model = getModel();
163         List<Service> result = model.getServices();
164         assertEquals(1, result.size());
165         for (Service service : result) {
166             log.info(service.getKey() + " " + service.getName() + " "
167                     + service.getVersion() + " " + service.getStatus()
168                     + " " + service.getComments()
169                     + " " + service.getUrl());
170         }
171     }
172 
173     /**
174      * Test of getXmlTypes method, of class ServiceContractModelQDoxLoader.
175      */
176     @Test
177     public void testGetXmlTypes() {
178         log.info("getXmlTypes");
179         ServiceContractModel model = getModel();
180         List<XmlType> result = model.getXmlTypes();
181         if (result.size() < 10) {
182             fail("too few: " + result.size());
183         }
184     }
185 
186     /**
187      * Test of getMessageStructures method, of class ServiceContractModelQDoxLoader.
188      */
189     @Test
190     public void testGetMessageStructures() {
191         log.info("getMessageStructures");
192         ServiceContractModel model = getModel();
193         List<MessageStructure> result = model.getMessageStructures();
194         if (result.size() < 10) {
195             fail("too few: " + result.size());
196         }
197         for (MessageStructure ms : result) {
198             new MessageStructureDumper(ms, System.out).dump();
199         }
200     }
201 }