/*
 * Copyright (C) 2016 the original author or authors.
 *
 * This file is part of jBB Application Project.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 */

package org.jbb.system.web.logging.controller;

import org.jbb.lib.mvc.MvcConfig;
import org.jbb.lib.properties.PropertiesConfig;
import org.jbb.lib.test.CoreConfigMocks;
import org.jbb.lib.test.SpringSecurityConfigMocks;
import org.jbb.system.api.data.StackTraceVisibilityLevel;
import org.jbb.system.api.service.StackTraceService;
import org.jbb.system.web.SystemConfigMock;
import org.jbb.system.web.SystemWebConfig;
import org.jbb.system.web.logging.form.LoggingSettingsForm;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
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;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {MvcConfig.class, SystemWebConfig.class, PropertiesConfig.class,
        SystemConfigMock.class, CoreConfigMocks.class, SpringSecurityConfigMocks.class})
@TestExecutionListeners(listeners = {DependencyInjectionTestExecutionListener.class,
        WithSecurityContextTestExecutionListener.class})
public class AcpLoggingControllerIT {
    @Autowired
    WebApplicationContext wac;

    @Autowired
    private StackTraceService stackTraceServiceMock;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac)
                .apply(SecurityMockMvcConfigurers.springSecurity()).build();
        Mockito.reset(stackTraceServiceMock);
    }

    @Test
    public void shouldPutCurrentStackTraceVisibilityLevelToModel_whenGET() throws Exception {
        // given
        given(stackTraceServiceMock.getCurrentStackTraceVisibilityLevel()).willReturn(StackTraceVisibilityLevel.USERS);

        // when
        ResultActions result = mockMvc.perform(get("/acp/general/logging"));

        // then
        result.andExpect(status().isOk())
                .andExpect(view().name("acp/general/logging"));

        LoggingSettingsForm loggingSettingsForm = (LoggingSettingsForm) result.andReturn()
                .getModelAndView().getModel().get("loggingSettingsForm");

        assertThat(loggingSettingsForm.getStackTraceVisibilityLevel()).isEqualTo("Users");
    }

    @Test
    public void shouldSetNewStackTraceVisibilityLevelToModel_whenPUT() throws Exception {
        // when
        ResultActions result = mockMvc.perform(post("/acp/general/logging")
                .param("stackTraceVisibilityLevel", "Administrators"));

        // then
        result.andExpect(status().isOk())
                .andExpect(view().name("acp/general/logging"))
                .andExpect(model().attribute("loggingSettingsFormSaved", true));

        verify(stackTraceServiceMock, times(1)).setStackTraceVisibilityLevel(eq(StackTraceVisibilityLevel.ADMINISTRATORS));
    }
}
@@ -323,16 +323,57 @@
 oller;%0A%0A
+import com.google.common.collect.Lists;%0A%0A
 import o
@@ -592,24 +592,144 @@
 ilityLevel;%0A
+import org.jbb.system.api.model.logging.LoggingConfiguration;%0Aimport org.jbb.system.api.service.LoggingSettingsService;%0A
 import org.j
@@ -2010,16 +2010,56 @@
 ers.eq;%0A
+import static org.mockito.Mockito.mock;%0A
 import s
@@ -2388,21 +2388,21 @@
 atchers.
-model
+flash
 ;%0Aimport
@@ -3113,16 +3113,95 @@
 eMock;%0A%0A
+    @Autowired%0A    private LoggingSettingsService loggingSettingsServiceMock;%0A%0A
     priv
@@ -3586,16 +3586,60 @@
 / given%0A
+        prepareLoggingConfigurationMocks();%0A
         
@@ -3747,24 +3747,25 @@
 el.USERS);%0A%0A
+%0A
         // w
@@ -4309,17 +4309,18 @@
 el_whenP
-U
+OS
 T() thro
@@ -4326,32 +4326,94 @@
 ows Exception %7B%0A
+        // given%0A        prepareLoggingConfigurationMocks();%0A%0A
         // when%0A
@@ -4605,34 +4605,46 @@
 pect(status().is
-Ok
+3xxRedirection
 ())%0A            
@@ -4663,32 +4663,42 @@
 ct(view().name(%22
+redirect:/
 acp/general/logg
@@ -4731,21 +4731,21 @@
 dExpect(
-model
+flash
 ().attri
@@ -4915,10 +4915,543 @@
 );%0A    %7D
+%0A%0A    private void prepareLoggingConfigurationMocks() %7B%0A        LoggingConfiguration loggingConfigurationMock = mock(LoggingConfiguration.class);%0A        given(loggingSettingsServiceMock.getLoggingConfiguration()).willReturn(loggingConfigurationMock);%0A        given(loggingConfigurationMock.getConsoleAppenders()).willReturn(Lists.newArrayList());%0A        given(loggingConfigurationMock.getFileAppenders()).willReturn(Lists.newArrayList());%0A        given(loggingConfigurationMock.getLoggers()).willReturn(Lists.newArrayList());%0A    %7D
 %0A%7D

Fix integration tests