/*
 * Copyright 2014 Cask, 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 co.cask.cdap.explore.service;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileOutputStream;

/**
 *
 */
public class ExploreServiceUtilsTest {

  @ClassRule
  public static TemporaryFolder tmpFolder = new TemporaryFolder();

  @Test
  public void testHiveVersion() throws Exception {
    // This would throw an exception if it didn't pass
    ExploreServiceUtils.checkHiveSupportWithSecurity(new Configuration(), this.getClass().getClassLoader());
  }

  @Test
  public void hijackHiveConfFileTest() throws Exception {
    Configuration conf = new Configuration(false);
    conf.set("foo", "bar");
    Assert.assertEquals(1, conf.size());

    File confFile = tmpFolder.newFile("hive-site.xml");
    FileOutputStream fos = new FileOutputStream(confFile);
    try {
      conf.writeXml(fos);
    } finally {
      fos.close();
    }

    File newConfFile = ExploreServiceUtils.hijackHiveConfFile(confFile);

    conf = new Configuration(false);
    conf.addResource(newConfFile.toURI().toURL());

    // If Hadoop config methods in ConfigUtil.java loaded the deprecated configurations, then
    // mapreduce.user.classpath.first, which is the old hadoop configuration, will not be persisted
    Assert.assertTrue(conf.size() >= 2 && conf.size() <= 3);
    if (conf.size() == 3) {
      Assert.assertEquals("true", conf.get("mapreduce.user.classpath.first"));
    }
    Assert.assertEquals("true", conf.get(Job.MAPREDUCE_JOB_USER_CLASSPATH_FIRST));
    Assert.assertEquals("bar", conf.get("foo"));

    // Ensure conf files that are not hive-site.xml are unchanged
    confFile = tmpFolder.newFile("yarn-site.xml");
    Assert.assertEquals(confFile, ExploreServiceUtils.hijackHiveConfFile(confFile));
  }

}

@@ -1977,16 +1977,90 @@
 rsisted%0A
+    if (Configuration.isDeprecated(%22mapreduce.user.classpath.first%22)) %7B%0A  
     Asse
@@ -2072,13 +2072,18 @@
 sert
-True(
+Equals(2, 
 conf
@@ -2093,44 +2093,53 @@
 ze()
- %3E= 2 && conf.size() %3C= 3);%0A    if (
+);%0A    %7D else %7B%0A      Assert.assertEquals(3, 
 conf
@@ -2149,16 +2149,10 @@
 ze()
- == 3) %7B
+);
 %0A   

Fix test better