/*
 * 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.tsfile.file.metadata;

import org.apache.tsfile.constant.TestConstant;
import org.apache.tsfile.file.metadata.utils.TestHelper;
import org.apache.tsfile.read.reader.LocalTsFileInput;
import org.apache.tsfile.read.reader.TsFileInput;
import org.apache.tsfile.utils.PublicBAOS;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;

public class TimeseriesMetadataTest {

  public static final String measurementUID = "sensor01";
  public static final int typeLength = 1024;
  final String PATH = TestConstant.BASE_OUTPUT_PATH.concat("outputTimeSeries.tsfile");

  @Before
  public void setUp() {}

  @After
  public void tearDown() {
    File file = new File(PATH);
    if (file.exists()) {
      file.delete();
    }
  }

  @Test
  public void testWriteIntoFile() {
    TimeseriesMetadata timeseriesMetadata =
        TestHelper.createSimpleTimeseriesMetaData(measurementUID);
    serialized(timeseriesMetadata);
    TimeseriesMetadata readMetadata = deSerialized();
    Assert.assertEquals(timeseriesMetadata.toString(), readMetadata.toString());
    readMetadata = deSerializedByTsFileInput();
    Assert.assertEquals(timeseriesMetadata.toString(), readMetadata.toString());
    serialized(readMetadata);
  }

  private TimeseriesMetadata deSerialized() {
    FileInputStream fis = null;
    TimeseriesMetadata metaData = null;
    try {
      fis = new FileInputStream(PATH);
      FileChannel fch = fis.getChannel();
      ByteBuffer buffer = ByteBuffer.allocate((int) fch.size());
      fch.read(buffer);
      buffer.flip();
      metaData = TimeseriesMetadata.deserializeFrom(buffer, true);
      metaData.setChunkMetadataListBuffer(new PublicBAOS());
      return metaData;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return metaData;
  }

  private TimeseriesMetadata deSerializedByTsFileInput() {
    TsFileInput tsFileInput = null;
    TimeseriesMetadata metaData = null;
    try {
      tsFileInput = new LocalTsFileInput(Paths.get(PATH));
      metaData = TimeseriesMetadata.deserializeFrom(tsFileInput, true);
      metaData.setChunkMetadataListBuffer(new PublicBAOS());
      return metaData;
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (tsFileInput != null) {
        try {
          tsFileInput.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return metaData;
  }

  private void serialized(TimeseriesMetadata metaData) {
    File file = new File(PATH);
    if (file.exists()) {
      file.delete();
    }
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(file);
      metaData.serializeTo(fos);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
