View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.edl.impl;
17  
18  import static org.junit.Assert.assertTrue;
19  
20  import org.apache.log4j.Logger;
21  import org.junit.Test;
22  import org.kuali.rice.edl.framework.workflow.EDocLitePostProcessor;
23  import org.kuali.rice.kew.test.KEWTestCase;
24  
25  
26  /**
27   * This is a Test class to verify the edoc lite post processor. 
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   *
31   */
32  public class EDocLitePostProcessorTest extends KEWTestCase {
33      private static final Logger LOG = Logger.getLogger(EDocLitePostProcessorTest.class);
34  
35      private static final String CONTEXT_NAME = "/edl-test";
36  
37      @Test
38      public void junk() {
39  
40      }
41      /*
42      @Test
43      public void testPostEvent() throws Exception {
44          String dummyData = "testing this stuff";
45          Integer testServerPort = Integer.valueOf(getJettyServerPort() + 1);
46          WorkflowDocument document = new WorkflowDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
47          String applicationContent = "<data><edlContent><edl><eventNotificationURL>" + ConfigContext.getCurrentContextConfig().getProperty(KewApiConstants.KEW_URL_HOST) + ":" + testServerPort + CONTEXT_NAME + "</eventNotificationURL><testThisData>" + dummyData + "</testThisData></edl></edlContent></data>";
48          document.setApplicationContent(applicationContent);
49          document.saveDocumentData();
50  
51          JettyServer server = null;
52          try {
53              server = new JettyServer(testServerPort.intValue(), CONTEXT_NAME, EDocLiteTestServlet.class);
54              server.setTestMode(true);
55              server.setFailOnContextFailure(true);
56              server.start();
57  
58              EDocLitePostProcessor postProcessor = new EDocLitePostProcessor();
59              postProcessor.doActionTaken(new ActionTakenEvent(document.getDocumentId(), document.getAppDocId(), new ActionTakenValue()));
60              String eventType = EDocLitePostProcessor.EVENT_TYPE_ACTION_TAKEN;
61              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
62  
63              postProcessor = new EDocLitePostProcessor();
64              postProcessor.doRouteLevelChange(new DocumentRouteLevelChange(document.getDocumentId(), document.getAppDocId(), 1, 2, null, null, null, null));
65              eventType = EDocLitePostProcessor.EVENT_TYPE_ROUTE_LEVEL_CHANGE;
66              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
67  
68              postProcessor = new EDocLitePostProcessor();
69              postProcessor.doRouteStatusChange(new DocumentRouteStatusChange(document.getDocumentId(), document.getAppDocId(), KewApiConstants.ROUTE_HEADER_INITIATED_CD, KewApiConstants.ROUTE_HEADER_ENROUTE_CD));
70              eventType = EDocLitePostProcessor.EVENT_TYPE_ROUTE_STATUS_CHANGE;
71              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
72  
73              postProcessor = new EDocLitePostProcessor();
74              postProcessor.doDeleteRouteHeader(new DeleteEvent(document.getDocumentId(), document.getAppDocId()));
75              eventType = EDocLitePostProcessor.EVENT_TYPE_DELETE_ROUTE_HEADER;
76              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
77  
78          } finally {
79              if (server != null) {
80                  try {
81                      server.stop();
82                  } catch (Exception e) {
83                      LOG.warn("Failed to shutdown one of the lifecycles!", e);
84                  }
85              }
86          }
87      } */
88  
89      private void testPostProcessorMethod(String documentId, String dummyData, String eventType) {
90          int maxWaitSeconds = EDocLitePostProcessor.SUBMIT_URL_MILLISECONDS_WAIT + 10000;
91          assertTrue("Test Servlet data map should not be null after wait period", waitForData(maxWaitSeconds, 5000));
92          testForStringExistence("Document Id", documentId);
93          testForStringExistence("Dummy Data", dummyData);
94          testForStringExistence("Event Type '" + eventType + "'", eventType);
95      }
96  
97      private void testForStringExistence(String dataDescriptor, String dataToFind) {
98          assertTrue(dataDescriptor = " should come back as part of the post data but didn't", EDocLiteTestServlet.postData.contains(dataToFind));
99      }
100 
101     public static boolean waitForData(int maxWaitMilliseconds, int pauseMilliseconds) {
102         boolean valueNotNull = false;
103         boolean interrupted = false;
104         long startTimeMs = System.currentTimeMillis();
105         long endTimeMs = startTimeMs + maxWaitMilliseconds;
106 
107         try {
108             Thread.sleep(pauseMilliseconds / 10); // the first time through, sleep a fraction of the specified time
109         } catch (InterruptedException e) {
110             interrupted = true;
111         }
112         valueNotNull = EDocLiteTestServlet.postData != null;
113         LOG.debug("starting wait loop");
114         while (!interrupted && !valueNotNull && (System.currentTimeMillis() < endTimeMs)) {
115             try {
116                 if (LOG.isDebugEnabled()) {
117                     LOG.debug("sleeping for " + pauseMilliseconds + " ms");
118                 }
119                 Thread.sleep(pauseMilliseconds);
120             } catch (InterruptedException e) {
121                 interrupted = true;
122             }
123             LOG.debug("checking wait loop sentinel");
124             valueNotNull = EDocLiteTestServlet.postData != null;
125         }
126         LOG.debug("finished wait loop (" + valueNotNull + ")");
127 
128         return valueNotNull;
129     }
130 }