View Javadoc

1   /*
2    * Copyright 2006-2011 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  
17  package org.kuali.rice.edl.impl;
18  
19  import static org.junit.Assert.assertTrue;
20  
21  import org.apache.log4j.Logger;
22  import org.junit.Test;
23  import org.kuali.rice.edl.framework.workflow.EDocLitePostProcessor;
24  import org.kuali.rice.kew.test.KEWTestCase;
25  
26  
27  /**
28   * This is a Test class to verify the edoc lite post processor. 
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   *
32   */
33  public class EDocLitePostProcessorTest extends KEWTestCase {
34      private static final Logger LOG = Logger.getLogger(EDocLitePostProcessorTest.class);
35  
36      private static final String CONTEXT_NAME = "/edl-test";
37  
38      @Test
39      public void junk() {
40  
41      }
42      /*
43      @Test
44      public void testPostEvent() throws Exception {
45          String dummyData = "testing this stuff";
46          Integer testServerPort = Integer.valueOf(getJettyServerPort() + 1);
47          WorkflowDocument document = new WorkflowDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
48          String applicationContent = "<data><edlContent><edl><eventNotificationURL>" + ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.KEW_URL_HOST) + ":" + testServerPort + CONTEXT_NAME + "</eventNotificationURL><testThisData>" + dummyData + "</testThisData></edl></edlContent></data>";
49          document.setApplicationContent(applicationContent);
50          document.saveDocumentData();
51  
52          JettyServer server = null;
53          try {
54              server = new JettyServer(testServerPort.intValue(), CONTEXT_NAME, EDocLiteTestServlet.class);
55              server.setTestMode(true);
56              server.setFailOnContextFailure(true);
57              server.start();
58  
59              EDocLitePostProcessor postProcessor = new EDocLitePostProcessor();
60              postProcessor.doActionTaken(new ActionTakenEvent(document.getDocumentId(), document.getAppDocId(), new ActionTakenValue()));
61              String eventType = EDocLitePostProcessor.EVENT_TYPE_ACTION_TAKEN;
62              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
63  
64              postProcessor = new EDocLitePostProcessor();
65              postProcessor.doRouteLevelChange(new DocumentRouteLevelChange(document.getDocumentId(), document.getAppDocId(), 1, 2, null, null, null, null));
66              eventType = EDocLitePostProcessor.EVENT_TYPE_ROUTE_LEVEL_CHANGE;
67              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
68  
69              postProcessor = new EDocLitePostProcessor();
70              postProcessor.doRouteStatusChange(new DocumentRouteStatusChange(document.getDocumentId(), document.getAppDocId(), KEWConstants.ROUTE_HEADER_INITIATED_CD, KEWConstants.ROUTE_HEADER_ENROUTE_CD));
71              eventType = EDocLitePostProcessor.EVENT_TYPE_ROUTE_STATUS_CHANGE;
72              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
73  
74              postProcessor = new EDocLitePostProcessor();
75              postProcessor.doDeleteRouteHeader(new DeleteEvent(document.getDocumentId(), document.getAppDocId()));
76              eventType = EDocLitePostProcessor.EVENT_TYPE_DELETE_ROUTE_HEADER;
77              testPostProcessorMethod(document.getDocumentId(), dummyData, eventType);
78  
79          } finally {
80              if (server != null) {
81                  try {
82                      server.stop();
83                  } catch (Exception e) {
84                      LOG.warn("Failed to shutdown one of the lifecycles!", e);
85                  }
86              }
87          }
88      } */
89  
90      private void testPostProcessorMethod(String documentId, String dummyData, String eventType) {
91          int maxWaitSeconds = EDocLitePostProcessor.SUBMIT_URL_MILLISECONDS_WAIT + 10000;
92          assertTrue("Test Servlet data map should not be null after wait period", waitForData(maxWaitSeconds, 5000));
93          testForStringExistence("Document Id", documentId);
94          testForStringExistence("Dummy Data", dummyData);
95          testForStringExistence("Event Type '" + eventType + "'", eventType);
96      }
97  
98      private void testForStringExistence(String dataDescriptor, String dataToFind) {
99          assertTrue(dataDescriptor = " should come back as part of the post data but didn't", EDocLiteTestServlet.postData.contains(dataToFind));
100     }
101 
102     public static boolean waitForData(int maxWaitMilliseconds, int pauseMilliseconds) {
103         boolean valueNotNull = false;
104         boolean interrupted = false;
105         long startTimeMs = System.currentTimeMillis();
106         long endTimeMs = startTimeMs + maxWaitMilliseconds;
107 
108         try {
109             Thread.sleep(pauseMilliseconds / 10); // the first time through, sleep a fraction of the specified time
110         } catch (InterruptedException e) {
111             interrupted = true;
112         }
113         valueNotNull = EDocLiteTestServlet.postData != null;
114         LOG.debug("starting wait loop");
115         while (!interrupted && !valueNotNull && (System.currentTimeMillis() < endTimeMs)) {
116             try {
117                 if (LOG.isDebugEnabled()) {
118                     LOG.debug("sleeping for " + pauseMilliseconds + " ms");
119                 }
120                 Thread.sleep(pauseMilliseconds);
121             } catch (InterruptedException e) {
122                 interrupted = true;
123             }
124             LOG.debug("checking wait loop sentinel");
125             valueNotNull = EDocLiteTestServlet.postData != null;
126         }
127         LOG.debug("finished wait loop (" + valueNotNull + ")");
128 
129         return valueNotNull;
130     }
131 }