UserLinksInjector { public UserDto injectLinks(UserDto userDto, ServiceContext serviceContext) { final UriBuilder uriBuilder = serviceContext.getBaseUriBuilder(); final List<Link> links = new ArrayList<>(6); links.add( LinksHelper.createLink( HttpMethod.GET, uriBuilder .clone() .path(UserService.class) .path(UserService.class, "getById") .build(userDto.getId()) .toString(), null, APPLICATION_JSON, LINK_REL_SELF)); links.add( LinksHelper.createLink( HttpMethod.GET, uriBuilder .clone() .path(UserService.class) .path(UserService.class, "getCurrent") .build() .toString(), null, APPLICATION_JSON, LINK_REL_CURRENT_USER)); links.add( LinksHelper.createLink( HttpMethod.POST, uriBuilder .clone() .path(UserService.class) .path(UserService.class, "updatePassword") .build() .toString(), APPLICATION_FORM_URLENCODED, null, LINK_REL_CURRENT_USER_PASSWORD)); links.add( LinksHelper.createLink( HttpMethod.GET, uriBuilder .clone() .path(ProfileService.class) .path(ProfileService.class, "getById") .build(userDto.getId()) .toString(), null, APPLICATION_JSON, LINK_REL_PROFILE)); links.add( LinksHelper.createLink( HttpMethod.GET, uriBuilder .clone() .path(UserService.class) .path(UserService.class, "getSettings") .build() .toString(), null, APPLICATION_JSON, LINK_REL_CURRENT_USER_SETTINGS)); links.add( LinksHelper.createLink( HttpMethod.GET, uriBuilder .clone() .path(PreferencesService.class) .path(PreferencesService.class, "find") .build() .toString(), null, APPLICATION_JSON, LINK_REL_PREFERENCES)); return userDto.withLinks(links); }  UserDto injectLinks(UserDto userDto, ServiceContext serviceContext);  }
@Test public void shouldInjectLinks() throws Exception { final UserDto userDto = DtoFactory.newDto(UserDto.class) .withId("user123") .withEmail("user@codenvy.com") .withName("user"); linksInjector.injectLinks(userDto, serviceContext); final Set<Pair<String, String>> links = userDto .getLinks() .stream() .map(link -> Pair.of(link.getMethod(), link.getRel())) .collect(Collectors.toSet()); final Set<Pair<String, String>> expectedLinks = new HashSet<>( asList( Pair.of("GET", Constants.LINK_REL_SELF), Pair.of("GET", Constants.LINK_REL_CURRENT_USER), Pair.of("GET", Constants.LINK_REL_PROFILE), Pair.of("GET", Constants.LINK_REL_CURRENT_USER_SETTINGS), Pair.of("GET", Constants.LINK_REL_PREFERENCES), Pair.of("POST", Constants.LINK_REL_CURRENT_USER_PASSWORD))); assertEquals( links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n"); }