package com.theironyard;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.theironyard.entities.Item;
import com.theironyard.entities.User;
import com.theironyard.entities.Work;
import com.theironyard.services.*;
import com.theironyard.utilities.PasswordStorage;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.ArrayList;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = VertoSwapApplication.class)
@WebAppConfiguration

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class VertoSwapApplicationTests {

	@Autowired
	WebApplicationContext wac;

	@Autowired
	UserRepository users;
	@Autowired
	ItemRepository items;
	@Autowired
	WorkRepository works;
	@Autowired
	PhotoRepository photos;
	@Autowired
	MessageaRepository messages;

	MockMvc mockMvc;

	@Before
	public void before() {
		mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
	}

	@Test
	public void aTestCreateAccount() throws Exception {
		mockMvc.perform(
				MockMvcRequestBuilders.post("/account-create")
				.param("username", "Bob")
				.param("password", "123")
		);
		Assert.assertTrue(PasswordStorage.verifyPassword("123", users.findByUsername("Bob").getPassword()));
	}

	@Test
	public void bTestLogin() throws Exception {
		mockMvc.perform(
				MockMvcRequestBuilders.post("/login")
				.param("username", "Bob")
				.param("password", "123")
		);
		Assert.assertTrue(PasswordStorage.verifyPassword("123", users.findByUsername("Bob").getPassword()));
	}

	@Test
	public void cTestItemCreate() throws Exception {
		mockMvc.perform(
				MockMvcRequestBuilders.post("/item-create")
				.sessionAttr("username", "Bob")
				.param("title", "TestTitle")
				.param("location", "TestLocation")
				.param("description", "TestDescription")
				.param("acceptableExchange", "TestSwap")
				.param("service", "true")
		);

		Iterable<Item> testItems = items.findByUser(users.findByUsername("Bob"));
		ArrayList<Item> testArr = new ArrayList<>();
		for (Item i : testItems) {
			testArr.add(i);
		}
		Assert.assertTrue(testArr.size() == 1);
	}

	@Test
	public void dTestWorkCreate() throws Exception {
		mockMvc.perform(
				MockMvcRequestBuilders.post("/work-create")
				.sessionAttr("username", "Bob")
				.param("jobTitle", "TestJob")
				.param("description", "TestJobDesc")
		);
		Iterable<Work> testWork = works.findByUser(users.findByUsername("Bob"));
		ArrayList<Work> workArr = new ArrayList<>();
		for (Work w : testWork) {
			workArr.add(w);
		}
		Assert.assertTrue(workArr.size() == 1);
	}

}

@@ -3041,33 +3041,51 @@
 ;%0A%09%09
-Iterable%3CWork%3E te
+ArrayList%3CWork%3E workArr = (ArrayLi
 st
+%3C
 Work
- =
+%3E)
  wor
@@ -3120,32 +3120,136 @@
 ername(%22Bob%22));%0A
+%09%09Assert.assertTrue(workArr.size() == 1);%0A%0A%09%7D%0A%0A%09@Test%0A%09public void eTestWorkDelete() throws Exception %7B%0A
 %09%09ArrayList%3CWork
@@ -3256,28 +3256,25 @@
 %3E workArr = 
-new 
+(
 ArrayList%3C%3E(
@@ -3275,63 +3275,324 @@
 ist%3C
-%3E();%0A%09%09for (Work w : testWork) %7B%0A%09%09%09workArr.add(w);%0A%09%09%7D
+Work%3E) works.findByUser(users.findByUsername(%22Bob%22));%0A%09%09Work w = workArr.get(0);%0A%0A%09%09mockMvc.perform(%0A%09%09%09%09MockMvcRequestBuilders.post(%22/work-delete%22)%0A%09%09%09%09%09%09.sessionAttr(%22username%22, %22Bob%22)%0A%09%09%09%09%09%09.param(%22id%22, String.valueOf(w.getId()))%0A%09%09);%0A%0A%0A%09%09workArr = (ArrayList%3CWork%3E) works.findByUser(users.findByUsername(%22Bob%22));
 %0A%09%09A
@@ -3626,18 +3626,19 @@
 ze() == 
-1
+0
 );%0A%09%7D%0A%0A
+%0A
 %7D%0A

Create delete work test (pass)