View Javadoc

1   package org.lanyonm.playground.web.controller;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
5   import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
6   import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
7   import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
8   import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
9   import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
10  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
12  
13  import java.util.List;
14  
15  import org.junit.Before;
16  import org.junit.Rule;
17  import org.junit.Test;
18  import org.junit.runner.RunWith;
19  import org.lanyonm.playground.config.DataConfig;
20  import org.lanyonm.playground.config.ViewResolver;
21  import org.lanyonm.playground.config.WebConfig;
22  import org.lanyonm.playground.domain.Todo;
23  import org.lanyonm.playground.service.ExceptionServiceImpl;
24  import org.lanyonm.playground.service.TodoServiceImpl;
25  import org.lanyonm.playground.service.UserServiceImpl;
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.restdocs.RestDocumentation;
28  import org.springframework.test.context.ContextConfiguration;
29  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30  import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
31  import org.springframework.test.context.web.WebAppConfiguration;
32  import org.springframework.test.web.servlet.MockMvc;
33  import org.springframework.test.web.servlet.MvcResult;
34  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
35  import org.springframework.web.context.WebApplicationContext;
36  
37  /**
38   * @author lanyonm
39   */
40  @RunWith(SpringJUnit4ClassRunner.class)
41  @WebAppConfiguration
42  @ContextConfiguration(loader=AnnotationConfigWebContextLoader.class, classes={DataConfig.class, ExceptionServiceImpl.class, TodoServiceImpl.class, UserServiceImpl.class, ViewResolver.class, WebConfig.class})
43  public class TodoControllerTest {
44  
45  	@Rule
46  	public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
47  
48  	@Autowired
49  	private WebApplicationContext wac;
50  
51  	private MockMvc mockMvc;
52  
53  	@Before
54  	public void setup() {
55  		this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
56  				.apply(documentationConfiguration(this.restDocumentation))
57  				.build();
58  	}
59  
60  	@Test
61  	public void testIndex() throws Exception {
62  		this.mockMvc.perform(get("/todo/")).andExpect(status().isOk())
63  				.andExpect(view().name("todo/index"))
64  				.andExpect(model().attributeExists("todoList"))
65  				.andDo(document("todo"));
66  	}
67  
68  	@Test
69  	public void testTodoAddEditShow() throws Exception {
70  		this.mockMvc.perform(get("/todo/{id}/edit", Integer.MAX_VALUE))
71  				.andExpect(status().is(302))
72  				.andExpect(redirectedUrl("/todo/"));
73  		this.mockMvc.perform(get("/todo/{id}/edit", 0))
74  				.andExpect(status().isOk())
75  				.andExpect(view().name("todo/addEdit"))
76  				.andExpect(model().attribute("todo", new Todo()));
77  		this.mockMvc.perform(get("/todo/{id}/edit", 1))
78  				.andExpect(status().isOk())
79  				.andExpect(view().name("todo/addEdit"))
80  				.andExpect(model().attributeExists("todo"));
81  	}
82  
83  	@Test
84  	public void testTodoEdit() throws Exception {
85  		// TODO add coverage for nefarious edit
86  		this.mockMvc.perform(post("/todo/{id}/edit", 1).param("title", "furst tudo"))
87  				.andExpect(status().is(302))
88  				.andExpect(redirectedUrl("/todo/"));
89  	}
90  
91  	@SuppressWarnings("unchecked")
92  	@Test
93  	public void testTodoAddDelete() throws Exception {
94  		this.mockMvc.perform(post("/todo/{id}/edit", 0).param("title", "second todo"))
95  				.andExpect(redirectedUrl("/todo/"));
96  		MvcResult result = this.mockMvc.perform(get("/todo/"))
97  				.andExpect(status().isOk())
98  				.andExpect(model().attributeExists("todoList"))
99  				.andReturn();
100 		List<Todo> todoList = (List<Todo>) result.getModelAndView().getModel().get("todoList");
101 		assertEquals("there should be two todos", todoList.size(), 2);
102 		long todoId = todoList.get(todoList.size() - 1).getId();
103 		this.mockMvc.perform(get("/todo/{id}/delete", todoId))
104 				.andExpect(status().is(302))
105 				.andExpect(redirectedUrl("/todo/"));
106 		result = this.mockMvc.perform(get("/todo/"))
107 				.andExpect(status().isOk())
108 				.andExpect(model().attributeExists("todoList"))
109 				.andReturn();
110 		todoList = (List<Todo>) result.getModelAndView().getModel().get("todoList");
111 		assertEquals("there should be one todo", todoList.size(), 1);
112 	}
113 }