001 /*
002 * Copyright 2007-2008 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.kew.edl;
017
018 import org.apache.log4j.Logger;
019 import org.junit.Test;
020 import org.kuali.rice.core.config.ConfigContext;
021 import org.kuali.rice.core.web.jetty.JettyServer;
022 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
023 import org.kuali.rice.kew.dto.NetworkIdDTO;
024 import org.kuali.rice.kew.postprocessor.ActionTakenEvent;
025 import org.kuali.rice.kew.postprocessor.DeleteEvent;
026 import org.kuali.rice.kew.postprocessor.DocumentRouteLevelChange;
027 import org.kuali.rice.kew.postprocessor.DocumentRouteStatusChange;
028 import org.kuali.rice.kew.service.WorkflowDocument;
029 import org.kuali.rice.kew.test.KEWTestCase;
030 import org.kuali.rice.kew.util.KEWConstants;
031
032
033 /**
034 * This is a Test class to verify the edoc lite post processor.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 *
038 */
039 public class EDocLitePostProcessorTest extends KEWTestCase {
040 private static final Logger LOG = Logger.getLogger(EDocLitePostProcessorTest.class);
041
042 private static final String CONTEXT_NAME = "/edl-test";
043
044 @Test
045 public void testPostEvent() throws Exception {
046 String dummyData = "testing this stuff";
047 Integer testServerPort = Integer.valueOf(getJettyServerPort() + 1);
048 WorkflowDocument document = new WorkflowDocument(new NetworkIdDTO("ewestfal"), "TestDocumentType");
049 String applicationContent = "<data><edlContent><edl><eventNotificationURL>" + ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.KEW_URL_HOST) + ":" + testServerPort + CONTEXT_NAME + "</eventNotificationURL><testThisData>" + dummyData + "</testThisData></edl></edlContent></data>";
050 document.setApplicationContent(applicationContent);
051 document.saveRoutingData();
052
053 JettyServer server = null;
054 try {
055 server = new JettyServer(testServerPort.intValue(), CONTEXT_NAME, EDocLiteTestServlet.class);
056 server.setTestMode(true);
057 server.setFailOnContextFailure(true);
058 server.start();
059
060 EDocLitePostProcessor postProcessor = new EDocLitePostProcessor();
061 postProcessor.doActionTaken(new ActionTakenEvent(document.getRouteHeaderId(), document.getAppDocId(), new ActionTakenValue()));
062 String eventType = EDocLitePostProcessor.EVENT_TYPE_ACTION_TAKEN;
063 testPostProcessorMethod(document.getRouteHeaderId(), dummyData, eventType);
064
065 postProcessor = new EDocLitePostProcessor();
066 postProcessor.doRouteLevelChange(new DocumentRouteLevelChange(document.getRouteHeaderId(), document.getAppDocId(), 1, 2, null, null, null, null));
067 eventType = EDocLitePostProcessor.EVENT_TYPE_ROUTE_LEVEL_CHANGE;
068 testPostProcessorMethod(document.getRouteHeaderId(), dummyData, eventType);
069
070 postProcessor = new EDocLitePostProcessor();
071 postProcessor.doRouteStatusChange(new DocumentRouteStatusChange(document.getRouteHeaderId(), document.getAppDocId(), KEWConstants.ROUTE_HEADER_INITIATED_CD, KEWConstants.ROUTE_HEADER_ENROUTE_CD));
072 eventType = EDocLitePostProcessor.EVENT_TYPE_ROUTE_STATUS_CHANGE;
073 testPostProcessorMethod(document.getRouteHeaderId(), dummyData, eventType);
074
075 postProcessor = new EDocLitePostProcessor();
076 postProcessor.doDeleteRouteHeader(new DeleteEvent(document.getRouteHeaderId(), document.getAppDocId()));
077 eventType = EDocLitePostProcessor.EVENT_TYPE_DELETE_ROUTE_HEADER;
078 testPostProcessorMethod(document.getRouteHeaderId(), dummyData, eventType);
079
080 } finally {
081 if (server != null) {
082 try {
083 server.stop();
084 } catch (Exception e) {
085 LOG.warn("Failed to shutdown one of the lifecycles!", e);
086 }
087 }
088 }
089 }
090
091 private void testPostProcessorMethod(Long routeHeaderId, String dummyData, String eventType) {
092 int maxWaitSeconds = EDocLitePostProcessor.SUBMIT_URL_MILLISECONDS_WAIT + 10000;
093 assertTrue("Test Servlet data map should not be null after wait period", waitForData(maxWaitSeconds, 5000));
094 testForStringExistence("Route Header Id", routeHeaderId.toString());
095 testForStringExistence("Dummy Data", dummyData);
096 testForStringExistence("Event Type '" + eventType + "'", eventType);
097 }
098
099 private void testForStringExistence(String dataDescriptor, String dataToFind) {
100 assertTrue(dataDescriptor = " should come back as part of the post data but didn't", EDocLiteTestServlet.postData.contains(dataToFind));
101 }
102
103 public static boolean waitForData(int maxWaitMilliseconds, int pauseMilliseconds) {
104 boolean valueNotNull = false;
105 boolean interrupted = false;
106 long startTimeMs = System.currentTimeMillis();
107 long endTimeMs = startTimeMs + maxWaitMilliseconds;
108
109 try {
110 Thread.sleep(pauseMilliseconds / 10); // the first time through, sleep a fraction of the specified time
111 } catch (InterruptedException e) {
112 interrupted = true;
113 }
114 valueNotNull = EDocLiteTestServlet.postData != null;
115 LOG.debug("starting wait loop");
116 while (!interrupted && !valueNotNull && (System.currentTimeMillis() < endTimeMs)) {
117 try {
118 if (LOG.isDebugEnabled()) {
119 LOG.debug("sleeping for " + pauseMilliseconds + " ms");
120 }
121 Thread.sleep(pauseMilliseconds);
122 } catch (InterruptedException e) {
123 interrupted = true;
124 }
125 LOG.debug("checking wait loop sentinel");
126 valueNotNull = EDocLiteTestServlet.postData != null;
127 }
128 LOG.debug("finished wait loop (" + valueNotNull + ")");
129
130 return valueNotNull;
131 }
132 }