001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: // Created on Jun 12, 2006
018: package edu.iu.uis.eden.batch;
019:
020: import java.io.File;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Properties;
026: import java.util.regex.Pattern;
027:
028: import javax.servlet.FilterChain;
029: import javax.servlet.ServletRequest;
030: import javax.servlet.ServletResponse;
031:
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.mock.MockHttpServletResponse;
035: import org.apache.struts.upload.FormFile;
036: import org.junit.Test;
037: import org.kuali.workflow.test.WorkflowTestCase;
038:
039: import edu.iu.uis.eden.batch.web.IngesterAction;
040: import edu.iu.uis.eden.batch.web.IngesterForm;
041: import edu.iu.uis.eden.test.web.MockFormFile;
042: import edu.iu.uis.eden.test.web.WorkflowServletRequest;
043: import edu.iu.uis.eden.web.UserLoginFilter;
044:
045: /**
046: * Tests workflow Struts IngesterAction
047: * @author Aaron Hamid (arh14 at cornell dot edu)
048: */
049: public class IngesterActionTest extends WorkflowTestCase {
050:
051: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
052: .getLogger(IngesterActionTest.class);
053:
054: private static final String TRANSACTION_FAILED_REGEX = "(?i)^Ingestion failed$";
055: private static final String SUCCESS_MESSAGE_REGEX_PRE = "(?ism)^Ingested xml doc.*";
056: private static final String SUCCESS_MESSAGE_REGEX_POST = ".*";
057: private static final String FAILURE_MESSAGE_REGEX_PRE = "(?ism)^((Failed to ingest xml doc)|(Rolled back doc)).*";
058: private static final String FAILURE_MESSAGE_REGEX_POST = ".*";
059:
060: private static final String escape(String fileName) {
061: return fileName.replaceAll("\\.", "\\\\.");
062: }
063:
064: private static final String getSuccessRegex(String fileName) {
065: return SUCCESS_MESSAGE_REGEX_PRE + escape(fileName)
066: + SUCCESS_MESSAGE_REGEX_POST;
067: }
068:
069: private static final String getFailureRegex(String fileName) {
070: return FAILURE_MESSAGE_REGEX_PRE + escape(fileName)
071: + FAILURE_MESSAGE_REGEX_POST;
072: }
073:
074: private boolean findMessage(List messages, String regex) {
075: Pattern p = Pattern.compile(regex);
076: Iterator it = messages.iterator();
077: LOG.error(regex);
078: while (it.hasNext()) {
079: String message = (String) it.next();
080: LOG.error(message);
081: if (p.matcher(message).matches()) {
082: return true;
083: }
084: }
085: return false;
086: }
087:
088: @Test
089: public void testSuccessfulIngestion() throws Exception {
090: testIngestion("IngesterActionTest_success.txt", true);
091: }
092:
093: @Test
094: public void testFailedIngestion() throws Exception {
095: testIngestion("IngesterActionTest_failure.txt", false);
096: }
097:
098: @SuppressWarnings("unchecked")
099: private void testIngestion(String config, boolean shouldSucceed)
100: throws Exception {
101: IngesterForm form = new IngesterForm();
102: Properties filesToIngest = new Properties();
103: filesToIngest.load(getClass().getResourceAsStream(config));
104: List shouldPass = new LinkedList();
105: List shouldFail = new LinkedList();
106:
107: // add all test files to form
108: Iterator entries = filesToIngest.entrySet().iterator();
109: int i = 0;
110: while (entries.hasNext()) {
111: Map.Entry entry = (Map.Entry) entries.next();
112: String filePath = entry.getKey().toString();
113: String fileName = new File(filePath).getName();
114: if (Boolean.valueOf(entry.getValue().toString())
115: .booleanValue()) {
116: shouldPass.add(fileName);
117: } else {
118: shouldFail.add(fileName);
119: }
120: FormFile file = new MockFormFile(new File(filePath));
121: form.setFile(i, file);
122: assertTrue(form.getFiles().size() == i + 1);
123: i++;
124: }
125:
126: assertTrue(form.getFiles().size() > 0);
127:
128: // invoke action
129: IngesterAction action = new IngesterAction();
130: ActionMapping mapping = new ActionMapping();
131: mapping.addForwardConfig(new ActionForward("view", "/nowhere",
132: false));
133: WorkflowServletRequest request = new WorkflowServletRequest();
134: MockHttpServletResponse response = new MockHttpServletResponse();
135: request.setUser("quickstart");
136: // add the user to the session
137: new UserLoginFilter().doFilter(request, response,
138: new FilterChain() {
139: public void doFilter(ServletRequest req,
140: ServletResponse res) {
141: }
142: });
143: request.setMethod("post");
144: action.execute(mapping, form, request, response);
145:
146: // test result
147: List messages = (List) request.getAttribute("messages");
148: assertNotNull(messages);
149:
150: Iterator it = shouldFail.iterator();
151: while (it.hasNext()) {
152: String file = it.next().toString();
153: LOG.error("file: " + file);
154: LOG.error("file replaced: " + escape(file));
155: assertTrue(findMessage(messages, getFailureRegex(file)));
156: }
157:
158: // test that the global transaction failure message was emitted
159: boolean failed = shouldFail.size() > 0;
160: if (failed && shouldSucceed) {
161: fail("Ingestation failed but should have succeeded");
162: } else if (!failed && !shouldSucceed) {
163: fail("Ingestation succeeded but should have failed");
164: }
165:
166: if (failed) {
167: assertTrue(findMessage(messages, TRANSACTION_FAILED_REGEX));
168: }
169:
170: it = shouldPass.iterator();
171: while (it.hasNext()) {
172: if (failed) {
173: assertTrue(findMessage(messages, getFailureRegex(it
174: .next().toString())));
175: } else {
176: assertTrue(findMessage(messages, getSuccessRegex(it
177: .next().toString())));
178: }
179: }
180: }
181: }
|