/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * 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 com.liferay.blade.cli.command;

import com.liferay.blade.cli.Extensions;
import com.liferay.blade.cli.TestUtil;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.powermock.api.easymock.PowerMock;
import org.powermock.reflect.Whitebox;

/**
 * @author Christopher Bryan Boyd
 * @author Gregory Amerson
 */
public class InstallExtensionCommandTest {
	
	@Before
	public void setUp() throws Exception {
		Whitebox.setInternalState(Extensions.class, "_USER_HOME_DIR", temporaryFolder.getRoot());
	}

	@Test
	public void testInstallCustomExtension() throws Exception {
		String[] args = {"extension install", _sampleCommandJarFile.getAbsolutePath()};

		String output = TestUtil.runBlade(args);

		PowerMock.verifyAll();

		Assert.assertTrue("Expected output to contain \"successful\"\n" + output, output.contains(" successful"));

		Assert.assertTrue(output.contains(_sampleCommandJarFile.getName()));

		File root = temporaryFolder.getRoot();

		File extensionJar = new File(root, ".blade/extensions/" + _sampleCommandJarFile.getName());

		Assert.assertTrue(extensionJar.getAbsolutePath() + " does not exist", extensionJar.exists());
	}

	@Test
	public void testInstallCustomGithubExtension() throws Exception {
		
		String[] args = {"extension install", _sampleCommandGithubString};

		String output = TestUtil.runBlade(args);

		PowerMock.verifyAll();

		Assert.assertTrue("Expected output to contain \"successful\"\n" + output, output.contains(" successful"));

		Path rootPath = temporaryFolder.getRoot().toPath();
		
		Path extensionJarPath = rootPath.resolve(Paths.get(".blade", "extensions", "blade-sample-command-master.jar"));
		
		boolean pathExists = Files.exists(extensionJarPath);
		
		Assert.assertTrue(extensionJarPath.toAbsolutePath() + " does not exist", pathExists);
	}
	
	@Rule
	public final TemporaryFolder temporaryFolder = new TemporaryFolder();
	
	private static final String _sampleCommandGithubString = "https://github.com/gamerson/blade-sample-command";

	private static final File _sampleCommandJarFile = new File(System.getProperty("sampleCommandJarFile"));

}
@@ -745,16 +745,17 @@
 stUtil;%0A
+%0A
 import j
@@ -763,24 +763,25 @@
 va.io.File;%0A
+%0A
 import java.
@@ -1186,18 +1186,17 @@
 dTest %7B%0A
-%09
 %0A
+
 %09@Before
@@ -2046,19 +2046,16 @@
 ption %7B%0A
-%09%09%0A
 %09%09String
@@ -2091,33 +2091,29 @@
 %22, _
-sampleCommandGithubString
+SAMPLE_COMMAND_STRING
 %7D;%0A%0A
@@ -2346,19 +2346,17 @@
 Path();%0A
-%09%09
 %0A
+
 %09%09Path e
@@ -2461,19 +2461,17 @@
 jar%22));%0A
-%09%09
 %0A
+
 %09%09boolea
@@ -2521,11 +2521,9 @@
 h);%0A
-%09%09
 %0A
+
 %09%09As
@@ -2609,18 +2609,17 @@
 ts);%0A%09%7D%0A
-%09
 %0A
+
 %09@Rule%0A%09
@@ -2688,17 +2688,16 @@
 lder();%0A
-%09
 %0A%09privat
@@ -2723,33 +2723,29 @@
 ng _
-sampleCommandGithubString
+SAMPLE_COMMAND_STRING
  = %22

Format source