BAEL-11: Added skip-tests configuration to pom.xml.
This commit is contained in:
parent
5978c00dc9
commit
674c783886
|
@ -0,0 +1,5 @@
|
|||
## Feign Hypermedia Client ##
|
||||
|
||||
This is the implementation of a [spring-hypermedia-api][1] client using Feign.
|
||||
|
||||
[1]: https://github.com/eugenp/spring-hypermedia-api
|
|
@ -0,0 +1,99 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.feign</groupId>
|
||||
<artifactId>feign-client</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
<version>9.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-okhttp</artifactId>
|
||||
<version>9.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-gson</artifactId>
|
||||
<version>9.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-slf4j</artifactId>
|
||||
<version>9.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.6.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>2.6.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.16.10</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.19.1</version>
|
||||
<configuration>
|
||||
<skipTests>true</skipTests>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>1.4.0.RELEASE</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.feign;
|
||||
|
||||
import com.baeldung.feign.clients.BookClient;
|
||||
import feign.Feign;
|
||||
import feign.Logger;
|
||||
import feign.gson.GsonDecoder;
|
||||
import feign.gson.GsonEncoder;
|
||||
import feign.okhttp.OkHttpClient;
|
||||
import feign.slf4j.Slf4jLogger;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Controller {
|
||||
private BookClient bookClient = createClient(BookClient.class,
|
||||
"http://localhost:8081/api/books");
|
||||
|
||||
private static <T> T createClient(Class<T> type, String uri) {
|
||||
return Feign.builder()
|
||||
.client(new OkHttpClient())
|
||||
.encoder(new GsonEncoder())
|
||||
.decoder(new GsonDecoder())
|
||||
.logger(new Slf4jLogger(type))
|
||||
.logLevel(Logger.Level.FULL)
|
||||
.target(type, uri);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.feign.clients;
|
||||
|
||||
import com.baeldung.feign.models.Book;
|
||||
import com.baeldung.feign.models.BookResource;
|
||||
import feign.Headers;
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BookClient {
|
||||
@RequestLine("GET /{isbn}")
|
||||
BookResource findByIsbn(@Param("isbn") String isbn);
|
||||
|
||||
@RequestLine("GET")
|
||||
List<BookResource> findAll();
|
||||
|
||||
@RequestLine("POST")
|
||||
@Headers("Content-Type: application/json")
|
||||
void create(Book book);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.feign.models;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Book {
|
||||
private String isbn;
|
||||
private String author;
|
||||
private String title;
|
||||
private String synopsis;
|
||||
private String language;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.feign.models;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BookResource {
|
||||
private Book book;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%n[+] %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="debug">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.feign.clients;
|
||||
|
||||
import com.baeldung.feign.Controller;
|
||||
import com.baeldung.feign.models.Book;
|
||||
import com.baeldung.feign.models.BookResource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@RunWith(JUnit4.class)
|
||||
public class BookClientTest {
|
||||
private Controller controller = new Controller();
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldRunSuccessfully() throws Exception {
|
||||
BookClient bookClient = controller.getBookClient();
|
||||
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 {
|
||||
BookClient bookClient = controller.getBookClient();
|
||||
Book book = bookClient.findByIsbn("0151072558").getBook();
|
||||
assertThat(book.getAuthor(), containsString("Orwell"));
|
||||
log.info("{}", book);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookClient_shouldPostBook() throws Exception {
|
||||
BookClient bookClient = controller.getBookClient();
|
||||
String isbn = UUID.randomUUID().toString();
|
||||
Book book = new Book(isbn, "Me", "It's me!", null, null);
|
||||
bookClient.create(book);
|
||||
|
||||
book = bookClient.findByIsbn(isbn).getBook();
|
||||
assertThat(book.getAuthor(), is("Me"));
|
||||
log.info("{}", book);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue