/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.jboss.as.test.integration.domain.management.cli;

import static org.jboss.as.test.deployment.DeploymentArchiveUtils.createCliArchive;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.Iterator;

import org.jboss.as.cli.CommandContext;
import org.jboss.as.test.integration.domain.management.util.DomainTestSupport;
import org.jboss.as.test.integration.domain.suites.CLITestSuite;
import org.jboss.as.test.integration.management.base.AbstractCliTestBase;
import org.jboss.as.test.integration.management.util.CLITestUtil;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test case for https://issues.redhat.com/browse/WFCORE-5029
 *
 * @author wangc
 *
 */
public class ServerGroupDeploymentManagedAttributeTestCase extends AbstractCliTestBase {

    protected static File testAppWar;
    protected static CommandContext ctx;
    protected static DomainTestSupport testSupport;

    protected static String sg;

    @BeforeClass
    public static void before() throws Exception {
        testSupport = CLITestSuite.createSupport(ServerGroupDeploymentManagedAttributeTestCase.class.getSimpleName());
        AbstractCliTestBase.initCLI(DomainTestSupport.primaryAddress);

        // deployment
        testAppWar = createCliArchive("testAppWar-deploy-all.war", "Version0");

        final Iterator<String> sgI = CLITestSuite.serverGroups.keySet().iterator();
        if (!sgI.hasNext()) {
            fail("Server groups aren't available.");
        }
        sg = sgI.next();
    }

    @AfterClass
    public static void after() throws Exception {
        ctx.handleSafe("deployment undeploy * --all-relevant-server-groups");

        CLITestSuite.stopSupport();
        AbstractCliTestBase.closeCLI();

        testAppWar.delete();
    }

    @Before
    public void beforeTest() throws Exception {
        ctx = CLITestUtil.getCommandContext(testSupport);
        ctx.connectController();
        ctx.handleSafe("deployment undeploy * --all-relevant-server-groups");
    }

    @After
    public void afterTest() {
        ctx.handleSafe("deployment undeploy * --all-relevant-server-groups");
        ctx.terminateSession();
    }

    @Test
    public void testManagedAttributeValue() throws Exception {
        cli.sendLine("deploy --server-groups=" + sg + " " + testAppWar.getAbsolutePath());
        cli.sendLine("/server-group=" + sg + "/deployment=" + testAppWar.getName() + ":read-attribute(name=managed)");
        String output = cli.readOutput();
        assertTrue("Server group deployment managed value is incorrect in " + output, output.contains("true"));
    }

    @Test
    public void testUnmanagedAttributeValue() throws Exception {
        cli.sendLine("deploy --server-groups=" + sg + " " + testAppWar.getAbsolutePath() + " --unmanaged");
        cli.sendLine("/server-group=" + sg + "/deployment=" + testAppWar.getName() + ":read-attribute(name=managed)");
        String output = cli.readOutput();
        assertTrue("Server group deployment managed value is incorrect in " + output, output.contains("false"));
    }
}
