Skip to content

Commit

Permalink
TESTING: Integration Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Janek1010 committed Feb 16, 2024
1 parent 97199bd commit e46a5c2
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.githubservice.model.dtos;

public record BranchDTO (
public record BranchDTO(
String name,
String sha
){}
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import org.springframework.http.HttpStatusCode;

public record UserNotFoundErrorResponseDTO (
public record UserNotFoundErrorResponseDTO(
HttpStatusCode status,
String message
) {}
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.example.githubservice.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.example.githubservice.model.Owner;
import org.example.githubservice.model.Repository;
import org.junit.jupiter.api.Test;
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 wiremock.org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
@WireMockTest // it will take random port, it includes also @ExtendWith(WireMockExtension.class)
@AutoConfigureWebTestClient
class GithubControllerTestIT {

@Autowired
WebTestClient webTestClient;

@Test
void shouldReturnCorrectResponseWithRepositoriesAndBranches() throws IOException {
// given
final String responseBodyRepositories = IOUtils.resourceToString("/files/correct-response-repositories-from-githubAPI.json", StandardCharsets.UTF_8);
final String responseBodyBranches = IOUtils.resourceToString("/files/correct-response-branches-from-githubAPI.json", StandardCharsets.UTF_8);
String expectedResponseBody = IOUtils.resourceToString("/files/correct-response-full-from-application.json", StandardCharsets.UTF_8);
final String username = "jotzet";
Repository repositoryTested = new Repository("gimmemoji", new Owner(username), false, "https://api.github.com/repos/jotzet/gimmemoji/branches{/branch}", null);

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

ObjectMapper mapper = new ObjectMapper();

webTestClient.get()
.uri("/api/v1/users/{username}/repos", username)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody(String.class)
.value(responseBody -> {
try {
JsonNode expectedNode = mapper.readTree(expectedResponseBody);
JsonNode actualNode = mapper.readTree(responseBody);

assertEquals(expectedNode, actualNode);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}

@Test
void shouldReturnNotFoundResponse() throws IOException {
// given
final String userNotFoundResponseGithHub = IOUtils.resourceToString("/files/user-not-found-response-from-github-api.json", StandardCharsets.UTF_8);
final String expectedUserNotFoundBody = IOUtils.resourceToString("/files/user-not-found-from-application.json", StandardCharsets.UTF_8);
final String badUsername = "20981x0n321x9012x029190x2";

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

ObjectMapper mapper = new ObjectMapper();

webTestClient.get()
.uri("/api/v1/users/{username}/repos", badUsername)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isNotFound()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody(String.class)
.value(responseBody -> {
try {
JsonNode expectedNode = mapper.readTree(expectedUserNotFoundBody);
JsonNode actualNode = mapper.readTree(responseBody);

assertEquals(expectedNode, actualNode);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "gimmemoji",
"ownerLogin": "jotzet",
"branches": [
{
"name": "main",
"sha": "7155aa7c2d68f7e8ab38abbed9ea22595441b32a"
}
]
}
]
4 changes: 4 additions & 0 deletions src/test/resources/files/user-not-found-from-application.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "NOT_FOUND",
"message": "User not found"
}

0 comments on commit e46a5c2

Please sign in to comment.