/*
 * Copyright © 2014-2015 Cask Data, 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.internal.app.deploy;

import co.cask.cdap.ToyApp;
import co.cask.cdap.WebCrawlApp;
import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms;
import co.cask.cdap.internal.app.deploy.pipeline.DeploymentInfo;
import co.cask.cdap.internal.app.namespace.NamespaceAdmin;
import co.cask.cdap.internal.test.AppJarHelper;
import co.cask.cdap.proto.ProgramType;
import co.cask.cdap.test.internal.AppFabricTestHelper;
import co.cask.cdap.test.internal.DefaultId;
import org.apache.twill.filesystem.LocalLocationFactory;
import org.apache.twill.filesystem.Location;
import org.apache.twill.filesystem.LocationFactory;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.jar.Manifest;

/**
 * Tests the functionality of Deploy Manager.
 */
public class LocalApplicationManagerTest {
  @ClassRule
  public static final TemporaryFolder TMP_FOLDER = new TemporaryFolder();

  private static LocationFactory lf;
  private static File temp;

  @BeforeClass
  public static void before() throws Exception {
    lf = new LocalLocationFactory(TMP_FOLDER.newFolder());
    temp = TMP_FOLDER.newFolder("pipeline");

    NamespaceAdmin namespaceAdmin = AppFabricTestHelper.getInjector().getInstance(NamespaceAdmin.class);
    namespaceAdmin.createNamespace(Constants.DEFAULT_NAMESPACE_META);
  }

  /**
   * Improper Manifest file should throw an exception.
   */
  @Test(expected = ExecutionException.class)
  public void testImproperOrNoManifestFile() throws Exception {
    Location deployedJar = AppJarHelper.createDeploymentJar(lf, WebCrawlApp.class, new Manifest());
    Location destination = new LocalLocationFactory().create(temp.toURI());
    DeploymentInfo info = new DeploymentInfo(new File(deployedJar.toURI()), destination);

    try {
      AppFabricTestHelper.getLocalManager().deploy(DefaultId.NAMESPACE, null, info).get();
    } finally {
      deployedJar.delete(true);
    }
  }

  /**
   * Good pipeline with good tests.
   */
  @Test
  public void testGoodPipeline() throws Exception {
    Location deployedJar = AppJarHelper.createDeploymentJar(lf, ToyApp.class);
    Location destination = new LocalLocationFactory().create(temp.toURI()).append("test.jar");
    DeploymentInfo info = new DeploymentInfo(new File(deployedJar.toURI()), destination);

    ApplicationWithPrograms input = AppFabricTestHelper.getLocalManager().deploy(DefaultId.NAMESPACE,
                                                                                 null, info).get();

    Assert.assertEquals(input.getPrograms().iterator().next().getType(), ProgramType.FLOW);
    Assert.assertEquals(input.getPrograms().iterator().next().getName(), "ToyFlow");
  }

  @AfterClass
  public static void cleanup() {
    TMP_FOLDER.delete();
  }

}

@@ -671,41 +671,8 @@
 pp;%0A
-import co.cask.cdap.WebCrawlApp;%0A
 impo
@@ -1253,37 +1253,8 @@
 ry;%0A
-import org.junit.AfterClass;%0A
 impo
@@ -1413,24 +1413,57 @@
 va.io.File;%0A
+import java.io.FileOutputStream;%0A
 import java.
@@ -1494,24 +1494,93 @@
 nException;%0A
+import java.util.jar.JarEntry;%0Aimport java.util.jar.JarOutputStream;%0A
 import java.
@@ -1824,36 +1824,8 @@
  lf;
-%0A  private static File temp;
 %0A%0A  
@@ -1948,53 +1948,8 @@
 ());
-%0A    temp = TMP_FOLDER.newFolder(%22pipeline%22);
 %0A%0A  
@@ -2310,103 +2310,300 @@
     
-Location deployedJar = AppJarHelper.createDeploymentJar(lf, WebCrawlApp.class, new Manifest());
+// Create an JAR without the MainClass set.%0A    File deployFile = TMP_FOLDER.newFile();%0A    JarOutputStream output = new JarOutputStream(new FileOutputStream(deployFile), new Manifest());%0A    try %7B%0A      output.putNextEntry(new JarEntry(%22dummy%22));%0A    %7D finally %7B%0A      output.close();%0A    %7D%0A
 %0A   
@@ -2652,36 +2652,78 @@
 actory().create(
-temp
+new File(TMP_FOLDER.newFolder(), %22deploy.jar%22)
 .toURI());%0A    D
@@ -2766,37 +2766,18 @@
 nfo(
-new File(deployedJar.toURI())
+deployFile
 , de
@@ -2789,28 +2789,16 @@
 tion);%0A%0A
-    try %7B%0A  
     AppF
@@ -2882,62 +2882,8 @@
 ();%0A
-    %7D finally %7B%0A      deployedJar.delete(true);%0A    %7D%0A
   %7D%0A
@@ -3135,39 +3135,62 @@
 ate(
-temp.toURI()).append(%22test.jar%22
+new File(TMP_FOLDER.newFolder(), %22deploy.jar%22).toURI()
 );%0A 
@@ -3667,84 +3667,6 @@
   %7D%0A
-%0A  @AfterClass%0A  public static void cleanup() %7B%0A    TMP_FOLDER.delete();%0A  %7D%0A%0A
 %7D%0A

fix unit-test.