Skip to content

Commit

Permalink
polish : adds template methods for easy access
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed May 10, 2024
1 parent d88bb3f commit 5f36cb3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,35 @@ public HttpClientService(RestClient restClient) {
this.restClient = restClient;
}

<T> T callAndFetchResponse(
<T> T callAndFetchResponseForGetMethod(
Function<UriBuilder, URI> uriFunction,
HttpMethod httpMethod,
@Nullable Object body,
@Nullable Map<String, String> headers,
Class<T> bodyType) {
return callServer(uriFunction, httpMethod, null, body, bodyType, null);
return callServer(uriFunction, HttpMethod.GET, headers, null, bodyType, null);
}

<T> T callAndFetchResponse(
Function<UriBuilder, URI> uriFunction,
HttpMethod httpMethod,
@Nullable Map<String, String> headers,
Class<T> bodyType) {
return callServer(uriFunction, httpMethod, headers, null, bodyType, null);
<T> T callAndFetchResponseForGetMethod(
Function<UriBuilder, URI> uriFunction, ParameterizedTypeReference<T> bodyType) {
return callServer(uriFunction, HttpMethod.GET, null, null, null, bodyType);
}

<T> T callAndFetchResponse(
Function<UriBuilder, URI> uriFunction,
HttpMethod httpMethod,
@Nullable Object body,
ParameterizedTypeReference<T> bodyType) {
return callServer(uriFunction, httpMethod, null, body, null, bodyType);
<T> T callAndFetchResponseForPostMethod(
Function<UriBuilder, URI> uriFunction, Object body, Class<T> bodyType) {
return callServer(uriFunction, HttpMethod.POST, null, body, bodyType, null);
}

public <T> T callAndFetchResponseForPutMethod(
Function<UriBuilder, URI> uriFunction, Object body, Class<T> bodyType) {
return callServer(uriFunction, HttpMethod.PUT, null, body, bodyType, null);
}

String callAndFetchResponseForDeleteMethod(Function<UriBuilder, URI> uriFunction) {
return callAndFetchResponseForDeleteMethod(uriFunction, String.class);
}

<T> T callAndFetchResponseForDeleteMethod(
Function<UriBuilder, URI> uriFunction, Class<T> bodyType) {
return callServer(uriFunction, HttpMethod.DELETE, null, null, bodyType, null);
}

private <T> T callServer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Map;
import java.util.Optional;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -18,46 +17,36 @@ public PostService(HttpClientService httpClientService) {
}

public List<PostDto> findAllPosts() {
return httpClientService.callAndFetchResponse(
return httpClientService.callAndFetchResponseForGetMethod(
uriBuilder -> uriBuilder.path("/posts").build(),
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<PostDto>>() {});
}

public Optional<PostDto> findPostById(Long id) {
PostDto response =
httpClientService.callAndFetchResponse(
httpClientService.callAndFetchResponseForGetMethod(
uriBuilder -> uriBuilder.path("/posts/{postId}").build(id),
HttpMethod.GET,
Map.of("apiKey", "123456"),
PostDto.class);
return Optional.ofNullable(response);
}

public PostDto savePost(PostDto post) {
return httpClientService.callAndFetchResponse(
uriBuilder -> uriBuilder.path("/posts").build(),
HttpMethod.POST,
post,
PostDto.class);
return httpClientService.callAndFetchResponseForPostMethod(
uriBuilder -> uriBuilder.path("/posts").build(), post, PostDto.class);
}

public Optional<PostDto> updatePostById(Long id, PostDto postDto) {
PostDto response =
httpClientService.callAndFetchResponse(
httpClientService.callAndFetchResponseForPutMethod(
uriBuilder -> uriBuilder.path("/posts/{postId}").build(id),
HttpMethod.PUT,
postDto,
PostDto.class);
return Optional.ofNullable(response);
}

public String deletePostById(Long id) {
return httpClientService.callAndFetchResponse(
uriBuilder -> uriBuilder.path("/posts/{postId}").build(id),
HttpMethod.DELETE,
null,
String.class);
return httpClientService.callAndFetchResponseForDeleteMethod(
uriBuilder -> uriBuilder.path("/posts/{postId}").build(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.ExpectedCount.times;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
Expand All @@ -14,6 +15,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
Expand Down Expand Up @@ -50,6 +52,7 @@ void findPostById() throws Exception {
mockServer
.expect(times(1), requestTo("https://jsonplaceholder.typicode.com/posts/1"))
.andExpect(method(HttpMethod.GET))
.andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andRespond(withSuccess(mockApiResponse, MediaType.APPLICATION_JSON));

// Perform the test
Expand Down Expand Up @@ -93,6 +96,7 @@ void createPost() throws Exception {
mockServer
.expect(times(1), requestTo("https://jsonplaceholder.typicode.com/posts"))
.andExpect(method(HttpMethod.POST))
.andExpect(header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andRespond(withSuccess(mockApiResponse, MediaType.APPLICATION_JSON));

// Perform the test
Expand Down

0 comments on commit 5f36cb3

Please sign in to comment.