From 0a9f7332dbc76e0b5492e1fc95a5ceb3f7e0072b Mon Sep 17 00:00:00 2001 From: Toshiaki Maki Date: Tue, 9 Apr 2024 01:56:32 +0900 Subject: [PATCH] Rename --- .../post/{PostService.java => PostClient.java} | 8 ++++---- src/main/java/com/example/post/PostController.java | 10 +++++----- .../java/com/example/post/ssr/SsrController.java | 12 ++++++------ .../java/com/example/post/ssr/SsrControllerTest.java | 8 ++++---- 4 files changed, 19 insertions(+), 19 deletions(-) rename src/main/java/com/example/post/{PostService.java => PostClient.java} (84%) diff --git a/src/main/java/com/example/post/PostService.java b/src/main/java/com/example/post/PostClient.java similarity index 84% rename from src/main/java/com/example/post/PostService.java rename to src/main/java/com/example/post/PostClient.java index 00ff067..3c82c7c 100644 --- a/src/main/java/com/example/post/PostService.java +++ b/src/main/java/com/example/post/PostClient.java @@ -6,16 +6,16 @@ import am.ik.spring.http.client.RetryableClientHttpRequestInterceptor; import org.springframework.core.ParameterizedTypeReference; -import org.springframework.stereotype.Service; +import org.springframework.stereotype.Component; import org.springframework.util.backoff.FixedBackOff; import org.springframework.web.client.RestClient; -@Service -public class PostService { +@Component +public class PostClient { private final RestClient restClient; - public PostService(PostApiProps props, RestClient.Builder restClientBuilder) { + public PostClient(PostApiProps props, RestClient.Builder restClientBuilder) { this.restClient = restClientBuilder.baseUrl(props.url()) .requestInterceptor(new RetryableClientHttpRequestInterceptor( new FixedBackOff(1_000, 2), opts -> opts.sensitiveHeaders(Set.of("nel", "report-to", diff --git a/src/main/java/com/example/post/PostController.java b/src/main/java/com/example/post/PostController.java index 5ac4d22..4875f07 100644 --- a/src/main/java/com/example/post/PostController.java +++ b/src/main/java/com/example/post/PostController.java @@ -9,20 +9,20 @@ @RestController public class PostController { - private final PostService postService; + private final PostClient postClient; - public PostController(PostService postService) { - this.postService = postService; + public PostController(PostClient postClient) { + this.postClient = postClient; } @GetMapping(path = "/api/posts") public List getPosts() { - return this.postService.getPosts(); + return this.postClient.getPosts(); } @GetMapping(path = "/api/posts/{id}") public Post getPost(@PathVariable int id) { - return this.postService.getPost(id); + return this.postClient.getPost(id); } } diff --git a/src/main/java/com/example/post/ssr/SsrController.java b/src/main/java/com/example/post/ssr/SsrController.java index cfc0956..c4a26dc 100644 --- a/src/main/java/com/example/post/ssr/SsrController.java +++ b/src/main/java/com/example/post/ssr/SsrController.java @@ -4,7 +4,7 @@ import java.util.Map; import com.example.post.Post; -import com.example.post.PostService; +import com.example.post.PostClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -15,22 +15,22 @@ public class SsrController { private final ReactRenderer reactRenderer; - private final PostService postService; + private final PostClient postClient; - public SsrController(ReactRenderer reactRenderer, PostService postService) { + public SsrController(ReactRenderer reactRenderer, PostClient postClient) { this.reactRenderer = reactRenderer; - this.postService = postService; + this.postClient = postClient; } @GetMapping(path = { "/", "/posts" }) public String index() { - List posts = this.postService.getPosts(); + List posts = this.postClient.getPosts(); return this.reactRenderer.render("/", Map.of("preLoadedPosts", posts)); } @GetMapping(path = { "/posts/{id}" }) public String post(@PathVariable int id) { - Post post = this.postService.getPost(id); + Post post = this.postClient.getPost(id); return this.reactRenderer.render("/posts/%d".formatted(id), Map.of("preLoadedPost", post)); } diff --git a/src/test/java/com/example/post/ssr/SsrControllerTest.java b/src/test/java/com/example/post/ssr/SsrControllerTest.java index e848dd4..5725213 100644 --- a/src/test/java/com/example/post/ssr/SsrControllerTest.java +++ b/src/test/java/com/example/post/ssr/SsrControllerTest.java @@ -3,7 +3,7 @@ import java.util.List; import com.example.post.Post; -import com.example.post.PostService; +import com.example.post.PostClient; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.DomNode; import com.gargoylesoftware.htmlunit.html.DomNodeList; @@ -32,11 +32,11 @@ class SsrControllerTest { WebClient webClient; @MockBean - PostService postService; + PostClient postClient; @Test void index() throws Exception { - given(this.postService.getPosts()).willReturn(List.of(new Post(2, "SSR", "Server Side Rendering!", 1000), + given(this.postClient.getPosts()).willReturn(List.of(new Post(2, "SSR", "Server Side Rendering!", 1000), new Post(1, "Hello", "Hello World!", 1000))); HtmlPage page = this.webClient.getPage("/"); HtmlDivision root = page.getHtmlElementById("root"); @@ -49,7 +49,7 @@ void index() throws Exception { @Test void post() throws Exception { - given(this.postService.getPost(1)).willReturn(new Post(1, "Hello", "Hello World!", 1000)); + given(this.postClient.getPost(1)).willReturn(new Post(1, "Hello", "Hello World!", 1000)); HtmlPage page = this.webClient.getPage("/posts/1"); HtmlDivision root = page.getHtmlElementById("root"); assertThat(root).isNotNull();