From c60406cc54dff0b3650e70b6b0a3e3382059c0c5 Mon Sep 17 00:00:00 2001 From: Jamal Kaabi-Mofrad Date: Thu, 2 Jun 2016 22:19:19 +0000 Subject: [PATCH] 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 --- .../rest/AbstractSingleNetworkSiteTest.java | 5 ++++- .../rest/api/tests/AbstractTestApi.java | 10 ++++++++-- .../rest/api/tests/AbstractTestFixture.java | 18 +++++++++++++++--- .../tests/EnterprisePublicApiTestFixture.java | 9 +++++++-- .../rest/api/tests/EnterpriseTestApi.java | 6 ++++++ .../alfresco/rest/api/tests/NodeApiTest.java | 19 ++++++++++--------- .../alfresco/rest/api/tests/RepoService.java | 6 +++--- 7 files changed, 53 insertions(+), 20 deletions(-) diff --git a/source/test-java/org/alfresco/rest/AbstractSingleNetworkSiteTest.java b/source/test-java/org/alfresco/rest/AbstractSingleNetworkSiteTest.java index eb4bafb51a..f4430bdddd 100644 --- a/source/test-java/org/alfresco/rest/AbstractSingleNetworkSiteTest.java +++ b/source/test-java/org/alfresco/rest/AbstractSingleNetworkSiteTest.java @@ -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.transaction.RetryingTransactionHelper; 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.client.HttpResponse; 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)); - networkOne = getTestFixture().getRandomNetwork(); + getTestFixture(false); + networkOne = getRepoService().createNetwork(this.getClass().getName().toLowerCase(), true); + networkOne.create(); u1 = networkOne.createUser(); tSite = createSite(networkOne, u1, SiteVisibility.PRIVATE); diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java b/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java index 04ab16bc80..4dd1dd7e29 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java @@ -48,8 +48,9 @@ public abstract class AbstractTestApi @Before public void setupTests() throws Exception { - this.applicationContext = getTestFixture().getApplicationContext(); - this.repoService = getTestFixture().getRepoService(); + TestFixture testFixture = getTestFixture(false); + this.applicationContext = testFixture.getApplicationContext(); + this.repoService = testFixture.getRepoService(); this.transactionHelper = (RetryingTransactionHelper)applicationContext.getBean("retryingTransactionHelper"); HttpClientProvider httpClientProvider = (HttpClientProvider)applicationContext.getBean("httpClientProvider"); @@ -197,4 +198,9 @@ public abstract class AbstractTestApi } 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; } diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java b/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java index b43383c76c..5536d5c970 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java @@ -75,16 +75,23 @@ public abstract class AbstractTestFixture implements TestFixture protected abstract RepoService makeRepoService() throws Exception; public void setup() throws Exception + { + setup(true); + } + + public void setup(boolean createTestData) throws Exception { this.jetty = makeJettyComponent(); this.jetty.start(); this.applicationContext = jetty.getApplicationContext(); this.repoService = makeRepoService(); this.transactionHelper = (RetryingTransactionHelper)repoService.getApplicationContext().getBean("retryingTransactionHelper"); - - populateTestData(); - createTestData(); + if (createTestData) + { + populateTestData(); + createTestData(); + } } public RepoService getRepoService() @@ -114,6 +121,11 @@ public abstract class AbstractTestFixture implements TestFixture public TestNetwork getRandomNetwork() { + if (networks.isEmpty()) + { + populateTestData(); + createTestData(); + } int r = random.nextInt(networks.size()); List a = new ArrayList(); a.addAll(networks.values()); diff --git a/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java b/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java index 1475f13c1a..d2dac62340 100644 --- a/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java +++ b/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java @@ -20,16 +20,21 @@ public class EnterprisePublicApiTestFixture extends EnterpriseTestFixture /* * 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) { instance = new EnterprisePublicApiTestFixture(); - instance.setup(); + instance.setup(createTestData); } return instance; } + public synchronized static EnterprisePublicApiTestFixture getInstance() throws Exception + { + return getInstance(true); + } + private EnterprisePublicApiTestFixture() { super(CONFIG_LOCATIONS, CLASS_LOCATIONS, PORT, CONTEXT_PATH, PUBLIC_API_SERVLET_NAME, DEFAULT_NUM_MEMBERS_PER_SITE, false); diff --git a/source/test-java/org/alfresco/rest/api/tests/EnterpriseTestApi.java b/source/test-java/org/alfresco/rest/api/tests/EnterpriseTestApi.java index ee30391a08..0f8fca4419 100644 --- a/source/test-java/org/alfresco/rest/api/tests/EnterpriseTestApi.java +++ b/source/test-java/org/alfresco/rest/api/tests/EnterpriseTestApi.java @@ -7,4 +7,10 @@ public class EnterpriseTestApi extends AbstractTestApi { return EnterprisePublicApiTestFixture.getInstance(); } + + @Override + protected TestFixture getTestFixture(boolean createTestData) throws Exception + { + return EnterprisePublicApiTestFixture.getInstance(createTestData); + } } diff --git a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java index ba20dda18f..e78df55685 100644 --- a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java @@ -32,6 +32,9 @@ import org.alfresco.repo.content.ContentLimitProvider.SimpleFixedLimitProvider; import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.node.archive.NodeArchiveService; 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.rest.api.Nodes; import org.alfresco.rest.api.model.NodeTarget; @@ -162,7 +165,9 @@ public class NodeApiTest extends AbstractBaseApiTest users.add(user1); users.add(user2); - networkOne = getTestFixture().getRandomNetwork(); + getTestFixture(false); + networkOne = getRepoService().createNetwork(NodeApiTest.class.getName().toLowerCase(), true); + networkOne.create(); userOneN1 = networkOne.createUser(); userTwoN1 = networkOne.createUser(); @@ -1520,24 +1525,20 @@ public class NodeApiTest extends AbstractBaseApiTest @Test public void testCopySite() throws Exception { - TestNetwork network = getTestFixture().getRandomNetwork(); - TestPerson cs1 = network.createUser(); - Site tSite = createSite(network.getId(), cs1.getId(), SiteVisibility.PRIVATE); - // create folder - Folder folderResp = createFolder(cs1.getId(), Nodes.PATH_MY, "siteCopytarget"); + Folder folderResp = createFolder(userOneN1.getId(), Nodes.PATH_MY, "siteCopytarget"); String targetId = folderResp.getId(); Map body = new HashMap<>(); body.put("targetParentId", targetId); //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 - post("nodes/"+docLibNodeId+"/copy", cs1.getId(), toJsonAsStringNonNull(body), null, 422); + post("nodes/"+docLibNodeId+"/copy", userOneN1.getId(), toJsonAsStringNonNull(body), null, 422); } diff --git a/source/test-java/org/alfresco/rest/api/tests/RepoService.java b/source/test-java/org/alfresco/rest/api/tests/RepoService.java index ae925c4d25..ba9437d745 100644 --- a/source/test-java/org/alfresco/rest/api/tests/RepoService.java +++ b/source/test-java/org/alfresco/rest/api/tests/RepoService.java @@ -1322,12 +1322,12 @@ public class RepoService public void create() { - if(!getId().equals(TenantService.DEFAULT_DOMAIN)) + if(!getId().equals(TenantService.DEFAULT_DOMAIN) && !tenantAdminService.existsTenant(getId())) { tenantAdminService.createTenant(getId(), "admin".toCharArray()); + numNetworks++; + log("Created network " + getId()); } - numNetworks++; - log("Created network " + getId()); } public TestSite createSite(SiteVisibility siteVisibility)