Skip to content

Commit

Permalink
Fix value loading at instance creation time
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkopp committed Jan 6, 2024
1 parent e253e4c commit 9b70214
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package de.unistuttgart.t2.modulith.config;


import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class OpenAPIConfiguration {

@Value("${info.app.version:unknown}")
private String version;

@Bean
public OpenAPI customOpenAPI() {
public OpenAPI customOpenAPI(@Value("${info.app.version:unknown}") String version) {
return new OpenAPI().components(new Components()).info(new Info()
.title("T2 Modulith API")
.description("API of the T2-Project's modulith implementation.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.resilience4j.retry.RetryRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
Expand All @@ -23,31 +24,31 @@
@Service
public class PaymentService {

@Value("${t2.payment.provider.dummy.url}")
protected String providerUrl;

@Value("${t2.payment.provider.timeout:5}")
public int timeout;

@Value("${t2.payment.provider.enabled:true}")
protected boolean enabled;

private final boolean enabled;
private final String providerUrl;
private final RestTemplate template;

private final Logger LOG = LoggerFactory.getLogger(getClass());

// retry stuff
RetryConfig config = RetryConfig.custom().maxAttempts(2).build();
RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("paymentRetry");

public PaymentService() {
@Autowired
public PaymentService(@Value("${t2.payment.provider.enabled:true}") boolean enabled,
@Value("${t2.payment.provider.dummy.url}") String providerUrl,
@Value("${t2.payment.provider.timeout:5}") int timeout) {
this.enabled = enabled;
this.providerUrl = providerUrl;

RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
this.template = restTemplateBuilder.setConnectTimeout(Duration.ofSeconds(timeout))
.setReadTimeout(Duration.ofSeconds(timeout)).build();
}

public PaymentService(RestTemplate restTemplate) {
public PaymentService(String providerUrl, RestTemplate restTemplate) {
this.enabled = true;
this.providerUrl = providerUrl;
this.template = restTemplate;
}

Expand All @@ -56,7 +57,7 @@ public PaymentService(RestTemplate restTemplate) {
* fail, or it is successful.
*/
public void doPayment(String cardNumber, String cardOwner, String checksum, double total) throws PaymentFailedException {
if(!enabled) {
if (!enabled) {
LOG.warn("Connecting to payment provider is disabled by configuration for testing purposes! " +
"Returning as payment was successful.");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;

import static de.unistuttgart.t2.modulith.payment.TestContext.testUrl;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
Expand All @@ -34,12 +35,9 @@ public class PaymentRequestTests {

private MockRestServiceServer mockServer;

private final String testUrl = "http://foo.bar/pay";

@BeforeEach
public void setUp() {
mockServer = MockRestServiceServer.createServer(template);
service.providerUrl = testUrl;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
@Configuration
public class TestContext {

public int timeout = 5;
public static final int timeout = 5;

public static final String testUrl = "http://foo.bar/pay";

@Bean
public RestTemplate template() {
Expand All @@ -22,6 +24,6 @@ public RestTemplate template() {

@Bean
public PaymentService service(@Autowired RestTemplate restTemplate) {
return new PaymentService(restTemplate);
return new PaymentService(testUrl, restTemplate);
}
}

0 comments on commit 9b70214

Please sign in to comment.