From 8f2cc51af441cb5700f34610aba8d57225cf80f7 Mon Sep 17 00:00:00 2001 From: Bhaskara Date: Mon, 28 Mar 2022 20:45:16 +0530 Subject: [PATCH] BAEL-5298 (#11965) * BAEL-5298 * Renamed the tests * Changed the Parent to parent-boot-2 Co-authored-by: Bhaskara Navuluri --- feign/pom.xml | 96 ++++++++++++++++-- .../feign/soap/FeignSoapApplication.java | 14 +++ .../com/baeldung/feign/soap/SoapClient.java | 14 +++ .../baeldung/feign/soap/UsersEndpoint.java | 36 +++++++ .../feign/soap/WebServicesConfiguration.java | 52 ++++++++++ .../src/main/resources/application.properties | 7 ++ feign/src/main/resources/users.xsd | 44 +++++++++ .../feign/clients/BookClientLiveTest.java | 35 +++---- .../retry/Custom5xxErrorDecoderUnitTest.java | 21 ++-- .../baeldung/feign/soap/FeignLiveTest.java | 99 +++++++++++++++++++ 10 files changed, 376 insertions(+), 42 deletions(-) create mode 100644 feign/src/main/java/com/baeldung/feign/soap/FeignSoapApplication.java create mode 100644 feign/src/main/java/com/baeldung/feign/soap/SoapClient.java create mode 100644 feign/src/main/java/com/baeldung/feign/soap/UsersEndpoint.java create mode 100644 feign/src/main/java/com/baeldung/feign/soap/WebServicesConfiguration.java create mode 100644 feign/src/main/resources/application.properties create mode 100644 feign/src/main/resources/users.xsd create mode 100644 feign/src/test/java/com/baeldung/feign/soap/FeignLiveTest.java diff --git a/feign/pom.xml b/feign/pom.xml index cf28890868..026afdfc7a 100644 --- a/feign/pom.xml +++ b/feign/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 com.baeldung.feign feign @@ -9,10 +7,17 @@ com.baeldung - parent-modules - 1.0.0-SNAPSHOT + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + 1.8 + 11.8 + 1.6.3 + + io.github.openfeign @@ -35,10 +40,85 @@ ${lombok.version} provided + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-web-services + + + wsdl4j + wsdl4j + ${wsdl4j.version} + + + io.github.openfeign + feign-hc5 + ${feign.version} + + + io.github.openfeign + feign-soap + ${feign.version} + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.codehaus.mojo + jaxb2-maven-plugin + 2.5.0 + + + xjc + + xjc + + + + + com.baeldung.feign.soap + + src/main/resources/users.xsd + - - 10.11 - + + + + org.jvnet.jaxb2.maven2 + maven-jaxb2-plugin + 0.14.0 + + + feign-soap-stub-generation + + generate + + + target/generated-sources/jaxb + + *.xsd + + + com.baeldung.feign.soap + target/generated-sources/jaxb + + + + + + + \ No newline at end of file diff --git a/feign/src/main/java/com/baeldung/feign/soap/FeignSoapApplication.java b/feign/src/main/java/com/baeldung/feign/soap/FeignSoapApplication.java new file mode 100644 index 0000000000..e10510bc2d --- /dev/null +++ b/feign/src/main/java/com/baeldung/feign/soap/FeignSoapApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.feign.soap; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication + +public class FeignSoapApplication { + + public static void main(String[] args) { + SpringApplication.run(FeignSoapApplication.class, args); + } + +} diff --git a/feign/src/main/java/com/baeldung/feign/soap/SoapClient.java b/feign/src/main/java/com/baeldung/feign/soap/SoapClient.java new file mode 100644 index 0000000000..b0dbb617ba --- /dev/null +++ b/feign/src/main/java/com/baeldung/feign/soap/SoapClient.java @@ -0,0 +1,14 @@ +package com.baeldung.feign.soap; + +import feign.Headers; +import feign.RequestLine; + +public interface SoapClient { + @RequestLine("POST") + @Headers({"SOAPAction: createUser", "Content-Type: text/xml;charset=UTF-8", "Accept: text/xml"}) + String createUserWithPlainText(String soapBody); + + @RequestLine("POST") + @Headers({"Content-Type: text/xml;charset=UTF-8"}) + CreateUserResponse createUserWithSoap(CreateUserRequest soapBody); +} diff --git a/feign/src/main/java/com/baeldung/feign/soap/UsersEndpoint.java b/feign/src/main/java/com/baeldung/feign/soap/UsersEndpoint.java new file mode 100644 index 0000000000..c39a4a265c --- /dev/null +++ b/feign/src/main/java/com/baeldung/feign/soap/UsersEndpoint.java @@ -0,0 +1,36 @@ +package com.baeldung.feign.soap; + +import org.springframework.ws.server.endpoint.annotation.Endpoint; +import org.springframework.ws.server.endpoint.annotation.PayloadRoot; +import org.springframework.ws.server.endpoint.annotation.RequestPayload; +import org.springframework.ws.server.endpoint.annotation.ResponsePayload; + +import java.util.HashMap; +import java.util.Map; + +@Endpoint +public class UsersEndpoint { + + private static final Map userMap = new HashMap<>(); + + @PayloadRoot(namespace = "http://www.baeldung.com/springbootsoap/feignclient", localPart = "getUserRequest") + @ResponsePayload + public GetUserResponse getUser(@RequestPayload GetUserRequest request) { + GetUserResponse response = new GetUserResponse(); + response.setUser(userMap.get(request.getId())); + return response; + } + + @PayloadRoot(namespace = "http://www.baeldung.com/springbootsoap/feignclient", localPart = "createUserRequest") + @ResponsePayload + public CreateUserResponse createUser(@RequestPayload CreateUserRequest request) { + CreateUserResponse response = new CreateUserResponse(); + if (request.getUser().getId().equalsIgnoreCase("500")) + throw new RuntimeException("This is a reserved user id"); + userMap.put(request.getUser().id, request.getUser()); + + response.setMessage("Success! Created the user with id - " + request.getUser().getId()); + return response; + } + +} diff --git a/feign/src/main/java/com/baeldung/feign/soap/WebServicesConfiguration.java b/feign/src/main/java/com/baeldung/feign/soap/WebServicesConfiguration.java new file mode 100644 index 0000000000..76ebfb392e --- /dev/null +++ b/feign/src/main/java/com/baeldung/feign/soap/WebServicesConfiguration.java @@ -0,0 +1,52 @@ +package com.baeldung.feign.soap; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.ws.config.annotation.EnableWs; +import org.springframework.ws.config.annotation.WsConfigurerAdapter; +import org.springframework.ws.transport.http.MessageDispatcherServlet; +import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition; +import org.springframework.xml.xsd.SimpleXsdSchema; +import org.springframework.xml.xsd.XsdSchema; + +@EnableWs +@Configuration +public class WebServicesConfiguration extends WsConfigurerAdapter { + + @Value("${ws.api.path:/ws/api/v1/*}") + private String webserviceApiPath; + @Value("${ws.port.type.name:UsersPort}") + private String webservicePortTypeName; + @Value("${ws.target.namespace:http://www.baeldung.com/springbootsoap/feignclient}") + private String webserviceTargetNamespace; + @Value("${ws.location.uri:http://localhost:18080/ws/api/v1/}") + private String locationUri; + + @Bean + public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { + MessageDispatcherServlet servlet = new MessageDispatcherServlet(); + servlet.setApplicationContext(applicationContext); + servlet.setTransformWsdlLocations(true); + return new ServletRegistrationBean<>(servlet, webserviceApiPath); + } + + @Bean(name = "users") + public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema usersSchema) { + DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); + wsdl11Definition.setPortTypeName(webservicePortTypeName); + wsdl11Definition.setTargetNamespace(webserviceTargetNamespace); + wsdl11Definition.setLocationUri(locationUri); + wsdl11Definition.setSchema(usersSchema); + return wsdl11Definition; + } + + @Bean + public XsdSchema userSchema() { + return new SimpleXsdSchema(new ClassPathResource("users.xsd")); + } + +} diff --git a/feign/src/main/resources/application.properties b/feign/src/main/resources/application.properties new file mode 100644 index 0000000000..a57c6e36a3 --- /dev/null +++ b/feign/src/main/resources/application.properties @@ -0,0 +1,7 @@ +server.port=18080 +# Custom properties begin here +ws.api.path=/ws/users/* +ws.port.type.name=UsersPort +ws.target.namespace=http://www.baeldung.com/springbootsoap/feignclient +ws.location.uri=http://localhost:${server.port}/ws/users/ +debug=false diff --git a/feign/src/main/resources/users.xsd b/feign/src/main/resources/users.xsd new file mode 100644 index 0000000000..0c7a139c9a --- /dev/null +++ b/feign/src/main/resources/users.xsd @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/feign/src/test/java/com/baeldung/feign/clients/BookClientLiveTest.java b/feign/src/test/java/com/baeldung/feign/clients/BookClientLiveTest.java index 6f6666de32..47caf308d0 100644 --- a/feign/src/test/java/com/baeldung/feign/clients/BookClientLiveTest.java +++ b/feign/src/test/java/com/baeldung/feign/clients/BookClientLiveTest.java @@ -4,8 +4,9 @@ import com.baeldung.feign.BookControllerFeignClientBuilder; import com.baeldung.feign.models.Book; import com.baeldung.feign.models.BookResource; import lombok.extern.slf4j.Slf4j; -import org.junit.Before; -import org.junit.Test; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.List; import java.util.UUID; @@ -13,49 +14,43 @@ import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Consumes https://github.com/Baeldung/spring-hypermedia-api */ @Slf4j -public class BookClientLiveTest { +class BookClientLiveTest { private BookClient bookClient; - @Before - public void setup() { + @BeforeEach + void setup() { BookControllerFeignClientBuilder feignClientBuilder = new BookControllerFeignClientBuilder(); bookClient = feignClientBuilder.getBookClient(); } @Test - public void givenBookClient_shouldRunSuccessfully() throws Exception { - List books = bookClient.findAll() - .stream() - .map(BookResource::getBook) - .collect(Collectors.toList()); + void givenBookClient_shouldRunSuccessfully() throws Exception { + List books = bookClient.findAll().stream().map(BookResource::getBook).collect(Collectors.toList()); assertTrue(books.size() > 2); log.info("{}", books); } @Test - public void givenBookClient_shouldFindOneBook() throws Exception { - Book book = bookClient.findByIsbn("0151072558") - .getBook(); + void givenBookClient_shouldFindOneBook() throws Exception { + Book book = bookClient.findByIsbn("0151072558").getBook(); assertThat(book.getAuthor(), containsString("Orwell")); log.info("{}", book); } @Test - public void givenBookClient_shouldPostBook() throws Exception { - String isbn = UUID.randomUUID() - .toString(); + void givenBookClient_shouldPostBook() throws Exception { + String isbn = UUID.randomUUID().toString(); Book book = new Book(isbn, "Me", "It's me!", null, null); bookClient.create(book); - book = bookClient.findByIsbn(isbn) - .getBook(); + book = bookClient.findByIsbn(isbn).getBook(); assertThat(book.getAuthor(), is("Me")); log.info("{}", book); } diff --git a/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java index 8c43b42b76..d78bf60516 100644 --- a/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java +++ b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java @@ -2,19 +2,17 @@ package com.baeldung.feign.retry; import feign.*; import feign.codec.ErrorDecoder; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.nio.charset.Charset; -import java.util.Collection; import java.util.HashMap; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class Custom5xxErrorDecoderUnitTest { +class Custom5xxErrorDecoderUnitTest { @Test - public void given5xxResponse_whenDecode_thenReturnRetryableException() { + void given5xxResponse_whenDecode_thenReturnRetryableException() { // given ErrorDecoder decoder = new Custom5xxErrorDecoder(); Response response = responseStub(500); @@ -27,7 +25,7 @@ public class Custom5xxErrorDecoderUnitTest { } @Test - public void given4xxResponse_whenDecode_thenReturnFeignException() { + void given4xxResponse_whenDecode_thenReturnFeignException() { // given ErrorDecoder decoder = new Custom5xxErrorDecoder(); Response response = responseStub(400); @@ -40,12 +38,7 @@ public class Custom5xxErrorDecoderUnitTest { assertFalse(exception instanceof RetryableException); } - @NotNull private Response responseStub(int status) { - return Response.builder() - .request(Request.create( - Request.HttpMethod.GET, "url", new HashMap<>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())) - .status(status) - .build(); + return Response.builder().request(Request.create(Request.HttpMethod.GET, "url", new HashMap<>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())).status(status).build(); } } diff --git a/feign/src/test/java/com/baeldung/feign/soap/FeignLiveTest.java b/feign/src/test/java/com/baeldung/feign/soap/FeignLiveTest.java new file mode 100644 index 0000000000..d9da31344b --- /dev/null +++ b/feign/src/test/java/com/baeldung/feign/soap/FeignLiveTest.java @@ -0,0 +1,99 @@ +package com.baeldung.feign.soap; + +import feign.Feign; +import feign.Logger; +import feign.hc5.ApacheHttp5Client; +import feign.jaxb.JAXBContextFactory; +import feign.slf4j.Slf4jLogger; +import feign.soap.SOAPDecoder; +import feign.soap.SOAPEncoder; +import feign.soap.SOAPErrorDecoder; +import org.junit.jupiter.api.Test; + +import javax.xml.ws.soap.SOAPFaultException; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +class FeignLiveTest { + @Test + void givenSOAPPayload_whenStringRequest_thenReturnSOAPResponse() { + //@formatter:off + String successMessage="Success! Created the user with id"; + SoapClient client = Feign.builder() + .client(new ApacheHttp5Client()) + .logger(new Slf4jLogger(SoapClient.class)) + .logLevel(Logger.Level.FULL) + .target(SoapClient.class, "http://localhost:18080/ws/users/"); + + assertDoesNotThrow(() -> client.createUserWithPlainText(soapPayload())); + + String soapResponse= client.createUserWithPlainText(soapPayload()); + + assertNotNull(soapResponse); + assertTrue(soapResponse.contains(successMessage)); + //@formatter:on + } + + @Test + void whenSoapRequest_thenReturnSoapResponse() { + JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8").build(); + SoapClient client = Feign.builder() + .encoder(new SOAPEncoder(jaxbFactory)) + .errorDecoder(new SOAPErrorDecoder()) + .logger(new Slf4jLogger()) + .logLevel(Logger.Level.FULL) + .decoder(new SOAPDecoder(jaxbFactory)) + .target(SoapClient.class, "http://localhost:18080/ws/users/"); + CreateUserRequest request = new CreateUserRequest(); + + User user = new User(); + user.setId("501"); + user.setName("John Doe"); + user.setEmail("john.doe@gmail"); + request.setUser(user); + try { + CreateUserResponse response = client.createUserWithSoap(request); + assertNotNull(response); + assertNotNull(response.getMessage()); + assertTrue(response.getMessage().contains("Success")); + } catch (SOAPFaultException soapFaultException) { + fail(); + } + + } + + @Test + void whenSoapFault_thenThrowSOAPFaultException() { + JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8").build(); + SoapClient client = Feign.builder() + .encoder(new SOAPEncoder(jaxbFactory)) + .errorDecoder(new SOAPErrorDecoder()) + .logger(new Slf4jLogger()) + .logLevel(Logger.Level.FULL) + .decoder(new SOAPDecoder(jaxbFactory)) + .target(SoapClient.class, "http://localhost:18080/ws/users/"); + CreateUserRequest request = new CreateUserRequest(); + + User user = new User(); + user.setId("500"); + user.setName("John Doe"); + user.setEmail("john.doe@gmail"); + request.setUser(user); + try { + client.createUserWithSoap(request); + } catch (SOAPFaultException soapFaultException) { + assertNotNull(soapFaultException.getMessage()); + assertTrue(soapFaultException.getMessage().contains("This is a reserved user id")); + } + + } + + private String soapPayload() { + return "\n" + " \n" + " \n" + " \n" + + " \n" + " 1\n" + " john doe\n" + " john.doe@gmail.com\n" + " \n" + " \n" + + " \n" + ""; + } +}