/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.ocelotds.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.*;
import org.mockito.ArgumentCaptor;
import org.ocelotds.Constants;

/**
 *
 * @author hhfrancois
 */
public class AbstractFileServletTest {
	private final String EXPECTED = "line1;\nline2;\nline3;";

	private final AbstractFileServlet instance = new AbstractServletImpl();

	HttpServletRequest request;
	HttpServletResponse response;
	ByteArrayOutputStream out;
	
	private String filepath;
	
	public void init() throws IOException {
		File file = File.createTempFile("ocelot", ".txt");
		try(FileWriter writer = new FileWriter(file)) {
			writer.write(EXPECTED);
		}
		filepath = file.getAbsolutePath();
		request = mock(HttpServletRequest.class);
		response = mock(HttpServletResponse.class);
		out = new ByteArrayOutputStream();
		when(response.getWriter()).thenReturn(new PrintWriter(out));
	}
	
	public void test() throws IOException {
		ArgumentCaptor<String> captureType = ArgumentCaptor.forClass(String.class);
		ArgumentCaptor<Integer> captureLength = ArgumentCaptor.forClass(Integer.class);
		verify(response).setContentType(captureType.capture());
		verify(response).setContentLength(captureLength.capture());
		assertThat(captureType.getValue()).isEqualTo("text/plain");
		assertThat(captureLength.getValue()).isEqualTo((int)EXPECTED.length());
		response.getWriter().close();
		assertThat(new String(out.toByteArray())).isEqualTo(EXPECTED);
	}

	/**
	 * Test of processRequest method, of class AbstractFileServlet.
	 * @throws java.lang.Exception
	 */
	@Test
	public void testProcessRequest() throws Exception {
		System.out.println("processRequest");
		init();
		instance.processRequest(request, response);
		test();
	}

	/**
	 * Test of doGet method, of class AbstractFileServlet.
	 * @throws java.lang.Exception
	 */
	@Test
	public void testDoGet() throws Exception {
		System.out.println("doGet");
		init();
		instance.doGet(request, response);
		test();
	}

	/**
	 * Test of doPost method, of class AbstractFileServlet.
	 * @throws java.lang.Exception
	 */
	@Test
	public void testDoPost() throws Exception {
		System.out.println("doPost");
		init();
		instance.doPost(request, response);
		test();
	}

	/**
	 * Test of getServletInfo method, of class AbstractServlet.
	 */
	/**
	 * Test of getServletInfo method, of class JSServlet.
	 */
	@Test
	public void testGetServletInfo() {
		System.out.println("getServletInfo");
		String result = instance.getServletInfo();
		assertThat(result).isEqualTo("ocelot-servlet");
	}
	
	@Test
	public void testGetProtocol() throws IOException {
		System.out.println("getServletInfo");
		init();
		when(request.isSecure()).thenReturn(false).thenReturn(true);

		String protocol = instance.getProtocol(request);
		assertThat(protocol).isEqualTo(Constants.WS);
		
		protocol = instance.getProtocol(request);
		assertThat(protocol).isEqualTo(Constants.WSS);
	}
	
	public class AbstractServletImpl extends AbstractFileServlet {
		@Override
		protected String getFilename(HttpServletRequest request) {
			return filepath;
		}

		@Override
		protected String getMimetype(HttpServletRequest request) {
			return "text/plain";
		}
	}
}
@@ -566,24 +566,73 @@
 sertions.*;%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import org.m
@@ -654,16 +654,16 @@
 Captor;%0A
-
 import o
@@ -1010,16 +1010,25 @@
 path;%0A%09%0A
+%09@Before%0A
 %09public 
@@ -1418,32 +1418,146 @@
 ter(out));%0A%09%7D%0A%09%0A
+%09@After%0A%09public void after() %7B%0A%09%09File file = new File(filepath);%0A%09%09if(file.exists()) %7B%0A%09%09%09file.delete();%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09public void tes
@@ -2301,34 +2301,24 @@
 sRequest%22);%0A
-%09%09init();%0A
 %09%09instance.p
@@ -2541,34 +2541,24 @@
 n(%22doGet%22);%0A
-%09%09init();%0A
 %09%09instance.d
@@ -2779,26 +2779,16 @@
 Post%22);%0A
-%09%09init();%0A
 %09%09instan
@@ -3201,32 +3201,32 @@
 s IOException %7B%0A
+
 %09%09System.out.pri
@@ -3253,18 +3253,8 @@
 %22);%0A
-%09%09init();%0A
 %09%09wh

improve test with @Before and @After for manage generated files