/**
   * Starts Syslog server and configures syslog handler in the AS.
   *
   * @see
   *     org.jboss.as.arquillian.api.ServerSetupTask#setup(org.jboss.as.arquillian.container.ManagementClient,
   *     java.lang.String)
   */
  @Override
  public void setup(ManagementClient managementClient, String containerId) throws Exception {
    // clear created server instances (TCP/UDP)
    SyslogServer.shutdown();

    // start and set syslog server
    final String host = Utils.getHost(managementClient);
    SyslogServerConfigIF config = getSyslogConfig();
    config.setUseStructuredData(true);
    config.setHost(host);
    config.setPort(SYSLOG_PORT);
    config.addEventHandler(new BlockedSyslogServerEventHandler());
    final String syslogProtocol = getSyslogProtocol();
    server = SyslogServer.createInstance(syslogProtocol, config);
    // start syslog server
    SyslogServer.getThreadedInstance(syslogProtocol);

    ModelNode op;

    // set logging to syslog
    final ModelNode compositeOp = new ModelNode();
    compositeOp.get(OP).set(COMPOSITE);
    compositeOp.get(OP_ADDR).setEmptyList();
    ModelNode steps = compositeOp.get(STEPS);
    op = Util.createAddOperation(AUDIT_SYSLOG_HANDLER_ADDR);
    op.get(FORMATTER).set(JSON_FORMATTER);
    op.get(SYSLOG_FORMAT).set("RFC5424");
    steps.add(op);
    final ModelNode auditlogSyslogProtocol = addAuditlogSyslogProtocol();
    auditlogSyslogProtocol.get(PORT).set(SYSLOG_PORT);
    auditlogSyslogProtocol.get(HOST).set(host);
    steps.add(auditlogSyslogProtocol);
    Utils.applyUpdate(compositeOp, managementClient.getControllerClient());

    List<ModelNode> protocolSettings = addProtocolSettings();
    if (protocolSettings != null) {
      Utils.applyUpdates(protocolSettings, managementClient.getControllerClient());
    }

    op = Util.createAddOperation(AUDIT_LOG_LOGGER_SYSLOG_HANDLER_ADDR);
    Utils.applyUpdate(op, managementClient.getControllerClient());

    op = Util.getWriteAttributeOperation(AUDIT_LOG_LOGGER_ADDR, LOG_READ_ONLY, false);
    Utils.applyUpdate(op, managementClient.getControllerClient());
  }
  /**
   * Stops syslog server and removes auditlog configuration.
   *
   * @see
   *     org.jboss.as.arquillian.api.ServerSetupTask#tearDown(org.jboss.as.arquillian.container.ManagementClient,
   *     java.lang.String)
   */
  @Override
  public void tearDown(ManagementClient managementClient, String containerId) throws Exception {
    // stop syslog server
    SyslogServer.shutdown();
    server.setThread(null);
    server.getConfig().removeAllEventHandlers();

    ModelNode op = Util.createRemoveOperation(AUDIT_LOG_LOGGER_SYSLOG_HANDLER_ADDR);
    op.get(OPERATION_HEADERS, ROLLBACK_ON_RUNTIME_FAILURE).set(false);
    op.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true);
    Utils.applyUpdate(op, managementClient.getControllerClient());

    op = Util.createRemoveOperation(AUDIT_SYSLOG_HANDLER_ADDR);
    op.get(OPERATION_HEADERS, ROLLBACK_ON_RUNTIME_FAILURE).set(false);
    op.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true);
    Utils.applyUpdate(op, managementClient.getControllerClient());

    Utils.applyUpdate(
        Util.getWriteAttributeOperation(AUDIT_LOG_LOGGER_ADDR, LOG_READ_ONLY, false),
        managementClient.getControllerClient());
  }
  @After
  public void afterTest() throws Exception {
    // stop syslog server
    SyslogServer.shutdown();
    server.setThread(null);
    server.getConfig().removeAllEventHandlers();

    final ModelControllerClient client = TestSuiteEnvironment.getModelControllerClient();
    ModelNode op =
        Util.getWriteAttributeOperation(
            auditLogConfigAddress,
            AuditLogLoggerResourceDefinition.ENABLED.getName(),
            new ModelNode(false));
    client.execute(op);
    op =
        Util.getWriteAttributeOperation(
            mgmtRealmConfigAddress, "default-user", new ModelNode("$local"));
    client.execute(op);

    op = Util.getResourceRemoveOperation(addSyslogHandler);
    client.execute(op);
    op = Util.getResourceRemoveOperation(syslogHandlerAddress);
    client.execute(op);

    if (file.exists()) {
      file.delete();
    }
    if (syslogFile.exists()) {
      syslogFile.delete();
    }
    try {
      // Stop the container
      container.stop(CONTAINER);
    } finally {
      IoUtils.safeClose(client);
    }
  }
  @Before
  public void beforeTest() throws Exception {
    file = new File(System.getProperty("jboss.home"));
    file = new File(file, "standalone");
    file = new File(file, "data");
    file = new File(file, "audit-log.log");
    if (file.exists()) {
      file.delete();
    }

    syslogFile = new File(System.getProperty("jboss.home"));
    syslogFile = new File(syslogFile, "standalone");
    syslogFile = new File(syslogFile, "data");
    syslogFile = new File(syslogFile, "syslog-audit-log.log");
    if (syslogFile.exists()) {
      syslogFile.delete();
    }

    // start and set syslog server
    server = SyslogServer.getInstance("udp");
    server.getConfig().setPort(PORT);
    queue = new LinkedBlockingQueue<String>();
    server
        .getConfig()
        .addEventHandler(
            new BlockedFileSyslogServerEventHandler(queue, syslogFile.getAbsolutePath(), false));
    SyslogServer.getThreadedInstance("udp");

    // Start the server
    container.start(CONTAINER);
    final ModelControllerClient client = TestSuiteEnvironment.getModelControllerClient();
    managementClient =
        new ManagementClient(
            client,
            TestSuiteEnvironment.getServerAddress(),
            TestSuiteEnvironment.getServerPort(),
            "http-remoting");

    ModelNode op;
    ModelNode result;

    mgmtRealmConfigAddress =
        PathAddress.pathAddress(
            PathElement.pathElement(CORE_SERVICE, MANAGEMENT),
            PathElement.pathElement(SECURITY_REALM, "ManagementRealm"),
            PathElement.pathElement(AUTHENTICATION, LOCAL));
    op =
        Util.getWriteAttributeOperation(
            mgmtRealmConfigAddress, "default-user", new ModelNode("IAmAdmin"));
    result = client.execute(op);

    auditLogConfigAddress =
        PathAddress.pathAddress(
            CoreManagementResourceDefinition.PATH_ELEMENT,
            AccessAuditResourceDefinition.PATH_ELEMENT,
            AuditLogLoggerResourceDefinition.PATH_ELEMENT);

    op =
        Util.getWriteAttributeOperation(
            auditLogConfigAddress,
            AuditLogLoggerResourceDefinition.ENABLED.getName(),
            new ModelNode(true));
    result = client.execute(op);
    Assert.assertEquals(
        result.get("failure-description").asString(), SUCCESS, result.get(OUTCOME).asString());

    ModelNode compositeOp = new ModelNode();
    compositeOp.get(OP).set(COMPOSITE);
    compositeOp.get(OP_ADDR).setEmptyList();
    ModelNode steps = compositeOp.get(STEPS);
    syslogHandlerAddress =
        PathAddress.pathAddress(
            PathElement.pathElement(CORE_SERVICE, MANAGEMENT),
            PathElement.pathElement(ACCESS, AUDIT),
            PathElement.pathElement(SYSLOG_HANDLER, SYSLOG_HANDLER_NAME));
    op = Util.createAddOperation(syslogHandlerAddress);
    op.get(FORMATTER).set(JSON_FORMATTER);
    op.get(SYSLOG_FORMAT).set("RFC5424");
    steps.add(op);
    op = new ModelNode();
    PathAddress syslogProtocol =
        PathAddress.pathAddress(syslogHandlerAddress, PathElement.pathElement(PROTOCOL, UDP));
    op = Util.createAddOperation(syslogProtocol);
    op.get("port").set(PORT);
    op.get("host").set("localhost");
    steps.add(op);
    result = client.execute(compositeOp);
    Assert.assertEquals(
        result.get("failure-description").asString(), SUCCESS, result.get(OUTCOME).asString());

    addSyslogHandler =
        PathAddress.pathAddress(
            auditLogConfigAddress, PathElement.pathElement(HANDLER, SYSLOG_HANDLER_NAME));
    op = Util.createAddOperation(addSyslogHandler);
    result = client.execute(op);
    Assert.assertEquals(
        result.get("failure-description").asString(), SUCCESS, result.get(OUTCOME).asString());

    container.stop(CONTAINER);
    Thread.sleep(1000);
    while (managementClient.isServerInRunningState()) {
      Thread.sleep(50);
    }
  }