@Test(expected = HttpClientErrorException.class) public void testSearchUsersWithError() throws Exception { when(restTemplate.exchange(eq(someUserServiceUrl), eq(HttpMethod.POST), any(HttpEntity.class), eq(someResponseType))) .thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); String someKeyword = "someKeyword"; int someOffset = 0; int someLimit = 10; ctripUserService.searchUsers(someKeyword, someOffset, someLimit); }
@Override public List<UserInfo> searchUsers(String keyword, int offset, int limit) { UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request); ResponseEntity<Map<String, List<UserServiceResponse>>> response = restTemplate.exchange(portalConfig.userServiceUrl(), HttpMethod.POST, entity, responseType); if (!response.getBody().containsKey("result")) { return Collections.emptyList(); } List<UserInfo> result = Lists.newArrayList(); result.addAll( response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo) .collect(Collectors.toList())); return result; }
CtripUserService implements UserService { @Override public List<UserInfo> searchUsers(String keyword, int offset, int limit) { UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request); ResponseEntity<Map<String, List<UserServiceResponse>>> response = restTemplate.exchange(portalConfig.userServiceUrl(), HttpMethod.POST, entity, responseType); if (!response.getBody().containsKey("result")) { return Collections.emptyList(); } List<UserInfo> result = Lists.newArrayList(); result.addAll( response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo) .collect(Collectors.toList())); return result; } }
CtripUserService implements UserService { @Override public List<UserInfo> searchUsers(String keyword, int offset, int limit) { UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request); ResponseEntity<Map<String, List<UserServiceResponse>>> response = restTemplate.exchange(portalConfig.userServiceUrl(), HttpMethod.POST, entity, responseType); if (!response.getBody().containsKey("result")) { return Collections.emptyList(); } List<UserInfo> result = Lists.newArrayList(); result.addAll( response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo) .collect(Collectors.toList())); return result; } CtripUserService(PortalConfig portalConfig); }
CtripUserService implements UserService { @Override public List<UserInfo> searchUsers(String keyword, int offset, int limit) { UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request); ResponseEntity<Map<String, List<UserServiceResponse>>> response = restTemplate.exchange(portalConfig.userServiceUrl(), HttpMethod.POST, entity, responseType); if (!response.getBody().containsKey("result")) { return Collections.emptyList(); } List<UserInfo> result = Lists.newArrayList(); result.addAll( response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo) .collect(Collectors.toList())); return result; } CtripUserService(PortalConfig portalConfig); @Override List<UserInfo> searchUsers(String keyword, int offset, int limit); @Override UserInfo findByUserId(String userId); @Override List<UserInfo> findByUserIds(List<String> userIds); }
CtripUserService implements UserService { @Override public List<UserInfo> searchUsers(String keyword, int offset, int limit) { UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); HttpEntity<UserServiceRequest> entity = new HttpEntity<>(request); ResponseEntity<Map<String, List<UserServiceResponse>>> response = restTemplate.exchange(portalConfig.userServiceUrl(), HttpMethod.POST, entity, responseType); if (!response.getBody().containsKey("result")) { return Collections.emptyList(); } List<UserInfo> result = Lists.newArrayList(); result.addAll( response.getBody().get("result").stream().map(this::transformUserServiceResponseToUserInfo) .collect(Collectors.toList())); return result; } CtripUserService(PortalConfig portalConfig); @Override List<UserInfo> searchUsers(String keyword, int offset, int limit); @Override UserInfo findByUserId(String userId); @Override List<UserInfo> findByUserIds(List<String> userIds);  }