本文整理汇总了Java中org.springframework.mock.web.MockHttpSession.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java MockHttpSession.setAttribute方法的具体用法?Java MockHttpSession.setAttribute怎么用?Java MockHttpSession.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.mock.web.MockHttpSession
的用法示例。
在下文中一共展示了MockHttpSession.setAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loginVerifiesSecretState
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void loginVerifiesSecretState() throws Exception {
User currentUser = WithAdminUserFactory.create();
when(mockGitHub.getCurrentUser(any(CurrentUserRequest.class))).thenReturn(currentUser);
MockHttpSession session = new MockHttpSession();
String redirect = mockMvc.perform(get("/sign/pivotal").session(session))
.andExpect(status().is3xxRedirection())
.andReturn().getResponse().getRedirectedUrl();
redirect = mockMvc.perform(get(redirect))
.andReturn().getResponse().getRedirectedUrl();
// change the expected secret state
session.setAttribute("state", "INVALID");
mockMvc.perform(get(redirect).session(session))
.andExpect(status().isBadRequest());
}
示例2: postTerritoryOwner_returnsNotFound_whenTerritoryNotFound
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void postTerritoryOwner_returnsNotFound_whenTerritoryNotFound() throws Exception{
Player player = Player.builder().playerNumber(1).playerTerritoriesList(new ArrayList<>()).build();
Player wrongPlayer = Player.builder().playerNumber(4).build();
when(mockPlayerRepository.findPlayersByGameName("gamename")).thenReturn(new ArrayList<>(Arrays.asList(player, wrongPlayer)));
when(mockPlayerTerritoryRepository.findPlayerTerritoryByTerritoryIdAndGameName(2L, "gamename")).thenReturn(null);
TerritoryController.TerritoryRequest territoryRequest = TerritoryController.TerritoryRequest.builder().playerNumber(1).territoryId(2).build();
ObjectMapper objectMapper = new ObjectMapper();
String content = objectMapper.writeValueAsString(territoryRequest);
MockHttpSession mockHttpSession = new MockHttpSession();
mockHttpSession.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
mockMvc.perform(post("/territories/owner").contentType(MediaType.APPLICATION_JSON).content(content).session(mockHttpSession))
.andExpect(status().isNotFound());
}
示例3: startGameRequest_callsMarkUnsuppliedThenSupplied_inSupplyService
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void startGameRequest_callsMarkUnsuppliedThenSupplied_inSupplyService() throws Exception {
List<Player> players = Arrays.asList(Player.builder().playerId(10L).build(), Player.builder().playerId(20L).build());
Game game = Game.builder().gameName("gamename").players(players).build();
when(mockRepository.findOne("gamename")).thenReturn(game);
List<Territory> territories = generateTerritoriesForTest();
when(mockTerritoryRepository.findAll()).thenReturn(territories);
MockHttpSession session = new MockHttpSession();
session.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
List<PlayerTerritory> playerTerritories = generatePlayerTerritoriesForTest();
when(mockPlayerTerritoryRepository.findByGameName("gamename")).thenReturn(playerTerritories);
mockMvc.perform(post("/game/start").contentType(MediaType.APPLICATION_JSON).session(session));
InOrder inOrder = inOrder(mockSuppliedStatusService);
inOrder.verify(mockSuppliedStatusService).markUnsupplied();
ArgumentCaptor<List> listArgumentCaptor = ArgumentCaptor.forClass(List.class);
inOrder.verify(mockSuppliedStatusService).markSupplied(listArgumentCaptor.capture(), eq(playerTerritories));
List<PlayerTerritory> value = (List<PlayerTerritory>) listArgumentCaptor.getValue();
assertThat(value
.stream()
.map(PlayerTerritory::getTerritoryId)
.collect(Collectors.toList())).containsExactlyInAnyOrder(5L, 10L, 15L, 20L);
}
示例4: getOwnerOfTerritory_returnsNullOwningPlayer_whenNoOwner
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void getOwnerOfTerritory_returnsNullOwningPlayer_whenNoOwner() throws Exception {
PlayerTerritory playerTerritory = PlayerTerritory.builder().playerId(30L).territoryId(1L).player(null).build();
when(mockRepository.findOne(1L)).thenReturn((Territory.builder().name("Location 1").territoryId(1L).build()));
when(mockPlayerTerritoryRepository.findPlayerTerritoryByTerritoryIdAndGameName(1L, "gamename"))
.thenReturn(playerTerritory);
MockHttpSession session = new MockHttpSession();
session.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
String response = mockMvc.perform(get("/territories/1").accept(MediaType.APPLICATION_JSON).session(session))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
JSONAssert.assertEquals("{\n" +
" \"name\" : \"Location 1\",\n" +
" \"owningPlayer\": null\n" +
"}", response, JSONCompareMode.LENIENT);
}
示例5: startGame_initializesNonSupplyTerritories_toHave12SurroundingTroops
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void startGame_initializesNonSupplyTerritories_toHave12SurroundingTroops() throws Exception {
List<Player> players = Arrays.asList(Player.builder().playerId(10L).build(), Player.builder().playerId(20L).build());
Game game = Game.builder().gameName("gamename").players(players).build();
when(mockRepository.findOne("gamename")).thenReturn(game);
List<Territory> territories = generateTerritoriesForTest();
when(mockTerritoryRepository.findAll()).thenReturn(territories);
when(mockPlayerTerritoryRepository.findByGameName("gamename")).thenReturn(generatePlayerTerritoriesForTest());
MockHttpSession session = new MockHttpSession();
session.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
mockMvc.perform(post("/game/start").contentType(MediaType.APPLICATION_JSON).session(session))
.andExpect(status().isOk());
ArgumentCaptor<List> listArgumentCaptor = ArgumentCaptor.forClass(List.class);
verify(mockPlayerTerritoryRepository).save(listArgumentCaptor.capture());
List<PlayerTerritory> value = listArgumentCaptor.getValue();
int totalTroops = 0;
for (PlayerTerritory pt:value) {
totalTroops += pt.getTroops();
}
assertThat(totalTroops).isEqualTo(80); // (8 troops per depot + 12 troops per adjacent)*2depotsperplayer*2players
}
示例6: getPlayer_returnsPlayerInGameWhosePlayerNumberMatchesRequest
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void getPlayer_returnsPlayerInGameWhosePlayerNumberMatchesRequest() throws Exception {
List<Player> players = Arrays.asList(
Player.builder().playerNumber(1).name("zxmbies").build(),
Player.builder().playerNumber(2).name("qxc").build(),
Player.builder().playerNumber(3).name("eidlyn").build());
when(mockRepository.findPlayersByGameName("gamename")).thenReturn(players);
MockHttpSession mockHttpSession = new MockHttpSession();
mockHttpSession.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
String actualResponse = mockMvc.perform(get("/players/3").contentType(MediaType.APPLICATION_JSON).session(mockHttpSession))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONAssert.assertEquals("{\n" +
" \"playerNumber\": 3,\n" +
" \"name\": \"eidlyn\"\n" +
"}", actualResponse, JSONCompareMode.LENIENT);
}
示例7: moveTroops_returnsForbiddenStatus_whenPlayerIsNotActive
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void moveTroops_returnsForbiddenStatus_whenPlayerIsNotActive() throws Exception {
MockHttpSession session = new MockHttpSession();
session.setAttribute(SESSION_PLAYER_NUMBER_FIELD, 1);
session.setAttribute(SESSION_GAME_NAME_FIELD, "gamename");
Game game = Game.builder()
.players(Arrays.asList(new Player(), new Player()))
.turnNumber(2)
.build();
when(mockGameRepository.findOne("gamename")).thenReturn(game);
ObjectMapper objectMapper = new ObjectMapper();
String request = objectMapper.writeValueAsString(new ActionsController.MoveRequest());
mockMvc.perform(post("/actions/move-troops").contentType(MediaType.APPLICATION_JSON).content(request).session(session)).andExpect(status().isForbidden());
}
示例8: postTerritoryOwner_setsOwnerOfTerritory_toRequestedPlayer
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void postTerritoryOwner_setsOwnerOfTerritory_toRequestedPlayer() throws Exception {
PlayerTerritory playerTerritory = PlayerTerritory.builder().territoryId(2L).build();
when(mockPlayerTerritoryRepository.findPlayerTerritoryByTerritoryIdAndGameName(2L, "gamename")).thenReturn(playerTerritory);
Player player = Player.builder().playerNumber(1).playerTerritoriesList(new ArrayList<>()).build();
Player wrongPlayer = Player.builder().playerNumber(4).build();
when(mockPlayerRepository.findPlayersByGameName("gamename")).thenReturn(new ArrayList<>(Arrays.asList(player, wrongPlayer)));
TerritoryController.TerritoryRequest territoryRequest = TerritoryController.TerritoryRequest.builder().playerNumber(1).territoryId(2).build();
ObjectMapper objectMapper = new ObjectMapper();
String content = objectMapper.writeValueAsString(territoryRequest);
MockHttpSession mockHttpSession = new MockHttpSession();
mockHttpSession.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
mockMvc.perform(post("/territories/owner").contentType(MediaType.APPLICATION_JSON).content(content).session(mockHttpSession))
.andExpect(status().isOk());
verify(mockPlayerRepository).save(player);
assertThat(player.getPlayerTerritoriesList().get(0)).isSameAs(playerTerritory);
}
示例9: skipAction_changesTurn_andSetsRemainingActionsToThree_whenRemainingActionsAreOne
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void skipAction_changesTurn_andSetsRemainingActionsToThree_whenRemainingActionsAreOne() throws Exception {
MockHttpSession session = new MockHttpSession();
session.setAttribute(SESSION_PLAYER_NUMBER_FIELD, 2);
session.setAttribute(SESSION_GAME_NAME_FIELD, "gamename");
Game game = new Game();
game.setTurnNumber(5);
Player thisPlayer = Player.builder().playerNumber(2).remainingActions(1).build();
game.setPlayers(Arrays.asList(new Player(), thisPlayer, new Player()));
when(mockGameRepository.findOne("gamename")).thenReturn(game);
String contentAsString = mockMvc.perform(post("/actions/skip-action").contentType(MediaType.APPLICATION_JSON).session(session)).andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
;
verify(mockGameRepository).save(game);
assertThat(game.getTurnNumber()).isEqualTo(6);
assertThat(thisPlayer.getRemainingActions()).isEqualTo(3);
assertEquals("{\n" +
" \"actionsRemaining\": 0\n" +
"}", contentAsString, JSONCompareMode.NON_EXTENSIBLE);
}
示例10: verifyFinishAuthentication
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void verifyFinishAuthentication() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(Clients.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
final MockHttpSession mockSession = new MockHttpSession();
mockSession.setAttribute(ClientAction.THEME, MY_THEME);
mockSession.setAttribute(ClientAction.LOCALE, MY_LOCALE);
mockSession.setAttribute(ClientAction.METHOD, MY_METHOD);
final Service service = new SimpleWebApplicationServiceImpl(MY_SERVICE);
mockSession.setAttribute(ClientAction.SERVICE, service);
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
final FacebookClient facebookClient = new MockFacebookClient();
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);
final TicketGrantingTicket tgt = new TicketGrantingTicketImpl(TGT_ID, mock(Authentication.class), mock(ExpirationPolicy.class));
final CentralAuthenticationService casImpl = mock(CentralAuthenticationService.class);
when(casImpl.createTicketGrantingTicket(any(Credential.class))).thenReturn(tgt);
final ClientAction action = new ClientAction(casImpl, clients);
final Event event = action.execute(mockRequestContext);
assertEquals("success", event.getId());
assertEquals(MY_THEME, mockRequest.getAttribute(ClientAction.THEME));
assertEquals(MY_LOCALE, mockRequest.getAttribute(ClientAction.LOCALE));
assertEquals(MY_METHOD, mockRequest.getAttribute(ClientAction.METHOD));
assertEquals(MY_SERVICE, mockRequest.getAttribute(ClientAction.SERVICE));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
final MutableAttributeMap requestScope = mockRequestContext.getRequestScope();
assertEquals(service, flowScope.get(ClientAction.SERVICE));
assertEquals(TGT_ID, flowScope.get(TGT_NAME));
assertEquals(TGT_ID, requestScope.get(TGT_NAME));
}
示例11: testFinishAuthentication
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void testFinishAuthentication() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.setParameter(Clients.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
final MockHttpSession mockSession = new MockHttpSession();
mockSession.setAttribute(ClientAction.THEME, MY_THEME);
mockSession.setAttribute(ClientAction.LOCALE, MY_LOCALE);
mockSession.setAttribute(ClientAction.METHOD, MY_METHOD);
final Service service = new SimpleWebApplicationServiceImpl(MY_SERVICE);
mockSession.setAttribute(ClientAction.SERVICE, service);
mockRequest.setSession(mockSession);
final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
final MockRequestContext mockRequestContext = new MockRequestContext();
mockRequestContext.setExternalContext(servletExternalContext);
final FacebookClient facebookClient = new MockFacebookClient();
final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);
final ClientAction action = new ClientAction(mock(CentralAuthenticationService.class), clients);
final Event event = action.execute(mockRequestContext);
assertEquals("success", event.getId());
assertEquals(MY_THEME, mockRequest.getAttribute(ClientAction.THEME));
assertEquals(MY_LOCALE, mockRequest.getAttribute(ClientAction.LOCALE));
assertEquals(MY_METHOD, mockRequest.getAttribute(ClientAction.METHOD));
final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
assertEquals(service, flowScope.get(ClientAction.SERVICE));
}
示例12: checkIfGameHasStarted_returnsExpectedResult
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void checkIfGameHasStarted_returnsExpectedResult() throws Exception {
MockHttpSession mockHttpSession = new MockHttpSession();
mockHttpSession.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gameName");
when(mockGameDataService.findGameByName("gameName")).thenReturn(Game.builder().started(true).build());
GameResponse gameResponse = gameService.checkIfGameHasStarted(mockHttpSession);
assertThat(gameResponse.isGameStarted()).isEqualTo(true);
}
示例13: startGame_throwsConflictException_onStartedGame
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void startGame_throwsConflictException_onStartedGame() throws Exception {
Game game = Game.builder().gameName("gamename").started(true).build();
when(mockGameDataService.findGameByName("gamename")).thenReturn(game);
MockHttpSession session = new MockHttpSession();
session.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
try {
gameService.startGame(session);
fail("Expected Game Already Started Exception");
} catch (Exceptions.ConflictException e) {
Assertions.assertThat(e.getClass()).isEqualTo(Exceptions.ConflictException.class);
Assertions.assertThat(e.getMessage()).isEqualTo("Game Already Started");
}
}
示例14: startGame_throwsConflictException_whenTooFewPlayers
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void startGame_throwsConflictException_whenTooFewPlayers() throws Exception {
Game game = Game.builder().gameName("gamename").started(false).players(Arrays.asList(new Player())).build();
when(mockGameDataService.findGameByName("gamename")).thenReturn(game);
MockHttpSession session = new MockHttpSession();
session.setAttribute(PlayerController.SESSION_GAME_NAME_FIELD, "gamename");
try {
gameService.startGame(session);
fail("Expected Too Few Players Exception");
} catch (Exceptions.ConflictException e) {
Assertions.assertThat(e.getClass()).isEqualTo(Exceptions.ConflictException.class);
Assertions.assertThat(e.getMessage()).isEqualTo("Invalid Game: Too Few Players");
}
}
示例15: moveTroops_returnsForbidden_whenPlayerDoesNotOwnEndingTerritory
import org.springframework.mock.web.MockHttpSession; //导入方法依赖的package包/类
@Test
public void moveTroops_returnsForbidden_whenPlayerDoesNotOwnEndingTerritory() throws Exception {
MockHttpSession session = new MockHttpSession();
session.setAttribute(SESSION_PLAYER_NUMBER_FIELD, 1);
session.setAttribute(SESSION_GAME_NAME_FIELD, "gamename");
PlayerTerritory playerTerritoryOne = PlayerTerritory.builder().id(1L).playerId(1L).territoryId(7L).troops(10).territory(Territory.builder().south(9L).build()).build();
PlayerTerritory playerTerritoryTwo = PlayerTerritory.builder().id(2L).playerId(1L).territoryId(9L).troops(2).territory(Territory.builder().north(7L).build()).build();
Player player = Player.builder()
.playerNumber(1)
.playerTerritoriesList(Arrays.asList(playerTerritoryOne, playerTerritoryTwo))
.remainingActions(2)
.build();
Game game = Game.builder()
.players(Arrays.asList(player))
.turnNumber(1)
.build();
when(mockGameRepository.findOne("gamename")).thenReturn(game);
ObjectMapper objectMapper = new ObjectMapper();
ActionsController.MoveRequest moveRequest = ActionsController.MoveRequest.builder()
.moveFrom(7L)
.moveTo(10L)
.numberOfTroops(5)
.build();
String request = objectMapper.writeValueAsString(moveRequest);
mockMvc.perform(post("/actions/move-troops").contentType(MediaType.APPLICATION_JSON).content(request).session(session)).andExpect(status().isForbidden());
}