这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions testing-modules/mockserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>${spring.webflux.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
Expand All @@ -35,11 +56,33 @@
<artifactId>httpcore</artifactId>
<version>${apche-http-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<mock-sever-netty-version>3.10.8</mock-sever-netty-version>
<apche-http-version>4.4.1</apche-http-version>
<spring-boot.version>3.3.2</spring-boot.version>
<spring.webflux.version>6.1.13</spring.webflux.version>
<jackson.version>2.18.0</jackson.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.mock.server.multiplerequests;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class Config {

@Bean
public WebClient webClient() {
return WebClient.create();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.baeldung.mock.server.multiplerequests;

import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

@RestController
@RequestMapping("/api/")
public class PaymentController {

private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);
private final WebClient webClient;

public PaymentController(WebClient webClient) {
this.webClient = webClient;
}

@PostMapping("payment/process")
public ResponseEntity<PaymentGatewayResponse> submitPayment(@RequestBody PaymentGatewayRequest paymentGatewayRequest) throws JSONException {

String paymentSubmissionResponse = webClient.post()
.uri("http://localhost:9090/payment/submit")
.body(BodyInserters.fromValue(paymentGatewayRequest))
.retrieve()
.bodyToMono(String.class)
.block();

UUID paymentId = UUID.fromString(new JSONObject(paymentSubmissionResponse).getString("paymentId"));
PaymentGatewayResponse.PaymentStatus paymentStatus = PaymentGatewayResponse.PaymentStatus.PENDING;
while (paymentStatus.equals(PaymentGatewayResponse.PaymentStatus.PENDING)) {
String paymentStatusResponse = webClient.get()
.uri("http://localhost:9090/payment/status/%s".formatted(paymentId))
.retrieve()
.bodyToMono(String.class)
.block();
paymentStatus = PaymentGatewayResponse.PaymentStatus.valueOf(new JSONObject(paymentStatusResponse).getString("paymentStatus"));
logger.info("Payment Status {}", paymentStatus);
}
return new ResponseEntity<>(new PaymentGatewayResponse(paymentId, paymentStatus), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.mock.server.multiplerequests;

public record PaymentGatewayRequest(String cardNumber, String expiryMonth, String expiryYear, String currency, int amount, String cvv) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.mock.server.multiplerequests;

import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record PaymentGatewayResponse(UUID id, PaymentStatus status) {

public enum PaymentStatus {
PENDING,
AUTHORIZED,
DECLINED,
REJECTED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.mock.server.multiplerequests;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringApplication {

public static void main(String[] args) {
org.springframework.boot.SpringApplication.run(SpringApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.baeldung.mock.server.multiplerequests;

import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import java.util.UUID;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.matchers.Times;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest
@AutoConfigureMockMvc
class PaymentControllerTest {

@Autowired
private WebTestClient webTestClient;

private ClientAndServer clientAndServer;

private final MockServerClient mockServerClient = new MockServerClient("localhost", 9090);

@BeforeEach
void setup() {
clientAndServer = startClientAndServer(9090);
}

@Test
void givenPaymentRequest_whenPaymentStatusChangesAfterFewSeconds_thenPaymentShouldBeProcessedSuccessfully() {

String paymentId = UUID.randomUUID()
.toString();
// When
mockServerClient.when(request().withMethod("POST")
.withPath("/payment/submit"))
.respond(response().withStatusCode(200)
.withBody("{\"paymentId\": \"%s\"}".formatted(paymentId))
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));

mockServerClient.when(request().withMethod("GET")
.withPath("/payment/status/%s".formatted(paymentId)), Times.exactly(4))
.respond(response().withStatusCode(200)
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withBody("{\"paymentStatus\": \"%s\"}".formatted(PaymentGatewayResponse.PaymentStatus.PENDING.toString())));

mockServerClient.when(request().withMethod("GET")
.withPath("/payment/status/%s".formatted(paymentId)))
.respond(response().withStatusCode(200)
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withBody("{\"paymentStatus\": \"%s\"}".formatted(PaymentGatewayResponse.PaymentStatus.AUTHORIZED.toString())));

webTestClient.post()
.uri("http://localhost:9000/api/payment/process")
.bodyValue(new PaymentGatewayRequest("4111111111111111", "12", "2025", "USD", 10000, "123"))
.exchange()
.expectStatus()
.isOk()
.expectBody(PaymentGatewayResponse.class)
.value(response -> {
Assertions.assertNotNull(response);
Assertions.assertEquals(PaymentGatewayResponse.PaymentStatus.AUTHORIZED, response.status());
});
}

@AfterEach
void tearDown() {
clientAndServer.stop();
}

}