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