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