Merged API-STRIKES-BACK (5.2.0) to HEAD (5.2)

127330 amukha: RA-941: V1 REST API Tests: rationalise the test startup - eg. only create test data when needed ;-)
      - Added checks to defer test data creation until it is required.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127601 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2016-06-02 22:19:19 +00:00
parent 084016b45c
commit c60406cc54
7 changed files with 53 additions and 20 deletions

View File

@@ -23,6 +23,7 @@ import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsStringNonNull
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.rest.api.tests.AbstractBaseApiTest; import org.alfresco.rest.api.tests.AbstractBaseApiTest;
import org.alfresco.rest.api.tests.NodeApiTest;
import org.alfresco.rest.api.tests.RepoService; import org.alfresco.rest.api.tests.RepoService;
import org.alfresco.rest.api.tests.client.HttpResponse; import org.alfresco.rest.api.tests.client.HttpResponse;
import org.alfresco.rest.api.tests.client.data.ContentInfo; import org.alfresco.rest.api.tests.client.data.ContentInfo;
@@ -67,7 +68,9 @@ public class AbstractSingleNetworkSiteTest extends AbstractBaseApiTest
jacksonUtil = new JacksonUtil(applicationContext.getBean("jsonHelper", JacksonHelper.class)); jacksonUtil = new JacksonUtil(applicationContext.getBean("jsonHelper", JacksonHelper.class));
networkOne = getTestFixture().getRandomNetwork(); getTestFixture(false);
networkOne = getRepoService().createNetwork(this.getClass().getName().toLowerCase(), true);
networkOne.create();
u1 = networkOne.createUser(); u1 = networkOne.createUser();
tSite = createSite(networkOne, u1, SiteVisibility.PRIVATE); tSite = createSite(networkOne, u1, SiteVisibility.PRIVATE);

View File

@@ -48,8 +48,9 @@ public abstract class AbstractTestApi
@Before @Before
public void setupTests() throws Exception public void setupTests() throws Exception
{ {
this.applicationContext = getTestFixture().getApplicationContext(); TestFixture testFixture = getTestFixture(false);
this.repoService = getTestFixture().getRepoService(); this.applicationContext = testFixture.getApplicationContext();
this.repoService = testFixture.getRepoService();
this.transactionHelper = (RetryingTransactionHelper)applicationContext.getBean("retryingTransactionHelper"); this.transactionHelper = (RetryingTransactionHelper)applicationContext.getBean("retryingTransactionHelper");
HttpClientProvider httpClientProvider = (HttpClientProvider)applicationContext.getBean("httpClientProvider"); HttpClientProvider httpClientProvider = (HttpClientProvider)applicationContext.getBean("httpClientProvider");
@@ -197,4 +198,9 @@ public abstract class AbstractTestApi
} }
protected abstract TestFixture getTestFixture() throws Exception; protected abstract TestFixture getTestFixture() throws Exception;
/**
* @param createTestData The created instance can optionally create test data if required
*/
protected abstract TestFixture getTestFixture(boolean createTestData) throws Exception;
} }

View File

@@ -75,16 +75,23 @@ public abstract class AbstractTestFixture implements TestFixture
protected abstract RepoService makeRepoService() throws Exception; protected abstract RepoService makeRepoService() throws Exception;
public void setup() throws Exception public void setup() throws Exception
{
setup(true);
}
public void setup(boolean createTestData) throws Exception
{ {
this.jetty = makeJettyComponent(); this.jetty = makeJettyComponent();
this.jetty.start(); this.jetty.start();
this.applicationContext = jetty.getApplicationContext(); this.applicationContext = jetty.getApplicationContext();
this.repoService = makeRepoService(); this.repoService = makeRepoService();
this.transactionHelper = (RetryingTransactionHelper)repoService.getApplicationContext().getBean("retryingTransactionHelper"); this.transactionHelper = (RetryingTransactionHelper)repoService.getApplicationContext().getBean("retryingTransactionHelper");
populateTestData();
createTestData(); if (createTestData)
{
populateTestData();
createTestData();
}
} }
public RepoService getRepoService() public RepoService getRepoService()
@@ -114,6 +121,11 @@ public abstract class AbstractTestFixture implements TestFixture
public TestNetwork getRandomNetwork() public TestNetwork getRandomNetwork()
{ {
if (networks.isEmpty())
{
populateTestData();
createTestData();
}
int r = random.nextInt(networks.size()); int r = random.nextInt(networks.size());
List<TestNetwork> a = new ArrayList<TestNetwork>(); List<TestNetwork> a = new ArrayList<TestNetwork>();
a.addAll(networks.values()); a.addAll(networks.values());

View File

@@ -20,16 +20,21 @@ public class EnterprisePublicApiTestFixture extends EnterpriseTestFixture
/* /*
* Note: synchronized for multi-threaded test access * Note: synchronized for multi-threaded test access
*/ */
public synchronized static EnterprisePublicApiTestFixture getInstance() throws Exception public synchronized static EnterprisePublicApiTestFixture getInstance(boolean createTestData) throws Exception
{ {
if(instance == null) if(instance == null)
{ {
instance = new EnterprisePublicApiTestFixture(); instance = new EnterprisePublicApiTestFixture();
instance.setup(); instance.setup(createTestData);
} }
return instance; return instance;
} }
public synchronized static EnterprisePublicApiTestFixture getInstance() throws Exception
{
return getInstance(true);
}
private EnterprisePublicApiTestFixture() private EnterprisePublicApiTestFixture()
{ {
super(CONFIG_LOCATIONS, CLASS_LOCATIONS, PORT, CONTEXT_PATH, PUBLIC_API_SERVLET_NAME, DEFAULT_NUM_MEMBERS_PER_SITE, false); super(CONFIG_LOCATIONS, CLASS_LOCATIONS, PORT, CONTEXT_PATH, PUBLIC_API_SERVLET_NAME, DEFAULT_NUM_MEMBERS_PER_SITE, false);

View File

@@ -7,4 +7,10 @@ public class EnterpriseTestApi extends AbstractTestApi
{ {
return EnterprisePublicApiTestFixture.getInstance(); return EnterprisePublicApiTestFixture.getInstance();
} }
@Override
protected TestFixture getTestFixture(boolean createTestData) throws Exception
{
return EnterprisePublicApiTestFixture.getInstance(createTestData);
}
} }

View File

@@ -32,6 +32,9 @@ import org.alfresco.repo.content.ContentLimitProvider.SimpleFixedLimitProvider;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.node.archive.NodeArchiveService; import org.alfresco.repo.node.archive.NodeArchiveService;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.tenant.TenantAdminService;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.rest.api.Nodes; import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.NodeTarget; import org.alfresco.rest.api.model.NodeTarget;
@@ -162,7 +165,9 @@ public class NodeApiTest extends AbstractBaseApiTest
users.add(user1); users.add(user1);
users.add(user2); users.add(user2);
networkOne = getTestFixture().getRandomNetwork(); getTestFixture(false);
networkOne = getRepoService().createNetwork(NodeApiTest.class.getName().toLowerCase(), true);
networkOne.create();
userOneN1 = networkOne.createUser(); userOneN1 = networkOne.createUser();
userTwoN1 = networkOne.createUser(); userTwoN1 = networkOne.createUser();
@@ -1520,24 +1525,20 @@ public class NodeApiTest extends AbstractBaseApiTest
@Test @Test
public void testCopySite() throws Exception public void testCopySite() throws Exception
{ {
TestNetwork network = getTestFixture().getRandomNetwork();
TestPerson cs1 = network.createUser();
Site tSite = createSite(network.getId(), cs1.getId(), SiteVisibility.PRIVATE);
// create folder // create folder
Folder folderResp = createFolder(cs1.getId(), Nodes.PATH_MY, "siteCopytarget"); Folder folderResp = createFolder(userOneN1.getId(), Nodes.PATH_MY, "siteCopytarget");
String targetId = folderResp.getId(); String targetId = folderResp.getId();
Map<String, String> body = new HashMap<>(); Map<String, String> body = new HashMap<>();
body.put("targetParentId", targetId); body.put("targetParentId", targetId);
//test that you can't copy a site //test that you can't copy a site
post("nodes/"+tSite.getGuid()+"/copy", cs1.getId(), toJsonAsStringNonNull(body), null, 422); post("nodes/"+userOneN1Site.getGuid()+"/copy", userOneN1.getId(), toJsonAsStringNonNull(body), null, 422);
String docLibNodeId = getSiteContainerNodeId(network.getId(), cs1.getId(), tSite.getId(), "documentLibrary"); String docLibNodeId = getSiteContainerNodeId(networkOne.getId(), userOneN1.getId(), userOneN1Site.getId(), "documentLibrary");
//test that you can't copy a site doclib //test that you can't copy a site doclib
post("nodes/"+docLibNodeId+"/copy", cs1.getId(), toJsonAsStringNonNull(body), null, 422); post("nodes/"+docLibNodeId+"/copy", userOneN1.getId(), toJsonAsStringNonNull(body), null, 422);
} }

View File

@@ -1322,12 +1322,12 @@ public class RepoService
public void create() public void create()
{ {
if(!getId().equals(TenantService.DEFAULT_DOMAIN)) if(!getId().equals(TenantService.DEFAULT_DOMAIN) && !tenantAdminService.existsTenant(getId()))
{ {
tenantAdminService.createTenant(getId(), "admin".toCharArray()); tenantAdminService.createTenant(getId(), "admin".toCharArray());
numNetworks++;
log("Created network " + getId());
} }
numNetworks++;
log("Created network " + getId());
} }
public TestSite createSite(SiteVisibility siteVisibility) public TestSite createSite(SiteVisibility siteVisibility)