Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.test.integration.domain.management.util;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CHILD_TYPE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.COMPOSITE;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.NAME;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_ATTRIBUTE_OPERATION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_CHILDREN_NAMES_OPERATION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.READ_CHILDREN_TYPES_OPERATION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RUNNING_SERVER;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_CONFIG;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.STEPS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
 
 import java.util.List;

Author(s):
Emanuel Muckenhuber
 
 public class DomainTestUtils {
 
     private static final int DEFAULT_TIMEOUT = 60;
 
     private DomainTestUtils() {
         //
     }

    
Get the address for a host.

Parameters:
hostName the host name
Returns:
the address
 
     public static ModelNode getHostAddress(final String hostName) {
         final ModelNode address = new ModelNode();
         address.add(hostName);
         return address;
     }

    
Get the address for a running server.

Parameters:
hostName the host name
serverName the server name
Returns:
the address
 
     public static ModelNode getRunningServerAddress(final String hostNamefinal String serverName) {
         final ModelNode address = getHostAddress(hostName);
         address.add(serverName);
         return address;
     }

    
Get the address for a server config.

Parameters:
hostName the host name
serverName the server name
Returns:
the address
 
     public static ModelNode getServerConfigAddress(final String hostNamefinal String serverName) {
         final ModelNode address = getHostAddress(hostName);
        address.add(serverName);
        return address;
    }

    
Create a composite operation.

Parameters:
steps the individual steps
Returns:
the composite operation
    public static ModelNode createCompositeOperation(ModelNode... steps) {
        final ModelNode composite = new ModelNode();
        composite.get().set();
        composite.get().setEmptyList();
        composite.get().setEmptyList();
        for(final ModelNode step : steps) {
            composite.get().add(step);
        }
        return composite;
    }

    
Execute multiple steps and check the result.

Parameters:
client the controller client
steps the individual steps
Returns:
the operation result
Throws:
java.io.IOException
org.jboss.as.test.integration.management.util.MgmtOperationException
    public static List<ModelNodeexecuteStepsForResult(final ModelControllerClient clientfinal ModelNode... stepsthrows IOExceptionMgmtOperationException {
        final ModelNode operationResult = executeForResult(createCompositeOperation(steps), client);
        if(! operationResult.hasDefined()) {
            return Collections.singletonList(operationResult);
        }
        final List<ModelNoderesult = new ArrayList<ModelNode>();
        final int size = operationResult.get().asPropertyList().size();
        for(int i = 0; i < sizei++) {
            result.add(operationResult.get().require("steps-" + (i+1)));
        }
        return result;
    }

    
Execute for a successful result.

Parameters:
op the operation to execute
modelControllerClient the controller client
Returns:
the result
Throws:
java.io.IOException
org.jboss.as.test.integration.management.util.MgmtOperationException
    public static ModelNode executeForResult(final ModelNode opfinal ModelControllerClient modelControllerClientthrows IOExceptionMgmtOperationException {
       final ModelNode ret = modelControllerClient.execute(op);
       if (! .equals(ret.get().asString())) {
           ..println("Failed operation:");
           ..println(op);
           ..println("Response:");
           ..println(ret);
           throw new MgmtOperationException("Management operation failed."opret);
       }
       return ret.get();
    }

    
Execute for a failed outcome.

Parameters:
op the operation to execute
modelControllerClient the controller client
Returns:
the failure description
Throws:
java.io.IOException
org.jboss.as.test.integration.management.util.MgmtOperationException
    public static ModelNode executeForFailure(final ModelNode opfinal ModelControllerClient modelControllerClientthrows IOExceptionMgmtOperationException {
        final ModelNode ret = modelControllerClient.execute(op);
        if (! .equals(ret.get().asString())) {
            ..println(ret);
            throw new MgmtOperationException("Management operation succeeded."opret);
        }
        return ret.get();
    }

    
Wait until a server reached a given state or fail if the timeout was reached.

Parameters:
client the controller client
hostName the host name
serverName the server name
state the required state
Throws:
java.io.IOException
    public static void waitUntilState(final ModelControllerClient clientfinal String hostNamefinal String serverNamefinal String statethrows IOException {
        waitUntilState(clientgetServerConfigAddress(hostNameserverName), state);
    }

    
Wait until a server reached a given state or fail if the timeout was reached.

Parameters:
client the controller client
serverAddress the server address
state the required state
Throws:
java.io.IOException
    public static void waitUntilState(final ModelControllerClient clientfinal ModelNode serverAddressfinal String statethrows IOException {
        waitUntilState(clientserverAddressstate.);
    }

    
Check if a path address exists.

Parameters:
address the child address
client the controller client
Returns:
whether the child exists or not
Throws:
java.io.IOException
org.jboss.as.test.integration.management.util.MgmtOperationException
    public static boolean exists(ModelNode addressModelControllerClient clientthrows IOExceptionMgmtOperationException {
        return exists(PathAddress.pathAddress(address), client);
    }

    
Check if a path address exists.

Parameters:
address the path address
client the controller client
Returns:
whether the child exists or not
Throws:
java.io.IOException
org.jboss.as.test.integration.management.util.MgmtOperationException
    public static boolean exists(PathAddress addressModelControllerClient clientthrows IOExceptionMgmtOperationException {
        final PathElement element = address.getLastElement();
        final PathAddress subAddress = address.subAddress(0, address.size() -1);
        final boolean checkType = element.isWildcard();
        final ModelNode e;
        final ModelNode operation;
        if(checkType) {
            e = new ModelNode().set(element.getKey());
            operation = createOperation(subAddress);
        } else {
            e = new ModelNode().set(element.getValue());
            operation = createOperation(subAddress);
            operation.get().set(element.getKey());
        }
        try {
            final ModelNode result = executeForResult(operationclient);
            return result.asList().contains(e);
        } catch (MgmtOperationException ex) {
            if(! checkType) {
                final String failureDescription = ex.getResult().get().asString();
                if(failureDescription.contains("WFLYCTL0202") && failureDescription.contains(element.getKey())) {
                    return false;
                }
            }
            throw ex;
        }
    }

    
Start a managed server.

Parameters:
connection the mgmt connection
host the host name
server the server name
Returns:
the server state
Throws:
java.io.IOException
org.jboss.as.test.integration.management.util.MgmtOperationException
    public static String startServer(final ModelControllerClient connectionfinal String hostfinal String serverthrows IOExceptionMgmtOperationException {
        return startServer(connectionhostservertrue);
    }

    
Start a managed server.

Parameters:
connection the mgmt connection
host the host name
server the server name
blocking whether to block until the server is started
Returns:
the server state
Throws:
java.io.IOException
org.jboss.as.test.integration.management.util.MgmtOperationException
    public static String startServer(final ModelControllerClient connectionfinal String hostfinal String serverfinal boolean blockingthrows IOExceptionMgmtOperationException {
        final ModelNode address = getServerConfigAddress(hostserver);
        final ModelNode operation = new ModelNode();
        operation.get().set("start");
        operation.get().set(address);
        operation.get("blocking").set(blocking);
        // Start
        executeForResult(operationconnection);
        // Check the starte
        return getServerState(connectionaddress);
    }

    
Wait until a server reached a given state or fail if the timeout was reached.

Parameters:
client the controller client
serverAddress the server address
required the required state
timeout the timeout
unit the time unit
Throws:
java.io.IOException
    public static void waitUntilState(final ModelControllerClient clientfinal ModelNode serverAddressfinal String requiredfinal long timeoutfinal TimeUnit unitthrows IOException {
        final long deadline = System.currentTimeMillis() + unit.toMillis(timeout);
        for(;;) {
            final long remaining = deadline - System.currentTimeMillis();
            if(remaining <= 0) {
                break;
            }
            if (checkState(clientserverAddressrequired)) {
                return;
            }
            try {
                ..sleep(1);
            } catch(InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
        final String state = getServerState(clientserverAddress);
        Assert.assertEquals(serverAddress.toString(), requiredstate);
    }

    
Get the state for a given server.

Parameters:
client the controller client
serverAddress the server config address
Returns:
the server state
Throws:
java.io.IOException
    public static String getServerState(final ModelControllerClient clientfinal ModelNode serverAddressthrows IOException {
        final ModelNode operation = new ModelNode();
        operation.get().set();
        operation.get().set(serverAddress);
        operation.get().set("status");
        ModelNode status = client.execute(operation);
        return status.get().asString();
    }

    
Check the state of server.

Parameters:
client the controller client
serverAddress the server config address
state the expected state
Returns:
true if the state matches, false otherwise
Throws:
java.io.IOException
    public static boolean checkState(final ModelControllerClient clientfinal ModelNode serverAddressfinal String statethrows IOException {
        final String serverState = getServerState(clientserverAddress);
        return state.equals(serverState);
    }
    public static ModelNode createOperation(String opModelNode address) {
        final ModelNode operation = new ModelNode();
        operation.get().set(op);
        operation.get().set(address);
        return operation;
    }
    public static ModelNode createOperation(String opPathAddress address) {
        return createOperation(opaddress.toModelNode());
    }
New to GrepCode? Check out our FAQ X