* BAEL-5298

* Renamed the tests

* Changed the Parent to parent-boot-2

Co-authored-by: Bhaskara Navuluri <bhaskara.navuluri@hpe.com>
This commit is contained in:
Bhaskara 2022-03-28 20:45:16 +05:30 committed by GitHub
parent 0c229c433f
commit 8f2cc51af4
10 changed files with 376 additions and 42 deletions

View File

@ -1,7 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.feign</groupId> <groupId>com.baeldung.feign</groupId>
<artifactId>feign</artifactId> <artifactId>feign</artifactId>
@ -9,10 +7,17 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId> <artifactId>parent-boot-2</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent> </parent>
<properties>
<java.version>1.8</java.version>
<feign.version>11.8</feign.version>
<wsdl4j.version>1.6.3</wsdl4j.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>io.github.openfeign</groupId> <groupId>io.github.openfeign</groupId>
@ -35,10 +40,85 @@
<version>${lombok.version}</version> <version>${lombok.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>${wsdl4j.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hc5</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-soap</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.baeldung.feign.soap</packageName>
<sources>
<source>src/main/resources/users.xsd</source>
</sources>
<properties> </configuration>
<feign.version>10.11</feign.version> </plugin>
</properties>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<executions>
<execution>
<id>feign-soap-stub-generation</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>target/generated-sources/jaxb</schemaDirectory>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
<generatePackage>com.baeldung.feign.soap</generatePackage>
<generateDirectory>target/generated-sources/jaxb</generateDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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<String, User> 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;
}
}

View File

@ -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> 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"));
}
}

View File

@ -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

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.baeldung.com/springbootsoap/feignclient"
targetNamespace="http://www.baeldung.com/springbootsoap/feignclient" elementFormDefault="qualified">
<xs:element name="getUserRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getUserResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="tns:user"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="createUserRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="tns:user"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="createUserResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="message" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Define the complex object Product -->
<xs:complexType name="user">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

View File

@ -4,8 +4,9 @@ import com.baeldung.feign.BookControllerFeignClientBuilder;
import com.baeldung.feign.models.Book; import com.baeldung.feign.models.Book;
import com.baeldung.feign.models.BookResource; import com.baeldung.feign.models.BookResource;
import lombok.extern.slf4j.Slf4j; 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.List;
import java.util.UUID; import java.util.UUID;
@ -13,49 +14,43 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Consumes https://github.com/Baeldung/spring-hypermedia-api * Consumes https://github.com/Baeldung/spring-hypermedia-api
*/ */
@Slf4j @Slf4j
public class BookClientLiveTest { class BookClientLiveTest {
private BookClient bookClient; private BookClient bookClient;
@Before @BeforeEach
public void setup() { void setup() {
BookControllerFeignClientBuilder feignClientBuilder = new BookControllerFeignClientBuilder(); BookControllerFeignClientBuilder feignClientBuilder = new BookControllerFeignClientBuilder();
bookClient = feignClientBuilder.getBookClient(); bookClient = feignClientBuilder.getBookClient();
} }
@Test @Test
public void givenBookClient_shouldRunSuccessfully() throws Exception { void givenBookClient_shouldRunSuccessfully() throws Exception {
List<Book> books = bookClient.findAll() List<Book> books = bookClient.findAll().stream().map(BookResource::getBook).collect(Collectors.toList());
.stream()
.map(BookResource::getBook)
.collect(Collectors.toList());
assertTrue(books.size() > 2); assertTrue(books.size() > 2);
log.info("{}", books); log.info("{}", books);
} }
@Test @Test
public void givenBookClient_shouldFindOneBook() throws Exception { void givenBookClient_shouldFindOneBook() throws Exception {
Book book = bookClient.findByIsbn("0151072558") Book book = bookClient.findByIsbn("0151072558").getBook();
.getBook();
assertThat(book.getAuthor(), containsString("Orwell")); assertThat(book.getAuthor(), containsString("Orwell"));
log.info("{}", book); log.info("{}", book);
} }
@Test @Test
public void givenBookClient_shouldPostBook() throws Exception { void givenBookClient_shouldPostBook() throws Exception {
String isbn = UUID.randomUUID() String isbn = UUID.randomUUID().toString();
.toString();
Book book = new Book(isbn, "Me", "It's me!", null, null); Book book = new Book(isbn, "Me", "It's me!", null, null);
bookClient.create(book); bookClient.create(book);
book = bookClient.findByIsbn(isbn) book = bookClient.findByIsbn(isbn).getBook();
.getBook();
assertThat(book.getAuthor(), is("Me")); assertThat(book.getAuthor(), is("Me"));
log.info("{}", book); log.info("{}", book);
} }

View File

@ -2,19 +2,17 @@ package com.baeldung.feign.retry;
import feign.*; import feign.*;
import feign.codec.ErrorDecoder; import feign.codec.ErrorDecoder;
import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class Custom5xxErrorDecoderUnitTest { class Custom5xxErrorDecoderUnitTest {
@Test @Test
public void given5xxResponse_whenDecode_thenReturnRetryableException() { void given5xxResponse_whenDecode_thenReturnRetryableException() {
// given // given
ErrorDecoder decoder = new Custom5xxErrorDecoder(); ErrorDecoder decoder = new Custom5xxErrorDecoder();
Response response = responseStub(500); Response response = responseStub(500);
@ -27,7 +25,7 @@ public class Custom5xxErrorDecoderUnitTest {
} }
@Test @Test
public void given4xxResponse_whenDecode_thenReturnFeignException() { void given4xxResponse_whenDecode_thenReturnFeignException() {
// given // given
ErrorDecoder decoder = new Custom5xxErrorDecoder(); ErrorDecoder decoder = new Custom5xxErrorDecoder();
Response response = responseStub(400); Response response = responseStub(400);
@ -40,12 +38,7 @@ public class Custom5xxErrorDecoderUnitTest {
assertFalse(exception instanceof RetryableException); assertFalse(exception instanceof RetryableException);
} }
@NotNull
private Response responseStub(int status) { private Response responseStub(int status) {
return Response.builder() return Response.builder().request(Request.create(Request.HttpMethod.GET, "url", new HashMap<>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())).status(status).build();
.request(Request.create(
Request.HttpMethod.GET, "url", new HashMap<>(), new byte[0], Charset.defaultCharset(), new RequestTemplate()))
.status(status)
.build();
} }
} }

View File

@ -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 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:feig=\"http://www.baeldung.com/springbootsoap/feignclient\">\n" + " <soapenv:Header/>\n" + " <soapenv:Body>\n" + " <feig:createUserRequest>\n"
+ " <feig:user>\n" + " <feig:id>1</feig:id>\n" + " <feig:name>john doe</feig:name>\n" + " <feig:email>john.doe@gmail.com</feig:email>\n" + " </feig:user>\n" + " </feig:createUserRequest>\n"
+ " </soapenv:Body>\n" + "</soapenv:Envelope>";
}
}