/*
 * 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 org.ngrinder.script.service;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;
import org.ngrinder.common.exception.NGrinderRuntimeException;
import org.ngrinder.model.User;
import org.ngrinder.script.handler.JythonScriptHandler;

public class FileEntryServiceTest {

	private FileEntryService fileEntryService = new FileEntryService(null, null, null, null);

	@Test
	public void testFileTemplateWithoutOptions() {
		User user = new User();
		user.setUserName("JunHo Yoon");
		String content = fileEntryService.loadTemplate(user, new JythonScriptHandler(), "http://helloworld/myname/is",
						"hello", null);
		assertThat(content, containsString("JunHo Yoon"));
		assertThat(content, containsString("http://helloworld/myname/is"));
	}

	@Test
	public void testFileTemplateWithOptions() {
		String options = "{\"method\":\"POST_TEST\"," +
			"\"headers\":[{\"name\":\"header\",\"value\":\"123\"}," +
			"{\"name\":\"auth\",\"value\":\"no\"}]," +
			"\"params\":[{\"name\":\"pName\",\"value\":\"pValue\"}]," +
			"\"cookies\":[{\"name\":\"cook\",\"value\":\"good\",\"domain\":\"naver.com\",\"path\":\"/home\"}]}";
		User user = new User();
		user.setUserName("Gisoo Gwon");
		String content = fileEntryService.loadTemplate(user, new JythonScriptHandler(), "http://helloworld/myname/is",
						"hello", options);
		System.out.println(content);
		assertThat(content, containsString("Gisoo Gwon"));
		assertThat(content, containsString("http://helloworld/myname/is"));
		assertThat(content, containsString("request1.POST_TEST"));
		assertThat(content, containsString("headers.append(NVPair(\"header\", \"123\"))"));
		assertThat(content, containsString("params.append(NVPair(\"pName\", \"pValue\"))"));
		assertThat(content, containsString("Cookie(\"cook\", \"good\", \"naver.com\", \"/home\""));
	}

	@Test
	public void testFileNameFromUrl() {
		assertThat(fileEntryService.getPathFromUrl("http://helloworld/wow;wow"), is("helloworld/wow_wow"));
		assertThat(fileEntryService.getPathFromUrl("http://hellowor%d/222$wewe"), is("hellowor_d/222_wewe"));
		assertThat(fileEntryService.getPathFromUrl("http://helloworld"), is("helloworld"));
		assertThat(fileEntryService.getPathFromUrl("http://helloworld.com"), is("helloworld.com"));
		assertThat(fileEntryService.getPathFromUrl("http://helloworld.com/wewe.nhn"), is("helloworld.com/wewe.nhn"));
		assertThat(fileEntryService.getPathFromUrl("http://helloworld.com/wewe.nhn?wow=%dd"),
						is("helloworld.com/wewe.nhn"));

	}

	@Test
	public void testPathDivide() {
		String[] dividePathAndFile = fileEntryService.dividePathAndFile("helloworld.com/hello");
		assertThat(dividePathAndFile[0], is("helloworld.com"));
		assertThat(dividePathAndFile[1], is("hello"));

		dividePathAndFile = fileEntryService.dividePathAndFile("helloworld.com");
		assertThat(dividePathAndFile[0], is(""));
		assertThat(dividePathAndFile[1], is("helloworld.com"));
	}

	@Test(expected = NGrinderRuntimeException.class)
	public void testFileNameFromInvalidUrl() {
		fileEntryService.getPathFromUrl("htt22p://helloworld22");
	}

}

@@ -765,16 +765,71 @@
 t.Test;%0A
+import org.ngrinder.AbstractNGrinderTransactionalTest;%0A
 import o
@@ -950,22 +950,16 @@
 handler.
-Jython
 ScriptHa
@@ -967,137 +967,279 @@
 dler
-;%0A%0Apublic class FileEntryServiceTest %7B%0A%0A%09private FileEntryService fileEntryService = new FileEntryService(null, null, null, null)
+Factory;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0A%0Apublic class FileEntryServiceTest extends AbstractNGrinderTransactionalTest %7B%0A%0A%09@Autowired%0A%09private FileEntryService fileEntryService;%0A%0A%09@Autowired%0A%09private ScriptHandlerFactory scriptHandlerFactory
 ;%0A%0A%09
@@ -1403,39 +1403,47 @@
 plate(user, 
-new JythonScrip
+scriptHandlerFactory.ge
 tHandler(), 
@@ -1431,32 +1431,40 @@
 tory.getHandler(
+%22jython%22
 ), %22http://hello
@@ -2125,23 +2125,31 @@
 er, 
-new JythonScrip
+scriptHandlerFactory.ge
 tHan
@@ -2153,16 +2153,24 @@
 Handler(
+%22jython%22
 ), %22http

Fix failed test