-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat : streamlining client request handling #1202
Conversation
Warning Rate Limit Exceeded@rajadilipkolli has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 27 minutes and 5 seconds before requesting another review. How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. WalkthroughThe recent update enhances the HTTP client module in a Spring Boot application by introducing retry capabilities, improving timeout settings, and refactoring service classes for better structure and readability. Key changes include adding dependencies, enabling retries, and streamlining HTTP request handling through a new service class. Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Out of diff range and nitpick comments (1)
httpClients/boot-restclient/src/main/java/com/example/restclient/bootrestclient/services/HttpClientService.java (1)
16-20
: Retry configuration with@Retryable
is correctly set up for generic exceptions. Consider specifying more specific exception types if applicable.
Quality Gate passedIssues Measures |
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Service | ||
public class PostService { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai, can you please generate junits for this class using @AutoConfigureMockRestServiceServer and @SpringBootTest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, here are the JUnit tests for the PostService
class using @AutoConfigureMockRestServiceServer
and @SpringBootTest
. Each test ensures that the service methods interact correctly with the HttpClientService
.
package com.example.restclient.bootrestclient.services;
import com.example.restclient.bootrestclient.model.response.PostDto;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureMockRestServiceServer;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Optional;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockRestServiceServer
public class PostServiceTest {
@Autowired
private PostService postService;
@MockBean
private HttpClientService httpClientService;
@Test
public void testFindAllPosts() {
when(httpClientService.callAndFetchResponse(any(), eq(HttpMethod.GET), isNull(), any()))
.thenReturn(List.of(new PostDto()));
List<PostDto> result = postService.findAllPosts();
assertNotNull(result);
assertFalse(result.isEmpty());
verify(httpClientService).callAndFetchResponse(any(), eq(HttpMethod.GET), isNull(), any());
}
@Test
public void testFindPostById() {
when(httpClientService.callAndFetchResponse(any(), eq(HttpMethod.GET), any(), eq(PostDto.class)))
.thenReturn(new PostDto());
Optional<PostDto> result = postService.findPostById(1L);
assertTrue(result.isPresent());
verify(httpClientService).callAndFetchResponse(any(), eq(HttpMethod.GET), any(), eq(PostDto.class));
}
@Test
public void testSavePost() {
PostDto post = new PostDto();
when(httpClientService.callAndFetchResponse(any(), eq(HttpMethod.POST), eq(post), eq(PostDto.class)))
.thenReturn(post);
PostDto result = postService.savePost(post);
assertNotNull(result);
verify(httpClientService).callAndFetchResponse(any(), eq(HttpMethod.POST), eq(post), eq(PostDto.class));
}
@Test
public void testUpdatePostById() {
PostDto post = new PostDto();
when(httpClientService.callAndFetchResponse(any(), eq(HttpMethod.PUT), eq(post), eq(PostDto.class)))
.thenReturn(post);
Optional<PostDto> result = postService.updatePostById(1L, post);
assertTrue(result.isPresent());
verify(httpClientService).callAndFetchResponse(any(), eq(HttpMethod.PUT), eq(post), eq(PostDto.class));
}
@Test
public void testDeletePostById() {
when(httpClientService.callAndFetchResponse(any(), eq(HttpMethod.DELETE), isNull(), eq(String.class)))
.thenReturn("Deleted");
String result = postService.deletePostById(1L);
assertEquals("Deleted", result);
verify(httpClientService).callAndFetchResponse(any(), eq(HttpMethod.DELETE), isNull(), eq(String.class));
}
}
These tests mock the HttpClientService
to verify that PostService
is making the correct calls and handling the responses as expected. Let me know if you need any more specific scenarios tested or further modifications!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai, I don't see using MockRestServiceServer
in the response, can you please use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, I've revised the JUnit tests for the PostService
class to explicitly use MockRestServiceServer
. This will allow us to set up expectations and responses for the HTTP requests made by the service methods. Here's the updated test class:
package com.example.restclient.bootrestclient.services;
import com.example.restclient.bootrestclient.model.response.PostDto;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureMockRestServiceServer;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Optional;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockRestServiceServer
public class PostServiceTest {
@Autowired
private PostService postService;
@Autowired
private RestTemplate restTemplate;
private MockRestServiceServer mockServer;
@BeforeEach
public void setUp() {
mockServer = MockRestServiceServer.createServer(restTemplate);
}
@Test
public void testFindAllPosts() {
mockServer.expect(requestTo("/posts"))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("[{}]", MediaType.APPLICATION_JSON));
List<PostDto> result = postService.findAllPosts();
assertNotNull(result);
assertFalse(result.isEmpty());
mockServer.verify();
}
@Test
public void testFindPostById() {
mockServer.expect(requestTo("/posts/1"))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
Optional<PostDto> result = postService.findPostById(1L);
assertTrue(result.isPresent());
mockServer.verify();
}
@Test
public void testSavePost() {
PostDto post = new PostDto();
mockServer.expect(requestTo("/posts"))
.andExpect(method(HttpMethod.POST))
.andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
PostDto result = postService.savePost(post);
assertNotNull(result);
mockServer.verify();
}
@Test
public void testUpdatePostById() {
PostDto post = new PostDto();
mockServer.expect(requestTo("/posts/1"))
.andExpect(method(HttpMethod.PUT))
.andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
Optional<PostDto> result = postService.updatePostById(1L, post);
assertTrue(result.isPresent());
mockServer.verify();
}
@Test
public void testDeletePostById() {
mockServer.expect(requestTo("/posts/1"))
.andExpect(method(HttpMethod.DELETE))
.andRespond(withSuccess("Deleted", MediaType.TEXT_PLAIN));
String result = postService.deletePostById(1L);
assertEquals("Deleted", result);
mockServer.verify();
}
}
These tests now use MockRestServiceServer
to set up expectations on the HTTP requests and provide mock responses, ensuring that PostService
is making the correct calls and handling the responses as expected. Let me know if you need any more specific scenarios tested or further modifications!
Summary by CodeRabbit
New Features
Refactor
Bug Fixes