package org.examples.spring.databinding;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class DataBindingControllerTest {

	@Autowired
	private WebApplicationContext wac;

	private MockMvc mockMvc;
	
	@Autowired
	private DataBindingController controller;

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

	@Test
	public void addOneUserToAnEmptyList() throws Exception {
		// Initialize the users to an empty List
		ArrayList<User> users = new ArrayList<User>();
		controller.setUsers(users);
		
		// Now POST one user data to be binded and added to the List
		mockMvc.perform(post("/")
				.param("users[0].name", "John")
				.param("users[0].email", "john@mail.com"))
				.andExpect(status().is3xxRedirection());
		
		// Let's check that's true
		assertEquals(1, controller.getUsers().size());
		assertEquals("John", controller.getUsers().get(0).getName());
		assertEquals("john@mail.com", controller.getUsers().get(0).getEmail());
	}

}

@@ -1219,19 +1219,22 @@
 ic void 
-a
+itA
 dd
+s
 OneUserT
@@ -1245,16 +1245,24 @@
 mptyList
+AtIndex0
 () throw
@@ -1760,24 +1760,24 @@
 getName());%0A
-
 %09%09assertEqua
@@ -1842,10 +1842,811 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void addingOneUserToAnEmptyListAtIndex1InsertsANewEmptyUserAtIndex0() throws Exception %7B%0A%09%09// Initialize the users to an empty List%0A%09%09ArrayList%3CUser%3E users = new ArrayList%3CUser%3E();%0A%09%09controller.setUsers(users);%0A%09%09%0A%09%09// Let's intentionally add the user at index 1 to see what will happen to index 0%0A%09%09mockMvc.perform(post(%22/%22)%0A%09%09%09%09.param(%22users%5B1%5D.name%22, %22John%22)%0A%09%09%09%09.param(%22users%5B1%5D.email%22, %22john@mail.com%22))%0A%09%09%09%09.andExpect(status().is3xxRedirection());%0A%09%09%0A%09%09// Let's check that's true%0A%09%09assertEquals(2, controller.getUsers().size());%0A%09%09assertNull(controller.getUsers().get(0).getName());%0A%09%09assertNull(controller.getUsers().get(0).getEmail());%0A%09%09assertEquals(%22John%22, controller.getUsers().get(1).getName());%0A%09%09assertEquals(%22john@mail.com%22, controller.getUsers().get(1).getEmail());%0A%09%7D%0A%0A
 %7D%0A

Test to find out what's at index 0