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