### Question:
GatewaySecurityService { public QueryResponse query(String token) { GatewayConfigProperties gatewayConfigProperties = gatewayClient.getGatewayConfigProperties(); String uri = String.format("%s: gatewayConfigProperties.getHostname(), authConfigurationProperties.getGatewayQueryEndpoint()); String cookie = String.format("%s=%s", authConfigurationProperties.getCookieProperties().getCookieName(), token); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.COOKIE, cookie); try { ResponseEntity<QueryResponse> response = restTemplate.exchange( uri, HttpMethod.GET, new HttpEntity<>(headers), QueryResponse.class); return response.getBody(); } catch (HttpClientErrorException | ResourceAccessException | HttpServerErrorException e) { responseHandler.handleBadResponse(e, ErrorType.TOKEN_NOT_VALID, "Can not access Gateway service. Uri '{}' returned: {}", uri, e.getMessage()); } return null; }  Optional<String> login(String username, String password); QueryResponse query(String token);  }### Answer:
@Test public void doSuccessfulQuery() { QueryResponse expectedQueryResponse = new QueryResponse("domain", "user", new Date(), new Date(), QueryResponse.Source.ZOWE); String uri = String.format("%s: gatewayConfigProperties.getHostname(), authConfigurationProperties.getGatewayQueryEndpoint()); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.COOKIE, cookie); HttpEntity httpEntity = new HttpEntity<>(headers); when(restTemplate.exchange( eq(uri), eq(HttpMethod.GET), eq(httpEntity), eq(QueryResponse.class))) .thenReturn(new ResponseEntity<>(expectedQueryResponse, HttpStatus.OK)); QueryResponse query = securityService.query("token"); assertEquals(expectedQueryResponse, query); }
@Test public void doQueryWhenGatewayUnauthorized() { String uri = String.format("%s: gatewayConfigProperties.getHostname(), authConfigurationProperties.getGatewayQueryEndpoint()); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.COOKIE, cookie); HttpEntity httpEntity = new HttpEntity<>(headers); when(restTemplate.exchange( eq(uri), eq(HttpMethod.GET), eq(httpEntity), eq(QueryResponse.class))) .thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); exceptionRule.expect(TokenNotValidException.class); exceptionRule.expectMessage("Token is not valid."); securityService.query("token"); }