package marketing.heyday.hartmann.fotodocumentation.rest;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

/**
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 14 Nov 2024
 */
@TestMethodOrder(OrderAnnotation.class)
public class CustomerPictureResourceTest extends AbstractRestTest {
	private static final Log LOG = LogFactory.getLog(CustomerPictureResourceTest.class);
	private static final String PATH = "api/customer-picture";
	private static final String BASE_UPLOAD = "src/test/resources/upload/";

	@BeforeAll
	public static void init() {
		initDB();
	}

	@Test
	@Order(2)
	public void doAddCustomerPicture() throws IOException {
		LOG.info("doAddCustomerPicture");
		
		assertEquals(3, customerCount());
		assertEquals(5, pictureCount());
		
		String authorization = getBasicHeader();
		LOG.info("authorization: " + authorization);
		String path = deploymentURL + PATH;
		Request request = Request.Post(path).addHeader("Accept", "application/json; charset=utf-8")
				.addHeader("Authorization", authorization)
				.bodyFile(new File(BASE_UPLOAD + "add.json"), ContentType.APPLICATION_JSON);

		HttpResponse httpResponse = executeRequest(request);
		int code = httpResponse.getStatusLine().getStatusCode();
		assertEquals(200, code);
		
		assertEquals(3, customerCount());
		assertEquals(6, pictureCount());
	}
	
	@Test
	@Order(3)
	public void doAddCustomerWithPicture() throws IOException {
		LOG.info("doAddCustomerWithPicture");
		
		assertEquals(3, customerCount());
		assertEquals(6, pictureCount());
		
		String authorization = getBasicHeader();
		LOG.info("authorization: " + authorization);
		String path = deploymentURL + PATH;
		Request request = Request.Post(path).addHeader("Accept", "application/json; charset=utf-8")
				.addHeader("Authorization", authorization)
				.bodyFile(new File(BASE_UPLOAD + "addNewCustomer.json"), ContentType.APPLICATION_JSON);

		HttpResponse httpResponse = executeRequest(request);
		int code = httpResponse.getStatusLine().getStatusCode();
		assertEquals(200, code);
		
		assertEquals(4, customerCount());
		assertEquals(7, pictureCount());
	}
	
	@Test
	@Order(1)
	public void doAddCustomerPictureWrongJson() throws IOException {
		LOG.info("doAddCustomerPictureWrongJson");
		
		String authorization = getBasicHeader();
		LOG.info("authorization: " + authorization);
		String path = deploymentURL + PATH;
		Request request = Request.Post(path).addHeader("Accept", "application/json; charset=utf-8")
				.addHeader("Authorization", authorization)
				.bodyFile(new File(BASE_UPLOAD + "addWrong.json"), ContentType.APPLICATION_JSON);

		HttpResponse httpResponse = executeRequest(request);
		int code = httpResponse.getStatusLine().getStatusCode();
		assertEquals(400, code);
		
		String text = getResponseText(httpResponse, "doGetAll");
		System.out.println(text);
	}

	@Test
	@Order(1)
	public void doAddCustomerPictureNoAuth() throws IOException {
		LOG.info("doAddCustomerPictureNoAuth");

		String path = deploymentURL + PATH;
		Request request = Request.Post(path).addHeader("Accept", "application/json; charset=utf-8")
				.bodyFile(new File(BASE_UPLOAD + "add.json"), ContentType.APPLICATION_JSON);

		HttpResponse httpResponse = executeRequest(request);
		int code = httpResponse.getStatusLine().getStatusCode();
		assertEquals(401, code);
	}

	public static void main(String[] args) throws IOException {

		var test = new CustomerPictureResourceTest();
		test.deploymentURL = "http://localhost:8080/";
		test.username = "adm";
		test.password = "x1t0e7Pb49";

		test.doAddCustomerPictureWrongJson();
	}
}
