package org.rocksdb;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;

import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;
import static org.assertj.core.api.Assertions.assertThat;

public class InfoLogLevelTest {

  @ClassRule
  public static final RocksMemoryResource rocksMemoryResource =
      new RocksMemoryResource();

  @Rule
  public TemporaryFolder dbFolder = new TemporaryFolder();

  @Test
  public void testInfoLogLevel() throws RocksDBException,
      IOException {
    RocksDB db = null;
    try {
      db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
      db.put("key".getBytes(), "value".getBytes());
      assertThat(getLogContents()).isNotEmpty();
    } finally {
      if (db != null) {
        db.close();
      }
    }
  }

  @Test
     public void testFatalLogLevel() throws RocksDBException,
      IOException {
    RocksDB db = null;
    Options options = null;
    try {
      options = new Options().
          setCreateIfMissing(true).
          setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
      assertThat(options.infoLogLevel()).
          isEqualTo(InfoLogLevel.FATAL_LEVEL);
      db = RocksDB.open(options,
          dbFolder.getRoot().getAbsolutePath());
      db.put("key".getBytes(), "value".getBytes());
      // As InfoLogLevel is set to FATAL_LEVEL, here we expect the log
      // content to be empty.
      assertThat(getLogContents()).isEmpty();
    } finally {
      if (db != null) {
        db.close();
      }
      if (options != null) {
        options.dispose();
      }
    }
  }

  @Test
  public void testFatalLogLevelWithDBOptions()
      throws RocksDBException, IOException {
    RocksDB db = null;
    Options options = null;
    DBOptions dbOptions = null;
    try {
      dbOptions = new DBOptions().
          setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
      options = new Options(dbOptions,
          new ColumnFamilyOptions()).
          setCreateIfMissing(true);
      assertThat(dbOptions.infoLogLevel()).
          isEqualTo(InfoLogLevel.FATAL_LEVEL);
      assertThat(options.infoLogLevel()).
          isEqualTo(InfoLogLevel.FATAL_LEVEL);
      db = RocksDB.open(options,
          dbFolder.getRoot().getAbsolutePath());
      db.put("key".getBytes(), "value".getBytes());
      assertThat(getLogContents()).isEmpty();
    } finally {
      if (db != null) {
        db.close();
      }
      if (options != null) {
        options.dispose();
      }
      if (dbOptions != null) {
        dbOptions.dispose();
      }
    }
  }

  @Test(expected = IllegalArgumentException.class)
  public void failIfIllegalByteValueProvided() {
    InfoLogLevel.getInfoLogLevel((byte)-1);
  }

  @Test
  public void valueOf() {
    assertThat(InfoLogLevel.valueOf("DEBUG_LEVEL")).
        isEqualTo(InfoLogLevel.DEBUG_LEVEL);
  }

  /**
   * Read LOG file contents into String.
   *
   * @return LOG file contents as String.
   * @throws IOException if file is not found.
   */
  private String getLogContents() throws IOException {
    return new String(readAllBytes(get(
        dbFolder.getRoot().getAbsolutePath()+ "/LOG")));
  }
}

@@ -778,24 +778,37 @@
 tLogContents
+WithoutHeader
 ()).isNotEmp
@@ -805,32 +805,32 @@
 ).isNotEmpty();%0A
-
     %7D finally %7B%0A
@@ -1510,32 +1510,45 @@
 t(getLogContents
+WithoutHeader
 ()).isEmpty();%0A 
@@ -2428,16 +2428,29 @@
 Contents
+WithoutHeader
 ()).isEm
@@ -3131,16 +3131,29 @@
 Contents
+WithoutHeader
 () throw
@@ -3176,98 +3176,670 @@
     
-return new String(readAllBytes(get(%0A        dbFolder.getRoot().getAbsolutePath()+ %22/LOG%22))
+final String separator = System.getProperty(%22line.separator%22);%0A    final String%5B%5D lines = new String(readAllBytes(get(%0A        dbFolder.getRoot().getAbsolutePath()+ %22/LOG%22))).split(separator);%0A%0A    int first_non_header = lines.length;%0A    // Identify the last line of the header%0A    for (int i = lines.length - 1; i %3E= 0; --i) %7B%0A      if (lines%5Bi%5D.indexOf(%22Options.%22) %3E= 0 && lines%5Bi%5D.indexOf(':') %3E= 0) %7B%0A        first_non_header = i + 1;%0A        break;%0A      %7D%0A    %7D%0A    StringBuilder builder = new StringBuilder();%0A    for (int i = first_non_header; i %3C lines.length; ++i) %7B%0A      builder.append(lines%5Bi%5D).append(separator);%0A    %7D%0A    return builder.toString(
 );%0A 

Fix test failure of InfoLogLevelTest