Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from ralf-ueberfuhr-ars/feature/layers
Browse files Browse the repository at this point in the history
Add MapStruct
  • Loading branch information
ralf-ueberfuhr-ars authored Dec 19, 2023
2 parents 6880d62 + 7d2df26 commit 677018d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -41,6 +42,11 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -49,6 +55,34 @@
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<!-- This is needed when using Lombok 1.18.16 and above -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.sample.schulung.spring.blog.boundary;

import de.sample.schulung.spring.blog.domain.BlogPost;
import de.sample.schulung.spring.blog.domain.BlogPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
Expand All @@ -20,20 +19,14 @@
public class BlogPostController {

private final BlogPostService service;
private final BlogPostDtoMapper mapper;

@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE
)
Stream<BlogPostDto> findAllBlogPosts() {
return service.findAll()
.map(post -> {
BlogPostDto dto = new BlogPostDto();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
dto.setContent(post.getContent());
dto.setTimestamp(post.getTimestamp());
return dto;
});
.map(mapper::map);
}

@GetMapping(
Expand All @@ -42,14 +35,7 @@ Stream<BlogPostDto> findAllBlogPosts() {
)
BlogPostDto findById(@PathVariable UUID id) {
return service.findById(id)
.map(post -> {
BlogPostDto dto = new BlogPostDto();
dto.setId(post.getId());
dto.setTitle(post.getTitle());
dto.setContent(post.getContent());
dto.setTimestamp(post.getTimestamp());
return dto;
})
.map(mapper::map)
.orElse(null);
}

Expand All @@ -58,13 +44,9 @@ BlogPostDto findById(@PathVariable UUID id) {
produces = MediaType.APPLICATION_JSON_VALUE
)
ResponseEntity<BlogPostDto> createBlogPost(@RequestBody BlogPostDto blogPostDto) {
final var post = BlogPost.builder()
.title(blogPostDto.getTitle())
.content(blogPostDto.getContent())
.build();
final var post = mapper.map(blogPostDto);
service.create(post);
blogPostDto.setId(post.getId());
blogPostDto.setTimestamp(post.getTimestamp());
mapper.copy(post, blogPostDto);
// Maven Dependency: spring-boot-starter-hateoas
final var location = linkTo(
methodOn(BlogPostController.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.sample.schulung.spring.blog.boundary;

import de.sample.schulung.spring.blog.domain.BlogPost;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface BlogPostDtoMapper {

BlogPost map(BlogPostDto source);

BlogPostDto map(BlogPost source);

void copy(BlogPost post, @MappingTarget BlogPostDto blogPostDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.sample.schulung.spring.blog.domain.BlogPostService;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import org.springframework.http.HttpStatus;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -11,7 +12,8 @@ public class BlogPostControllerTests {
@Test
void shouldCreateBlogPostSuccessfully() {
final var service = new BlogPostService();
final var controller = new BlogPostController(service);
final var mapper = Mappers.getMapper(BlogPostDtoMapper.class);
final var controller = new BlogPostController(service, mapper);
final var blogPost = new BlogPostDto();
blogPost.setTitle("test");
blogPost.setContent("Das ist ein Test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import de.sample.schulung.spring.blog.domain.BlogPost;
import de.sample.schulung.spring.blog.domain.BlogPostService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mapstruct.factory.Mappers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

Expand All @@ -20,9 +21,14 @@ public class BlogPostControllerWithMockedServiceTest {
@Mock
BlogPostService service;

@InjectMocks
BlogPostController sut;

@BeforeEach
void setup() {
final var mapper = Mappers.getMapper(BlogPostDtoMapper.class);
this.sut = new BlogPostController(service, mapper);
}

@Test
void findByIdShouldReturnBlogPost() {

Expand Down

0 comments on commit 677018d

Please sign in to comment.