/*
 * #%L
 * unidle
 * %%
 * Copyright (C) 2013 Martin Lau
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */
package org.unidle.controller;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import org.unidle.analytics.config.AnalyticsTestConfiguration;
import org.unidle.config.CacheConfiguration;
import org.unidle.config.DataConfiguration;
import org.unidle.config.I18NConfiguration;
import org.unidle.config.MvcConfiguration;
import org.unidle.config.ServiceConfiguration;
import org.unidle.domain.User;
import org.unidle.repository.AttachmentRepository;
import org.unidle.repository.QuestionRepository;
import org.unidle.repository.UserRepository;

import static org.fest.assertions.Assertions.assertThat;
import static org.hamcrest.Matchers.startsWith;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@ContextHierarchy({@ContextConfiguration(classes = CacheConfiguration.class),
                   @ContextConfiguration(classes = DataConfiguration.class),
                   @ContextConfiguration(classes = I18NConfiguration.class),
                   @ContextConfiguration(classes = ServiceConfiguration.class),
                   @ContextConfiguration(classes = AnalyticsTestConfiguration.class),
                   @ContextConfiguration(classes = MvcConfiguration.class)})
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@WebAppConfiguration
public class CreateQuestionControllerTest {

    @Autowired
    private AttachmentRepository attachmentRepository;

    @Autowired
    private QuestionRepository questionRepository;

    private MockMvc subject;

    private User user;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setUp() throws Exception {
        subject = webAppContextSetup(webApplicationContext).build();

        user = new User();

        user.setEmail("email@example.com");
        user.setFirstName("first name");
        user.setLastName("last name");

        user = userRepository.save(user);
    }

    @After
    public void tearDown() throws Exception {
        SecurityContextHolder.clearContext();
    }

    @Test
    public void testQuestion() throws Exception {
        SecurityContextHolder.getContext()
                             .setAuthentication(new UsernamePasswordAuthenticationToken(user.getUuid(), null));

        subject.perform(get("/question/create"))
               .andExpect(status().isOk())
               .andExpect(model().attributeExists("questionForm"));
    }

    @Test
    public void testQuestionPost() throws Exception {
        SecurityContextHolder.getContext()
                             .setAuthentication(new UsernamePasswordAuthenticationToken(user.getUuid(), null));

        subject.perform(fileUpload("/question/create")
                                .param("question", "this is a test question"))
               .andExpect(view().name(startsWith("redirect:/question/")));

        assertThat(questionRepository.count()).isEqualTo(1L);
        assertThat(questionRepository.findAll().get(0).getTags()).isEmpty();
    }

    @Test
    public void testQuestionPostWithErrors() throws Exception {
        SecurityContextHolder.getContext()
                             .setAuthentication(new UsernamePasswordAuthenticationToken(user.getUuid(), null));

        subject.perform(post("/question/create"))
               .andExpect(view().name(".create-question"))
               .andExpect(model().attributeExists("questionForm"));

    }

    @Test
    public void testQuestionPostWithTags() throws Exception {
        SecurityContextHolder.getContext()
                             .setAuthentication(new UsernamePasswordAuthenticationToken(user.getUuid(), null));

        subject.perform(fileUpload("/question/create")
                                .param("question", "this is a test question")
                                .param("tags", "tag1, tag2, tag3"))
               .andExpect(view().name(startsWith("redirect:/question/")));

        assertThat(questionRepository.count()).isEqualTo(1L);
        assertThat(questionRepository.findAll().get(0).getTags()).containsOnly("tag1", "tag2", "tag3");
    }

    @Test
    public void testQuestionPostWithTagsAndAttachments() throws Exception {
        SecurityContextHolder.getContext()
                             .setAuthentication(new UsernamePasswordAuthenticationToken(user.getUuid(), null));

        subject.perform(fileUpload("/question/create")
                                .file(new MockMultipartFile("attachments",
                                                            "test.txt",
                                                            "text/plain",
                                                            "this is a text file".getBytes()))
                                .param("question", "this is a test question")
                                .param("tags", "tag1, tag2, tag3"))
               .andExpect(view().name(startsWith("redirect:/question/")));

        assertThat(questionRepository.count()).isEqualTo(1L);
        assertThat(questionRepository.findAll().get(0).getTags()).containsOnly("tag1", "tag2", "tag3");
        assertThat(attachmentRepository.count()).isEqualTo(1L);
    }

    @Test
    public void testQuestionPostWithoutAuthentication() throws Exception {
        subject.perform(post("/question/create")
                                .param("question", "this is a test question"))
               .andExpect(view().name("redirect:/signin"));
    }

    @Test
    public void testQuestionWithoutAuthentication() throws Exception {
        subject.perform(get("/question/create"))
               .andExpect(view().name("redirect:/signin"));
    }

}
