Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 718b7aa

Browse files
dev/codeforces/test Написал тесты для CodeforcesClient
1 parent f4d696a commit 718b7aa

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.cf.cfteam.services.client;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4+
import lombok.SneakyThrows;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.test.context.DynamicPropertyRegistry;
10+
import org.springframework.test.context.DynamicPropertySource;
11+
import org.springframework.web.client.HttpClientErrorException;
12+
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
16+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
20+
21+
@SpringBootTest
22+
@WireMockTest(httpPort = 8080)
23+
class CodeforcesClientTest {
24+
25+
@Autowired
26+
private CodeforcesClient codeforcesClient;
27+
28+
private static final String MOCK_JSON_PATH = "src/test/resources/codeforces/responses/";
29+
30+
31+
@DynamicPropertySource
32+
static void setProperties(DynamicPropertyRegistry registry) {
33+
registry.add("codeforces.api.base.url", () -> "http://localhost:8080/api");
34+
}
35+
36+
@Test
37+
@SneakyThrows
38+
void fetchRatingFromApi_ShouldReturnRating() {
39+
String responseBody = Files.readString(Paths.get(MOCK_JSON_PATH + "tourist.json"));
40+
41+
stubFor(get(urlPathEqualTo("/api/user.info"))
42+
.withQueryParam("handles", equalTo("tourist"))
43+
.withQueryParam("checkHistoricHandles", equalTo("false"))
44+
.willReturn(aResponse()
45+
.withStatus(200)
46+
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
47+
.withBody(responseBody)));
48+
49+
Double rating = codeforcesClient.fetchRatingFromApi("tourist");
50+
51+
assertThat(rating).isEqualTo(4009);
52+
}
53+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"status": "OK",
3+
"result": [
4+
{
5+
"handle": "tourist",
6+
"rating": 4009
7+
}
8+
]
9+
}

0 commit comments

Comments
 (0)