diff --git a/feign/src/main/java/com/baeldung/core/client/FormClient.java b/feign/src/main/java/com/baeldung/core/client/FormClient.java new file mode 100644 index 0000000000..074402aef3 --- /dev/null +++ b/feign/src/main/java/com/baeldung/core/client/FormClient.java @@ -0,0 +1,37 @@ +package com.baeldung.core.client; + +import java.util.Map; + +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.boot.autoconfigure.http.HttpMessageConverters; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.cloud.openfeign.support.SpringEncoder; +import org.springframework.context.annotation.Bean; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import com.baeldung.core.defaulterrorhandling.model.FormData; + +import feign.codec.Encoder; +import feign.form.spring.SpringFormEncoder; + +@FeignClient(name = "form-client", url = "http://localhost:8085/api", configuration = FormFeignEncoderConfig.class) +public interface FormClient { + + @PostMapping(value = "/form", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + void postFormData(@RequestBody FormData data); + + @PostMapping(value = "/form/map", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + void postFormMapData(Map data); + +} + +class FormFeignEncoderConfig { + + @Bean + public Encoder encoder(ObjectFactory converters) { + return new SpringFormEncoder(new SpringEncoder(converters)); + } + +} \ No newline at end of file diff --git a/feign/src/main/java/com/baeldung/core/defaulterrorhandling/model/FormData.java b/feign/src/main/java/com/baeldung/core/defaulterrorhandling/model/FormData.java new file mode 100644 index 0000000000..6210451f2d --- /dev/null +++ b/feign/src/main/java/com/baeldung/core/defaulterrorhandling/model/FormData.java @@ -0,0 +1,12 @@ +package com.baeldung.core.defaulterrorhandling.model; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class FormData { + + int id; + String name; +} diff --git a/feign/src/test/java/com/baeldung/core/client/FormClientUnitTest.java b/feign/src/test/java/com/baeldung/core/client/FormClientUnitTest.java new file mode 100644 index 0000000000..b9c263b60b --- /dev/null +++ b/feign/src/test/java/com/baeldung/core/client/FormClientUnitTest.java @@ -0,0 +1,78 @@ +package com.baeldung.core.client; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import com.baeldung.core.defaulterrorhandling.model.FormData; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; + +import lombok.extern.slf4j.Slf4j; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +@Slf4j +class FormClientUnitTest { + + private static WireMockServer wireMockServer; + + @Autowired + FormClient formClient; + + @BeforeAll + public static void startWireMockServer() { + wireMockServer = new WireMockServer(8085); + configureFor("localhost", 8085); + wireMockServer.start(); + + } + + @AfterAll + public static void stopWireMockServer() { + wireMockServer.stop(); + } + + @Test + public void givenFormData_whenPostFormDataCalled_thenReturnSuccess() { + FormData formData = new FormData(1, "baeldung"); + stubFor(WireMock.post(urlEqualTo("/api/form")) + .willReturn(aResponse().withStatus(HttpStatus.OK.value()))); + + formClient.postFormData(formData); + wireMockServer.verify(postRequestedFor(urlPathEqualTo("/api/form")) + .withHeader("Content-Type", equalTo("application/x-www-form-urlencoded; charset=UTF-8")) + .withRequestBody(equalTo("name=baeldung&id=1"))); + } + + @Test + public void givenFormMap_whenPostFormMapDataCalled_thenReturnSuccess() { + Map mapData = new HashMap<>(); + mapData.put("name", "baeldung"); + mapData.put("id", "1"); + stubFor(WireMock.post(urlEqualTo("/api/form/map")) + .willReturn(aResponse().withStatus(HttpStatus.OK.value()))); + + formClient.postFormMapData(mapData); + wireMockServer.verify(postRequestedFor(urlPathEqualTo("/api/form/map")) + .withHeader("Content-Type", equalTo("application/x-www-form-urlencoded; charset=UTF-8")) + .withRequestBody(equalTo("name=baeldung&id=1"))); + } + +} \ No newline at end of file