diff --git a/src/test/java/org/example/githubservice/controller/GithubControllerIT.java b/src/test/java/org/example/githubservice/controller/GithubControllerIT.java deleted file mode 100644 index f2a6094..0000000 --- a/src/test/java/org/example/githubservice/controller/GithubControllerIT.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.example.githubservice.controller; - -import com.github.tomakehurst.wiremock.junit5.WireMockTest; -import org.example.githubservice.client.api.GithubClient; -import org.example.githubservice.model.dtos.RepositoryDTO; -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.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import reactor.core.publisher.Flux; -import wiremock.org.apache.commons.io.IOUtils; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; - -import static com.github.tomakehurst.wiremock.client.WireMock.*; - - -@SpringBootTest -@WireMockTest(httpPort = 8081) -@ExtendWith(SpringExtension.class) -public class GithubControllerIT { - @Autowired - GithubClient githubClient; - @DynamicPropertySource - static void configure(DynamicPropertyRegistry registry) { - registry.add("github.api.base-url", () -> "http://localhost:8081"); - } - - @Test - void should_return_correct_repositories() 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); - String username = "jotzet"; - String repositoryName = "gimmemoji"; - - stubFor(get(urlEqualTo("/users/{username}/repos".replace("{username}", username))) - .willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody(responseBodyRepositories) - ) - ); - - stubFor(get(urlEqualTo("/repos/{username}/{repository}/branches".replace("{username}", username).replace("{repository}", repositoryName))) - .willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody(responseBodyBranches) - ) - ); - //when - Flux response = githubClient.getAllRepositoriesByUser("jotzet"); - - // then - response.doOnNext(repositoryDTO -> { - System.out.println(repositoryDTO.name()); - System.out.println(repositoryDTO.owner().login()); - System.out.println(repositoryDTO.branches()); - }).blockLast(); - } - -} - - -// @Autowired -// WebTestClient webTestClient; -// -// @Test -// void testListAllRepositoriesOfUser() { -// // Given -// String username = "Kondziow"; -// int page = 1; -// int perPage = 2; -// -// // When/Then -// webTestClient.get() -// .uri("/api/v1/users/{username}/repos?page={page}&perPage={perPage}", username, page, perPage) -// .accept(MediaType.APPLICATION_JSON) -// .exchange() -// .expectStatus() -// .isOk() -// .expectBody() -// .jsonPath("$[0].name").exists() -// .jsonPath("$[1].name").exists(); -// } -// -// @Test -// void testUserNotFound() { -// // Given -// String username = "12i9b12dsblasdklaasd"; -// -// // When/Then -// webTestClient.get() -// .uri("/api/v1/users/{username}/repos", username) -// .accept(MediaType.APPLICATION_JSON) -// .exchange() -// .expectStatus().isNotFound() -// .expectBody() -// .jsonPath("$.status").exists() -// .jsonPath("$.message").exists(); -// } - diff --git a/src/test/java/org/example/githubservice/service/impl/GithubWebClientTest.java b/src/test/java/org/example/githubservice/service/impl/GithubWebClientTest.java index c2df17a..d8a670e 100644 --- a/src/test/java/org/example/githubservice/service/impl/GithubWebClientTest.java +++ b/src/test/java/org/example/githubservice/service/impl/GithubWebClientTest.java @@ -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; @@ -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) @@ -56,6 +55,30 @@ void should_return_correct_repositories_Flux() throws IOException { ) ); + //when + Flux 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() @@ -64,16 +87,42 @@ void should_return_correct_repositories_Flux() throws IOException { .withBody(responseBodyBranches) ) ); - //when - Flux response = githubClient.getAllRepositoriesByUser("jotzet"); + //when + Flux 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(); + + } } \ No newline at end of file diff --git a/src/test/resources/files/user-not-found-response-from-github-api.json b/src/test/resources/files/user-not-found-response-from-github-api.json new file mode 100644 index 0000000..1890327 --- /dev/null +++ b/src/test/resources/files/user-not-found-response-from-github-api.json @@ -0,0 +1,4 @@ +{ + "message": "Not Found", + "documentation_url": "https://docs.github.com/rest/repos/repos#list-repositories-for-a-user" +} \ No newline at end of file