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.util;
017    
018    import static org.junit.Assert.assertTrue;
019    import static org.junit.Assert.fail;
020    
021    import java.io.File;
022    import java.util.ArrayList;
023    import java.util.Collection;
024    import java.util.Date;
025    import java.util.List;
026    
027    import org.joda.time.DateTime;
028    import org.junit.After;
029    import org.junit.AfterClass;
030    import org.junit.Before;
031    import org.junit.BeforeClass;
032    import org.junit.Test;
033    import org.kuali.student.contract.model.ServiceContractModel;
034    import org.kuali.student.contract.model.impl.ServiceContractModelCache;
035    import org.kuali.student.contract.model.impl.ServiceContractModelPescXsdLoader;
036    import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
037    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
038    
039    /**
040     * 
041     * @author nwright
042     */
043    public class HtmlContractWriterTest {
044    
045        public HtmlContractWriterTest() {
046        }
047    
048        @BeforeClass
049        public static void setUpClass() throws Exception {
050        }
051    
052        @AfterClass
053        public static void tearDownClass() throws Exception {
054        }
055    
056        @Before
057        public void setUp() {
058        }
059    
060        @After
061        public void tearDown() {
062        }
063        private static final String CORE_DIRECTORY =
064                "C:/svn/ks-1.3/ks-core/ks-core-api/src/main/java";
065        // "C:/svn/maven-dictionary-generator/trunk/src/main/java/org/kuali/student/core";
066        private static final String COMMON_DIRECTORY =
067                "C:/svn/ks-1.3/ks-common/ks-common-api/src/main/java";
068        private static final String ENROLL_PROJECT_SOURCE_DIRECTORY =
069                "C:/svn/ks-1.3/ks-enroll/ks-enroll-api/src/main/java";
070        private static final String LUM_DIRECTORY =
071                "C:/svn/ks-1.3/ks-lum/ks-lum-api/src/main/java";
072        private static final String RICE_DIRECTORY =
073                "C:/svn/rice/rice-release-1-0-2-1-br/api/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 final String RICE_KRMS_IMPL_DIRECTORY = "C:/svn/rice/trunk/krms/impl/src/main/java";
082        private static final String TEST_SOURCE_DIRECTORY =
083                "src/test/java/org/kuali/student/contract/model/test/source";
084        private static final String HTML_CONTRACT_DIRECTORY = "target/html/contract";
085        private static final String HTML_CONTRACT_DIRECTORY_TEST = HTML_CONTRACT_DIRECTORY + "/test";    
086        private static final String HTML_CONTRACT_DIRECTORY_ENROLL = HTML_CONTRACT_DIRECTORY + "/enroll";         
087        private static final String HTML_CONTRACT_DIRECTORY_RICE = HTML_CONTRACT_DIRECTORY + "/rice";      
088        private static final String HTML_CONTRACT_DIRECTORY_PESC = HTML_CONTRACT_DIRECTORY + "/pesc";    
089        private static final String RESOURCES_DIRECTORY =
090                // "C:/svn/student/ks-core/ks-core-api/src/main/java";
091                "src/main/resources";
092    
093        private ServiceContractModel getTestModel() {
094            List<String> srcDirs = new ArrayList<String>();
095            srcDirs.add(TEST_SOURCE_DIRECTORY);
096    //              srcDirs.add(ENROLL_PROJECT_SOURCE_DIRECTORY);
097    //              srcDirs.add(CORE_DIRECTORY);
098    //              srcDirs.add(COMMON_DIRECTORY);
099    //              srcDirs.add(LUM_DIRECTORY);
100            ServiceContractModel instance = new ServiceContractModelQDoxLoader(
101                    srcDirs);
102            return new ServiceContractModelCache(instance);
103    
104        }
105        
106        private ServiceContractModel getRiceModel() {
107            List<String> srcDirs = new ArrayList<String>();
108    //        srcDirs.add(TEST_SOURCE_DIRECTORY);
109            srcDirs.add(RICE_CORE_API_DIRECTORY); 
110            srcDirs.add(RICE_KIM_API_DIRECTORY); 
111            srcDirs.add(RICE_LOCATION_API_DIRECTORY); 
112            srcDirs.add(RICE_KEW_API_DIRECTORY); 
113            srcDirs.add(RICE_KEN_API_DIRECTORY); 
114            srcDirs.add(RICE_KSB_API_DIRECTORY); 
115            srcDirs.add(RICE_KRMS_API_DIRECTORY); 
116                    srcDirs.add(RICE_KRMS_IMPL_DIRECTORY);
117    //              srcDirs.add(CORE_DIRECTORY);
118    //              srcDirs.add(COMMON_DIRECTORY);
119    //              srcDirs.add(LUM_DIRECTORY);
120            ServiceContractModel instance = new ServiceContractModelQDoxLoader(
121                    srcDirs, false);
122            return new ServiceContractModelCache(instance);
123    
124        }    
125        
126        private ServiceContractModel getEnrollModel() {
127            List<String> srcDirs = new ArrayList<String>();
128    //        srcDirs.add(TEST_SOURCE_DIRECTORY);
129                    srcDirs.add(ENROLL_PROJECT_SOURCE_DIRECTORY);
130    //              srcDirs.add(CORE_DIRECTORY);
131    //              srcDirs.add(COMMON_DIRECTORY);
132    //              srcDirs.add(LUM_DIRECTORY);
133            ServiceContractModel instance = new ServiceContractModelQDoxLoader(
134                    srcDirs);
135            return new ServiceContractModelCache(instance);
136    
137        }
138        // pesc stuff
139        private static final String PESC_DIRECTORY =
140                RESOURCES_DIRECTORY + "/pesc";
141        private static final String PESC_CORE_MAIN = PESC_DIRECTORY + "/CoreMain.xsd";
142        private static final String PESC_ACAD_REC = PESC_DIRECTORY + "/AcademicRecord_v1.5.0.xsd";
143        private static final String PESC_COLL_TRANS = PESC_DIRECTORY + "/CollegeTranscript_v1.2.0.xsd";
144    
145        private ServiceContractModel getPescModel() {
146            List<String> xsdFileNames = new ArrayList();
147    //        xsdFileNames.add(PESC_CORE_MAIN);
148    //        xsdFileNames.add(PESC_ACAD_REC);
149            xsdFileNames.add(PESC_COLL_TRANS);
150            ServiceContractModel instance = new ServiceContractModelPescXsdLoader(xsdFileNames);
151            instance = new ServiceContractModelCache(instance);
152            validate(instance);
153            return instance;
154        }
155    
156    
157        private void validate(ServiceContractModel model) {
158            Collection<String> errors =
159                    new ServiceContractModelValidator(model).validate();
160            if (errors.size() > 0) {
161                StringBuilder buf = new StringBuilder();
162                buf.append(errors.size()).append(" errors found while validating the data.");
163                int cnt = 0;
164                for (String msg : errors) {
165                    cnt++;
166                    buf.append("\n");
167                    buf.append("*error*").append(cnt).append(":").append(msg);
168                }
169    
170                fail(buf.toString());
171            }
172        }
173    
174        /**
175         * Test of run
176         */
177    //    @Test
178        public void testRunPesc() {
179            ServiceContractModel model = null;
180            HtmlContractWriter writer = null;
181    
182            model = this.getPescModel();
183            this.validate(model);
184            writer = new HtmlContractWriter(HTML_CONTRACT_DIRECTORY_PESC, model);
185            writer.write("testVersion", DateUtility.asYMD(new Date()));
186    
187            assertTrue(new File(HTML_CONTRACT_DIRECTORY_PESC + "/" + "index.html").exists());
188        }
189        
190        /**
191         * Test of run
192         */
193        @Test
194        public void testTestRun () {
195            ServiceContractModel model = null;
196            HtmlContractWriter writer = null;
197    
198            model = this.getTestModel();
199            this.validate(model);
200            writer = new HtmlContractWriter(HTML_CONTRACT_DIRECTORY_TEST, model);
201            writer.write("testVersion", DateUtility.asYMD(new Date()));
202    
203            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "index.html").exists());
204            assertTrue(
205                    new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "LprService.html").exists());
206            assertTrue(
207                    new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "LprInfo.html").exists());
208            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "ContextInfo.html").exists());
209            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "RichTextInfo.html").exists());
210            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "MetaInfo.html").exists());
211    
212            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "StateService.html").exists());
213            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "StateInfo.html").exists());
214    
215            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "TypeService.html").exists());
216            assertTrue(new File(HTML_CONTRACT_DIRECTORY_TEST + "/" + "TypeInfo.html").exists());
217        }
218        
219        
220        /**
221         * Test of run
222         */
223    //    @Test
224        public void testEnrollRun () {
225            ServiceContractModel model = null;
226            HtmlContractWriter writer = null;
227    
228            model = this.getEnrollModel();
229            this.validate(model);
230            writer = new HtmlContractWriter(HTML_CONTRACT_DIRECTORY_ENROLL, model);
231            writer.write("testVersion", DateUtility.asYMDHMInEasternTimeZone(new DateTime()));
232    
233    //        assertTrue(new File(HTML_CONTRACT_DIRECTORY_ENROLL + "/" + "index.html").exists());
234        }
235        
236        /**
237         * Test of run
238         */
239    //    @Test
240        public void testRiceRun () {
241            ServiceContractModel model = null;
242            HtmlContractWriter writer = null;
243    
244            model = this.getRiceModel();
245            this.validate(model);
246            writer = new HtmlContractWriter(HTML_CONTRACT_DIRECTORY_RICE, model);
247            writer.write("testVersion", DateUtility.asYMD(new Date()));
248    
249    //        assertTrue(new File(HTML_CONTRACT_DIRECTORY_ENROLL + "/" + "index.html").exists());
250        }
251    }