-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issues with wrong path variables names
- Loading branch information
1 parent
cf4fa66
commit 1aef8e6
Showing
2 changed files
with
96 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/io/github/polysantiago/spring/rest/RestClientInterceptorHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |