Example usage for org.springframework.mock.web MockHttpSession setAttribute

List of usage examples for org.springframework.mock.web MockHttpSession setAttribute

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpSession setAttribute.

Prototype

@Override
    public void setAttribute(String name, @Nullable Object value) 

Source Link

Usage

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testMakeClassValid() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("Hei@gmail.com");
    mockHttpSession.setAttribute("user", user);
    AddNewClassId ancid = new AddNewClassId();
    ancid.setClassId("klasse 1");
    when(personService.getPerson(any(String.class))).thenReturn(new Person());
    when(personService.makeAdmin(any(Person.class), any(String.class))).thenReturn(false);
    when(classService.registrateNewClassId("klasse 1")).thenReturn(true);
    this.mockMvc.perform(post("/makeClass").param("classId", "klasse 1").session(mockHttpSession))
            .andExpect(model().attribute("makeClassMessage", "Ny klasse ble registrert.  "))
            .andExpect(view().name("administrateAccount"));
}

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testChangePassWithWrongPass() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("Hei@gmail.com");
    mockHttpSession.setAttribute("user", user);
    when(personService.getPerson(any(String.class))).thenReturn(new Person());
    when(personService.changePassword(any(Person.class), any(String.class), any(String.class),
            any(String.class))).thenReturn(false);
    this.mockMvc/*from  w  ww  . j a  va 2  s  .c o  m*/
            .perform(post("/changePassword").param("oldPw", "asdasdasd").param("newPw", "asdasdasdasd")
                    .param("confirmPw", "asdasdasdasd").session(mockHttpSession))
            .andExpect(model().attribute("changedPassword",
                    "Feil. Husk at passordet m vre lengre enn 8 tegn."))
            .andExpect(model().attribute("chooseSite", 1)).andExpect(view().name("administrateAccount"));
}

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testAddClassIdInValidInfo() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("Hei@gmail.com");
    mockHttpSession.setAttribute("user", user);
    AddNewClassId ancid = new AddNewClassId();
    ancid.setClassId("klasse 1");

    when(personService.setClassId(any(Person.class), any(String.class))).thenReturn(false);
    when(personService.getPerson(any(String.class))).thenReturn(new Person());
    this.mockMvc.perform(post("/addClassId").param("classId", "klasse 1").session(mockHttpSession))
            .andExpect(model().attribute("chooseSite", 2))
            .andExpect(model().attribute("NewClassMessage", "Feil. Klassen eksisterer ikke. "))
            .andExpect(view().name("administrateAccount"));
}

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testMakeClassInValid() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("Hei@gmail.com");
    mockHttpSession.setAttribute("user", user);
    AddNewClassId ancid = new AddNewClassId();
    ancid.setClassId("klasse 1");
    when(personService.getPerson(any(String.class))).thenReturn(new Person());
    when(personService.makeAdmin(any(Person.class), any(String.class))).thenReturn(false);
    when(classService.registrateNewClassId("klasse 1")).thenReturn(false);
    this.mockMvc.perform(post("/makeClass").param("classId", "klasse 1").session(mockHttpSession))
            .andExpect(model().attribute("chooseSite", 4))
            .andExpect(model().attribute("makeClassMessage", "Feil, Klassen finnes fra fr eller .  "))
            .andExpect(view().name("administrateAccount"));
}

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testChangePassWithValidUser() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("Hei@gmail.com");
    mockHttpSession.setAttribute("user", user);
    Person person = new Person();
    when(personService.getPerson(any(String.class))).thenReturn(person);
    when(personService.changePassword(any(Person.class), any(String.class), any(String.class),
            any(String.class))).thenReturn(true);
    this.mockMvc/*from w w  w.ja v a  2 s .  co m*/
            .perform(post("/changePassword").param("oldPw", "asdasdasd").param("newPw", "asdasdasdasd")
                    .param("confirmPw", "asdasdasdasd").session(mockHttpSession))
            .andExpect(model().attribute("changedPassword", "Passordet er endret."))
            .andExpect(view().name("administrateAccount"));

}

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testMakeAdminValid() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("Hei@gmail.com");
    mockHttpSession.setAttribute("user", user);
    AddNewClassId ancid = new AddNewClassId();
    ancid.setClassId("klasse 1");
    Person person = new Person();

    when(personService.setClassId(any(Person.class), any(String.class))).thenReturn(false);
    when(personService.getPerson(any(String.class))).thenReturn(person);
    when(personService.makeAdmin(person, "123")).thenReturn(true);
    this.mockMvc.perform(post("/makeNewAdmin").param("makeAdminPw", "123").session(mockHttpSession))
            .andExpect(model().attribute("makeAdminMessage", "Du har n admin-rettigheter. "))
            .andExpect(view().name("administrateAccount"));
}

From source file:Controllers.AdministrateControllerTest.java

@Test
public void testAddClassIdValidInfo() throws Exception {
    MockHttpSession mockHttpSession = new MockHttpSession();
    User user = new User();
    user.setEmail("Hei@gmail.com");
    mockHttpSession.setAttribute("user", user);
    AddNewClassId ancid = new AddNewClassId();
    ancid.setClassId("klasse 1");

    when(personService.setClassId(any(Person.class), any(String.class))).thenReturn(true);
    when(personService.getPerson(any(String.class))).thenReturn(new Person());
    when(personService.changePassword(any(Person.class), any(String.class), any(String.class),
            any(String.class))).thenReturn(false);
    this.mockMvc.perform(post("/addClassId").param("classId", "klasse 1").session(mockHttpSession))
            .andExpect(model().attribute("NewClassMessage",
                    "Du er n registrert i klasse: " + ancid.getClassId()))
            .andExpect(view().name("administrateAccount"));
}

From source file:Controllers.ResembleControllerTest.java

@Test
public void testSubmitResembleGameCreateTask() throws Exception {
    User user = new User();
    user.setInLogged(true);//from   w  w w . ja  v  a  2 s  .  co m
    CreateResembleGame createResembleGame = new CreateResembleGame();
    ResembleGame rg = new ResembleGame();
    rg.setCreatorId("asd");
    rg.setInfo("hasdasd");
    rg.setLearningGoal("iasdasd");
    rg.setDifficulty(2);
    createResembleGame.setResembleGame(rg);

    MockHttpSession mockHttpSession = new MockHttpSession();
    mockHttpSession.setAttribute("user", user);
    mockHttpSession.setAttribute("createResembleGame", createResembleGame);
    this.mockMvc.perform(post("/submitresemblegame").param("button", "Lag deloppgave").session(mockHttpSession))
            .andExpect(model().attributeExists("createResembleTask"))
            .andExpect(view().name("createresembletask"));
}

From source file:Controllers.ResembleControllerTest.java

@Test
public void testSubmitResembleGameError() throws Exception {
    User user = new User();
    user.setInLogged(true);/*  w  w w . j a va 2 s  .  co m*/
    CreateResembleGame createResembleGame = new CreateResembleGame();
    ResembleGame rg = new ResembleGame();
    rg.setCreatorId("asd");
    rg.setInfo("hasdasd");
    rg.setLearningGoal("iasdasd");
    rg.setDifficulty(2);
    createResembleGame.setResembleGame(rg);

    MockHttpSession mockHttpSession = new MockHttpSession();
    mockHttpSession.setAttribute("user", user);
    mockHttpSession.setAttribute("createResembleGame", createResembleGame);

    this.mockMvc.perform(
            post("/submitresemblegame").param("learningGoal", "").param("button", "").session(mockHttpSession))
            .andExpect(view().name("createresemblegame"));
}

From source file:Controllers.ResembleControllerTest.java

@Test
public void testSubmitResembleGameDuplicateName() throws Exception {
    User user = new User();
    user.setInLogged(true);/*from  ww w.j av a2s. c o m*/
    CreateResembleGame createResembleGame = new CreateResembleGame();
    ResembleGame rg = new ResembleGame();
    rg.setCreatorId("asd");
    rg.setInfo("hasdasd");
    rg.setLearningGoal("iasdasd");
    rg.setDifficulty(2);
    createResembleGame.setResembleGame(rg);

    MockHttpSession mockHttpSession = new MockHttpSession();
    mockHttpSession.setAttribute("user", user);
    mockHttpSession.setAttribute("createResembleGame", createResembleGame);
    when(gameListService.getResemleGameByName(any(String.class))).thenReturn(new ResembleGame());

    this.mockMvc.perform(post("/submitresemblegame").param("button", "").session(mockHttpSession))
            .andExpect(model().attributeExists("message")).andExpect(view().name("createresemblegame"));
}