DefaultParametersControl implements ParametersControl { public String fillUri(String[] paramNames, Object... paramValues) { if (paramNames.length != paramValues.length) { throw new IllegalArgumentException("paramNames must have the same length as paramValues. Names: " + Arrays.toString(paramNames) + " Values: " + Arrays.toString(paramValues)); } String[] splittedPatterns = StringUtils.extractParameters(originalPattern); String base = originalPattern; for (int i=0; i<parameters.size(); i++) { String key = parameters.get(i); Object param = selectParam(key, paramNames, paramValues); Object result = evaluator.get(param, key); if (result != null) { Class<?> type = result.getClass(); if (converters.existsTwoWayFor(type)) { TwoWayConverter converter = converters.twoWayConverterFor(type); result = converter.convert(result); } } base = base.replace('{' + splittedPatterns[i] + '}', result == null ? "" : result.toString()); } return base.replaceAll("\\.\\*", ""); } DefaultParametersControl(String originalPattern, Map<String, String> parameterPatterns, Converters converters, Evaluator evaluator);  DefaultParametersControl(String originalPattern, Converters converters, Evaluator evaluator); String fillUri(String[] paramNames, Object... paramValues); boolean matches(String uri); void fillIntoRequest(String uri, MutableRequest request); String apply(String[] values);  }
@Test public void shouldTranslateAsteriskAsEmpty() { String uri = new DefaultParametersControl("/clients/.*", converters, evaluator).fillUri(new String[] {"client"}, client(3L)); assertThat(uri, is(equalTo("/clients/"))); }
@Test public void shouldTranslatePatternArgs() { String uri = new DefaultParametersControl("/clients/{client.id}", converters, evaluator).fillUri(new String[] {"client"}, client(3L)); assertThat(uri, is(equalTo("/clients/3"))); }
@Test public void shouldTranslatePatternArgsWithRegex() { String uri = new DefaultParametersControl("/clients/{id:[0-9]{1,}}", converters, evaluator).fillUri(new String[] {"id"}, 30L); assertThat(uri, is(equalTo("/clients/30"))); }
@Test public void shouldTranslatePatternArgsWithMultipleRegexes() { String uri = new DefaultParametersControl("/test/{hash1:[a-z0-9]{16}}{id}{hash2:[a-z0-9]{16}}/", converters, evaluator).fillUri(new String[] {"hash1", "id", "hash2"}, "0123456789abcdef", "1234", "fedcba9876543210"); assertThat(uri, is(equalTo("/test/0123456789abcdef1234fedcba9876543210/"))); }
@Test public void shouldTranslatePatternArgNullAsEmpty() { String uri = new DefaultParametersControl("/clients/{client.id}", converters, evaluator).fillUri(new String[] {"client"}, client(null)); assertThat(uri, is(equalTo("/clients/"))); }
@Test public void shouldUseConverterIfItExists() { when(converters.existsTwoWayFor(Client.class)).thenReturn(true); when(converters.twoWayConverterFor(Client.class)).thenReturn(converter); when(converter.convert(any(Client.class))).thenReturn("john"); String uri = new DefaultParametersControl("/clients/{client}", converters, evaluator).fillUri(new String[] {"client"}, client(null)); assertThat(uri, is(equalTo("/clients/john"))); }
@Test public void shouldTranslatePatternArgInternalNullAsEmpty() { String uri = new DefaultParametersControl("/clients/{client.child.id}", converters, evaluator) .fillUri(new String[] {"client"}, client(null)); assertThat(uri, is(equalTo("/clients/"))); }
@Test public void fillURLWithGreedyParameters() throws SecurityException, NoSuchMethodException { DefaultParametersControl control = new DefaultParametersControl("/clients/{pathToFile*}", converters, evaluator); String filled = control.fillUri(new String[] {"pathToFile"}, "my/path/to/file"); assertThat(filled, is("/clients/my/path/to/file")); }
@Test public void fillURLWithoutGreedyParameters() throws SecurityException, NoSuchMethodException { DefaultParametersControl control = new DefaultParametersControl("/clients/{pathToFile}", converters, evaluator); String filled = control.fillUri(new String[] {"pathToFile"}, "my/path/to/file"); assertThat(filled, is("/clients/my/path/to/file")); }