package org.synyx.urlaubsverwaltung.sicknote.web;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.synyx.urlaubsverwaltung.application.service.VacationTypeService;
import org.synyx.urlaubsverwaltung.person.PersonService;
import org.synyx.urlaubsverwaltung.sicknote.SickNote;
import org.synyx.urlaubsverwaltung.sicknote.SickNoteCommentService;
import org.synyx.urlaubsverwaltung.sicknote.SickNoteInteractionService;
import org.synyx.urlaubsverwaltung.sicknote.SickNoteService;
import org.synyx.urlaubsverwaltung.sicknote.SickNoteStatus;
import org.synyx.urlaubsverwaltung.sicknote.SickNoteTypeService;
import org.synyx.urlaubsverwaltung.workingtime.WorkDaysService;

import java.util.Optional;

import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
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.standaloneSetup;

@RunWith(MockitoJUnitRunner.class)
public class SickNoteControllerTest {

    private SickNoteController sut;

    @Mock
    private SickNoteService sickNoteServiceMock;
    @Mock
    private SickNoteInteractionService sickNoteInteractionServiceMock;
    @Mock
    private SickNoteCommentService sickNoteCommentServiceMock;
    @Mock
    private SickNoteTypeService sickNoteTypeServiceMock;
    @Mock
    private VacationTypeService vacationTypeServiceMock;
    @Mock
    private PersonService personServiceMock;
    @Mock
    private WorkDaysService calendarServiceMock;
    @Mock
    private SickNoteValidator validatorMock;
    @Mock
    private SickNoteConvertFormValidator sickNoteConvertFormValidatorMock;


    @Before
    public void setUp() {

        sut = new SickNoteController(sickNoteServiceMock,
            sickNoteInteractionServiceMock, sickNoteCommentServiceMock, sickNoteTypeServiceMock,
            vacationTypeServiceMock, personServiceMock, calendarServiceMock, validatorMock,
            sickNoteConvertFormValidatorMock);
    }

    @Test
    public void ensureNewHasCorrectModelAttributes() throws Exception {
        final ResultActions resultActions = perform(get("/web/sicknote/new"));

        resultActions.andExpect(status().isOk());
        resultActions.andExpect(model().attribute("sickNote", instanceOf(SickNoteForm.class)));
        resultActions.andExpect(model().attribute("persons", personServiceMock.getActivePersons()));
        resultActions.andExpect(model().attribute("sickNoteTypes", sickNoteTypeServiceMock.getSickNoteTypes()));
        resultActions.andExpect(view().name("sicknote/sick_note_form"));
    }

    @Test
    public void ensureEditHasCorrectModelAttributes() throws Exception {
        SickNote sickNote = new SickNote();
        sickNote.setStatus(SickNoteStatus.ACTIVE);

        when(sickNoteServiceMock.getById(0)).thenReturn(Optional.of(sickNote));

        final ResultActions resultActions = perform(get("/web/sicknote/0/edit"));

        resultActions.andExpect(status().isOk());
        resultActions.andExpect(model().attribute("sickNote", instanceOf(SickNoteForm.class)));
        resultActions.andExpect(model().attribute("sickNoteTypes", sickNoteTypeServiceMock.getSickNoteTypes()));
        resultActions.andExpect(view().name("sicknote/sick_note_form"));
    }

    private ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
        return standaloneSetup(sut).build().perform(builder);
    }
}

@@ -48,33 +48,8 @@
 b;%0A%0A
-import org.junit.Before;%0A
 impo
@@ -1523,45 +1523,8 @@
  %7B%0A%0A
-    private SickNoteController sut;%0A%0A
     
@@ -2126,348 +2126,8 @@
 ;%0A%0A%0A
-    @Before%0A    public void setUp() %7B%0A%0A        sut = new SickNoteController(sickNoteServiceMock,%0A            sickNoteInteractionServiceMock, sickNoteCommentServiceMock, sickNoteTypeServiceMock,%0A            vacationTypeServiceMock, personServiceMock, calendarServiceMock, validatorMock,%0A            sickNoteConvertFormValidatorMock);%0A    %7D%0A%0A
     
@@ -2208,51 +2208,16 @@
 n %7B%0A
+%0A
        
- final ResultActions resultActions =
  per
@@ -2246,27 +2246,28 @@
 e/new%22))
-;%0A%0A
+%0A   
         
 resultAc
@@ -2254,37 +2254,25 @@
 %0A           
-resultActions
+ 
 .andExpect(s
@@ -2278,33 +2278,32 @@
 status().isOk())
-;
 %0A        resultA
@@ -2291,37 +2291,28 @@
 ())%0A        
-resultActions
+    
 .andExpect(m
@@ -2364,33 +2364,32 @@
 oteForm.class)))
-;
 %0A        resultA
@@ -2377,37 +2377,28 @@
 )))%0A        
-resultActions
+    
 .andExpect(m
@@ -2459,25 +2459,24 @@
 ePersons()))
-;
 %0A        res
@@ -2468,37 +2468,28 @@
 )))%0A        
-resultActions
+    
 .andExpect(m
@@ -2558,33 +2558,32 @@
 ickNoteTypes()))
-;
 %0A        resultA
@@ -2571,37 +2571,28 @@
 )))%0A        
-resultActions
+    
 .andExpect(v
@@ -2729,16 +2729,22 @@
         
+final 
 SickNote
@@ -2915,44 +2915,8 @@
     
- final ResultActions resultActions =
  per
@@ -2948,18 +2948,16 @@
 /edit%22))
-;%0A
 %0A       
@@ -2953,37 +2953,28 @@
 %22))%0A        
-resultActions
+    
 .andExpect(s
@@ -2988,17 +2988,16 @@
 .isOk())
-;
 %0A       
@@ -2993,37 +2993,28 @@
 ())%0A        
-resultActions
+    
 .andExpect(m
@@ -3074,17 +3074,16 @@
 class)))
-;
 %0A       
@@ -3079,37 +3079,28 @@
 )))%0A        
-resultActions
+    
 .andExpect(m
@@ -3177,17 +3177,16 @@
 ypes()))
-;
 %0A       
@@ -3190,21 +3190,12 @@
     
-resultActions
+    
 .and
@@ -3333,32 +3333,353 @@
 ows Exception %7B%0A
+%0A        final SickNoteController sut = new SickNoteController(sickNoteServiceMock,%0A            sickNoteInteractionServiceMock, sickNoteCommentServiceMock, sickNoteTypeServiceMock,%0A            vacationTypeServiceMock, personServiceMock, calendarServiceMock, validatorMock,%0A            sickNoteConvertFormValidatorMock);%0A%0A
         return s

improve test readability