001 /**
002 * Copyright 2005-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 package org.kuali.rice.edl.impl;
017
018 import org.junit.Test;
019 import org.kuali.rice.core.api.config.property.Config;
020 import org.kuali.rice.core.api.config.property.ConfigContext;
021 import org.kuali.rice.core.api.impex.xml.XmlIngestionException;
022 import org.kuali.rice.coreservice.api.style.Style;
023 import org.kuali.rice.coreservice.api.style.StyleService;
024 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
025 import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
026 import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
027 import org.kuali.rice.edl.impl.service.EDocLiteService;
028 import org.kuali.rice.edl.impl.service.EdlServiceLocator;
029 import org.kuali.rice.kew.test.KEWTestCase;
030 import org.kuali.rice.kew.test.TestUtilities;
031 import org.kuali.rice.test.BaselineTestCase;
032
033 import javax.xml.transform.Templates;
034 import java.io.ByteArrayInputStream;
035 import java.io.FileNotFoundException;
036 import java.util.List;
037
038 import static org.junit.Assert.*;
039
040 /**
041 * Tests EDocLiteServiceImpl
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
045 public class EDocLiteServiceImplTest extends KEWTestCase {
046
047 @Test public void testLoadXML() throws FileNotFoundException {
048 loadXmlFile("EDocLiteContent.xml");
049 loadXmlFile("edlstyle.xml");
050
051 EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
052 StyleService styleService = CoreServiceApiServiceLocator.getStyleService();
053 //edls.loadXml(new FileInputStream("conf/examples/xml/EDocLiteContent.xml"));
054 assertTrue("Definition not found", edls.getEDocLiteDefinitions().contains("profile"));
055 Style defaultStyle = styleService.getStyle("Default");
056 assertNotNull("Style not found", defaultStyle);
057 assertEquals(1, edls.getEDocLiteAssociations().size());
058 EDocLiteDefinition def = edls.getEDocLiteDefinition("profile");
059 assertNotNull("'profile' definition not found", def);
060 assertEquals("profile", def.getName());
061 assertNotNull(def.getActiveInd());
062 assertTrue(def.getActiveInd().booleanValue());
063 Style style = styleService.getStyle("Default");
064 assertNotNull("'Default' style not found", style);
065 assertEquals("Default", style.getName());
066 assertTrue(style.isActive());
067 assertNotNull(style.getXmlContent());
068 }
069
070 @Test public void testLoadBadDefinition() throws FileNotFoundException {
071 EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
072 try {
073 edls.loadXml(TestUtilities.loadResource(getClass(), "BadDefinition.xml"), null);
074 fail("BadDefinition was successfully parsed.");
075 } catch (XmlIngestionException re) {
076 // should probably use type system to detect type of error, not just message string...
077 // maybe we need general parsing or "semantic" validation exception
078 assertTrue("Wrong exception occurred", re.getMessage().contains("EDocLite definition contains references to non-existent attributes"));
079 }
080 }
081
082 @Test public void testStoreDefinition() {
083 EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
084 String defXml = "<edl></edl>";
085 try {
086 edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml.getBytes()));
087 fail("Storing edl with no name succeeded");
088 } catch (XmlIngestionException wsee) {
089 // expected due to lack of name
090 }
091 defXml = "<edl name=\"test\"></edl>";
092 edls.saveEDocLiteDefinition(new ByteArrayInputStream(defXml.getBytes()));
093 EDocLiteDefinition def = edls.getEDocLiteDefinition("test");
094 assertNotNull(def);
095 assertEquals("test", def.getName());
096 }
097
098 @Test public void testStoreAssociation() {
099 EDocLiteService edls = EdlServiceLocator.getEDocLiteService();
100 String assocXml = "<association></association>";
101 try {
102 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
103 fail("Storing association with no docType succeeded");
104 } catch (XmlIngestionException wsee) {
105 // expected due to lack of doctype
106 }
107 assocXml = "<association><docType></docType></association>";
108 try {
109 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
110 fail("Storing association with empty docType succeeded");
111 } catch (XmlIngestionException wsee) {
112 // expected due to emtpy doctype value
113 }
114 assocXml = "<association><docType>foobar</docType></association>";
115 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
116 EDocLiteAssociation assoc = edls.getEDocLiteAssociation("foobar");
117 assertNull("Inactive Association was found", assoc);
118
119 assocXml = "<association><docType>foobar</docType><active>true</active></association>";
120 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
121 assoc = edls.getEDocLiteAssociation("foobar");
122 assertNotNull("Association was not found", assoc);
123 assertEquals("foobar", assoc.getEdlName());
124 assertNull(assoc.getDefinition());
125 assertNull(assoc.getStyle());
126
127 List<EDocLiteAssociation> assocs = edls.getEDocLiteAssociations();
128 assertEquals(1, assocs.size());
129 assoc = assocs.get(0);
130 assertEquals("foobar", assoc.getEdlName());
131 assertNull(assoc.getDefinition());
132 assertNull(assoc.getStyle());
133 assertNotNull(assoc.getActiveInd());
134 assertTrue(assoc.getActiveInd().booleanValue());
135
136 assocXml = "<association><style>style name</style><definition>definition name</definition><docType>foobar</docType><active>true</active></association>";
137 edls.saveEDocLiteAssociation(new ByteArrayInputStream(assocXml.getBytes()));
138 assoc = edls.getEDocLiteAssociation("foobar");
139 assertNotNull("Association was not found", assoc);
140 assertEquals("foobar", assoc.getEdlName());
141 assertEquals("definition name", assoc.getDefinition());
142 assertEquals("style name", assoc.getStyle());
143
144 assocs = edls.getEDocLiteAssociations();
145 assertEquals(1, assocs.size());
146 assoc = (EDocLiteAssociation) assocs.get(0);
147 assertNotNull("Association was not found", assoc);
148 assertEquals("foobar", assoc.getEdlName());
149 assertEquals("definition name", assoc.getDefinition());
150 assertEquals("style name", assoc.getStyle());
151 assertNotNull(assoc.getActiveInd());
152 assertTrue(assoc.getActiveInd().booleanValue());
153 }
154
155 /**
156 * Tests the caching of "styles" in EDocLiteServiceImpl.
157 *
158 * The style cache is really a cache of java.xml.transform.Templates objects which represent
159 * the "compiled" stylesheets.
160 */
161 @Test public void testStyleCaching() throws Exception {
162 ConfigContext.getCurrentContextConfig().putProperty(Config.EDL_CONFIG_LOCATION, "classpath:org/kuali/rice/kew/edl/TestEDLConfig.xml");
163
164 loadXmlFile("EDocLiteContent.xml");
165 loadXmlFile("edlstyle.xml");
166 loadXmlFile("widgets.xml");
167
168 // try to grab the templates out of the cache, it shouldn't be cached yet
169 // Templates cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
170 // assertNull("The default style template should not be cached yet.", cachedTemplates);
171
172 // fetch the Templates object from the service
173 EDocLiteAssociation association = EdlServiceLocator.getEDocLiteService().getEDocLiteAssociation("EDocLiteDocType");
174 assertNull("We should be using the Default style.", association.getStyle());
175 Templates templates = EdlServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
176 assertNotNull("Templates should not be null.", templates);
177
178 // the Templates should now be cached
179 // cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
180 // assertNotNull("Templates should now be cached.", cachedTemplates);
181
182 // // the cached Templates should be the same as the Templates we fetched from the service
183 // assertEquals("Templates should be the same.", templates, cachedTemplates);
184
185 // now re-import the style and the templates should no longer be cached
186 loadXmlFile("edlstyle.xml");
187 // cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
188 // assertNull("After re-import, the Default style Templates should no longer be cached.", cachedTemplates);
189
190 // re-fetch the templates from the service and verify they are in the cache
191 Templates newTemplates = EdlServiceLocator.getEDocLiteService().getStyleAsTranslet(association.getStyle());
192 assertNotNull("Templates should not be null.", templates);
193 // cachedTemplates = new EDocLiteServiceImpl().fetchTemplatesFromCache("Default");
194 // assertNotNull("Templates should now be cached.", cachedTemplates);
195
196 // lastly, check that the newly cached templates are not the same as the original templates
197 assertFalse("Old Templates should be different from new Templates.", templates.equals(newTemplates));
198
199 }
200 }