001    /*
002     * To change this template, choose Tools | Templates
003     * and open the template in the editor.
004     */
005    package org.kuali.student.contract.model.impl;
006    
007    import org.kuali.student.contract.model.MessageStructure;
008    import org.kuali.student.contract.model.Service;
009    import org.kuali.student.contract.model.ServiceContractModel;
010    import org.kuali.student.contract.model.ServiceMethod;
011    import org.kuali.student.contract.model.ServiceMethodParameter;
012    import org.kuali.student.contract.model.XmlType;
013    import org.kuali.student.contract.model.util.MessageStructureDumper;
014    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
015    import java.util.ArrayList;
016    import java.util.Collection;
017    import java.util.List;
018    import org.junit.After;
019    import org.junit.AfterClass;
020    import org.junit.Before;
021    import org.junit.BeforeClass;
022    import org.junit.Test;
023    import static org.junit.Assert.*;
024    
025    /**
026     *
027     * @author nwright
028     */
029    public class ServiceContractModelPescXsdLoaderTest {
030    
031        public ServiceContractModelPescXsdLoaderTest() {
032        }
033    
034        @BeforeClass
035        public static void setUpClass() throws Exception {
036        }
037    
038        @AfterClass
039        public static void tearDownClass() throws Exception {
040        }
041    
042        @Before
043        public void setUp() {
044        }
045    
046        @After
047        public void tearDown() {
048        }
049        private static final String RESOURCES_DIRECTORY =
050                //                             "C:/svn/student/ks-core/ks-core-api/src/main/java";
051                "src/main/resources";
052        private static final String PESC_DIRECTORY =
053                RESOURCES_DIRECTORY + "/pesc";
054        private static final String PESC_CORE_MAIN = PESC_DIRECTORY + "/CoreMain.xsd";
055        private static final String PESC_ACAD_REC = PESC_DIRECTORY + "/AcademicRecord_v1.5.0.xsd";
056        private static final String PESC_COLL_TRANS = PESC_DIRECTORY + "/CollegeTranscript_v1.2.0.xsd";
057    
058        private ServiceContractModel getModel() {
059            List<String> xsdFileNames = new ArrayList();
060    //        xsdFileNames.add(PESC_CORE_MAIN);
061    //        xsdFileNames.add(PESC_ACAD_REC);
062            xsdFileNames.add(PESC_COLL_TRANS);
063            ServiceContractModel instance = new ServiceContractModelPescXsdLoader(xsdFileNames);
064            instance = new ServiceContractModelCache(instance);
065            validate(instance);
066            return instance;
067        }
068    
069        private String dump(ServiceMethod method) {
070            StringBuilder bldr = new StringBuilder();
071            bldr.append(method.getName());
072            String comma = "";
073            bldr.append("(");
074            for (ServiceMethodParameter param : method.getParameters()) {
075                bldr.append(comma);
076                comma = ", ";
077                bldr.append(param.getType());
078                bldr.append(" ");
079                bldr.append(param.getName());
080            }
081            bldr.append(")");
082            return bldr.toString();
083        }
084    
085        private void validate(ServiceContractModel model) {
086            Collection<String> errors =
087                    new ServiceContractModelValidator(model).validate();
088            if (errors.size() > 0) {
089                StringBuilder buf = new StringBuilder();
090                buf.append(errors.size()).append(" errors found while validating the data.");
091                int cnt = 0;
092                for (String msg : errors) {
093                    cnt++;
094                    buf.append("\n");
095                    buf.append("*error*").append(cnt).append(":").append(msg);
096                }
097    
098                fail(buf.toString());
099            }
100        }
101    
102        /**
103         * Test of getServiceMethods method, of class ServiceContractModelQDoxLoader.
104         */
105        @Test
106        public void testGetServiceMethods() {
107            System.out.println("getServiceMethods");
108            ServiceContractModel model = getModel();
109            List<ServiceMethod> result = model.getServiceMethods();
110            System.out.println("Number of methods=" + result.size());
111            for (ServiceMethod method : result) {
112                System.out.println(dump(method));
113            }
114            if (result.size() < 1) {
115                fail("too few: " + result.size());
116            }
117        }
118    
119        /**
120         * Test of getSourceNames method, of class ServiceContractModelQDoxLoader.
121         */
122        @Test
123        public void testGetSourceNames() {
124            System.out.println("getSourceNames");
125            ServiceContractModel model = getModel();
126            List<String> expResult = new ArrayList();
127            expResult.add(PESC_COLL_TRANS);
128            List result = model.getSourceNames();
129            assertEquals(expResult, result);
130        }
131    
132        /**
133         * Test of getServices method, of class ServiceContractModelQDoxLoader.
134         */
135        @Test
136        public void testGetServices() {
137            System.out.println("getServices");
138            ServiceContractModel model = getModel();
139            List<Service> result = model.getServices();
140            assertEquals(1, result.size());
141            for (Service service : result) {
142                System.out.println(service.getKey() + " " + service.getName() + " "
143                        + service.getVersion() + " " + service.getStatus()
144                        + " " + service.getComments()
145                        + " " + service.getUrl());
146            }
147        }
148    
149        /**
150         * Test of getXmlTypes method, of class ServiceContractModelQDoxLoader.
151         */
152        @Test
153        public void testGetXmlTypes() {
154            System.out.println("getXmlTypes");
155            ServiceContractModel model = getModel();
156            List<XmlType> result = model.getXmlTypes();
157            if (result.size() < 10) {
158                fail("too few: " + result.size());
159            }
160        }
161    
162        /**
163         * Test of getMessageStructures method, of class ServiceContractModelQDoxLoader.
164         */
165        @Test
166        public void testGetMessageStructures() {
167            System.out.println("getMessageStructures");
168            ServiceContractModel model = getModel();
169            List<MessageStructure> result = model.getMessageStructures();
170            if (result.size() < 10) {
171                fail("too few: " + result.size());
172            }
173            for (MessageStructure ms : result) {
174                new MessageStructureDumper(ms, System.out).dump();
175            }
176        }
177    }