/*
 * Druid - a distributed column store.
 * Copyright 2012 - 2015 Metamarkets Group Inc.
 *
 * 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 io.druid.indexing.common.tasklogs;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import io.druid.indexing.common.config.FileTaskLogsConfig;
import io.druid.tasklogs.TaskLogs;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class FileTaskLogsTest
{
  @Test
  public void testSimple() throws Exception
  {
    final File tmpDir = Files.createTempDir();
    try {
      final File logDir = new File(tmpDir, "druid/logs");
      final File logFile = new File(tmpDir, "log");
      Files.write("blah", logFile, Charsets.UTF_8);
      final TaskLogs taskLogs = new FileTaskLogs(new FileTaskLogsConfig(logDir));
      taskLogs.pushTaskLog("foo", logFile);

      final Map<Long, String> expected = ImmutableMap.of(0L, "blah", 1L, "lah", -2L, "ah", -5L, "blah");
      for (Map.Entry<Long, String> entry : expected.entrySet()) {
        final byte[] bytes = ByteStreams.toByteArray(taskLogs.streamTaskLog("foo", entry.getKey()).get().getInput());
        final String string = new String(bytes);
        Assert.assertEquals(String.format("Read with offset %,d", entry.getKey()), string, entry.getValue());
      }
    }
    finally {
      FileUtils.deleteDirectory(tmpDir);
    }
  }

  @Test
  public void testPushTaskLogDirCreationFails() throws Exception
  {
    final File tmpDir = Files.createTempDir();
    
    try {
      IOException thrown = null;
      
      final File logDir = new File(tmpDir, "druid/logs");
      final File logFile = new File(tmpDir, "log");
      Files.write("blah", logFile, Charsets.UTF_8);
      
      if(!tmpDir.setWritable(false)) {
        new RuntimeException("failed to make tmp dir read-only");
      }

      final TaskLogs taskLogs = new FileTaskLogs(new FileTaskLogsConfig(logDir));
      try {
        taskLogs.pushTaskLog("foo", logFile);
      } catch(IOException ex) {
        thrown = ex;
      }
      Assert.assertNotNull("pushTaskLog should fail with exception of dir creation error", thrown);
    }
    finally {
      tmpDir.setWritable(true);
      FileUtils.deleteDirectory(tmpDir);
    }
  }
}

@@ -1012,16 +1012,39 @@
 Assert;%0A
+import org.junit.Rule;%0A
 import o
@@ -1134,40 +1134,156 @@
 ap;%0A
-%0Apublic class FileTaskLogsTest%0A%7B
+import org.junit.rules.TemporaryFolder;%0A%0Apublic class FileTaskLogsTest%0A%7B%0A%0A  @Rule%0A  public TemporaryFolder temporaryFolder = new TemporaryFolder();%0A
 %0A  @
@@ -1359,34 +1359,40 @@
 mpDir = 
-Files.createTempDi
+temporaryFolder.newFolde
 r();%0A   
@@ -2320,26 +2320,32 @@
 r = 
-Files.createTempDi
+temporaryFolder.newFolde
 r();

clean temporary files