diff --git a/src/main/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelper.java b/src/main/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelper.java index 452490b..25ec4f6 100644 --- a/src/main/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelper.java +++ b/src/main/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelper.java @@ -1,6 +1,22 @@ package io.github.polysantiago.spring.rest; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonList; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; +import static org.apache.commons.lang3.ArrayUtils.isNotEmpty; +import static org.apache.commons.lang3.StringUtils.substringAfter; +import static org.apache.commons.lang3.StringUtils.substringBefore; +import static org.springframework.http.MediaType.parseMediaType; +import static org.springframework.util.CollectionUtils.isEmpty; + import io.github.polysantiago.spring.rest.support.MethodParameters; +import java.lang.reflect.Method; +import java.net.URI; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; import lombok.NonNull; import lombok.Setter; import lombok.experimental.Accessors; @@ -16,24 +32,14 @@ import org.springframework.http.RequestEntity.BodyBuilder; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.util.UriComponentsBuilder; -import java.lang.reflect.Method; -import java.net.URI; -import java.util.Collection; -import java.util.List; -import java.util.stream.Stream; - -import static java.util.Collections.singletonList; -import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toMap; -import static org.apache.commons.lang3.ArrayUtils.isNotEmpty; -import static org.apache.commons.lang3.StringUtils.substringAfter; -import static org.apache.commons.lang3.StringUtils.substringBefore; -import static org.springframework.http.MediaType.parseMediaType; -import static org.springframework.util.CollectionUtils.isEmpty; - class RestClientInterceptorHelper { private static final TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class); @@ -121,14 +127,15 @@ private String convertToString(TypeDescriptor sourceType, Object value) { return value.toString(); } - private Object[] getPathParameters(List parameters, Object[] arguments) { + private Map getPathParameters(List parameters, Object[] arguments) { if (!isEmpty(parameters)) { return parameters.stream() .filter(parameter -> parameter.hasParameterAnnotation(PathVariable.class)) - .map(parameter -> arguments[parameter.getParameterIndex()]) - .toArray(Object[]::new); + .collect(toMap( + parameter -> parameter.getParameterAnnotation(PathVariable.class).value(), + parameter -> arguments[parameter.getParameterIndex()])); } - return new Object[]{}; + return emptyMap(); } private static HttpMethod toHttpMethod(RequestMethod requestMethod) { diff --git a/src/test/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelperTest.java b/src/test/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelperTest.java new file mode 100644 index 0000000..13692d7 --- /dev/null +++ b/src/test/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelperTest.java @@ -0,0 +1,69 @@ +package io.github.polysantiago.spring.rest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Method; +import java.net.URI; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.aopalliance.intercept.MethodInvocation; +import org.junit.Test; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +public class RestClientInterceptorHelperTest { + + private static final URI ANY_URL = URI.create("http://example.com"); + + @Test + public void testCorrectPathVariableName() throws Exception { + Method method = RestClientInterface.class.getMethod("correctPathVariable", String.class); + MethodInvocation invocation = new MockMethodInvocation(method, new Object[]{"id"}); + RestClientInterceptorHelper helper = RestClientInterceptorHelper.from(invocation); + assertThat(helper.buildRequest(ANY_URL)).isNotNull(); + } + + @Test + public void testWrongPathVariableName() throws Exception { + Method method = RestClientInterface.class.getMethod("wrongPathVariable", String.class); + MethodInvocation invocation = new MockMethodInvocation(method, new Object[]{"not-id"}); + RestClientInterceptorHelper helper = RestClientInterceptorHelper.from(invocation); + assertThatIllegalArgumentException() + .isThrownBy(() -> helper.buildRequest(ANY_URL)) + .withMessage("Map has no value for 'id'"); + } + + interface RestClientInterface { + + @GetMapping("/{id}") + void correctPathVariable(@PathVariable("id") String id); + + @GetMapping("/{id}") + void wrongPathVariable(@PathVariable("not-id") String notId); + + } + + @Getter + @RequiredArgsConstructor + private class MockMethodInvocation implements MethodInvocation { + private final Method method; + private final Object[] arguments; + + @Override + public Object proceed() throws Throwable { + return null; + } + + @Override + public Object getThis() { + return null; + } + + @Override + public AccessibleObject getStaticPart() { + return null; + } + } +} \ No newline at end of file