├── rest-example-master ├── Dockerfile ├── src │ ├── main │ │ ├── java │ │ │ └── pl │ │ │ │ └── finsys │ │ │ │ └── example │ │ │ │ ├── repository │ │ │ │ └── BookRepository.java │ │ │ │ ├── service │ │ │ │ ├── exception │ │ │ │ │ └── BookAlreadyExistsException.java │ │ │ │ ├── BookService.java │ │ │ │ └── BookServiceImpl.java │ │ │ │ ├── BookstoreApplication.java │ │ │ │ ├── configuration │ │ │ │ └── SwaggerConfig.java │ │ │ │ ├── domain │ │ │ │ └── Book.java │ │ │ │ └── controller │ │ │ │ └── BookController.java │ │ └── resources │ │ │ ├── application.properties │ │ │ └── logback.xml │ └── test │ │ └── java │ │ └── pl │ │ └── finsys │ │ └── example │ │ ├── util │ │ └── UserUtil.java │ │ ├── controller │ │ └── BookControllerTest.java │ │ └── service │ │ └── BookServiceImplTest.java ├── service.yaml ├── service.json ├── deployment.yaml ├── README.md ├── deployment.json └── pom.xml ├── LICENSE └── README.md /rest-example-master/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM jeanblanchard/java:8 2 | COPY target/rest-example-0.1.0.jar rest-example-0.1.0.jar 3 | CMD java -jar rest-example-0.1.0.jar 4 | EXPOSE 8080 -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.repository; 2 | 3 | import pl.finsys.example.domain.Book; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface BookRepository extends JpaRepository { 7 | } 8 | -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/service/exception/BookAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.service.exception; 2 | 3 | public class BookAlreadyExistsException extends RuntimeException { 4 | 5 | public BookAlreadyExistsException(final String message) { 6 | super(message); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /rest-example-master/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: rest-example 5 | labels: 6 | app: rest-example 7 | tier: backend 8 | spec: 9 | type: NodePort 10 | ports: 11 | # the port that this service should serve on 12 | - port: 8080 13 | selector: 14 | app: rest-example 15 | tier: backend -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/BookstoreApplication.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class BookstoreApplication { 8 | 9 | public static void main(final String[] args) { 10 | SpringApplication.run(BookstoreApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /rest-example-master/service.json: -------------------------------------------------------------------------------- 1 | { 2 | "apiVersion": "v1", 3 | "kind": "Service", 4 | "metadata": { 5 | "name": "rest-example", 6 | "labels": { 7 | "app": "rest-example", 8 | "tier": "backend" 9 | } 10 | }, 11 | "spec": { 12 | "type": "NodePort", 13 | "ports": [ 14 | { 15 | "port": 8080 16 | } 17 | ], 18 | "selector": { 19 | "app": "rest-example", 20 | "tier": "backend" 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /rest-example-master/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # suppress inspection "UnusedProperty" for whole file 2 | 3 | # Spring 4 | spring.profiles.active=dev 5 | 6 | # Server 7 | server.port=8080 8 | server.sessionTimeout=30 9 | 10 | # JPA 11 | spring.jpa.hibernate.ddl-auto=create-drop 12 | 13 | # Tomcat 14 | tomcat.accessLogEnabled=false 15 | tomcat.protocolHeader=x-forwarded-proto 16 | tomcat.remoteIpHeader=x-forwarded-for 17 | tomcat.backgroundProcessorDelay=30 18 | -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/service/BookService.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.service; 2 | 3 | import pl.finsys.example.domain.Book; 4 | 5 | import javax.validation.Valid; 6 | import javax.validation.constraints.NotNull; 7 | import java.util.List; 8 | 9 | public interface BookService { 10 | 11 | Book saveBook(@NotNull @Valid final Book book); 12 | 13 | List getList(); 14 | 15 | Book getBook(Long bookId); 16 | 17 | void deleteBook(final Long bookId); 18 | } 19 | -------------------------------------------------------------------------------- /rest-example-master/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | System.out 5 | 6 | %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /rest-example-master/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: rest-example 5 | spec: 6 | replicas: 1 7 | template: 8 | metadata: 9 | labels: 10 | app: rest-example 11 | tier: backend 12 | spec: 13 | containers: 14 | - name: rest-example 15 | image: jotka/rest-example 16 | imagePullPolicy: IfNotPresent 17 | resources: 18 | requests: 19 | cpu: 100m 20 | memory: 100Mi 21 | env: 22 | - name: GET_HOSTS_FROM 23 | value: dns 24 | ports: 25 | - containerPort: 8080 -------------------------------------------------------------------------------- /rest-example-master/README.md: -------------------------------------------------------------------------------- 1 | Example Spring Boot REST Service 2 | 3 | Quick start 4 | ----------- 5 | 1. `mvn package` 6 | 2. `java -jar target/example-spring-boot-rest-1.0-SNAPSHOT.jar` 7 | 8 | 9 | 10 | curl -H "Content-Type: application/json" -X POST -d '{"id":"1","author":"Krochmalski", "title":"IDEA"}' http://localhost:8080/books 11 | 12 | 13 | 14 | 15 | 16 | curl -s http://localhost:8080/api/v1/namespaces/default/services -XPOST -H 'Content-Type: application/json' -d@service.json 17 | 18 | curl -s http://localhost:8080/apis/extensions/v1beta1/namespaces/default/deployments -XPOST -H 'Content-Type: application/json' -d@deployment.json 19 | 20 | 21 | https://192.168.99.100:8443/api/v1/namespaces/default/pods 22 | curl http://localhost:8080/api/v1/namespaces/default/pods -------------------------------------------------------------------------------- /rest-example-master/src/test/java/pl/finsys/example/util/UserUtil.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.util; 2 | 3 | import pl.finsys.example.domain.Book; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class UserUtil { 9 | 10 | private static final String author = "author"; 11 | private static final String title = "title"; 12 | 13 | private UserUtil() { 14 | } 15 | 16 | public static Book createBook() { 17 | return new Book(1L, author, title); 18 | } 19 | 20 | public static List createBookList(int howMany) { 21 | List bookList = new ArrayList<>(); 22 | for (int i = 0; i < howMany; i++) { 23 | bookList.add(new Book((long) i, author, title)); 24 | } 25 | return bookList; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/configuration/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.configuration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import springfox.documentation.builders.PathSelectors; 6 | import springfox.documentation.builders.RequestHandlerSelectors; 7 | import springfox.documentation.spi.DocumentationType; 8 | import springfox.documentation.spring.web.plugins.Docket; 9 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 10 | 11 | @Configuration 12 | @EnableSwagger2 13 | public class SwaggerConfig { 14 | @Bean 15 | public Docket api() { 16 | return new Docket(DocumentationType.SWAGGER_2) 17 | .select() 18 | .apis(RequestHandlerSelectors.any()) 19 | .paths(PathSelectors.any()).build(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /rest-example-master/deployment.json: -------------------------------------------------------------------------------- 1 | { 2 | "apiVersion": "extensions/v1beta1", 3 | "kind": "Deployment", 4 | "metadata": { 5 | "name": "rest-example" 6 | }, 7 | "spec": { 8 | "replicas": 1, 9 | "template": { 10 | "metadata": { 11 | "labels": { 12 | "app": "rest-example", 13 | "tier": "backend" 14 | } 15 | }, 16 | "spec": { 17 | "containers": [ 18 | { 19 | "name": "rest-example", 20 | "image": "jotka/rest-example", 21 | "imagePullPolicy": "IfNotPresent", 22 | "resources": { 23 | "requests": { 24 | "cpu": "100m", 25 | "memory": "100Mi" 26 | } 27 | }, 28 | "env": [ 29 | { 30 | "name": "GET_HOSTS_FROM", 31 | "value": "dns" 32 | } 33 | ], 34 | "ports": [ 35 | { 36 | "containerPort": 8080 37 | } 38 | ] 39 | } 40 | ] 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/domain/Book.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.domain; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Id; 6 | import javax.validation.constraints.NotNull; 7 | import javax.validation.constraints.Size; 8 | 9 | @Entity 10 | public class Book { 11 | 12 | @Id 13 | @NotNull 14 | @Column(name = "id", nullable = false, updatable = false) 15 | private Long id; 16 | 17 | @NotNull 18 | @Size(max = 64) 19 | @Column(name = "author", nullable = false) 20 | private String author; 21 | 22 | @NotNull 23 | @Size(max = 64) 24 | @Column(name = "title", nullable = false) 25 | private String title; 26 | 27 | public Book() { 28 | } 29 | 30 | public Book(final Long id, final String author, final String title) { 31 | this.id = id; 32 | this.title = title; 33 | this.author = author; 34 | } 35 | 36 | public Long getId() { 37 | return id; 38 | } 39 | 40 | public String getAuthor() { 41 | return author; 42 | } 43 | 44 | public String getTitle() { 45 | return title; 46 | } 47 | 48 | public void setTitle(String title) { 49 | this.title = title; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "Book{" + 55 | "id=" + id + 56 | ", author='" + author + '\'' + 57 | ", title='" + title + '\'' + 58 | '}'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/service/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import pl.finsys.example.domain.Book; 5 | import pl.finsys.example.repository.BookRepository; 6 | import pl.finsys.example.service.exception.BookAlreadyExistsException; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.stereotype.Service; 10 | import org.springframework.transaction.annotation.Transactional; 11 | import org.springframework.validation.annotation.Validated; 12 | 13 | import javax.validation.Valid; 14 | import javax.validation.constraints.NotNull; 15 | import java.util.List; 16 | 17 | @Service 18 | @Validated 19 | public class BookServiceImpl implements BookService { 20 | 21 | private static final Logger LOGGER = LoggerFactory.getLogger(BookServiceImpl.class); 22 | private final BookRepository repository; 23 | 24 | @Autowired 25 | public BookServiceImpl(final BookRepository repository) { 26 | this.repository = repository; 27 | } 28 | 29 | @Override 30 | @Transactional 31 | public Book saveBook(@NotNull @Valid final Book book) { 32 | LOGGER.debug("Creating {}", book); 33 | Book existing = repository.findOne(book.getId()); 34 | if (existing != null) { 35 | throw new BookAlreadyExistsException( 36 | String.format("There already exists a book with id=%s", book.getId())); 37 | } 38 | return repository.save(book); 39 | } 40 | 41 | @Override 42 | @Transactional(readOnly = true) 43 | public List getList() { 44 | LOGGER.debug("Retrieving the list of all users"); 45 | return repository.findAll(); 46 | } 47 | 48 | @Override 49 | public Book getBook(Long bookId) { 50 | return repository.findOne(bookId); 51 | } 52 | 53 | @Override 54 | @Transactional 55 | public void deleteBook(final Long bookId) { 56 | LOGGER.debug("deleting {}", bookId); 57 | repository.delete(bookId); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /rest-example-master/src/test/java/pl/finsys/example/controller/BookControllerTest.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.controller; 2 | 3 | import pl.finsys.example.domain.Book; 4 | import pl.finsys.example.service.BookService; 5 | import pl.finsys.example.util.UserUtil; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.Mock; 10 | import org.mockito.runners.MockitoJUnitRunner; 11 | 12 | import java.util.Collection; 13 | 14 | import static org.junit.Assert.assertEquals; 15 | import static org.junit.Assert.assertNotNull; 16 | import static org.mockito.Mockito.*; 17 | 18 | @RunWith(MockitoJUnitRunner.class) 19 | public class BookControllerTest { 20 | 21 | @Mock 22 | private BookService bookService; 23 | 24 | private BookController bookController; 25 | 26 | @Before 27 | public void setUp() throws Exception { 28 | bookController = new BookController(bookService); 29 | } 30 | 31 | @Test 32 | public void shouldCreateUser() throws Exception { 33 | final Book savedBook = stubServiceToReturnStoredBook(); 34 | final Book book = UserUtil.createBook(); 35 | Book returnedBook = bookController.saveBook(book); 36 | // verify book was passed to BookService 37 | verify(bookService, times(1)).saveBook(book); 38 | assertEquals("Returned book should come from the service", savedBook, returnedBook); 39 | } 40 | 41 | private Book stubServiceToReturnStoredBook() { 42 | final Book book = UserUtil.createBook(); 43 | when(bookService.saveBook(any(Book.class))).thenReturn(book); 44 | return book; 45 | } 46 | 47 | 48 | @Test 49 | public void shouldListAllUsers() throws Exception { 50 | stubServiceToReturnExistingUsers(10); 51 | Collection books = bookController.listBooks(); 52 | assertNotNull(books); 53 | assertEquals(10, books.size()); 54 | // verify user was passed to BookService 55 | verify(bookService, times(1)).getList(); 56 | } 57 | 58 | private void stubServiceToReturnExistingUsers(int howMany) { 59 | when(bookService.getList()).thenReturn(UserUtil.createBookList(howMany)); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /rest-example-master/src/main/java/pl/finsys/example/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.controller; 2 | 3 | import io.swagger.annotations.ApiOperation; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import pl.finsys.example.domain.Book; 6 | import pl.finsys.example.service.BookService; 7 | import pl.finsys.example.service.exception.BookAlreadyExistsException; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.http.HttpStatus; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import javax.validation.Valid; 14 | import java.util.List; 15 | 16 | @RestController 17 | public class BookController { 18 | 19 | private static final Logger LOGGER = LoggerFactory.getLogger(BookController.class); 20 | private final BookService bookService; 21 | 22 | @Autowired 23 | public BookController(final BookService bookService) { 24 | this.bookService = bookService; 25 | } 26 | 27 | @RequestMapping(value = "/books", method = RequestMethod.POST, consumes = {"application/json"}) 28 | public Book saveBook(@RequestBody @Valid final Book book) { 29 | LOGGER.debug("Received request to create the {}", book); 30 | return bookService.saveBook(book); 31 | } 32 | 33 | @ApiOperation(value = "Retrieve a list of books.", 34 | responseContainer = "List") 35 | @RequestMapping(value = "/books", method = RequestMethod.GET, produces = {"application/json"}) 36 | public List listBooks() { 37 | LOGGER.debug("Received request to list all books"); 38 | return bookService.getList(); 39 | } 40 | 41 | @RequestMapping(value = "/books/{id}", method = RequestMethod.GET, produces = {"application/json"}) 42 | public @ResponseBody 43 | Book singleBook(@PathVariable Long id) { 44 | LOGGER.debug("Received request to list a specific book"); 45 | return bookService.getBook(id); 46 | } 47 | 48 | @RequestMapping(value = "/books/{id}", method = RequestMethod.DELETE) 49 | public void deleteBook(@PathVariable Long id) { 50 | LOGGER.debug("Received request to delete a specific book"); 51 | bookService.deleteBook(id); 52 | } 53 | 54 | @ExceptionHandler 55 | @ResponseStatus(HttpStatus.CONFLICT) 56 | public String handleUserAlreadyExistsException(BookAlreadyExistsException e) { 57 | return e.getMessage(); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Docker and Kubernetes for Java Developers 5 | This is the code repository for [Docker and Kubernetes for Java Developers](https://www.packtpub.com/virtualization-and-cloud/docker-and-kubernetes-java-developers?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781786468390). It contains all the supporting project files necessary to work through the book from start to finish. 6 | 7 | ## About the Book 8 | This book will start by introducing Docker and delve deep into its networking and persistent storage concepts. You will then proceed to learn how to refactor monolith application into separate services by building an application and then packaging it into Docker containers. Next, you will create an image containing Java Enterprise Application and later run it using Docker. Moving on, the book will focus on Kubernetes and its features and you will learn to deploy a Java application to Kubernetes using Maven and monitor a Java application in production. By the end of the book, you will get hands-on with some more advanced topics to further extend your knowledge about Docker and Kubernetes. 9 | 10 | 11 | ## Instructions and Navigation 12 | All of the code is organized into folder. 13 | 14 | The code will look like the following: 15 | ``` 16 | { 17 | "apiVersion": "v1", 18 | "kind": "Pod", 19 | "metadata":{ 20 | "name": ”rest_service”, 21 | "labels": { 22 | "name": "rest_service" 23 | } 24 | }, 25 | "spec": { 26 | "containers": [{ 27 | "name": "rest_service", 28 | "image": "rest_service", 29 | "ports": [{"containerPort": 8080}], 30 | }] 31 | } 32 | } 33 | 34 | ``` 35 | 36 | ## Related Products 37 | * [DevOps: Puppet, Docker, and Kubernetes](https://www.packtpub.com/virtualization-and-cloud/devops-puppet-docker-and-kubernetes?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781788297615) 38 | 39 | * [Docker for Web Developers [Video]](https://www.packtpub.com/virtualization-and-cloud/docker-web-developers-video?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781786465931) 40 | 41 | * [Docker: Creating Structured Containers](https://www.packtpub.com/virtualization-and-cloud/docker-creating-structured-containers?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781786465931) 42 | 43 | ### Download a free PDF 44 | 45 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
46 |

https://packt.link/free-ebook/9781786468390

-------------------------------------------------------------------------------- /rest-example-master/src/test/java/pl/finsys/example/service/BookServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package pl.finsys.example.service; 2 | 3 | import pl.finsys.example.domain.Book; 4 | import pl.finsys.example.repository.BookRepository; 5 | import pl.finsys.example.service.exception.BookAlreadyExistsException; 6 | import pl.finsys.example.util.UserUtil; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.mockito.Mock; 11 | import org.mockito.runners.MockitoJUnitRunner; 12 | 13 | import java.util.Collection; 14 | 15 | import static org.junit.Assert.*; 16 | import static org.mockito.Matchers.any; 17 | import static org.mockito.Mockito.*; 18 | 19 | @RunWith(MockitoJUnitRunner.class) 20 | public class BookServiceImplTest { 21 | 22 | @Mock 23 | private BookRepository bookRepository; 24 | 25 | private BookService bookService; 26 | 27 | @Before 28 | public void setUp() throws Exception { 29 | bookService = new BookServiceImpl(bookRepository); 30 | } 31 | 32 | @Test 33 | public void shouldSaveNewUser_GivenThereDoesNotExistOneWithTheSameId_ThenTheSavedUserShouldBeReturned() throws Exception { 34 | final Book savedBook = stubRepositoryToReturnUserOnSave(); 35 | final Book book = UserUtil.createBook(); 36 | final Book returnedBook = bookService.saveBook(book); 37 | // verify repository was called with book 38 | verify(bookRepository, times(1)).save(book); 39 | assertEquals("Returned book should come from the repository", savedBook, returnedBook); 40 | } 41 | 42 | private Book stubRepositoryToReturnUserOnSave() { 43 | Book book = UserUtil.createBook(); 44 | when(bookRepository.save(any(Book.class))).thenReturn(book); 45 | return book; 46 | } 47 | 48 | @Test 49 | public void shouldSaveNewUser_GivenThereExistsOneWithTheSameId_ThenTheExceptionShouldBeThrown() throws Exception { 50 | stubRepositoryToReturnExistingUser(); 51 | try { 52 | bookService.saveBook(UserUtil.createBook()); 53 | fail("Expected exception"); 54 | } catch (BookAlreadyExistsException ignored) { 55 | } 56 | verify(bookRepository, never()).save(any(Book.class)); 57 | } 58 | 59 | private void stubRepositoryToReturnExistingUser() { 60 | final Book book = UserUtil.createBook(); 61 | when(bookRepository.findOne(book.getId())).thenReturn(book); 62 | } 63 | 64 | @Test 65 | public void shouldListAllUsers_GivenThereExistSome_ThenTheCollectionShouldBeReturned() throws Exception { 66 | stubRepositoryToReturnExistingUsers(10); 67 | Collection list = bookService.getList(); 68 | assertNotNull(list); 69 | assertEquals(10, list.size()); 70 | verify(bookRepository, times(1)).findAll(); 71 | } 72 | 73 | private void stubRepositoryToReturnExistingUsers(int howMany) { 74 | when(bookRepository.findAll()).thenReturn(UserUtil.createBookList(howMany)); 75 | } 76 | 77 | @Test 78 | public void shouldListAllUsers_GivenThereNoneExist_ThenTheEmptyCollectionShouldBeReturned() throws Exception { 79 | stubRepositoryToReturnExistingUsers(0); 80 | Collection list = bookService.getList(); 81 | assertNotNull(list); 82 | assertTrue(list.isEmpty()); 83 | verify(bookRepository, times(1)).findAll(); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /rest-example-master/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | pl.finsys 7 | rest-example 8 | 0.1.0 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 1.5.2.RELEASE 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-web 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-data-jpa 24 | 25 | 26 | org.hibernate 27 | hibernate-validator 28 | 29 | 30 | org.hsqldb 31 | hsqldb 32 | runtime 33 | 34 | 35 | io.springfox 36 | springfox-swagger2 37 | 2.6.1 38 | 39 | 40 | io.springfox 41 | springfox-swagger-ui 42 | 2.5.0 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-starter-test 54 | test 55 | 56 | 57 | com.jayway.jsonpath 58 | json-path 59 | test 60 | 61 | 62 | 63 | 64 | 1.8 65 | 66 | 67 | 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-maven-plugin 72 | 73 | 74 | org.springframework.boot 75 | spring-boot-maven-plugin 76 | 77 | 78 | io.fabric8 79 | docker-maven-plugin 80 | 0.20.1 81 | 82 | 83 | 84 | rest-example:${project.version} 85 | rest-example 86 | 87 | openjdk:latest 88 | 89 | artifact 90 | 91 | java -jar maven/${project.name}-${project.version}.jar 92 | 93 | 94 | 95 | 8080:8080 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | spring-releases 109 | https://repo.spring.io/libs-release 110 | 111 | 112 | 113 | 114 | spring-releases 115 | https://repo.spring.io/libs-release 116 | 117 | 118 | --------------------------------------------------------------------------------