/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.iotdb.db.integration;

import org.apache.iotdb.db.utils.EnvironmentUtils;
import org.apache.iotdb.itbase.category.LocalStandaloneTest;
import org.apache.iotdb.jdbc.Config;
import org.apache.iotdb.jdbc.IoTDBSQLException;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Notice that, all test begins with "IoTDB" is integration test. All test which will start the
 * IoTDB server should be defined as integration test.
 */
@Category({LocalStandaloneTest.class})
public class IoTDBCreateAlignedTimeseriesIT {

  private Statement statement;
  private Connection connection;

  @Before
  public void setUp() throws Exception {
    EnvironmentUtils.envSetUp();

    Class.forName(Config.JDBC_DRIVER_NAME);
    connection = DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
    statement = connection.createStatement();
  }

  @After
  public void tearDown() throws Exception {
    statement.close();
    connection.close();
    EnvironmentUtils.cleanEnv();
  }

  @Test
  public void testCreateAlignedTimeseries() throws Exception {
    String[] timeSeriesArray =
        new String[] {
          "root.sg1.d1.vector1.s1,FLOAT,PLAIN,UNCOMPRESSED",
          "root.sg1.d1.vector1.s2,INT64,RLE,SNAPPY"
        };

    statement.execute("SET STORAGE GROUP TO root.sg1");
    try {
      statement.execute(
          "CREATE ALIGNED TIMESERIES root.sg1.d1.vector1(s1 FLOAT encoding=PLAIN compressor=UNCOMPRESSED,s2 INT64 encoding=RLE)");
    } catch (IoTDBSQLException ignored) {
    }

    // ensure that current storage group in cache is right.
    assertTimeseriesEquals(timeSeriesArray);

    statement.close();
    connection.close();
    EnvironmentUtils.stopDaemon();
    setUp();

    // ensure storage group in cache is right after recovering.
    assertTimeseriesEquals(timeSeriesArray);
  }

  @Test
  public void testCreateAlignedTimeseriesWithDeletion() throws Exception {
    String[] timeSeriesArray =
        new String[] {
          "root.sg1.d1.vector1.s1,DOUBLE,PLAIN,SNAPPY", "root.sg1.d1.vector1.s2,INT64,RLE,SNAPPY"
        };

    statement.execute("SET STORAGE GROUP TO root.sg1");
    try {
      statement.execute(
          "CREATE ALIGNED TIMESERIES root.sg1.d1.vector1(s1 FLOAT encoding=PLAIN compressor=UNCOMPRESSED,s2 INT64 encoding=RLE)");
      statement.execute("DELETE TIMESERIES root.sg1.d1.vector1.s1");
      statement.execute(
          "CREATE ALIGNED TIMESERIES root.sg1.d1.vector1(s1 DOUBLE encoding=PLAIN compressor=SNAPPY)");
    } catch (IoTDBSQLException e) {
      e.printStackTrace();
    }

    // ensure that current storage group in cache is right.
    assertTimeseriesEquals(timeSeriesArray);

    statement.close();
    connection.close();
    EnvironmentUtils.stopDaemon();
    setUp();

    // ensure storage group in cache is right after recovering.
    assertTimeseriesEquals(timeSeriesArray);
  }

  private void assertTimeseriesEquals(String[] timeSeriesArray) throws SQLException {
    boolean hasResult = statement.execute("SHOW TIMESERIES");
    Assert.assertTrue(hasResult);

    int count = 0;
    try (ResultSet resultSet = statement.getResultSet()) {
      while (resultSet.next()) {
        String ActualResult =
            resultSet.getString("timeseries")
                + ","
                + resultSet.getString("dataType")
                + ","
                + resultSet.getString("encoding")
                + ","
                + resultSet.getString("compression");
        Assert.assertEquals(timeSeriesArray[count], ActualResult);
        count++;
      }
    }
    Assert.assertEquals(timeSeriesArray.length, count);
  }
}
