You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ga...@apache.org on 2015/12/07 11:19:54 UTC

[25/50] [abbrv] stratos git commit: Refactor GitHookTestCase: Use restClient deployEntity. Make artifactUpdateEventCount AtomicInteger. Terminate event receiver and shutdown executor service at the end of test case.

Refactor GitHookTestCase: Use restClient deployEntity. Make artifactUpdateEventCount AtomicInteger. Terminate event receiver and shutdown executor service at the end of test case.


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/cff8f0b9
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/cff8f0b9
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/cff8f0b9

Branch: refs/heads/release-4.1.5
Commit: cff8f0b9334bedf99958ad90466cd1457dc94239
Parents: 8c615ac
Author: Akila Perera <ra...@gmail.com>
Authored: Mon Nov 30 00:24:46 2015 +0530
Committer: gayangunarathne <ga...@wso2.com>
Committed: Mon Dec 7 10:16:26 2015 +0000

----------------------------------------------------------------------
 .../integration/tests/adc/GitHookTestCase.java  | 108 +++++++++----------
 1 file changed, 49 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/cff8f0b9/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/adc/GitHookTestCase.java
----------------------------------------------------------------------
diff --git a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/adc/GitHookTestCase.java b/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/adc/GitHookTestCase.java
index 5e3ff70..3f708db 100644
--- a/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/adc/GitHookTestCase.java
+++ b/products/stratos/modules/integration/test-integration/src/test/java/org/apache/stratos/integration/tests/adc/GitHookTestCase.java
@@ -26,14 +26,14 @@ import org.apache.stratos.common.threading.StratosThreadPool;
 import org.apache.stratos.integration.common.RestConstants;
 import org.apache.stratos.integration.common.TopologyHandler;
 import org.apache.stratos.integration.tests.StratosIntegrationTest;
-import org.apache.stratos.messaging.domain.application.ApplicationStatus;
 import org.apache.stratos.messaging.event.Event;
 import org.apache.stratos.messaging.listener.instance.notifier.ArtifactUpdateEventListener;
 import org.apache.stratos.messaging.message.receiver.instance.notifier.InstanceNotifierEventReceiver;
 import org.testng.annotations.Test;
 
-import java.net.URI;
+import java.io.File;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
@@ -42,24 +42,26 @@ import static org.testng.AssertJUnit.assertTrue;
 /**
  * Test case to test the /repo/notify endpoint
  */
+@Test(groups = { "adc" })
 public class GitHookTestCase extends StratosIntegrationTest {
 
     private static Log log = LogFactory.getLog(GitHookTestCase.class);
-    private static final String RESOURCES_PATH = "/git-hook-test";
-    private int artifactUpdateEvents = 0;
+    private static final String RESOURCES_PATH = File.separator + "git-hook-test";
+    private AtomicInteger artifactUpdateEventCount = new AtomicInteger(0);
     private static final String autoscalePolicyId = "autoscaling-policy-git-hook-test";
     private static final String cartridgeId = "c1-git-hook-test";
     private static final String networkPartitionId = "network-partition-git-hook-test";
     private static final String depPolicyId = "deployment-policy-git-hook-test";
     private static final String applicationId = "git-hook-test";
     private static final String appPolicyId = "application-policy-git-hook-test";
+    private static final String GIT_HOOK_ARTIFACT_FILENAME = "hook-req.json";
+    private static final int ARTIFACT_UPDATED_EXPECTED_COUNT = 2;
+    private ExecutorService eventListenerExecutorService = StratosThreadPool
+            .getExecutorService("stratos.integration.test.git.thread.pool", 5);
 
-    @Test(timeOut = GLOBAL_TEST_TIMEOUT, groups = {"adc", "smoke"})
+    @Test(timeOut = DEFAULT_TEST_TIMEOUT)
     public void sendRepoNotify() throws Exception {
         deployArtifacts();
-        ExecutorService eventListenerExecutorService = StratosThreadPool.getExecutorService(
-                                                                            "stratos.integration.test.git.thread.pool",
-                                                                            5);
         final InstanceNotifierEventReceiver instanceNotifierEventReceiver = new InstanceNotifierEventReceiver();
         eventListenerExecutorService.submit(new Runnable() {
             @Override
@@ -71,57 +73,48 @@ public class GitHookTestCase extends StratosIntegrationTest {
         ArtifactUpdateEventListener artifactUpdateEventListener = new ArtifactUpdateEventListener() {
             @Override
             protected void onEvent(Event event) {
-                artifactUpdateEvents++;
+                artifactUpdateEventCount.addAndGet(1);
             }
         };
 
         instanceNotifierEventReceiver.addEventListener(artifactUpdateEventListener);
+        restClient.addEntity(RESOURCES_PATH + File.separator + GIT_HOOK_ARTIFACT_FILENAME, RestConstants.REPO_NOTIFY,
+                RestConstants.REPO_NOTIFY_NAME);
 
-        String gitHookFile = "hook-req.json";
-        restClient.doPost(
-                new URI(stratosBackendURL + RestConstants.REPO_NOTIFY),
-                restClient.getJsonStringFromFile(RESOURCES_PATH + "/" + gitHookFile));
-
-        while (artifactUpdateEvents < 2) {
-            log.info("Waiting till artifact updated comes in... ");
+        while (artifactUpdateEventCount.get() < ARTIFACT_UPDATED_EXPECTED_COUNT) {
+            log.info("Waiting until artifact updated event is received...");
             Thread.sleep(1000);
         }
-
-        log.info("Waiting for application status to become ACTIVE...");
-        TopologyHandler.getInstance().assertApplicationStatus(applicationId, ApplicationStatus.Active);
-
+        TopologyHandler.getInstance().assertApplicationActiveStatus(applicationId);
+        instanceNotifierEventReceiver.terminate();
+        eventListenerExecutorService.shutdownNow();
         undeployArtifacts();
     }
 
     private void deployArtifacts() throws Exception {
-        boolean autoscalePolicyAdded = restClient.addEntity(
-                RESOURCES_PATH + RestConstants.AUTOSCALING_POLICIES_PATH + "/" + autoscalePolicyId + ".json",
-                RestConstants.AUTOSCALING_POLICIES,
-                RestConstants.AUTOSCALING_POLICIES_NAME);
+        boolean autoscalePolicyAdded = restClient
+                .addEntity(RESOURCES_PATH + RestConstants.AUTOSCALING_POLICIES_PATH + "/" + autoscalePolicyId + ".json",
+                        RestConstants.AUTOSCALING_POLICIES, RestConstants.AUTOSCALING_POLICIES_NAME);
         assertTrue(autoscalePolicyAdded);
 
-        boolean cartridgeAdded = restClient.addEntity(
-                RESOURCES_PATH + RestConstants.CARTRIDGES_PATH + "/" + cartridgeId + ".json",
-                RestConstants.CARTRIDGES,
-                RestConstants.CARTRIDGES_NAME);
+        boolean cartridgeAdded = restClient
+                .addEntity(RESOURCES_PATH + RestConstants.CARTRIDGES_PATH + "/" + cartridgeId + ".json",
+                        RestConstants.CARTRIDGES, RestConstants.CARTRIDGES_NAME);
         assertTrue(cartridgeAdded);
 
-        boolean networkPartitionAdded = restClient.addEntity(
-                RESOURCES_PATH + RestConstants.NETWORK_PARTITIONS_PATH + "/" + networkPartitionId + ".json",
-                RestConstants.NETWORK_PARTITIONS,
-                RestConstants.NETWORK_PARTITIONS_NAME);
+        boolean networkPartitionAdded = restClient
+                .addEntity(RESOURCES_PATH + RestConstants.NETWORK_PARTITIONS_PATH + "/" + networkPartitionId + ".json",
+                        RestConstants.NETWORK_PARTITIONS, RestConstants.NETWORK_PARTITIONS_NAME);
         assertTrue(networkPartitionAdded);
 
-        boolean deploymentPolicyAdded = restClient.addEntity(
-                RESOURCES_PATH + RestConstants.DEPLOYMENT_POLICIES_PATH + "/" + depPolicyId + ".json",
-                RestConstants.DEPLOYMENT_POLICIES,
-                RestConstants.DEPLOYMENT_POLICIES_NAME);
+        boolean deploymentPolicyAdded = restClient
+                .addEntity(RESOURCES_PATH + RestConstants.DEPLOYMENT_POLICIES_PATH + "/" + depPolicyId + ".json",
+                        RestConstants.DEPLOYMENT_POLICIES, RestConstants.DEPLOYMENT_POLICIES_NAME);
         assertTrue(deploymentPolicyAdded);
 
-        boolean applicationAdded = restClient.addEntity(
-                RESOURCES_PATH + RestConstants.APPLICATIONS_PATH + "/" + applicationId + ".json",
-                RestConstants.APPLICATIONS,
-                RestConstants.APPLICATIONS_NAME);
+        boolean applicationAdded = restClient
+                .addEntity(RESOURCES_PATH + RestConstants.APPLICATIONS_PATH + "/" + applicationId + ".json",
+                        RestConstants.APPLICATIONS, RestConstants.APPLICATIONS_NAME);
         assertTrue(applicationAdded);
 
         ApplicationBean bean = (ApplicationBean) restClient
@@ -129,19 +122,18 @@ public class GitHookTestCase extends StratosIntegrationTest {
                         RestConstants.APPLICATIONS_NAME);
         assertEquals(bean.getApplicationId(), applicationId);
 
-        boolean appPolicyAdded = restClient.addEntity(
-                RESOURCES_PATH + RestConstants.APPLICATION_POLICIES_PATH + "/" + appPolicyId + ".json",
-                RestConstants.APPLICATION_POLICIES,
-                RestConstants.APPLICATION_POLICIES_NAME);
+        boolean appPolicyAdded = restClient
+                .addEntity(RESOURCES_PATH + RestConstants.APPLICATION_POLICIES_PATH + "/" + appPolicyId + ".json",
+                        RestConstants.APPLICATION_POLICIES, RestConstants.APPLICATION_POLICIES_NAME);
         assertTrue(appPolicyAdded);
 
         boolean appDeployed = restClient.deployEntity(
-                RestConstants.APPLICATIONS + "/" + applicationId + RestConstants.APPLICATIONS_DEPLOY + "/" + appPolicyId,
-                RestConstants.APPLICATIONS_NAME);
+                RestConstants.APPLICATIONS + "/" + applicationId + RestConstants.APPLICATIONS_DEPLOY + "/"
+                        + appPolicyId, RestConstants.APPLICATIONS_NAME);
         assertTrue(appDeployed);
     }
 
-    private void undeployArtifacts() throws Exception{
+    private void undeployArtifacts() throws Exception {
         log.info(String.format("Un-deploying the application [application id] %s", applicationId));
         String resourcePathUndeploy = RestConstants.APPLICATIONS + "/" + applicationId +
                 RestConstants.APPLICATIONS_UNDEPLOY;
@@ -158,12 +150,13 @@ public class GitHookTestCase extends StratosIntegrationTest {
                     RestConstants.APPLICATIONS_UNDEPLOY + "?force=true", RestConstants.APPLICATIONS);
 
             boolean forceUndeployed = TopologyHandler.getInstance().assertApplicationUndeploy(applicationId);
-            assertTrue(String.format("Forceful undeployment failed for the application %s", applicationId), forceUndeployed);
+            assertTrue(String.format("Forceful undeployment failed for the application %s", applicationId),
+                    forceUndeployed);
         }
 
         log.info("Removing the application [application id] sample-application-startup-test");
-        boolean removedApp = restClient.removeEntity(RestConstants.APPLICATIONS, applicationId,
-                RestConstants.APPLICATIONS_NAME);
+        boolean removedApp = restClient
+                .removeEntity(RestConstants.APPLICATIONS, applicationId, RestConstants.APPLICATIONS_NAME);
         assertTrue(removedApp);
 
         ApplicationBean beanRemoved = (ApplicationBean) restClient
@@ -173,13 +166,12 @@ public class GitHookTestCase extends StratosIntegrationTest {
 
         log.info(String.format("Removing the application policy [application policy id] %s", appPolicyId));
         boolean removeAppPolicy = restClient
-                .removeEntity(RestConstants.APPLICATION_POLICIES, appPolicyId,
-                        RestConstants.APPLICATION_POLICIES_NAME);
+                .removeEntity(RestConstants.APPLICATION_POLICIES, appPolicyId, RestConstants.APPLICATION_POLICIES_NAME);
         assertTrue(removeAppPolicy);
 
         log.info(String.format("Removing the cartridge [cartridge type] %s", cartridgeId));
-        boolean removedC1 = restClient.removeEntity(RestConstants.CARTRIDGES, cartridgeId,
-                RestConstants.CARTRIDGES_NAME);
+        boolean removedC1 = restClient
+                .removeEntity(RestConstants.CARTRIDGES, cartridgeId, RestConstants.CARTRIDGES_NAME);
         assertTrue(removedC1);
 
         log.info(String.format("Removing the autoscaling policy [autoscaling policy id] %s", autoscalePolicyId));
@@ -189,14 +181,12 @@ public class GitHookTestCase extends StratosIntegrationTest {
 
         log.info(String.format("Removing the deployment policy [deployment policy id] %s", depPolicyId));
         boolean removedDep = restClient
-                .removeEntity(RestConstants.DEPLOYMENT_POLICIES, depPolicyId,
-                        RestConstants.DEPLOYMENT_POLICIES_NAME);
+                .removeEntity(RestConstants.DEPLOYMENT_POLICIES, depPolicyId, RestConstants.DEPLOYMENT_POLICIES_NAME);
         assertTrue(removedDep);
 
         log.info(String.format("Removing the network partition [network partition id] %s", networkPartitionId));
-        boolean removedNet = restClient
-                .removeEntity(RestConstants.NETWORK_PARTITIONS, networkPartitionId,
-                        RestConstants.NETWORK_PARTITIONS_NAME);
+        boolean removedNet = restClient.removeEntity(RestConstants.NETWORK_PARTITIONS, networkPartitionId,
+                RestConstants.NETWORK_PARTITIONS_NAME);
         assertTrue(removedNet);
     }
 }