Skip to content

Commit

Permalink
TESTING: testing wireMocked communication with github
Browse files Browse the repository at this point in the history
  • Loading branch information
Janek1010 committed Feb 16, 2024
1 parent 670d5d8 commit 69ab8c2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 131 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.example.githubservice.client.api.GithubClient;
import org.example.githubservice.model.dtos.BranchDTO;
import org.example.githubservice.model.dtos.RepositoryDTO;
import org.example.githubservice.model.Branch;
import org.example.githubservice.model.Commit;
import org.example.githubservice.model.Owner;
import org.example.githubservice.model.Repository;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import wiremock.org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;


@SpringBootTest
@WireMockTest // it will take random port
@WireMockTest // it will take random port, it includes also @ExtendWith(WireMockExtension.class)
class GithubWebClientTest {
@Autowired
GithubClient githubClient;
Expand All @@ -32,22 +36,17 @@ class GithubWebClientTest {
void testing_random_port_and_localhost(WireMockRuntimeInfo wmRuntimeInfo) {
int port = wmRuntimeInfo.getHttpPort();
System.out.println("Port: " + port);

System.out.println("Host: "+ wmRuntimeInfo.getHttpBaseUrl());
}

@Test
void should_return_correct_repositories_Flux() throws IOException {
// given
String responseBodyRepositories = IOUtils.resourceToString("/files/correct-response-repositories-from-githubAPI.json", StandardCharsets.UTF_8);
String responseBodyBranches = IOUtils.resourceToString("/files/correct-response-branches-from-githubAPI.json", StandardCharsets.UTF_8);

BranchDTO branchDTO = new BranchDTO("main", new CommitDTO("7155aa7c2d68f7e8ab38abbed9ea22595441b32a"));
RepositoryDTO repositoryTested = new RepositoryDTO("gimmemoji", new OwnerDTO("jotzet"), new LinkedList<>(List.of(branchDTO)));
final String responseBodyRepositories = IOUtils.resourceToString("/files/correct-response-repositories-from-githubAPI.json", StandardCharsets.UTF_8);
Repository repositoryTested = new Repository("gimmemoji", new Owner("jotzet"), false, "https://api.github.com/repos/jotzet/gimmemoji/branches{/branch}", null);
Repository repositoryTested2 = new Repository("pianoroll-frontend-challenge", new Owner("jotzet"), true, "https://api.github.com/repos/jotzet/pianoroll-frontend-challenge/branches{/branch}", null);

String username = "jotzet";

stubFor(get(urlEqualTo("/users/{username}/repos".replace("{username}", username)))
stubFor(get(urlEqualTo("/users/{username}/repos".replace("{username}", repositoryTested.owner().login())))
.willReturn(
aResponse()
.withStatus(200)
Expand All @@ -56,6 +55,30 @@ void should_return_correct_repositories_Flux() throws IOException {
)
);

//when
Flux<Repository> response = githubClient.getAllRepositoriesByUser(repositoryTested.owner().login());

// then
StepVerifier.create(response)
.recordWith(ArrayList::new)
.thenConsumeWhile(repository -> true)
.consumeRecordedWith(repositories -> {
assertThat(repositories.size()).isEqualTo(2);
assertThat(repositories).containsExactlyInAnyOrder(repositoryTested, repositoryTested2);
})
.verifyComplete();
}


@Test
void should_return_correct_branches_Flux() throws IOException {
// given
final String responseBodyBranches = IOUtils.resourceToString("/files/correct-response-branches-from-githubAPI.json", StandardCharsets.UTF_8);
Branch branch = new Branch("main", new Commit("7155aa7c2d68f7e8ab38abbed9ea22595441b32a"));
final String username = "jotzet";
Repository repositoryTested = new Repository("gimmemoji", new Owner("jotzet"), false, "https://api.github.com/repos/jotzet/gimmemoji/branches{/branch}", null);


stubFor(get(urlEqualTo("/repos/{username}/{repository}/branches".replace("{username}", username).replace("{repository}", repositoryTested.name())))
.willReturn(
aResponse()
Expand All @@ -64,16 +87,42 @@ void should_return_correct_repositories_Flux() throws IOException {
.withBody(responseBodyBranches)
)
);
//when
Flux<RepositoryDTO> response = githubClient.getAllRepositoriesByUser("jotzet");

//when
Flux<Branch> response = githubClient.getAllBranches(repositoryTested);
// then
response.doOnNext(repositoryDTO -> {
assertThat(repositoryDTO.name()).isEqualTo(repositoryTested.name());
assertThat(repositoryDTO.owner()).isEqualTo(repositoryTested.owner());
assertThat(repositoryDTO.branches().getFirst().name()).isEqualTo(repositoryTested.branches().getFirst().name());
assertThat(repositoryDTO.branches().getFirst().commit().sha()).isEqualTo(repositoryTested.branches().getFirst().commit().sha());
}).blockLast();
StepVerifier.create(response)
.recordWith(ArrayList::new)
.thenConsumeWhile(branches -> true)
.consumeRecordedWith(branches -> {
assertThat(branches.size()).isEqualTo(1);
assertThat(branches).containsExactlyInAnyOrder(branch);
})
.verifyComplete();
}

@Test
void should_return_not_found_error_when_user_not_found() throws IOException {
// given
final String badUsername = "andoidio12812h90d0aa9s0dah901";
final String responseNotFound = IOUtils.resourceToString("/files/user-not-found-response-from-github-api.json", StandardCharsets.UTF_8);

stubFor(get(urlEqualTo("/users/{username}/repos".replace("{username}", badUsername)))
.willReturn(
aResponse()
.withStatus(404)
.withHeader("Content-Type", "application/json")
.withBody(responseNotFound)
)
);

StepVerifier
.create(githubClient.getAllRepositoriesByUser(badUsername))
.expectErrorMatches(throwable ->
throwable instanceof ResponseStatusException &&
((ResponseStatusException) throwable).getStatusCode().equals(HttpStatus.NOT_FOUND) &&
throwable.getMessage().contains("User not found")
).verify();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/repos/repos#list-repositories-for-a-user"
}

0 comments on commit 69ab8c2

Please sign in to comment.