001/**
002 * Copyright 2004-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016/*
017 * To change this template, choose Tools | Templates
018 * and open the template in the editor.
019 */
020package org.kuali.student.contract.model.impl;
021
022import org.kuali.student.contract.model.MessageStructure;
023import org.kuali.student.contract.model.Service;
024import org.kuali.student.contract.model.ServiceContractModel;
025import org.kuali.student.contract.model.ServiceMethod;
026import org.kuali.student.contract.model.ServiceMethodParameter;
027import org.kuali.student.contract.model.XmlType;
028import org.kuali.student.contract.model.util.MessageStructureDumper;
029import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
030import org.kuali.student.validation.decorator.mojo.ValidationDecoratorWriterForOneService;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.List;
037
038import org.junit.After;
039import org.junit.AfterClass;
040import org.junit.Before;
041import org.junit.BeforeClass;
042import org.junit.Test;
043
044import static org.junit.Assert.*;
045
046/**
047 *
048 * @author nwright
049 */
050public class ServiceContractModelPescXsdLoaderTest {
051    
052    private static Logger log = LoggerFactory.getLogger(ServiceContractModelPescXsdLoaderTest.class);
053    
054
055    public ServiceContractModelPescXsdLoaderTest() {
056    }
057
058    @BeforeClass
059    public static void setUpClass() throws Exception {
060    }
061
062    @AfterClass
063    public static void tearDownClass() throws Exception {
064    }
065
066    @Before
067    public void setUp() {
068    }
069
070    @After
071    public void tearDown() {
072    }
073    private static final String RESOURCES_DIRECTORY =
074            //                             "C:/svn/student/ks-core/ks-core-api/src/main/java";
075            "src/main/resources";
076    private static final String PESC_DIRECTORY =
077            RESOURCES_DIRECTORY + "/pesc";
078    private static final String PESC_CORE_MAIN = PESC_DIRECTORY + "/CoreMain.xsd";
079    private static final String PESC_ACAD_REC = PESC_DIRECTORY + "/AcademicRecord_v1.5.0.xsd";
080    private static final String PESC_COLL_TRANS = PESC_DIRECTORY + "/CollegeTranscript_v1.2.0.xsd";
081
082    private ServiceContractModel getModel() {
083        List<String> xsdFileNames = new ArrayList();
084//        xsdFileNames.add(PESC_CORE_MAIN);
085//        xsdFileNames.add(PESC_ACAD_REC);
086        xsdFileNames.add(PESC_COLL_TRANS);
087        ServiceContractModel instance = new ServiceContractModelPescXsdLoader(xsdFileNames);
088        instance = new ServiceContractModelCache(instance);
089        validate(instance);
090        return instance;
091    }
092
093    private String dump(ServiceMethod method) {
094        StringBuilder bldr = new StringBuilder();
095        bldr.append(method.getName());
096        String comma = "";
097        bldr.append("(");
098        for (ServiceMethodParameter param : method.getParameters()) {
099            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}