Skip to content

Commit 6a27f35

Browse files
thombergsakuksin
authored andcommitted
updated with valueOf()
1 parent 1f9fbc2 commit 6a27f35

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

spring-boot/argumentresolver/src/main/java/io/reflectoring/argumentresolver/GitRepositoryArgumentResolver.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ public Object resolveArgument(
3232
.substring(0, requestPath.indexOf("/", 1))
3333
.replaceAll("^/", "");
3434

35-
Optional<GitRepository> repository = gitRepositoryFinder.findBySlug(slug);
36-
37-
if (repository.isEmpty()) {
38-
throw new NotFoundException();
39-
}
40-
41-
return repository.get();
35+
return gitRepositoryFinder.findBySlug(slug)
36+
.orElseThrow(NotFoundException::new);
4237
}
4338
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.reflectoring.argumentresolver;
2+
3+
import lombok.Value;
4+
5+
@Value
6+
class GitRepositoryIdWithValueOf {
7+
8+
private final long value;
9+
10+
/**
11+
* If we have a static valueOf(String) method, we don't need a Converter!
12+
*/
13+
public static GitRepositoryIdWithValueOf valueOf(String value){
14+
return new GitRepositoryIdWithValueOf(Long.parseLong(value));
15+
}
16+
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.reflectoring.argumentresolver;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
9+
import org.springframework.boot.test.mock.mockito.MockBean;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
12+
@WebMvcTest(controllers = GitRepositoryIdWithValueOfTestController.class)
13+
class GitRepositoryIdWithValueOfTest {
14+
15+
@Autowired
16+
private MockMvc mockMvc;
17+
18+
@MockBean
19+
private GitRepositoryFinder gitRepositoryFinder;
20+
21+
@Test
22+
void resolvesRepositoryId() throws Exception {
23+
mockMvc.perform(get("/repositories/42"))
24+
.andExpect(status().isOk());
25+
}
26+
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.reflectoring.argumentresolver;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
class GitRepositoryIdWithValueOfTestController {
11+
12+
@GetMapping("/repositories/{repositoryId}")
13+
String getRepository(@PathVariable("repositoryId") GitRepositoryIdWithValueOf gitRepositoryId) {
14+
assertThat(gitRepositoryId).isNotNull();
15+
return "test";
16+
}
17+
18+
}

0 commit comments

Comments
 (0)