Skip to content

Commit

Permalink
#62 Extract HTTP method parameter from generic call to Git API
Browse files Browse the repository at this point in the history
  • Loading branch information
conorheffron committed Sep 29, 2024
1 parent 62c881f commit 55052c5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/ironoc/portfolio/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

public interface Client {

<T> List<T> callGitHubApi(String apiUri, String uri, Class<T> type);
<T> List<T> callGitHubApi(String apiUri, String uri, Class<T> type, String httpMethod);

HttpsURLConnection createConn(String url, String baseUrl) throws IOException;
HttpsURLConnection createConn(String url, String baseUrl, String httpMethod) throws IOException;

InputStream readInputStream(HttpsURLConnection conn) throws IOException;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/ironoc/portfolio/client/GitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public GitClient(PropertyConfigI propertyConfig,
}

@Override
public <T> List<T> callGitHubApi(String apiUri, String uri, Class<T> type) {
public <T> List<T> callGitHubApi(String apiUri, String uri, Class<T> type, String httpMethod) {
info("Triggering GET request: url={}", apiUri);
List<T> dtos = new ArrayList<>();
InputStream inputStream = null;
try {
HttpsURLConnection conn = this.createConn(apiUri, uri);
HttpsURLConnection conn = this.createConn(apiUri, uri, httpMethod);
if (conn == null) {
error("Failed to created connection");
return Collections.emptyList();
Expand Down Expand Up @@ -78,7 +78,7 @@ public <T> List<T> callGitHubApi(String apiUri, String uri, Class<T> type) {
}

@Override
public HttpsURLConnection createConn(String url, String baseUrl) throws IOException {
public HttpsURLConnection createConn(String url, String baseUrl, String httpMethod) throws IOException {
URL urlBase = new URL(baseUrl);
String base = urlBase.getProtocol() + "://" + urlBase.getHost();
if (!urlUtils.isValidURL(url) || !url.startsWith(base)) {
Expand All @@ -93,7 +93,7 @@ public HttpsURLConnection createConn(String url, String baseUrl) throws IOExcept
} else {
conn.setRequestProperty("Authorization", token);
}
conn.setRequestMethod(HttpMethod.GET.name());
conn.setRequestMethod(httpMethod);
conn.setFollowRedirects(propertyConfig.getGitFollowRedirects());
conn.setConnectTimeout(propertyConfig.getGitTimeoutConnect());
conn.setReadTimeout(propertyConfig.getGitTimeoutRead());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.ironoc.portfolio.utils.UrlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

Expand Down Expand Up @@ -61,7 +62,7 @@ public List<RepositoryDetailDto> getRepoDetails(String username) {
warn("URL is not valid: url={}", apiUri);
return Collections.emptyList();
}
return gitClient.callGitHubApi(apiUri, uri, RepositoryDetailDto.class);
return gitClient.callGitHubApi(apiUri, uri, RepositoryDetailDto.class, HttpMethod.GET.name());
}

@Override
Expand Down Expand Up @@ -107,7 +108,7 @@ public List<RepositoryIssueDto> getIssues(String userId, String repo) {
warn("URL is not valid: url={}", apiUri);
return Collections.emptyList();
}
return gitClient.callGitHubApi(apiUri, uri, RepositoryIssueDto.class);
return gitClient.callGitHubApi(apiUri, uri, RepositoryIssueDto.class, HttpMethod.GET.name());
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/com/ironoc/portfolio/client/GitClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpMethod;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class GitClientTest {
public void test_callGitHubApi_fail() {
// when
Collection<RepositoryDetailDto> result = gitClient
.callGitHubApi(TEST_URL, TEST_URL, RepositoryDetailDto.class);
.callGitHubApi(TEST_URL, TEST_URL, RepositoryDetailDto.class, HttpMethod.GET.name());

// then
assertThat(result, is(emptyIterable()));
Expand Down Expand Up @@ -95,7 +96,7 @@ public void test_createConn_without_token_success() throws IOException {
when(urlUtilsMock.isValidURL(TEST_URL)).thenReturn(true);

// when
HttpsURLConnection result = gitClient.createConn(TEST_URL, TEST_URL);
HttpsURLConnection result = gitClient.createConn(TEST_URL, TEST_URL, HttpMethod.GET.name());

// then
verify(urlUtilsMock).isValidURL(TEST_URL);
Expand All @@ -115,7 +116,7 @@ public void test_createConn_with_token_success() throws IOException {
when(secretManagerMock.getGitSecret()).thenReturn("test_fake_token");

// when
HttpsURLConnection result = gitClient.createConn(TEST_URL, TEST_URL);
HttpsURLConnection result = gitClient.createConn(TEST_URL, TEST_URL, HttpMethod.GET.name());

// then
verify(urlUtilsMock).isValidURL(TEST_URL);
Expand All @@ -134,7 +135,7 @@ public void test_createConn_invalid_url_fail() throws IOException {
when(urlUtilsMock.isValidURL(TEST_URL)).thenReturn(false);

// when
HttpsURLConnection result = gitClient.createConn(TEST_URL, TEST_URL);
HttpsURLConnection result = gitClient.createConn(TEST_URL, TEST_URL, HttpMethod.GET.name());

// then
verify(urlUtilsMock).isValidURL(TEST_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void test_get_repos_success() throws IOException {
when(urlUtilsMock.isValidURL(anyString())).thenReturn(true);
CollectionType listType = objectMapper.getTypeFactory()
.constructCollectionType(ArrayList.class, RepositoryDetailDto.class);
when(gitClientMock.callGitHubApi(anyString(), anyString(), any()))
when(gitClientMock.callGitHubApi(anyString(), anyString(), any(), anyString()))
.thenReturn(objectMapper.readValue(jsonInputStream, listType));

// when
Expand All @@ -96,7 +96,7 @@ public void test_get_repos_success() throws IOException {
// then
verify(propertyConfigMock).getGitApiEndpointRepos();
verify(urlUtilsMock).isValidURL(anyString());
verify(gitClientMock).callGitHubApi(anyString(), anyString(), any());
verify(gitClientMock).callGitHubApi(anyString(), anyString(), any(), anyString());

assertThat(results, is(hasSize(2)));
Optional<RepositoryDetailDto> result = results.stream().findFirst();
Expand Down Expand Up @@ -136,7 +136,7 @@ public void test_get_repos_parseNull_values_success() throws IOException {
when(urlUtilsMock.isValidURL(anyString())).thenReturn(true);
CollectionType listType = objectMapper.getTypeFactory()
.constructCollectionType(ArrayList.class, RepositoryDetailDto.class);
when(gitClientMock.callGitHubApi(anyString(), anyString(), any()))
when(gitClientMock.callGitHubApi(anyString(), anyString(), any(), anyString()))
.thenReturn(objectMapper.readValue(jsonInputStream, listType));

// when
Expand All @@ -145,7 +145,7 @@ public void test_get_repos_parseNull_values_success() throws IOException {
// then
verify(propertyConfigMock).getGitApiEndpointRepos();
verify(urlUtilsMock).isValidURL(anyString());
verify(gitClientMock).callGitHubApi(anyString(), anyString(), any());
verify(gitClientMock).callGitHubApi(anyString(), anyString(), any(), anyString());

assertThat(results, is(hasSize(1)));
Optional<RepositoryDetailDto> result = results.stream().findFirst();
Expand Down

0 comments on commit 55052c5

Please sign in to comment.