package lombelo.controller;
import lombelo.AbstractionWebIntegrationTests;
import lombelo.model.Note;
import lombelo.model.NoteRepository;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.RequestBuilder;

import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
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.view;

/**
 * @author Niklas Wünsche
 */
public class SiteControllerWebIntegrationTests extends AbstractionWebIntegrationTests {

    @Autowired private NoteRepository notes;

    @Test
    public void executeLandingPageMapper() throws Exception {
        RequestBuilder serviceRequest = post("/");

        mvc.perform(serviceRequest)
                .andExpect(view().name("landingPage"));
    }

    @Test
    public void executeAddNoteMapper() throws Exception {
        RequestBuilder serviceRequest = post("/addNote");

        mvc.perform(serviceRequest)
                .andExpect(model().attributeExists("content"))
                .andExpect(view().name("addNote"));
    }

    @Test
    public void executeSaveNewNote() throws Exception {
        String noteTitle = "My Note";
        RequestBuilder serviceRequest = post("/addNote/created")
                .param("titleOfNote", noteTitle)
                .param("contentOfNote", "BlaBlaBla");

        mvc.perform(serviceRequest)
                .andExpect(view().name("landingPage"));

        Optional<Note> savedNote = StreamSupport
                .stream(notes.findAll().spliterator(), false)
                .filter(note -> note.getTitle().equals(noteTitle))
                .findAny();

        assertThat(savedNote.isPresent(), is(true));
    }

    @Test
    public void executeShowAllNotes() throws Exception {
        RequestBuilder serviceRequest = post("/showNotes");
        mvc.perform(serviceRequest)
            .andExpect(model().attribute("notes", notes.findAll()))
            .andExpect(view().name("showNotes"));
    }

    @Test
    public void executeEditNote() throws Exception {
        Note note = new Note("title", "text");
        notes.save(note);

        RequestBuilder serviceRequest = get("/editNote/" + note.getId());

        mvc.perform(serviceRequest)
                .andExpect(view().name("editNote"));
    }

    @Test
    public void executeFinishEditNote() throws Exception {
        Note note = new Note("title", "text");
        notes.save(note);

        RequestBuilder serviceRequest = get("/editNote/finished/" + note.getId())
                .param("editedNoteId", note.getId().toString())
                .param("titleOfNote", "newTitle")
                .param("textOfNote", "newText");

        mvc.perform(serviceRequest)
                .andExpect(model().attribute("notes", notes.findAll()))
                .andExpect(view().name("showNotes"));
    }

}

@@ -690,16 +690,104 @@
 .model;%0A
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;%0A
 import s
@@ -1646,16 +1646,56 @@
  Note%22;%0A
+        String textTitle = %22BlaBlaBla%22;%0A
         
@@ -1824,22 +1824,19 @@
 .param(%22
-con
 te
-n
+x
 tOfNote%22
@@ -1837,27 +1837,25 @@
 fNote%22, 
-%22BlaBlaBla%22
+textTitle
 );%0A%0A    
@@ -2199,24 +2199,86 @@
  is(true));%0A
+        assertThat(savedNote.get().getText(), is(textTitle));%0A
     %7D%0A%0A    @
@@ -3043,34 +3043,35 @@
 erviceRequest = 
-ge
+pos
 t(%22/editNote/fin
@@ -3098,72 +3098,8 @@
 ())%0A
-                .param(%22editedNoteId%22, note.getId().toString())%0A
     
@@ -3261,102 +3261,370 @@
 ect(
-model().attribute(%22notes%22, notes.findAll()))%0A                .andExpect(view().name(%22showNotes
+status().is3xxRedirection());%0A%0A        Optional%3CNote%3E editedNote = StreamSupport%0A                .stream(notes.findAll().spliterator(), false)%0A                .filter(savedNote -%3E savedNote.getTitle().equals(%22newTitle%22))%0A                .findAny();%0A%0A        assertThat(editedNote.isPresent(), is(true));%0A        assertThat(editedNote.get().getText(), is(%22newText
 %22));

Add test for edit finished + fix tests