From 1acc95dad201e7dad8579afe9d418e70f63bd43b Mon Sep 17 00:00:00 2001
From: James Perkins <jperkins@redhat.com>
Date: Mon, 6 Jun 2016 16:15:20 -0700
Subject: [PATCH] ** TEST ONLY [WFCORE-1577] Added a test to show the empty
 subsystem model after a full-replace-deployment.

---
 .../domain/deployment/DeploymentTestCase.java      | 230 +++++++++++++++++++++
 1 file changed, 230 insertions(+)
 create mode 100644 testsuite/domain/src/test/java/org/jboss/as/test/integration/domain/deployment/DeploymentTestCase.java

diff --git a/testsuite/domain/src/test/java/org/jboss/as/test/integration/domain/deployment/DeploymentTestCase.java b/testsuite/domain/src/test/java/org/jboss/as/test/integration/domain/deployment/DeploymentTestCase.java
new file mode 100644
index 0000000..8a13fb6
--- /dev/null
+++ b/testsuite/domain/src/test/java/org/jboss/as/test/integration/domain/deployment/DeploymentTestCase.java
@@ -0,0 +1,230 @@
+/*
+ * Copyright 2016 Red Hat, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.as.test.integration.domain.deployment;
+
+import static org.jboss.as.controller.client.helpers.ClientConstants.CHILD_TYPE;
+import static org.jboss.as.controller.client.helpers.ClientConstants.CONTENT;
+import static org.jboss.as.controller.client.helpers.ClientConstants.DEPLOYMENT;
+import static org.jboss.as.controller.client.helpers.ClientConstants.DEPLOYMENT_FULL_REPLACE_OPERATION;
+import static org.jboss.as.controller.client.helpers.ClientConstants.NAME;
+import static org.jboss.as.controller.client.helpers.ClientConstants.RUNTIME_NAME;
+import static org.jboss.as.controller.client.helpers.ClientConstants.SERVER_GROUP;
+import static org.jboss.as.controller.client.helpers.Operations.createAddOperation;
+import static org.jboss.as.controller.client.helpers.Operations.createAddress;
+import static org.jboss.as.controller.client.helpers.Operations.createOperation;
+import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.EXTENSION;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import org.jboss.as.controller.PathAddress;
+import org.jboss.as.controller.client.ModelControllerClient;
+import org.jboss.as.controller.client.Operation;
+import org.jboss.as.controller.client.OperationBuilder;
+import org.jboss.as.controller.client.helpers.ClientConstants;
+import org.jboss.as.controller.client.helpers.Operations;
+import org.jboss.as.controller.client.helpers.domain.DomainClient;
+import org.jboss.as.controller.operations.common.Util;
+import org.jboss.as.test.integration.domain.ServerManagementTestCase;
+import org.jboss.as.test.integration.domain.extension.ExtensionSetup;
+import org.jboss.as.test.integration.domain.management.util.DomainLifecycleUtil;
+import org.jboss.as.test.integration.domain.management.util.DomainTestSupport;
+import org.jboss.as.test.integration.domain.management.util.DomainTestUtils;
+import org.jboss.dmr.ModelNode;
+import org.jboss.dmr.ModelType;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.exporter.ZipExporter;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
+ */
+public class DeploymentTestCase {
+
+    private static DomainTestSupport testSupport;
+    private static DomainLifecycleUtil domainMasterLifecycleUtil;
+
+    @BeforeClass
+    public static void setupDomain() throws Exception {
+        testSupport = DomainTestSupport.createAndStartSupport(DomainTestSupport.Configuration.create(ServerManagementTestCase.class.getSimpleName(),
+                "domain-configs/domain-minimal.xml", "host-configs/host-master.xml", null));
+
+        domainMasterLifecycleUtil = testSupport.getDomainMasterLifecycleUtil();
+        ExtensionSetup.initialiseProfileIncludesExtension(testSupport);
+        final DomainClient masterClient = domainMasterLifecycleUtil.getDomainClient();
+        DomainTestUtils.executeForResult(Util.createAddOperation(
+                PathAddress.pathAddress(EXTENSION, "org.wildfly.extension.profile-includes-test")), masterClient);
+    }
+
+    @AfterClass
+    public static void tearDownDomain() throws Exception {
+
+        testSupport.stop();
+        testSupport = null;
+        domainMasterLifecycleUtil = null;
+    }
+
+    @Test
+    public void testDomainJarDeployment() throws Exception {
+        final String name = "test.jar";
+        // Create a deployment archive
+        final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, name);
+        archive.add(EmptyAsset.INSTANCE, "META-INF/MANIFEST.MF");
+
+        testDeployment(archive);
+    }
+
+    private void testDeployment(final Archive<?> archive) throws IOException {
+        final ModelControllerClient client = domainMasterLifecycleUtil.getDomainClient();
+        final ModelNode readServerSubsystems = Operations.createOperation(ClientConstants.READ_CHILDREN_NAMES_OPERATION,
+                Operations.createAddress("host", "master", "server", "main-one"));
+        readServerSubsystems.get(ClientConstants.CHILD_TYPE).set(ClientConstants.SUBSYSTEM);
+        final ModelNode readProfileSubsystems = Operations.createOperation(ClientConstants.READ_CHILDREN_NAMES_OPERATION,
+                Operations.createAddress("profile", "default"));
+        readProfileSubsystems.get(ClientConstants.CHILD_TYPE).set(ClientConstants.SUBSYSTEM);
+
+        final String name = archive.getName();
+
+        // Check if a deployment with the name already exists, if not deploy it
+        if (!hasDeployment(client, name)) {
+            execute(client, createDeployAddOperation(archive.as(ZipExporter.class).exportAsInputStream(), name, null));
+            Assert.assertTrue("Deployment " + name + "  was not deployed.", hasDeployment(client, name));
+        }
+
+        // Validate the subsystem child names on the profile
+        ModelNode result = execute(client, readProfileSubsystems);
+        validateSubsystemModel("/profile=default", result);
+
+        // Validate the subsystem child names on a server
+        result = execute(client, readServerSubsystems);
+        validateSubsystemModel("/host=master/server=main-one", result);
+        result = execute(client, readServerSubsystems);
+        validateSubsystemModel("/host=master/server=main-two", result);
+
+        // Fully replace the deployment
+        final Operation fullReplaceOp = createReplaceOperation(archive.as(ZipExporter.class).exportAsInputStream(), name, null);
+        execute(client, fullReplaceOp);
+
+        // Below validates that WFCORE-1577 is fixed, the model should not be missing on the /host=master/server=main-one or main-two
+
+        // Validate the subsystem child names on the profile
+        result = execute(client, readProfileSubsystems);
+        validateSubsystemModel("/profile=default", result);
+
+        // Validate the subsystem child names
+        result = execute(client, readServerSubsystems);
+        validateSubsystemModel("/host=master/server=main-one", result);
+        result = execute(client, readServerSubsystems);
+        validateSubsystemModel("/host=master/server=main-two", result);
+    }
+
+    private void validateSubsystemModel(final String address, final ModelNode subsystemModel) {
+        Assert.assertEquals("List type was expected for the subsystem list at " + address, ModelType.LIST, subsystemModel.getType());
+        Assert.assertFalse("Expected a list of subsystem names with at least one entry got an empty list at " + address,
+                subsystemModel.asList().isEmpty());
+    }
+
+    private static ModelNode execute(final ModelControllerClient client, final ModelNode op) throws IOException {
+        return execute(client, new OperationBuilder(op).build(), false);
+    }
+
+    private static ModelNode execute(final ModelControllerClient client, final Operation op) throws IOException {
+        return execute(client, op, false);
+    }
+
+    private static ModelNode execute(final ModelControllerClient client, final Operation op, final boolean expectFailure) throws IOException {
+        final ModelNode result = client.execute(op);
+        if (Operations.isSuccessfulOutcome(result)) {
+            return Operations.readResult(result);
+        }
+        if (!expectFailure) {
+            throw new RuntimeException(String.format("Failed to execute operation: %s%n%s", op.getOperation(), Operations.getFailureDescription(result)));
+        }
+        return Operations.getFailureDescription(result);
+    }
+
+    private static Operation createReplaceOperation(final InputStream content, final String name, final String runtimeName) throws IOException {
+        final Operations.CompositeOperationBuilder builder = Operations.CompositeOperationBuilder.create(true);
+        final ModelNode op = createOperation(DEPLOYMENT_FULL_REPLACE_OPERATION);
+        op.get(NAME).set(name);
+        if (runtimeName != null) {
+            op.get(RUNTIME_NAME).set(runtimeName);
+        }
+        addContent(builder, op, content);
+        op.get("enable").set(true);
+        builder.addStep(op);
+        return builder.build();
+    }
+
+    private static Operation createDeployAddOperation(final InputStream content, final String name, final String runtimeName) {
+        final Operations.CompositeOperationBuilder builder = Operations.CompositeOperationBuilder.create(true);
+        final ModelNode address = createAddress(DEPLOYMENT, name);
+        final ModelNode addOperation = createAddOperation(address);
+        if (runtimeName != null) {
+            addOperation.get(RUNTIME_NAME).set(runtimeName);
+        }
+        addContent(builder, addOperation, content);
+        builder.addStep(addOperation);
+
+        final ModelNode sgAddress = Operations.createAddress(SERVER_GROUP, "main-server-group", DEPLOYMENT, name);
+        final ModelNode op = Operations.createAddOperation(sgAddress);
+        op.get("enable").set(true);
+        if (runtimeName != null) {
+            op.get(RUNTIME_NAME).set(runtimeName);
+        }
+        builder.addStep(op);
+        return builder.build();
+    }
+
+    private static void addContent(final OperationBuilder builder, final ModelNode op, final InputStream content) {
+        final ModelNode contentNode = op.get(CONTENT);
+        final ModelNode contentItem = contentNode.get(0);
+        contentItem.get(ClientConstants.INPUT_STREAM_INDEX).set(0);
+        builder.addInputStream(content);
+    }
+
+    private static boolean hasDeployment(final ModelControllerClient client, final String name) throws IOException {
+        final ModelNode op = Operations.createOperation(ClientConstants.READ_CHILDREN_NAMES_OPERATION);
+        op.get(CHILD_TYPE).set(DEPLOYMENT);
+        final ModelNode listDeploymentsResult;
+        try {
+            listDeploymentsResult = client.execute(op);
+            // Check to make sure there is an outcome
+            if (Operations.isSuccessfulOutcome(listDeploymentsResult)) {
+                final List<ModelNode> deployments = Operations.readResult(listDeploymentsResult).asList();
+                for (ModelNode deployment : deployments) {
+                    if (name.equals(deployment.asString())) {
+                        return true;
+                    }
+                }
+            } else {
+                throw new IllegalStateException(Operations.getFailureDescription(listDeploymentsResult).asString());
+            }
+        } catch (IOException e) {
+            throw new IllegalStateException(String.format("Could not execute operation '%s'", op), e);
+        }
+        return false;
+    }
+
+}
-- 
2.5.5

