/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets 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 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.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

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

public class FileTaskLogsTest
{

  @Rule
  public TemporaryFolder temporaryFolder = new TemporaryFolder();

  @Test
  public void testSimple() throws Exception
  {
    final File tmpDir = temporaryFolder.newFolder();
    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 = temporaryFolder.newFolder();
    
    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);
    }
  }
}

@@ -1212,16 +1212,58 @@
 t.Test;%0A
+import org.junit.rules.ExpectedException;%0A
 import o
@@ -1475,16 +1475,98 @@
 der();%0A%0A
+  @Rule%0A  public ExpectedException expectedException = ExpectedException.none();%0A%0A
   @Test%0A
@@ -2631,65 +2631,8 @@
     
-%0A    try %7B%0A      IOException thrown = null;%0A      %0A      
 fina
@@ -2675,34 +2675,32 @@
 uid/logs%22);%0A    
- 
- 
 final File logFi
@@ -2721,34 +2721,32 @@
 tmpDir, %22log%22);%0A
-  
     Files.write(
@@ -2783,23 +2783,16 @@
 8);%0A
-      %0A  
+%0A
     if
+ 
 (!tm
@@ -2824,17 +2824,21 @@
 %7B%0A      
- 
+throw
  new Run
@@ -2892,23 +2892,19 @@
 %22);%0A    
- 
- 
 %7D%0A%0A
-  
     fina
@@ -2979,325 +2979,169 @@
 ));%0A
+%0A
     
-  try %7B%0A        taskLogs.pushTaskLog(%22foo%22, logFile);%0A      %7D catch(IOException ex) %7B%0A        thrown = ex;%0A      %7D%0A      Assert.assertNotNull(%22pushTaskLog should fail with exception of dir creation error%22, thrown);%0A    %7D%0A    finally %7B%0A      tmpDir.setWritable(true
+expectedException.expect(IOException.class);%0A    expectedException.expectMessage(%22Unable to create task log dir%22
 );%0A    
- 
- FileUtils.deleteDirectory(tmpDir);%0A    %7D
+taskLogs.pushTaskLog(%22foo%22, logFile);
 %0A  %7D

Throw unthrown exception. (#3352)