package bookmarks;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
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.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.http.MockHttpOutputMessage;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;


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

	private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
			MediaType.APPLICATION_JSON.getSubtype(),
			Charset.forName("utf8"));
	
	private MockMvc mockMvc;
	
	private String userName = "bdussault";
	
	@SuppressWarnings("rawtypes")
	private HttpMessageConverter mappingJackson2HttpMessageConverter;
	
	private Account account;
	
	private List<Bookmark> bookmarkList = new ArrayList<Bookmark>();
	
	@Autowired
	private BookmarkRepository bookmarkRepository;
	
	@Autowired
	private WebApplicationContext webApplicationContext;
	
	@Autowired
	private AccountRepository accountRepository;
	
	@Autowired
	void setConverters(HttpMessageConverter<?>[] converters) {
		this.mappingJackson2HttpMessageConverter = Arrays.asList(converters).stream().filter(
				hmc -> hmc instanceof MappingJackson2HttpMessageConverter
				).findAny().get();
		Assert.assertNotNull("the JSON message converter must not be null",
				this.mappingJackson2HttpMessageConverter);
	}
	
	@Before
	public void setup() throws Exception {
		this.mockMvc = webAppContextSetup(webApplicationContext).build();
		
		this.bookmarkRepository.deleteAllInBatch();
		this.accountRepository.deleteAllInBatch();
		
		this.account = accountRepository.save(new Account(userName,"password"));
		this.bookmarkList.add(bookmarkRepository.save(new Bookmark(account, "http://bookmark.com/1/" + userName, "A description")));
        this.bookmarkList.add(bookmarkRepository.save(new Bookmark(account, "http://bookmark.com/2/" + userName, "A description")));
	}
	
	@Test
	public void userNotFound() throws Exception {
		mockMvc.perform(post("/george/bookmarks")
				.content(this.json(new Bookmark()))
				.contentType(contentType))
				.andExpect(status().isNotFound());
	}
	
	@Test
	public void readSingleBookmark() throws Exception {
		mockMvc.perform(get("/" + userName + "/bookmarks/"
				+ this.bookmarkList.get(0).getId())
				.contentType(contentType))
				.andExpect(status().isOk())
				//.andExpect(content().contentType(contentType))
				.andExpect(jsonPath("$.id", is(this.bookmarkList.get(0).getId().intValue())))
				.andExpect(jsonPath("$.uri",is("http://bookmark.com/1" + userName)))
				.andExpect(jsonPath("$.description",is("A description")));
	}
	
	@SuppressWarnings("unchecked")
	protected String json(Object o) throws IOException {
		MockHttpOutputMessage mockHttpOutputMessage = new MockHttpOutputMessage();
		this.mappingJackson2HttpMessageConverter.write(o, MediaType.APPLICATION_JSON, mockHttpOutputMessage);
		return mockHttpOutputMessage.getBodyAsString();
	}

}

@@ -3835,24 +3835,1184 @@
 ));%0D%0A%09%7D%0D%0A%09%0D%0A
+%09@Test%0D%0A%09public void readBookmarks() throws Exception %7B%0D%0A%09%09mockMvc.perform(get(%22/%22 + userName + %22/bookmarks%22))%0D%0A%09%09%09%09.andExpect(status().isOk())%0D%0A%09%09%09%09.andExpect(content().contentType(contentType))%0D%0A%09%09%09%09.andExpect(jsonPath(%22$%22, hasSize(2)))%0D%0A%09%09%09%09.andExpect(jsonPath(%22$%5B0%5D.id%22, is(this.bookmarkList.get(0).getId().intValue())))%0D%0A%09%09%09%09.andExpect(jsonPath(%22$%5B0%5D.uri%22, is(%22http://bookmark.com/1/%22 + userName)))%0D%0A%09%09%09%09.andExpect(jsonPath(%22$%5B0%5D.description%22, is(%22A description%22)))%0D%0A                .andExpect(jsonPath(%22$%5B1%5D.id%22, is(this.bookmarkList.get(1).getId().intValue())))%0D%0A                .andExpect(jsonPath(%22$%5B1%5D.uri%22, is(%22http://bookmark.com/2/%22 + userName)))%0D%0A                .andExpect(jsonPath(%22$%5B1%5D.description%22, is(%22A description%22)));%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void createBookmark() throws Exception %7B%0D%0A%09%09String bookmarkJson = json(new Bookmark(%0D%0A%09%09%09%09this.account, %22http://spring.io%22, %22a bookmark to the best resource for Spring news and information%22));%0D%0A%09%09this.mockMvc.perform(post(%22/%22 + userName + %22/bookmarks%22)%0D%0A                .contentType(contentType)%0D%0A                .content(bookmarkJson))%0D%0A                .andExpect(status().isCreated());%0D%0A%09%7D%0D%0A%09%0D%0A
 %09@SuppressWa

add function