Skip to content

Commit

Permalink
TESTING: added correct responses in files and testing githubService c…
Browse files Browse the repository at this point in the history
…orrect Flux
  • Loading branch information
Janek1010 committed Feb 15, 2024
1 parent f7ab927 commit 699f98c
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 47 deletions.
27 changes: 18 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<description>githubService</description>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -42,22 +43,30 @@
<version>4.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Service
@Primary
public class GithubServiceImpl implements GithubService {
private final String REPOS_OF_USER = "/users/{username}/repos";
public static final String REPOS_OF_USER = "/users/{username}/repos";
private final WebClient webClient;

public GithubServiceImpl(WebClient.Builder webClientBuilder, @Value("${github.api.base-url}") String rootUrl) {
Expand Down
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();
// }

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();
}

}
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.
Loading

0 comments on commit 699f98c

Please sign in to comment.