package com.theironyard;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.theironyard.entities.User;
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;

@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()));
	}



}

@@ -126,24 +126,62 @@
 jectMapper;%0A
+import com.theironyard.entities.Item;%0A
 import com.t
@@ -202,24 +202,24 @@
 ities.User;%0A
-
 import com.t
@@ -998,16 +998,45 @@
 ntext;%0A%0A
+import java.util.ArrayList;%0A%0A
 @RunWith
@@ -2178,12 +2178,589 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void cTestItemCreate() throws Exception %7B%0A%09%09mockMvc.perform(%0A%09%09%09%09MockMvcRequestBuilders.post(%22/item-create%22)%0A%09%09%09%09.sessionAttr(%22username%22, %22Bob%22)%0A%09%09%09%09.param(%22title%22, %22TestTitle%22)%0A%09%09%09%09.param(%22location%22, %22TestLocation%22)%0A%09%09%09%09.param(%22description%22, %22TestDescription%22)%0A%09%09%09%09.param(%22acceptableExchange%22, %22TestSwap%22)%0A%09%09%09%09.param(%22service%22, %22true%22)%0A%09%09);%0A%0A%09%09Iterable%3CItem%3E testItems = items.findByUser(users.findByUsername(%22Bob%22));%0A%09%09ArrayList%3CItem%3E testArr = new ArrayList%3C%3E();%0A%09%09for (Item i : testItems) %7B%0A%09%09%09testArr.add(i);%0A%09%09%7D%0A%09%09Assert.assertTrue(testArr.size() == 1);%0A%09%7D
 %0A%0A%7D%0A

Create add item test (pass)