BAEL-5298 (#11965)
* 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:
@@ -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<Book> books = bookClient.findAll()
|
||||
.stream()
|
||||
.map(BookResource::getBook)
|
||||
.collect(Collectors.toList());
|
||||
void givenBookClient_shouldRunSuccessfully() throws Exception {
|
||||
List<Book> 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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user