/*
 * Copyright (C) 2014 Stratio (http://stratio.com)
 *
 * 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 com.stratio.qa.utils;

import com.stratio.qa.specs.BaseGSpec;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class ZookeeperSecUtilsIT extends BaseGSpec {

    @Test
    public void zookeeperSecTest() throws Exception {
        ZookeeperSecUtils zkUtils = new ZookeeperSecUtils();

        // Connect
        zkUtils.connectZk();

        // createNonEphemeralZnode
        String znodePath = "/mypath";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, false);
        assertThat(zkUtils.exists(znodePath)).isTrue();

        // createAnEphemeralZnode
        znodePath = "/mypath3";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, true);
        assertThat(zkUtils.exists(znodePath)).isTrue();

        // deleteANonEphemeralZnode
        znodePath = "/mypath5";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, false);
        zkUtils.delete(znodePath);
        assertThat(zkUtils.exists(znodePath)).isFalse();

        // deleteAnEphemeralZnode
        znodePath = "/mypath6";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, true);
        zkUtils.delete(znodePath);
        assertThat(zkUtils.exists(znodePath)).isFalse();

        // verifyWriteAndReadDataToAnEphemeralZnode
        String znodeContent = "hello";
        znodePath = "/mypath7";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, znodeContent, false);
        assertThat(zkUtils.zRead(znodePath)).isEqualToIgnoringCase(znodeContent);
        zkUtils.delete(znodePath);

        // Disconnect
        zkUtils.disconnect();
    }

    @Test
    public void zookeeperSecConnectionTest() throws Exception {
        System.setProperty("SECURIZED_ZOOKEEPER", "true");
        String zk_hosts = System.getProperty("ZOOKEEPER_HOSTS", "localhost");
        int timeout = Integer.parseInt(System.getProperty("ZOOKEEPER_SESSION_TIMEOUT", "1"));
        ZookeeperSecUtils zkUtils = new ZookeeperSecUtils(zk_hosts, timeout);

        // Connect
        zkUtils.connectZk();
        System.setProperty("SECURIZED_ZOOKEEPER", "false");

        // createNonEphemeralZnode
        String znodePath = "/mypath";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, false);
        assertThat(zkUtils.exists(znodePath)).isTrue();

        // createAnEphemeralZnode
        znodePath = "/mypath3";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, true);
        assertThat(zkUtils.exists(znodePath)).isTrue();

        // deleteANonEphemeralZnode
        znodePath = "/mypath5";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, false);
        zkUtils.delete(znodePath);
        assertThat(zkUtils.exists(znodePath)).isFalse();

        // deleteAnEphemeralZnode
        znodePath = "/mypath6";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, true);
        zkUtils.delete(znodePath);
        assertThat(zkUtils.exists(znodePath)).isFalse();

        // verifyWriteAndReadDataToAnEphemeralZnode
        String znodeContent = "hello";
        znodePath = "/mypath7";
        if (zkUtils.exists(znodePath)) {
            zkUtils.delete(znodePath);
        }
        zkUtils.zCreate(znodePath, znodeContent, false);
        assertThat(zkUtils.zRead(znodePath)).isEqualToIgnoringCase(znodeContent);
        zkUtils.delete(znodePath);

        // Disconnect
        zkUtils.disconnect();
    }
}

@@ -2503,2051 +2503,8 @@
 );%0A%0A
-        // Disconnect%0A        zkUtils.disconnect();%0A    %7D%0A%0A    @Test%0A    public void zookeeperSecConnectionTest() throws Exception %7B%0A        System.setProperty(%22SECURIZED_ZOOKEEPER%22, %22true%22);%0A        String zk_hosts = System.getProperty(%22ZOOKEEPER_HOSTS%22, %22localhost%22);%0A        int timeout = Integer.parseInt(System.getProperty(%22ZOOKEEPER_SESSION_TIMEOUT%22, %221%22));%0A        ZookeeperSecUtils zkUtils = new ZookeeperSecUtils(zk_hosts, timeout);%0A%0A        // Connect%0A        zkUtils.connectZk();%0A        System.setProperty(%22SECURIZED_ZOOKEEPER%22, %22false%22);%0A%0A        // createNonEphemeralZnode%0A        String znodePath = %22/mypath%22;%0A        if (zkUtils.exists(znodePath)) %7B%0A            zkUtils.delete(znodePath);%0A        %7D%0A        zkUtils.zCreate(znodePath, false);%0A        assertThat(zkUtils.exists(znodePath)).isTrue();%0A%0A        // createAnEphemeralZnode%0A        znodePath = %22/mypath3%22;%0A        if (zkUtils.exists(znodePath)) %7B%0A            zkUtils.delete(znodePath);%0A        %7D%0A        zkUtils.zCreate(znodePath, true);%0A        assertThat(zkUtils.exists(znodePath)).isTrue();%0A%0A        // deleteANonEphemeralZnode%0A        znodePath = %22/mypath5%22;%0A        if (zkUtils.exists(znodePath)) %7B%0A            zkUtils.delete(znodePath);%0A        %7D%0A        zkUtils.zCreate(znodePath, false);%0A        zkUtils.delete(znodePath);%0A        assertThat(zkUtils.exists(znodePath)).isFalse();%0A%0A        // deleteAnEphemeralZnode%0A        znodePath = %22/mypath6%22;%0A        if (zkUtils.exists(znodePath)) %7B%0A            zkUtils.delete(znodePath);%0A        %7D%0A        zkUtils.zCreate(znodePath, true);%0A        zkUtils.delete(znodePath);%0A        assertThat(zkUtils.exists(znodePath)).isFalse();%0A%0A        // verifyWriteAndReadDataToAnEphemeralZnode%0A        String znodeContent = %22hello%22;%0A        znodePath = %22/mypath7%22;%0A        if (zkUtils.exists(znodePath)) %7B%0A            zkUtils.delete(znodePath);%0A        %7D%0A        zkUtils.zCreate(znodePath, znodeContent, false);%0A        assertThat(zkUtils.zRead(znodePath)).isEqualToIgnoringCase(znodeContent);%0A        zkUtils.delete(znodePath);%0A%0A
     

remove zkSec test