Skip to content

Commit cc973c1

Browse files
committed
first working tests
right before rebuilding models
1 parent 7490ce9 commit cc973c1

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pl.pawelosinski.dynatrace.nbp.task.backend.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Profile;
7+
import pl.pawelosinski.dynatrace.nbp.task.backend.service.CurrencyService;
8+
9+
@Configuration
10+
@ComponentScan(basePackageClasses = CurrencyService.class)
11+
public class CurrencyServiceConfig {
12+
13+
@Bean
14+
@Profile("!integration")
15+
public String getApiCoreUrl(){
16+
return "http://api.nbp.pl";
17+
}
18+
19+
@Bean
20+
@Profile("integration")
21+
public String testApiCoreUrl(){
22+
return "http://localhost:8123";
23+
}
24+
25+
26+
27+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717
@Service
1818
public class CurrencyService {
1919

20+
21+
private final String apiCoreUrl;
22+
2023
private final RestTemplate restTemplate;
2124

2225
@Autowired
23-
public CurrencyService(RestTemplateBuilder builder) {
26+
public CurrencyService(String apiCoreUrl, RestTemplateBuilder builder) {
27+
this.apiCoreUrl = apiCoreUrl;
2428
this.restTemplate = builder.build();
2529
}
2630

2731
public CurrencyRateTable getRateFromDay(String currency, String date) throws Exception {
2832
CurrencyRateTable currencyRateTable;
29-
String url = "http://api.nbp.pl/api/exchangerates/rates/a/" + currency + "/" + date + "/?format=json";
33+
String url = apiCoreUrl + "/api/exchangerates/rates/a/" + currency + "/" + date + "/?format=json";
3034
System.out.println(url);
3135

3236
currencyRateTable = restTemplate.getForObject(url, CurrencyRateTable.class);

src/test/java/pl/pawelosinski/dynatrace/nbp/task/backend/service/CurrencyServiceTests.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package pl.pawelosinski.dynatrace.nbp.task.backend.service;
22

33
import com.github.tomakehurst.wiremock.WireMockServer;
4-
import com.github.tomakehurst.wiremock.junit.WireMockRule;
5-
import org.junit.Rule;
64
import org.junit.jupiter.api.*;
75
import org.junit.runner.RunWith;
86
import org.springframework.beans.factory.annotation.Autowired;
97
import org.springframework.boot.test.context.SpringBootTest;
108
import org.springframework.boot.test.web.client.TestRestTemplate;
9+
import org.springframework.test.context.ActiveProfiles;
1110
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12-
import org.springframework.test.context.web.WebAppConfiguration;
13-
import org.springframework.test.web.servlet.MockMvc;
14-
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1511
import org.springframework.web.client.HttpClientErrorException;
16-
import org.springframework.web.context.WebApplicationContext;
1712
import pl.pawelosinski.dynatrace.nbp.task.backend.model.CurrencyRateTable;
1813

1914
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@@ -23,6 +18,7 @@
2318
@RunWith(SpringJUnit4ClassRunner.class)
2419
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2520
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
21+
@ActiveProfiles("integration")
2622
public class CurrencyServiceTests {
2723

2824
@Autowired
@@ -31,7 +27,7 @@ public class CurrencyServiceTests {
3127
@Autowired
3228
private TestRestTemplate restTemplate;
3329

34-
private final WireMockServer wireMockServer = new WireMockServer(8080);
30+
private final WireMockServer wireMockServer = new WireMockServer(8123);
3531

3632
@BeforeAll
3733
void startWireMock() {
@@ -56,9 +52,9 @@ public void shouldGetRateFromDay() throws Exception {
5652
String currency = "gbp";
5753
String date = "2012-01-02";
5854

59-
configureFor("localhost", 8080);
55+
configureFor("localhost", 8123);
6056

61-
stubFor(get(urlPathEqualTo("http://api.nbp.pl/api/exchangerates/rates/a/gbp/2012-01-02/?format=json"))
57+
stubFor(get(urlEqualTo("/api/exchangerates/rates/a/gbp/2012-01-02/?format=json"))
6258
.willReturn(aResponse()
6359
.withHeader("Content-Type", "application/json")
6460
.withBodyFile("fromDay.json")));
@@ -79,7 +75,7 @@ public void shouldNotGetAnyResultFromDay() {
7975
// Given
8076
String currency = "USD";
8177
String date = "2023-04-22";
82-
stubFor(get(urlPathEqualTo("http://api.nbp.pl/api/exchangerates/rates/a/USD/2023-04-22/?format=json"))
78+
stubFor(get(urlEqualTo("/api/exchangerates/rates/a/USD/2023-04-22/?format=json"))
8379
.willReturn(aResponse().withStatus(404)));
8480

8581

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"table":"A","currency":"funt szterling","code":"GBP","rates":[{"no":"1/A/NBP/2012","effectiveDate":"2012-01-02","mid":5.348,"bid":0.0,"ask":0.0}]}

src/test/resources/fromDay.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)