Repository -> GitRepository
This commit is contained in:
@@ -4,7 +4,7 @@ package io.reflectoring.argumentresolver;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
class Repository {
|
||||
class GitRepository {
|
||||
|
||||
private final Long id;
|
||||
private final String slug;
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.reflectoring.argumentresolver;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
@@ -11,13 +10,13 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
class RepositoryArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
class GitRepositoryArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private final RepositoryFinder repositoryFinder;
|
||||
private final GitRepositoryFinder gitRepositoryFinder;
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return parameter.getParameter().getType() == Repository.class;
|
||||
return parameter.getParameter().getType() == GitRepository.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,7 +32,7 @@ class RepositoryArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
.substring(0, requestPath.indexOf("/", 1))
|
||||
.replaceAll("^/", "");
|
||||
|
||||
Optional<Repository> repository = repositoryFinder.findBySlug(slug);
|
||||
Optional<GitRepository> repository = gitRepositoryFinder.findBySlug(slug);
|
||||
|
||||
if (repository.isEmpty()) {
|
||||
throw new NotFoundException();
|
||||
@@ -8,13 +8,13 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
class RepositoryArgumentResolverConfiguration implements WebMvcConfigurer {
|
||||
class GitRepositoryArgumentResolverConfiguration implements WebMvcConfigurer {
|
||||
|
||||
private final RepositoryFinder repositoryFinder;
|
||||
private final GitRepositoryFinder gitRepositoryFinder;
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
resolvers.add(new RepositoryArgumentResolver(repositoryFinder));
|
||||
resolvers.add(new GitRepositoryArgumentResolver(gitRepositoryFinder));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.reflectoring.argumentresolver;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface GitRepositoryFinder {
|
||||
|
||||
Optional<GitRepository> findBySlug(String slug);
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package io.reflectoring.argumentresolver;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
class RepositoryId {
|
||||
class GitRepositoryId {
|
||||
|
||||
private final long value;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.reflectoring.argumentresolver;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class GitRepositoryIdConverter implements Converter<String, GitRepositoryId> {
|
||||
|
||||
@Override
|
||||
public GitRepositoryId convert(String source) {
|
||||
return new GitRepositoryId(Long.parseLong(source));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package io.reflectoring.argumentresolver;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface RepositoryFinder {
|
||||
|
||||
Optional<Repository> findBySlug(String slug);
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package io.reflectoring.argumentresolver;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class RepositoryIdConverter implements Converter<String, RepositoryId> {
|
||||
|
||||
@Override
|
||||
public RepositoryId convert(String source) {
|
||||
return new RepositoryId(Long.parseLong(source));
|
||||
}
|
||||
}
|
||||
@@ -11,20 +11,20 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@WebMvcTest(controllers = RepositoryArgumentResolverTestController.class)
|
||||
class RepositoryArgumentResolverTest {
|
||||
@WebMvcTest(controllers = GitRepositoryArgumentResolverTestController.class)
|
||||
class GitRepositoryArgumentResolverTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private RepositoryFinder repositoryFinder;
|
||||
private GitRepositoryFinder gitRepositoryFinder;
|
||||
|
||||
@Test
|
||||
void resolvesSiteSuccessfully() throws Exception {
|
||||
|
||||
given(repositoryFinder.findBySlug("my-repo"))
|
||||
.willReturn(Optional.of(new Repository(1L, "my-repo")));
|
||||
given(gitRepositoryFinder.findBySlug("my-repo"))
|
||||
.willReturn(Optional.of(new GitRepository(1L, "my-repo")));
|
||||
|
||||
mockMvc.perform(get("/my-repo/listContributors"))
|
||||
.andExpect(status().isOk());
|
||||
@@ -33,7 +33,7 @@ class RepositoryArgumentResolverTest {
|
||||
@Test
|
||||
void notFoundOnUnknownSlug() throws Exception {
|
||||
|
||||
given(repositoryFinder.findBySlug("unknownSlug"))
|
||||
given(gitRepositoryFinder.findBySlug("unknownSlug"))
|
||||
.willReturn(Optional.empty());
|
||||
|
||||
mockMvc.perform(get("/unknownSlug/listContributors"))
|
||||
@@ -8,11 +8,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "/{repositorySlug}")
|
||||
class RepositoryArgumentResolverTestController {
|
||||
class GitRepositoryArgumentResolverTestController {
|
||||
|
||||
@GetMapping("/listContributors")
|
||||
String listContributors(Repository repository) {
|
||||
assertThat(repository.getId()).isEqualTo(1L);
|
||||
String listContributors(GitRepository gitRepository) {
|
||||
assertThat(gitRepository.getId()).isEqualTo(1L);
|
||||
return "test";
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@WebMvcTest(controllers = RepositoryIdConverterTestController.class)
|
||||
class RepositoryIdConverterTest {
|
||||
@WebMvcTest(controllers = GitRepositoryIdConverterTestController.class)
|
||||
class GitRepositoryIdConverterTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private RepositoryFinder repositoryFinder;
|
||||
private GitRepositoryFinder gitRepositoryFinder;
|
||||
|
||||
@Test
|
||||
void resolvesRepositoryId() throws Exception {
|
||||
@@ -7,11 +7,11 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
class RepositoryIdConverterTestController {
|
||||
class GitRepositoryIdConverterTestController {
|
||||
|
||||
@GetMapping("/repositories/{repositoryId}")
|
||||
String getRepository(@PathVariable("repositoryId") RepositoryId repositoryId) {
|
||||
assertThat(repositoryId).isNotNull();
|
||||
String getRepository(@PathVariable("repositoryId") GitRepositoryId gitRepositoryId) {
|
||||
assertThat(gitRepositoryId).isNotNull();
|
||||
return "test";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user