package com.pollistics.controllers;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.fail;

import java.util.List;
import java.util.Arrays;
import java.util.HashMap;

import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.pollistics.models.Poll;
import com.pollistics.services.PollService;


@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class)
public class HomeControllerTests {
	@MockBean
	private PollService pollService;

	@Autowired
	private MockMvc mockMvc;

	@Test
	public void getAllPollsTest() throws Exception {
		try {
			HashMap<String, Integer> options = new HashMap<>();
			options.put("Blauw", 1);
			options.put("Rood", 12);
			Poll poll1 = new Poll("Mooi kleur", options);
			Poll poll2 = new Poll("Vies kleur", options);
			Poll poll3 = new Poll("Raar kleur", options);
			List<Poll> polls = Arrays.asList(poll1, poll2, poll3);
			when(pollService.getAllPolls()).thenReturn(polls);

			this.mockMvc.perform(get("/")).andDo(print())
			.andExpect(status().isOk())
			.andExpect(model().attribute("polls", hasItem(Matchers.<Poll>hasProperty("name", equalTo("Mooi kleur")))))
			.andExpect(model().attribute("polls", hasItem(Matchers.<Poll>hasProperty("name", equalTo("Vies kleur")))))
			.andExpect(model().attribute("polls", hasItem(Matchers.<Poll>hasProperty("name", equalTo("Raar kleur")))))
			.andExpect(model().attribute("polls", hasItem(Matchers.<Poll>hasProperty("options", Matchers.<String,Integer>hasEntry("Blauw", 1)))))
			.andExpect(model().attribute("polls", hasItem(Matchers.<Poll>hasProperty("options", Matchers.<String,Integer>hasEntry("Rood", 12)))));
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
}

@@ -157,16 +157,104 @@
 rs.get;%0A
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;%0A
 import s
@@ -419,16 +419,111 @@
 .model;%0A
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;%0A
 import s
@@ -2655,10 +2655,730 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09@Test%0A%09public void createPollTest() %7B%0A%09%09try %7B%0A%09%09%09HashMap%3CString, Integer%3E options = new HashMap%3C%3E();%0A%09%09%09String title = %22Poll title%22;%0A%09%09%09String option1 = %22option1%22;%0A%09%09%09String option2 = %22option2%22;%0A%09%09%09String option3 = %22option3%22;%0A%09%09%09options.put(option1, 0);%0A%09%09%09options.put(option2, 0);%0A%09%09%09options.put(option3, 0);%0A%09%09%09when(pollService.createPoll(title, options)).thenReturn(%22someId123%22);%0A%0A%09%09%09this.mockMvc.perform(post(%22/%22)%0A%09%09%09%09%09.param(%22title%22, title)%0A%09%09%09%09%09.param(%22option1%22,option1)%0A%09%09%09%09%09.param(%22option2%22, option2)%0A%09%09%09%09%09.param(%22option3%22, option3))%09%09%09%09%09%0A%09%09%09.andDo(print())%0A%09%09%09.andExpect(status().is3xxRedirection())%0A%09%09%09.andExpect(redirectedUrl(%22/polls/someId123%22));%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09fail(e.getMessage());%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Add controller tests