Skip to content

Commit

Permalink
Fixed issues with wrong path variables names
Browse files Browse the repository at this point in the history
  • Loading branch information
polysantiago authored and Pablo Santiago committed Jun 21, 2017
1 parent cf4fa66 commit 1aef8e6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -121,14 +127,15 @@ private String convertToString(TypeDescriptor sourceType, Object value) {
return value.toString();
}

private Object[] getPathParameters(List<MethodParameter> parameters, Object[] arguments) {
private Map<String, Object> getPathParameters(List<MethodParameter> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 1aef8e6

Please sign in to comment.