Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions architecture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Architecture

This module contains articles about software architecture.

### Relevant articles
72 changes: 72 additions & 0 deletions architecture/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.architecture</groupId>
<artifactId>architecture</artifactId>
<name>architecture</name>
<packaging>jar</packaging>
<description>Architecture-related articles</description>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit-jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.hexagonalarchitecture;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baeldung.hexagonalarchitecture.domain.BookService;
import com.baeldung.hexagonalarchitecture.domain.IBookRepository;
import com.baeldung.hexagonalarchitecture.domain.IBookService;
import com.baeldung.hexagonalarchitecture.infrastructure.DatabaseBookRepository;
import com.baeldung.hexagonalarchitecture.infrastructure.JpaBookRepository;

@Configuration
public class BookAppConfiguration {

@Bean
IBookRepository bookRepository(JpaBookRepository jpaBookRepository) {
return new DatabaseBookRepository(jpaBookRepository);
}

@Bean
IBookService bookService(IBookRepository bookRepository) {
return new BookService(bookRepository);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.hexagonalarchitecture;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource(value = { "classpath:hexagonal-architecture.properties" })
public class BookApplication {

public static void main(final String[] args) {
SpringApplication.run(BookApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.hexagonalarchitecture.application;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baeldung.hexagonalarchitecture.domain.Book;
import com.baeldung.hexagonalarchitecture.domain.IBookService;

@RestController
public class BookController {

@Autowired
private IBookService bookService;

@GetMapping("/books")
ResponseEntity<List<Book>> listAllBooks() {
return ResponseEntity.ok(bookService.listAllBooks());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.hexagonalarchitecture.domain;

public class Book {
private String isbn;
private String title;

public Book() {

}

public Book(String isbn, String title) {
this.isbn = isbn;
this.title = title;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.hexagonalarchitecture.domain;

import java.util.List;

public class BookService implements IBookService {

private IBookRepository bookRepository;

public BookService(IBookRepository bookRepository) {
this.bookRepository = bookRepository;
}

@Override
public List<Book> listAllBooks() {
return bookRepository.findAllBooks();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung.hexagonalarchitecture.domain;

import java.util.List;

public interface IBookRepository {
List<Book> findAllBooks();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.baeldung.hexagonalarchitecture.domain;

import java.util.List;

public interface IBookService {
List<Book> listAllBooks();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.hexagonalarchitecture.infrastructure;

import java.util.ArrayList;
import java.util.List;

import com.baeldung.hexagonalarchitecture.domain.Book;
import com.baeldung.hexagonalarchitecture.domain.IBookRepository;

public class BasicBookRepository implements IBookRepository {
@Override
public List<Book> findAllBooks() {
List<Book> books = new ArrayList<Book>();
books.add(new Book("9780136083238", "Clean Code: A Handbook of Agile Software Craftsmanship"));
return books;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.hexagonalarchitecture.infrastructure;

import java.util.List;
import java.util.stream.Collectors;

import com.baeldung.hexagonalarchitecture.domain.Book;
import com.baeldung.hexagonalarchitecture.domain.IBookRepository;

public class DatabaseBookRepository implements IBookRepository {

private JpaBookRepository jpaBookRepository;

public DatabaseBookRepository(JpaBookRepository jpaBookRepository) {
this.jpaBookRepository = jpaBookRepository;
}

@Override
public List<Book> findAllBooks() {
return jpaBookRepository.findAll()
.stream()
.map(bookEntity -> new Book(bookEntity.getIsbn(), bookEntity.getTitle()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.hexagonalarchitecture.infrastructure;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class JpaBookEntity {
@Id
private String isbn;
private String title;

public String getIsbn() {
return isbn;
}

public void setIsbn(String iSBN) {
isbn = iSBN;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.hexagonalarchitecture.infrastructure;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface JpaBookRepository extends JpaRepository<JpaBookEntity, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring.datasource.url=jdbc:h2:mem:db
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.hexagonalarchitecture;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.baeldung.hexagonalarchitecture.domain.Book;
import com.baeldung.hexagonalarchitecture.domain.BookService;
import com.baeldung.hexagonalarchitecture.domain.IBookRepository;
import com.baeldung.hexagonalarchitecture.domain.IBookService;

@DisplayName("Book service")
@ExtendWith(MockitoExtension.class)
public class BookServiceUnitTest {

private IBookService bookService;

@Mock
private IBookRepository bookRepository;

@BeforeEach
void init() {
bookService = new BookService(bookRepository);
}

@Test
@DisplayName("When listing all books then it should use repository")
void whenListingAllBooks_thenItShouldUseRepository() {
List<Book> booksFromRepository = new ArrayList<>();
booksFromRepository.add(new Book("testIsbn", "testTitle"));
when(bookRepository.findAllBooks()).thenReturn(booksFromRepository);

List<Book> booksFromService = bookService.listAllBooks();

verify(bookRepository).findAllBooks();
assertThat(booksFromService).containsExactlyElementsOf(booksFromRepository);
}
}
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@
<module>apache-tika</module>
<module>apache-velocity</module>

<module>architecture</module>

<module>asciidoctor</module>
<module>asm</module>

Expand Down Expand Up @@ -842,6 +844,8 @@
<module>apache-thrift</module>
<module>apache-tika</module>
<module>apache-velocity</module>

<module>architecture</module>

<module>asciidoctor</module>
<module>asm</module>
Expand Down