Files
alfresco-community-repo/source/test-java/org/alfresco/rest/api/tests/PublicApiTestContext.java
Martin Muller a65e2f48f1 Merged RETURN-OF-THE-API (5.2.0) to 5.2.N (5.2.1)
128640 jvonka: V1 REST API: cleanup and rationalise new api tests (re: remotable helpers & runAs user / admin) - round 5
   REPO-113 (also relates to REPO-28, REPO-114, REPO-825)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@129179 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-08-05 13:47:37 +00:00

157 lines
5.2 KiB
Java

/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.tests;
import java.util.ArrayList;
import java.util.List;
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.transaction.RetryingTransactionHelper;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.extensions.webscripts.TestWebScriptServer;
/**
* Helper for Cloud Tests
*/
public class PublicApiTestContext
{
private ApplicationContext applicationContext;
private BaseWebScriptTest test;
private List<String> usersToBeTidied = new ArrayList<String>();
private List<String> invalidDomainsToBeTidied = new ArrayList<String>();
private MutableAuthenticationService authenticationService;
private RetryingTransactionHelper transactionHelper;
public PublicApiTestContext()
{
init(ApplicationContextHelper.getApplicationContext());
}
public PublicApiTestContext(ApplicationContext context)
{
init(context);
}
public PublicApiTestContext(BaseWebScriptTest test)
{
this.test = test;
this.test.setCustomContext("cloud-test-context.xml");
init(test.getServer().getApplicationContext());
}
private void init(ApplicationContext context)
{
applicationContext = context;
transactionHelper = (RetryingTransactionHelper)applicationContext.getBean("retryingTransactionHelper");
authenticationService = (MutableAuthenticationService)applicationContext.getBean("authenticationService");
}
public ApplicationContext getApplicationContext()
{
return applicationContext;
}
public TestWebScriptServer getTestServer()
{
return test.getServer();
}
public String createUserName(String alias, String tenant)
{
StringBuilder sb = new StringBuilder();
sb.append(alias);
if ((tenant != null) && (! tenant.equals(TenantService.DEFAULT_DOMAIN)) && (! alias.contains(TenantService.SEPARATOR)))
{
sb.append(TenantService.SEPARATOR);
sb.append(tenant);
}
return sb.toString();
}
public void addUser(String user)
{
usersToBeTidied.add(user);
}
public void addInvalidDomain(String domain)
{
invalidDomainsToBeTidied.add(domain);
}
public void removeInvalidDomain(String domain)
{
invalidDomainsToBeTidied.remove(domain);
}
public void cleanup()
{
RunAsWork<Void> work = new RunAsWork<Void>()
{
@Override
public Void doWork() throws Exception
{
transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
{
@SuppressWarnings("synthetic-access")
public Void execute() throws Throwable
{
return null;
}
});
return null;
}
};
AuthenticationUtil.runAs(work, AuthenticationUtil.getSystemUserName());
for (final String user : this.usersToBeTidied)
{
transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>()
{
@Override
public Void execute() throws Throwable
{
if (authenticationService.authenticationExists(user))
{
authenticationService.deleteAuthentication(user);
}
return null;
}
});
}
usersToBeTidied.clear();
// finally clear authentication
AuthenticationUtil.clearCurrentSecurityContext();
}
}