001    /*
002     * Copyright 2011 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.osedu.org/licenses/ECL-2.0
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    package org.kuali.student.contract.model.impl;
017    
018    import java.io.File;
019    import java.io.FileNotFoundException;
020    import java.io.PrintStream;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.List;
024    import java.util.Set;
025    import java.util.Stack;
026    
027    import org.junit.After;
028    import org.junit.AfterClass;
029    import org.junit.Before;
030    import org.junit.BeforeClass;
031    import org.junit.Test;
032    
033    import static org.junit.Assert.*;
034    
035    import org.kuali.student.contract.model.MessageStructure;
036    import org.kuali.student.contract.model.Service;
037    import org.kuali.student.contract.model.ServiceContractModel;
038    import org.kuali.student.contract.model.ServiceMethod;
039    import org.kuali.student.contract.model.ServiceMethodParameter;
040    import org.kuali.student.contract.model.XmlType;
041    import org.kuali.student.contract.model.util.HtmlContractServiceWriter;
042    import org.kuali.student.contract.model.util.MessageStructureHierarchyDumper;
043    import org.kuali.student.contract.model.util.ModelFinder;
044    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
045    import org.kuali.student.validation.decorator.mojo.ValidationDecoratorWriterForOneService;
046    import org.slf4j.Logger;
047    import org.slf4j.LoggerFactory;
048    
049    /**
050     *
051     * @author nwright
052     */
053    public class ServiceContractModelQDoxLoaderTest {
054        
055        private static Logger log = LoggerFactory.getLogger(ServiceContractModelQDoxLoaderTest.class);
056        
057    
058        public ServiceContractModelQDoxLoaderTest() {
059        }
060    
061        @BeforeClass
062        public static void setUpClass() throws Exception {
063        }
064    
065        @AfterClass
066        public static void tearDownClass() throws Exception {
067        }
068    
069        @Before
070        public void setUp() {
071        }
072    
073        @After
074        public void tearDown() {
075        }
076    
077        private static final String RESOURCES_DIRECTORY = "src/test/resources";
078        private static final String TEST_SOURCE_DIRECTORY =
079                "src/test/java/org/kuali/student/contract/model/test/source";
080        private static final String ENROLL_PROJECT_SRC_MAIN = "C:/svn/ks-1.3/ks-enroll/ks-enroll-api/src/main";
081        private static final String ENROLL_PROJECT_JAVA_DIRECTORY = ENROLL_PROJECT_SRC_MAIN + "/java";
082        private static final String RICE_CORE_API_DIRECTORY = "C:/svn/rice/trunk/core/api/src/main/java";
083        private static final String RICE_KIM_API_DIRECTORY = "C:/svn/rice/trunk/kim/kim-api/src/main/java";
084        private static final String RICE_LOCATION_API_DIRECTORY = "C:/svn/rice/trunk/location/api/src/main/java";
085        private static final String RICE_KEW_API_DIRECTORY = "C:/svn/rice/trunk/kew/api/src/main/java";
086        private static final String RICE_KEN_API_DIRECTORY = "C:/svn/rice/trunk/ken/api/src/main/java";
087        private static final String RICE_KSB_API_DIRECTORY = "C:/svn/rice/trunk/ksb/api/src/main/java";
088        private static final String RICE_KRMS_API_DIRECTORY = "C:/svn/rice/trunk/krms/api/src/main/java";
089        private static ServiceContractModel model = null;
090        private ServiceContractModel getModel() {
091            if (model != null) {
092                return model;
093            }
094            List<String> srcDirs = new ArrayList();
095            log.info("User directory=" + System.getProperty("user.dir"));
096            log.info("Current directory=" + new File(".").getAbsolutePath());
097    //        srcDirs.add (ENROLL_PROJECT_JAVA_DIRECTORY);
098            srcDirs.add(TEST_SOURCE_DIRECTORY);
099    //        srcDirs.add(RICE_CORE_API_DIRECTORY); 
100    //        srcDirs.add(RICE_KIM_API_DIRECTORY); 
101    //        srcDirs.add(RICE_LOCATION_API_DIRECTORY); 
102    //        srcDirs.add(RICE_KEW_API_DIRECTORY); 
103    //        srcDirs.add(RICE_KEN_API_DIRECTORY); 
104    //        srcDirs.add(RICE_KSB_API_DIRECTORY); 
105    //        srcDirs.add(RICE_KRMS_API_DIRECTORY);     
106            boolean validateKualiStudent = false;
107            ServiceContractModel instance = new ServiceContractModelQDoxLoader(srcDirs, validateKualiStudent);
108            
109            instance = new ServiceContractModelCache(instance);
110            validate(instance);
111            model = instance;
112            return instance;
113        }
114    
115        private String dump(ServiceMethod method) {
116            StringBuilder bldr = new StringBuilder();
117            bldr.append(method.getName());
118            String comma = "";
119            bldr.append("(");
120            for (ServiceMethodParameter param : method.getParameters()) {
121                bldr.append(comma);
122                comma = ", ";
123                bldr.append(param.getType());
124                bldr.append(" ");
125                bldr.append(param.getName());
126            }
127            bldr.append(")");
128            return bldr.toString();
129        }
130    
131        private void validate(ServiceContractModel model) {
132            Collection<String> errors =
133                    new ServiceContractModelValidator(model).validate();
134            if (errors.size() > 0) {
135                StringBuilder buf = new StringBuilder();
136                buf.append(errors.size()).append(" errors found while validating the data.");
137                int cnt = 0;
138                for (String msg : errors) {
139                    cnt++;
140                    buf.append("\n");
141                    buf.append("*error*").append(cnt).append(":").append(msg);
142                }
143    
144                fail(buf.toString());
145            }
146        }
147    
148        /**
149         * Test of getServiceMethods method, of class ServiceContractModelQDoxLoader.
150         */
151        @Test
152        public void testGetServiceMethods() {
153            log.info("getServiceMethods");
154            ServiceContractModel model = getModel();
155            List<ServiceMethod> result = model.getServiceMethods();
156            log.info("Number of methods=" + result.size());
157            boolean getAtpFound = false;
158            for (ServiceMethod method : result) {
159                log.info(dump(method));
160                if (method.getName().equals("getAtp")) {
161                    getAtpFound = true;
162                    assertEquals ("this is an implementation note\nthis is another", method.getImplNotes());
163                }
164            }
165            assertTrue (getAtpFound);
166            if (result.size() < 10) {
167                fail("too few: " + result.size());
168            }
169            
170        }
171        
172    
173        /**
174         * Test of getSourceNames method, of class ServiceContractModelQDoxLoader.
175         */
176    //    @Test
177        public void testGetSourceNames() {
178            log.info("getSourceNames");
179            ServiceContractModel model = getModel();
180            List<String> expResult = new ArrayList();
181            expResult.add(TEST_SOURCE_DIRECTORY);
182            List result = model.getSourceNames();
183            assertEquals(expResult, result);
184        }
185    
186        /**
187         * Test of getServices method, of class ServiceContractModelQDoxLoader.
188         */
189    //    @Test
190        public void testGetServices() {
191            log.info("getServices");
192            ServiceContractModel model = getModel();
193            List<Service> result = model.getServices();
194            for (Service service : result) {
195                log.info(service.getKey() + " " + service.getName() + " "
196                        + service.getVersion() + " " + service.getStatus());
197            }
198            assertEquals(4, result.size());
199        }
200    
201        /**
202         * Test of getXmlTypes method, of class ServiceContractModelQDoxLoader.
203         */
204    //    @Test
205        public void testGetXmlTypes() {
206            log.info("getXmlTypes");
207            ServiceContractModel model = getModel();
208            List<XmlType> result = model.getXmlTypes();
209            for (XmlType xmlType : result) {
210                log.info("XmlType=" + xmlType.getName() + " "
211                        + xmlType.getPrimitive());
212            }
213            if (result.size() < 10) {
214                fail("too few: " + result.size());
215            }
216        }
217    
218        /**
219         * Test of getMessageStructures method, of class ServiceContractModelQDoxLoader.
220         */
221    //    @Test
222        public void testGetMessageStructures() throws FileNotFoundException {
223            log.info("getMessageStructures");
224            ServiceContractModel model = getModel();
225            List<MessageStructure> result = model.getMessageStructures();
226            for (MessageStructure ms : result) {
227                if (ms.getId().equalsIgnoreCase("academicCalendarInfo.typeKey")) {
228                    log.info("MessageStructure=" + ms.getId() + " " + ms.getType() + "required=["+ ms.getRequired() + "]");
229                }
230            }
231            if (result.size() < 10) {
232                fail("too few: " + result.size());
233            }
234            String outputFileName = "target/messageStructures.txt";
235            File file = new File(outputFileName);
236            PrintStream out = new PrintStream(file);
237            new MessageStructureHierarchyDumper(out, model).writeTabbedHeader();
238            Set<XmlType> rootTypes = HtmlContractServiceWriter.calcMainMessageStructures(
239                    model, null);
240            ModelFinder finder = new ModelFinder(model);
241            for (XmlType rootType : rootTypes) {
242                Stack<String> stack = new Stack();
243                stack.push(rootType.getName());
244                for (MessageStructure ms : finder.findMessageStructures(rootType.getName())) {
245                    new MessageStructureHierarchyDumper(out, model).writeTabbedData(ms, stack);
246                }
247            }
248        }
249    }