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+ }
0 commit comments