/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.repo.action.executer;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.management.subsystems.ApplicationContextFactory;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.preference.PreferenceService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.PropertyMap;
import org.alfresco.util.test.junitrules.AlfrescoPerson;
import org.alfresco.util.test.junitrules.ApplicationContextInit;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
/**
* Provides tests for the MailActionExecuter class. Most of this logic was in MailActionExecuterTest.
* Cloud code now includes extra tests and different rule config.
* Unfortunately this is messy due to the extensive use of static variables and Junit rules annotations.
* This class contains most of the test code, child classes actually setup the ClassRules and test fixtures, of
* particular importance is the static setupRuleChain() method in the child classes. The name is just a convention because
* it can't actually be enforced. The setupRuleChain() actually creates the users, as well as ordering the rules.
* You will see the AlfrescoPerson variables below are initialized as null, the assumption is that the child classes will
* create these users before they are needed (in the setupRuleChain() method), again this can't be enforced :(.
*
*/
public abstract class AbstractMailActionExecuterTest
{
// Rule to initialise the default Alfresco spring configuration
public static ApplicationContextInit APP_CONTEXT_INIT = new ApplicationContextInit();
// Rules to create test users, these are actually created in the setupRuleChain() method of child classes.
public static AlfrescoPerson BRITISH_USER = null;
public static AlfrescoPerson FRENCH_USER = null;
public static AlfrescoPerson AUSTRALIAN_USER = null;
private static String ALFRESCO_EE_USER = "plainUser";
protected static TransactionService TRANSACTION_SERVICE;
protected static ActionService ACTION_SERVICE;
protected static MailActionExecuter ACTION_EXECUTER;
protected static PreferenceService PREFERENCE_SERVICE;
protected static PersonService PERSON_SERVICE;
protected static AuthorityService AUTHORITY_SERVICE;
protected static NodeService NODE_SERVICE;
protected static boolean WAS_IN_TEST_MODE;
public static void setupTests(ApplicationContext appCtx)
{
TRANSACTION_SERVICE = appCtx.getBean("TransactionService", TransactionService.class);
ACTION_SERVICE = appCtx.getBean("ActionService", ActionService.class);
ACTION_EXECUTER = appCtx.getBean("OutboundSMTP", ApplicationContextFactory.class).getApplicationContext().getBean("mail", MailActionExecuter.class);
PREFERENCE_SERVICE = appCtx.getBean("PreferenceService", PreferenceService.class);
PERSON_SERVICE = appCtx.getBean("PersonService", PersonService.class);
NODE_SERVICE = appCtx.getBean("NodeService", NodeService.class);
AUTHORITY_SERVICE = appCtx.getBean("AuthorityService", AuthorityService.class);
WAS_IN_TEST_MODE = ACTION_EXECUTER.isTestMode();
ACTION_EXECUTER.setTestMode(true);
AuthenticationUtil.setRunAsUserSystem();
Map properties = new HashMap(1);
properties.put(ContentModel.PROP_USERNAME, ALFRESCO_EE_USER);
properties.put(ContentModel.PROP_EMAIL, "testemail@testdomain.com");
PERSON_SERVICE.createPerson(properties, null);
// All these test users are in the same tenant - either they're enterprise where there's only one,
// or they're cloud, where they have the same email domain
final String tenantId = getUsersHomeTenant(FRENCH_USER.getUsername());
TenantUtil.runAsSystemTenant(new TenantRunAsWork