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 org.kuali.student.validation.decorator.mojo.ValidationDecoratorWriterForOneService;
016    import org.slf4j.Logger;
017    import org.slf4j.LoggerFactory;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    
023    import org.junit.After;
024    import org.junit.AfterClass;
025    import org.junit.Before;
026    import org.junit.BeforeClass;
027    import org.junit.Test;
028    
029    import static org.junit.Assert.*;
030    
031    /**
032     *
033     * @author nwright
034     */
035    public class ServiceContractModelPescXsdLoaderTest {
036        
037        private static Logger log = LoggerFactory.getLogger(ServiceContractModelPescXsdLoaderTest.class);
038        
039    
040        public ServiceContractModelPescXsdLoaderTest() {
041        }
042    
043        @BeforeClass
044        public static void setUpClass() throws Exception {
045        }
046    
047        @AfterClass
048        public static void tearDownClass() throws Exception {
049        }
050    
051        @Before
052        public void setUp() {
053        }
054    
055        @After
056        public void tearDown() {
057        }
058        private static final String RESOURCES_DIRECTORY =
059                //                             "C:/svn/student/ks-core/ks-core-api/src/main/java";
060                "src/main/resources";
061        private static final String PESC_DIRECTORY =
062                RESOURCES_DIRECTORY + "/pesc";
063        private static final String PESC_CORE_MAIN = PESC_DIRECTORY + "/CoreMain.xsd";
064        private static final String PESC_ACAD_REC = PESC_DIRECTORY + "/AcademicRecord_v1.5.0.xsd";
065        private static final String PESC_COLL_TRANS = PESC_DIRECTORY + "/CollegeTranscript_v1.2.0.xsd";
066    
067        private ServiceContractModel getModel() {
068            List<String> xsdFileNames = new ArrayList();
069    //        xsdFileNames.add(PESC_CORE_MAIN);
070    //        xsdFileNames.add(PESC_ACAD_REC);
071            xsdFileNames.add(PESC_COLL_TRANS);
072            ServiceContractModel instance = new ServiceContractModelPescXsdLoader(xsdFileNames);
073            instance = new ServiceContractModelCache(instance);
074            validate(instance);
075            return instance;
076        }
077    
078        private String dump(ServiceMethod method) {
079            StringBuilder bldr = new StringBuilder();
080            bldr.append(method.getName());
081            String comma = "";
082            bldr.append("(");
083            for (ServiceMethodParameter param : method.getParameters()) {
084                bldr.append(comma);
085                comma = ", ";
086                bldr.append(param.getType());
087                bldr.append(" ");
088                bldr.append(param.getName());
089            }
090            bldr.append(")");
091            return bldr.toString();
092        }
093    
094        private void validate(ServiceContractModel model) {
095            Collection<String> errors =
096                    new ServiceContractModelValidator(model).validate();
097            if (errors.size() > 0) {
098                StringBuilder buf = new StringBuilder();
099                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    }