package at.ac.tuwien.inso.integration_tests;

import at.ac.tuwien.inso.entity.Lecturer;
import at.ac.tuwien.inso.entity.Subject;
import at.ac.tuwien.inso.repository.SubjectRepository;
import at.ac.tuwien.inso.repository.UisUserRepository;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

import static java.util.Arrays.asList;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
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.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Transactional
public class AdminSubjectLecturersTests {

    @Autowired
    protected MockMvc mockMvc;

    @Autowired
    private UisUserRepository uisUserRepository;

    @Autowired
    private SubjectRepository subjectRepository;

    private Lecturer lecturer1 = new Lecturer("l1", "lecturer1", "l1@uis.at");
    private Lecturer lecturer2 = new Lecturer("l2", "lecturer2", "l2@uis.at");
    private Lecturer lecturer3 = new Lecturer("l3", "lecturer3", "l3@uis.at");
    private Subject ase = new Subject("ase", new BigDecimal(3.0));

    @Before
    public void setUp() {
        uisUserRepository.save(asList(lecturer1, lecturer2, lecturer3));
        ase.addLecturers(lecturer1);
        subjectRepository.save(ase);
    }

    @Test
    public void addLecturerToSubjectTest() {
        //TODO
    }

    @Test
    public void removeLecturerFromSubjectTest() throws Exception {

        // given 3 lecturers, where lecturer1 is a already a lecturer of the subject ase

        // when removing lecturer1 from subject ase
        mockMvc.perform(
                post("/admin/subjects/" + ase.getId() + "/lecturers/"+ lecturer1.getId() +"/delete")
                        .param("subjectId", ase.getId().toString())
                        .param("lecturerId", lecturer1.getId().toString())
                        .with(user("admin").roles("ADMIN"))
                        .with(csrf())
        ).
        andExpect(
                redirectedUrl("/admin/subjects/" + ase.getId().toString())
        );

        assertEquals(asList(), ase.getLecturers());
    }

    @Test
    public void availableLecturersJsonTest() throws Exception {

        // given 3 lecturers, where lecturer1 is a already a lecturer of the subject ase

        // when searching for "lecturer"
        MvcResult result =  mockMvc.perform(
                get("/admin/subjects/"+ase.getId() +"/availableLecturers.json")
                        .with(user("admin").roles("ADMIN"))
                        .param("id", ase.getId().toString())
                        .param("search", "lecturer")
        ).andExpect((status().isOk())
        ).andReturn();

        // the other 2 lecturers should be available for ase
        assertTrue(result.getResponse().getContentAsString().contains("lecturer2"));
        assertTrue(result.getResponse().getContentAsString().contains("lecturer3"));
        assertFalse(result.getResponse().getContentAsString().contains("lecturer1"));
    }
}

@@ -1423,95 +1423,8 @@
 st;%0A
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;%0A
 impo
@@ -3219,28 +3219,941 @@
     
-assertEquals(asList(
+// lecturer1 shouldn't be part of ase anymore%0A        assertEquals(asList(), ase.getLecturers());%0A    %7D%0A%0A    @Test%0A    public void removeLecturerFromASubjectForWhichHeAlreadyDoesNotExistTest() throws Exception %7B%0A%0A        // given 3 lecturers, where lecturer1 is a already a lecturer of the subject ase%0A%0A        // when removing lecturer2 from subject ase%0A        mockMvc.perform(%0A                post(%22/admin/subjects/%22 + ase.getId() + %22/lecturers/%22+ lecturer2.getId() +%22/delete%22)%0A                        .param(%22subjectId%22, ase.getId().toString())%0A                        .param(%22lecturerId%22, lecturer2.getId().toString())%0A                        .with(user(%22admin%22).roles(%22ADMIN%22))%0A                        .with(csrf())%0A        ).%0A                andExpect(%0A                        redirectedUrl(%22/admin/subjects/%22 + ase.getId().toString())%0A                );%0A%0A        // nothing should change%0A        assertEquals(asList(lecturer1
 ), a

add another test