View Javadoc

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