Skip to content

Commit 07710e2

Browse files
committed
finished tests
fully working
1 parent 8a49ad9 commit 07710e2

File tree

9 files changed

+271
-50
lines changed

9 files changed

+271
-50
lines changed

src/main/java/pl/pawelosinski/dynatrace/nbp/task/backend/config/CurrencyServiceConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
public class CurrencyServiceConfig {
1212

1313
@Bean
14-
@Profile("!integration")
14+
@Profile("!test")
1515
public String getApiCoreUrl(){
1616
return "http://api.nbp.pl";
1717
}
1818

1919
@Bean
20-
@Profile("integration")
20+
@Profile("test")
2121
public String testApiCoreUrl(){
2222
return "http://localhost:8123";
2323
}

src/main/java/pl/pawelosinski/dynatrace/nbp/task/backend/controller/CurrencyController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class CurrencyController {
2323
private CurrencyService currencyService;
2424

2525
@GetMapping(value = "/exchanges/{currency}", produces="application/json")
26-
public ResponseEntity<TableA> averageExchangeRate(@PathVariable String currency, @RequestParam(value = "date") String date) {
26+
public ResponseEntity<Optional<TableA>> averageExchangeRate(@PathVariable String currency, @RequestParam(value = "date") String date) {
2727

2828
try {
2929
return ResponseEntity.status(OK).body(currencyService.getRateFromDay(currency, date));
@@ -40,7 +40,7 @@ public ResponseEntity<TableA> averageExchangeRate(@PathVariable String currency,
4040
}
4141
}
4242

43-
@GetMapping(value = "/minmaxrate/{currency}", produces="application/json")
43+
@GetMapping(value = "/extremum/{currency}", produces="application/json")
4444
public ResponseEntity<Optional<MinMaxRate>> averageExchangeRate(@PathVariable String currency, @RequestParam(value = "quotations") int quotations) {
4545
try {
4646
return ResponseEntity.status(OK).body(currencyService.getMinMaxFromQuotations(quotations, currency));

src/main/java/pl/pawelosinski/dynatrace/nbp/task/backend/service/CurrencyService.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
@Service
2020
public class CurrencyService {
2121

22-
2322
private final String apiCoreUrl;
2423

2524
private final RestTemplate restTemplate;
@@ -30,7 +29,7 @@ public CurrencyService(String apiCoreUrl, RestTemplateBuilder builder) {
3029
this.restTemplate = builder.build();
3130
}
3231

33-
public TableA getRateFromDay(String currency, String date) throws Exception {
32+
public Optional<TableA> getRateFromDay(String currency, String date) throws Exception {
3433
TableA tableA;
3534
String url = apiCoreUrl + "/api/exchangerates/rates/a/" + currency + "/" + date + "/?format=json";
3635

@@ -39,7 +38,7 @@ public TableA getRateFromDay(String currency, String date) throws Exception {
3938
throw new Exception("Failed to retrieve data from API");
4039
}
4140

42-
return tableA;
41+
return Optional.of(tableA);
4342

4443
}
4544

@@ -48,21 +47,19 @@ public Optional<MinMaxRate> getMinMaxFromQuotations(int n, String currency) thro
4847
MinMaxRate minMaxRate;
4948
List<RateA> rates;
5049

51-
String url = "http://api.nbp.pl/api/exchangerates/rates/a/" + currency + "/last/" + n;
50+
String url = apiCoreUrl + "/api/exchangerates/rates/a/" + currency + "/last/" + n + "/?format=json";
5251
tableA = restTemplate.getForObject(url, TableA.class);
5352
if (tableA == null) {
5453
throw new Exception("Failed to retrieve data from API");
5554
}
5655
rates = tableA.getRates();
5756
if (!rates.isEmpty()) {
58-
5957
minMaxRate = new MinMaxRate(
60-
rates.stream().min(Comparator.comparing(RateA::getMid)).get(),
61-
rates.stream().max(Comparator.comparing(RateA::getMid)).get()
58+
rates.stream().min(Comparator.comparing(RateA::getMid)).orElse(null),
59+
rates.stream().max(Comparator.comparing(RateA::getMid)).orElse(null)
6260
);
6361
return Optional.of(minMaxRate);
6462
}
65-
6663
return Optional.empty();
6764
}
6865

@@ -71,7 +68,7 @@ public Optional<RateC> getDifferenceFromQuotations(int n, String currency) throw
7168
RateC rate; // rate with major difference
7269
List<RateC> rates;
7370

74-
String url = "http://api.nbp.pl/api/exchangerates/rates/c/" + currency + "/last/" + n;
71+
String url = apiCoreUrl + "/api/exchangerates/rates/c/" + currency + "/last/" + n + "/?format=json";
7572
tableC = restTemplate.getForObject(url, TableC.class);
7673
if (tableC == null) {
7774
throw new Exception("Failed to retrieve data from API");
@@ -81,7 +78,6 @@ public Optional<RateC> getDifferenceFromQuotations(int n, String currency) throw
8178
rate = rates.stream().max(Comparator.comparing(v -> abs(v.getBid() - v.getAsk()))).get();
8279
return Optional.of(rate);
8380
}
84-
8581
return Optional.empty();
8682
}
8783

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
server.port=8080

src/test/java/pl/pawelosinski/dynatrace/nbp/task/backend/ApplicationTests.java

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package pl.pawelosinski.dynatrace.nbp.task.backend.controller;
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import org.junit.jupiter.api.AfterAll;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.TestInstance;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.http.HttpStatus;
13+
import org.springframework.http.ResponseEntity;
14+
import org.springframework.test.context.ActiveProfiles;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
import pl.pawelosinski.dynatrace.nbp.task.backend.model.MinMaxRate;
17+
import pl.pawelosinski.dynatrace.nbp.task.backend.model.rate.RateC;
18+
import pl.pawelosinski.dynatrace.nbp.task.backend.model.table.TableA;
19+
20+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
21+
import static org.junit.Assert.*;
22+
23+
@RunWith(SpringJUnit4ClassRunner.class)
24+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
25+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
26+
@ActiveProfiles("test")
27+
public class CurrencyControllerTests {
28+
29+
@Autowired
30+
private TestRestTemplate testRestTemplate;
31+
32+
private final WireMockServer wireMockServer = new WireMockServer(8123);
33+
34+
@BeforeAll
35+
void startWireMock() {
36+
wireMockServer.start();
37+
}
38+
39+
@AfterAll
40+
void stopWireMock() {
41+
wireMockServer.stop();
42+
}
43+
44+
45+
@Test
46+
public void shouldGetRateFromDay() {
47+
// given
48+
String currency = "gbp";
49+
String date = "2012-01-02";
50+
51+
configureFor("localhost", 8123);
52+
stubFor(get(urlEqualTo("/api/exchangerates/rates/a/gbp/2012-01-02/?format=json"))
53+
.willReturn(aResponse()
54+
.withHeader("Content-Type", "application/json")
55+
.withBodyFile("fromDay.json")));
56+
57+
// when
58+
ResponseEntity<TableA> result = testRestTemplate.getForEntity("/exchanges/{currency}?date={date}", TableA.class, currency, date);
59+
60+
// then
61+
assertEquals(HttpStatus.OK, result.getStatusCode());
62+
assertNotNull(result.getBody());
63+
assertEquals(1, result.getBody().getRates().size());
64+
assertEquals("GBP", result.getBody().getCode());
65+
assertEquals(5.348, result.getBody().getRates().get(0).getMid(), 0.0001);
66+
}
67+
68+
@Test
69+
public void shouldNotGetAnyResultFromDay() {
70+
// given
71+
String currency = "USD";
72+
String date = "2023-04-22";
73+
74+
configureFor("localhost", 8123);
75+
stubFor(get(urlEqualTo("/api/exchangerates/rates/a/USD/2023-04-22/?format=json"))
76+
.willReturn(aResponse().withStatus(404)));
77+
78+
// when
79+
ResponseEntity<TableA> result = testRestTemplate.getForEntity("/exchanges/{currency}?date={date}", TableA.class, currency, date);
80+
81+
// then
82+
assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
83+
assertNull(result.getBody());
84+
}
85+
@Test
86+
public void shouldGetMinAndMaxRate() {
87+
// given
88+
String currency = "gbp";
89+
int quotations = 5;
90+
91+
configureFor("localhost", 8123);
92+
stubFor(get(urlEqualTo("/api/exchangerates/rates/a/gbp/last/5/?format=json"))
93+
.willReturn(aResponse()
94+
.withHeader("Content-Type", "application/json")
95+
.withBodyFile("minMax.json")));
96+
97+
// when
98+
ResponseEntity<MinMaxRate> result = testRestTemplate.getForEntity("/extremum/{currency}?quotations={quotations}", MinMaxRate.class, currency, quotations);
99+
100+
// then
101+
assertEquals(HttpStatus.OK, result.getStatusCode());
102+
assertNotNull(result.getBody());
103+
assertEquals(5.4321, result.getBody().getMax().getMid(), 0.0001);
104+
assertEquals(1.2345, result.getBody().getMin().getMid(), 0.0001);
105+
}
106+
107+
@Test
108+
public void shouldNotGetAnyResultMinMaxWithWrongParam() {
109+
// given
110+
String currency = "gbp";
111+
int quotations = 305;
112+
113+
configureFor("localhost", 8123);
114+
stubFor(get(urlEqualTo("/api/exchangerates/rates/a/gbp/last/305/?format=json"))
115+
.willReturn(aResponse().withStatus(400)));
116+
117+
// when
118+
ResponseEntity<MinMaxRate> result = testRestTemplate.getForEntity("/extremum/{currency}?quotations={quotations}", MinMaxRate.class, currency, quotations);
119+
120+
121+
// then
122+
assertEquals(HttpStatus.BAD_REQUEST, result.getStatusCode());
123+
assertNull(result.getBody());
124+
}
125+
126+
127+
@Test
128+
public void shouldGetRateWithMajorDifference() {
129+
// given
130+
String currency = "gbp";
131+
int quotations = 5;
132+
133+
configureFor("localhost", 8123);
134+
stubFor(get(urlEqualTo("/api/exchangerates/rates/c/gbp/last/5/?format=json"))
135+
.willReturn(aResponse()
136+
.withHeader("Content-Type", "application/json")
137+
.withBodyFile("majorDifference.json")));
138+
139+
// when
140+
ResponseEntity<RateC> result = testRestTemplate.getForEntity("/majordifference/{currency}?quotations={quotations}", RateC.class, currency, quotations);
141+
142+
// then
143+
assertEquals(HttpStatus.OK, result.getStatusCode());
144+
assertNotNull(result.getBody());
145+
assertEquals("074/C/NBP/2023", result.getBody().getNo());
146+
assertEquals(5.4321, result.getBody().getAsk(), 0.0001);
147+
assertEquals(1.2345, result.getBody().getBid(), 0.0001);
148+
}
149+
150+
@Test
151+
public void shouldNotGetMajorDifferenceWithWrongParam() {
152+
// given
153+
String currency = "abc";
154+
int quotations = 123;
155+
156+
configureFor("localhost", 8123);
157+
stubFor(get(urlEqualTo("/api/exchangerates/rates/a/abc/last/123/?format=json"))
158+
.willReturn(aResponse().withStatus(404)));
159+
160+
// when
161+
ResponseEntity<RateC> result = testRestTemplate.getForEntity("/majordifference/{currency}?quotations={quotations}", RateC.class, currency, quotations);
162+
163+
// then
164+
assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
165+
assertNull(result.getBody());
166+
}
167+
168+
}

0 commit comments

Comments
 (0)