RouteTokenFormatter implements TokenFormatter { @Override public PlaceRequest toPlaceRequest(String placeToken) throws TokenFormatException { if (!placeTokenIsValid(placeToken)) return toPlaceRequest("/" + placeToken); int split = placeToken.indexOf('?'); String place = (split != -1) ? placeToken.substring(0, split) : placeToken; String query = (split != -1) ? placeToken.substring(split + 1) : ""; TreeSet<RouteMatch> allMatches = matchRoute(place); RouteMatch match = (!allMatches.isEmpty()) ? allMatches.last() : new RouteMatch(place, 0, emptyMap()); return PlaceRequest.of(match.route).with(parseQueryString(query, match.parameters)).build(); } @Inject  RouteTokenFormatter(UrlUtils urlUtils, Set<Place> places); @Override String toPlaceToken(PlaceRequest placeRequest); @Override PlaceRequest toPlaceRequest(String placeToken);  }
@Test public void testToPlaceRequestStaticVsParameterMatch() { PlaceRequest placeRequest01 = tokenFormatter.toPlaceRequest("/user/0x42/albums/0xAFFE"); PlaceRequest placeRequest02 = tokenFormatter.toPlaceRequest("/user/staticUserId/albums/staticAlbumId"); PlaceRequest placeRequest03 = tokenFormatter.toPlaceRequest("/user/0x42/albums/staticAlbumId"); PlaceRequest placeRequest04 = tokenFormatter.toPlaceRequest("/user/staticUserId/albums/0xAFFE"); PlaceRequest placeRequest05 = tokenFormatter.toPlaceRequest("/privacy"); PlaceRequest placeRequest06 = tokenFormatter.toPlaceRequest("/vanity"); PlaceRequest placeRequest07 = tokenFormatter.toPlaceRequest("/user/0x42/albums/0xAFFE?start=0"); PlaceRequest placeRequest08 = tokenFormatter.toPlaceRequest("/vanity?a=valueA&b=valueB"); PlaceRequest placeRequest09 = tokenFormatter.toPlaceRequest("vanity?a=valueA&b=valueB"); PlaceRequest placeRequest10 = tokenFormatter.toPlaceRequest("!/crawl/vanity"); PlaceRequest placeRequest11 = tokenFormatter.toPlaceRequest("!/crawl/vanity?a=valueA&b=valueB"); assertEquals("/user/{userId}/albums/{albumId}", placeRequest01.getNameToken()); assertEquals(2, placeRequest01.getParameterNames().size()); assertEquals("0x42", placeRequest01.getParameter("userId", null)); assertEquals("0xAFFE", placeRequest01.getParameter("albumId", null)); assertEquals("/user/staticUserId/albums/staticAlbumId", placeRequest02.getNameToken()); assertEquals(0, placeRequest02.getParameterNames().size()); assertEquals("/user/{userId}/albums/staticAlbumId", placeRequest03.getNameToken()); assertEquals(1, placeRequest03.getParameterNames().size()); assertEquals("0x42", placeRequest03.getParameter("userId", null)); assertEquals("/user/staticUserId/albums/{albumId}", placeRequest04.getNameToken()); assertEquals(1, placeRequest04.getParameterNames().size()); assertEquals("0xAFFE", placeRequest04.getParameter("albumId", null)); assertEquals("/privacy", placeRequest05.getNameToken()); assertEquals(0, placeRequest05.getParameterNames().size()); assertEquals("/{vanityId}", placeRequest06.getNameToken()); assertEquals(1, placeRequest06.getParameterNames().size()); assertEquals("vanity", placeRequest06.getParameter("vanityId", null)); assertEquals("/user/{userId}/albums/{albumId}", placeRequest07.getNameToken()); assertEquals(3, placeRequest07.getParameterNames().size()); assertEquals("0x42", placeRequest07.getParameter("userId", null)); assertEquals("0xAFFE", placeRequest07.getParameter("albumId", null)); assertEquals("0", placeRequest07.getParameter("start", null)); assertEquals("/{vanityId}", placeRequest08.getNameToken()); assertEquals(3, placeRequest08.getParameterNames().size()); assertEquals("vanity", placeRequest08.getParameter("vanityId", null)); assertEquals("valueA", placeRequest08.getParameter("a", null)); assertEquals("valueB", placeRequest08.getParameter("b", null)); assertEquals("/{vanityId}", placeRequest09.getNameToken()); assertEquals(3, placeRequest09.getParameterNames().size()); assertEquals("vanity", placeRequest09.getParameter("vanityId", null)); assertEquals("valueA", placeRequest09.getParameter("a", null)); assertEquals("valueB", placeRequest09.getParameter("b", null)); assertEquals("!/crawl/{vanityId}", placeRequest10.getNameToken()); assertEquals(1, placeRequest10.getParameterNames().size()); assertEquals("vanity", placeRequest10.getParameter("vanityId", null)); assertEquals("!/crawl/{vanityId}", placeRequest11.getNameToken()); assertEquals(3, placeRequest11.getParameterNames().size()); assertEquals("vanity", placeRequest10.getParameter("vanityId", null)); assertEquals("valueA", placeRequest11.getParameter("a", null)); assertEquals("valueB", placeRequest11.getParameter("b", null)); }
@Test public void testToPlaceRequestEmptyRoute() { PlaceRequest placeRequest10 = tokenFormatter.toPlaceRequest("/"); PlaceRequest placeRequest11 = tokenFormatter.toPlaceRequest("/?a=valueA&b=valueB"); PlaceRequest placeRequest12 = tokenFormatter.toPlaceRequest(""); PlaceRequest placeRequest13 = tokenFormatter.toPlaceRequest("?a=valueA&b=valueB"); assertEquals("/", placeRequest10.getNameToken()); assertEquals(0, placeRequest10.getParameterNames().size()); assertEquals("/", placeRequest11.getNameToken()); assertEquals(2, placeRequest11.getParameterNames().size()); assertEquals("valueA", placeRequest11.getParameter("a", null)); assertEquals("valueB", placeRequest11.getParameter("b", null)); assertEquals("/", placeRequest12.getNameToken()); assertEquals(0, placeRequest12.getParameterNames().size()); assertEquals("/", placeRequest13.getNameToken()); assertEquals(2, placeRequest13.getParameterNames().size()); assertEquals("valueA", placeRequest13.getParameter("a", null)); assertEquals("valueB", placeRequest13.getParameter("b", null)); }
@Test public void testToPlaceRequestNotExistingRoute() { PlaceRequest placeRequest13 = tokenFormatter.toPlaceRequest("/not/existing"); PlaceRequest placeRequest14 = tokenFormatter.toPlaceRequest("/not/existing?a=valueA&b=valueB"); PlaceRequest placeRequest15 = tokenFormatter.toPlaceRequest("not/existing"); assertEquals("/not/existing", placeRequest13.getNameToken()); assertEquals(0, placeRequest13.getParameterNames().size()); assertEquals("/not/existing", placeRequest14.getNameToken()); assertEquals(2, placeRequest14.getParameterNames().size()); assertEquals("valueA", placeRequest14.getParameter("a", null)); assertEquals("valueB", placeRequest14.getParameter("b", null)); assertEquals("/not/existing", placeRequest15.getNameToken()); assertEquals(0, placeRequest15.getParameterNames().size()); }