-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TESTING: added correct responses in files and testing githubService c…
…orrect Flux
- Loading branch information
Showing
7 changed files
with
407 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 96 additions & 37 deletions
133
src/test/java/org/example/githubservice/controller/GithubControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,109 @@ | ||
package org.example.githubservice.controller; | ||
|
||
import com.github.tomakehurst.wiremock.junit5.WireMockTest; | ||
import org.example.githubservice.model.dtos.RepositoryDTO; | ||
import org.example.githubservice.service.api.GithubService; | ||
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.autoconfigure.web.reactive.AutoConfigureWebTestClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
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 | ||
@AutoConfigureWebTestClient | ||
@WireMockTest(httpPort = 8081) | ||
@ExtendWith(SpringExtension.class) | ||
public class GithubControllerIT { | ||
|
||
@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(); | ||
GithubService githubService; | ||
@DynamicPropertySource | ||
static void configure(DynamicPropertyRegistry registry) { | ||
registry.add("github.api.base-url", () -> "http://localhost:8081"); | ||
} | ||
|
||
@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(); | ||
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<RepositoryDTO> response = githubService.listAllRepositoriesOfUser("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(); | ||
// } | ||
|
80 changes: 80 additions & 0 deletions
80
src/test/java/org/example/githubservice/service/impl/GithubServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.example.githubservice.service.impl; | ||
|
||
import com.github.tomakehurst.wiremock.junit5.WireMockTest; | ||
import org.example.githubservice.model.dtos.BranchDTO; | ||
import org.example.githubservice.model.dtos.CommitDTO; | ||
import org.example.githubservice.model.dtos.OwnerDTO; | ||
import org.example.githubservice.model.dtos.RepositoryDTO; | ||
import org.example.githubservice.service.api.GithubService; | ||
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 java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest | ||
@WireMockTest(httpPort = 8081) | ||
@ExtendWith(SpringExtension.class) | ||
class GithubServiceImplTest { | ||
@Autowired | ||
GithubService githubService; | ||
@DynamicPropertySource | ||
static void configure(DynamicPropertyRegistry registry) { | ||
registry.add("github.api.base-url", () -> "http://localhost:8081"); | ||
} | ||
|
||
@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))); | ||
|
||
String username = "jotzet"; | ||
|
||
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}", repositoryTested.name()))) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(200) | ||
.withHeader("Content-Type", "application/json") | ||
.withBody(responseBodyBranches) | ||
) | ||
); | ||
//when | ||
Flux<RepositoryDTO> response = githubService.listAllRepositoriesOfUser("jotzet"); | ||
|
||
// 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(); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/test/resources/files/correct-response-branches-from-githubAPI.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"name": "main", | ||
"commit": { | ||
"sha": "7155aa7c2d68f7e8ab38abbed9ea22595441b32a", | ||
"url": "https://api.github.com/repos/jotzet/gimmemoji/commits/7155aa7c2d68f7e8ab38abbed9ea22595441b32a" | ||
}, | ||
"protected": false | ||
} | ||
] |
Empty file.
Oops, something went wrong.