001 /** 002 * Copyright 2005-2013 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.batch; 017 018 import static org.junit.Assert.assertNotNull; 019 import static org.junit.Assert.assertTrue; 020 import static org.junit.Assert.fail; 021 022 import java.io.File; 023 import java.util.Iterator; 024 import java.util.LinkedList; 025 import java.util.List; 026 import java.util.Map; 027 import java.util.Properties; 028 import java.util.regex.Pattern; 029 030 import javax.servlet.FilterChain; 031 import javax.servlet.ServletRequest; 032 import javax.servlet.ServletResponse; 033 034 import org.apache.struts.action.ActionForward; 035 import org.apache.struts.action.ActionMapping; 036 import org.apache.struts.upload.FormFile; 037 import org.junit.Test; 038 import org.kuali.rice.core.web.impex.IngesterAction; 039 import org.kuali.rice.core.web.impex.IngesterForm; 040 import org.kuali.rice.kew.test.KEWTestCase; 041 import org.kuali.rice.kew.test.web.MockFormFile; 042 import org.kuali.rice.kew.test.web.WorkflowServletRequest; 043 import org.kuali.rice.krad.web.filter.UserLoginFilter; 044 import org.kuali.rice.krad.UserSession; 045 import org.kuali.rice.krad.util.GlobalVariables; 046 import org.kuali.rice.krad.util.KRADConstants; 047 import org.springframework.mock.web.MockHttpServletResponse; 048 049 /** 050 * Tests workflow Struts IngesterAction 051 * @author Kuali Rice Team (rice.collab@kuali.org) 052 */ 053 public class IngesterActionTest extends KEWTestCase { 054 055 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(IngesterActionTest.class); 056 057 private static final String TRANSACTION_FAILED_REGEX = "(?i)^Ingestion failed$"; 058 private static final String SUCCESS_MESSAGE_REGEX_PRE = "(?ism)^Ingested xml doc.*"; 059 private static final String SUCCESS_MESSAGE_REGEX_POST = ".*"; 060 private static final String FAILURE_MESSAGE_REGEX_PRE = "(?ism)^((Failed to ingest xml doc)|(Rolled back doc)).*"; 061 private static final String FAILURE_MESSAGE_REGEX_POST = ".*"; 062 063 private static final String escape(String fileName) { 064 return fileName.replaceAll("\\.", "\\\\."); 065 } 066 067 private static final String getSuccessRegex(String fileName) { 068 return SUCCESS_MESSAGE_REGEX_PRE + escape(fileName) + SUCCESS_MESSAGE_REGEX_POST; 069 } 070 071 private static final String getFailureRegex(String fileName) { 072 return FAILURE_MESSAGE_REGEX_PRE + escape(fileName) + FAILURE_MESSAGE_REGEX_POST; 073 } 074 075 private boolean findMessage(List messages, String regex) { 076 Pattern p = Pattern.compile(regex); 077 Iterator it = messages.iterator(); 078 LOG.error(regex); 079 while (it.hasNext()) { 080 String message = (String) it.next(); 081 LOG.error(message); 082 if (p.matcher(message).matches()) { 083 return true; 084 } 085 } 086 return false; 087 } 088 089 @Test public void testSuccessfulIngestion() throws Exception { 090 testIngestion("IngesterActionTest_success.txt", true); 091 } 092 093 @Test public void testFailedIngestion() throws Exception { 094 testIngestion("IngesterActionTest_failure.txt", false); 095 } 096 097 @SuppressWarnings("unchecked") 098 private void testIngestion(String config, boolean shouldSucceed) throws Exception { 099 IngesterForm form = new IngesterForm(); 100 Properties filesToIngest = new Properties(); 101 filesToIngest.load(getClass().getResourceAsStream(config)); 102 List shouldPass = new LinkedList(); 103 List shouldFail = new LinkedList(); 104 105 // add all test files to form 106 Iterator entries = filesToIngest.entrySet().iterator(); 107 int i = 0; 108 while (entries.hasNext()) { 109 Map.Entry entry = (Map.Entry) entries.next(); 110 String filePath = entry.getKey().toString(); 111 filePath = filePath.replace("${basedir}", getBaseDir()); 112 String fileName = new File(filePath).getName(); 113 if (Boolean.valueOf(entry.getValue().toString()).booleanValue()) { 114 shouldPass.add(fileName); 115 } else { 116 shouldFail.add(fileName); 117 } 118 FormFile file = new MockFormFile(new File(filePath)); 119 form.setFile(i, file); 120 assertTrue(form.getFiles().size() == i+1); 121 i++; 122 } 123 124 assertTrue(form.getFiles().size() > 0); 125 126 // invoke action 127 IngesterAction action = new IngesterAction(); 128 ActionMapping mapping = new ActionMapping(); 129 mapping.addForwardConfig(new ActionForward("view", "/nowhere", false)); 130 WorkflowServletRequest request = new WorkflowServletRequest(); 131 MockHttpServletResponse response = new MockHttpServletResponse(); 132 request.setUser("admin"); 133 // add the user to the session 134 new UserLoginFilter().doFilter(request, response, new FilterChain() { 135 public void doFilter(ServletRequest req, ServletResponse res) { 136 } 137 }); 138 request.setMethod("post"); 139 try { 140 UserSession userSession = (UserSession)request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY); 141 assertNotNull("UserSession should have been established.", userSession); 142 GlobalVariables.setUserSession(userSession); 143 action.execute(mapping, form, request, response); 144 } finally { 145 GlobalVariables.setUserSession(null); 146 } 147 148 // test result 149 List messages = (List) request.getAttribute("messages"); 150 assertNotNull(messages); 151 152 Iterator it = shouldFail.iterator(); 153 while (it.hasNext()) { 154 String file = it.next().toString(); 155 LOG.error("file: " + file); 156 LOG.error("file replaced: " + escape(file)); 157 assertTrue(findMessage(messages, getFailureRegex(file))); 158 } 159 160 161 // test that the global transaction failure message was emitted 162 boolean failed = shouldFail.size() > 0; 163 if (failed && shouldSucceed) { 164 fail("Ingestation failed but should have succeeded"); 165 } else if (!failed && !shouldSucceed) { 166 fail("Ingestation succeeded but should have failed"); 167 } 168 169 if (failed) { 170 assertTrue(findMessage(messages, TRANSACTION_FAILED_REGEX)); 171 } 172 173 it = shouldPass.iterator(); 174 while (it.hasNext()) { 175 if (failed) { 176 assertTrue(findMessage(messages, getFailureRegex(it.next().toString()))); 177 } else { 178 assertTrue(findMessage(messages, getSuccessRegex(it.next().toString()))); 179 } 180 } 181 } 182 }