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

Commit

Permalink
add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Dec 20, 2023
1 parent 7d2df26 commit 3895546
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
16 changes: 16 additions & 0 deletions http-requests/POST-blogposts.http
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ Content-Type: application/json
client.assert(response.status === 201);
});
%}

###

POST {{endpoint}}/blogposts
Accept: application/json
Content-Type: application/json

{
"content": "Das ist ein Test"
}

> {%
client.test("BlogPosts returned", () => {
client.assert(response.status === 400);
});
%}
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.sample.schulung.spring.blog.boundary;

import jakarta.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
void handleValidationException() {
// do nothing
// return ResponseEntity, Fehler-Object (JSON), ProblemDetail
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.sample.schulung.spring.blog.domain;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -13,7 +15,11 @@
public class BlogPost {

private UUID id;
@Size(min = 3)
@NotNull
private String title;
@Size(min = 10)
@NotNull
private String content;
private LocalDateTime timestamp;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package de.sample.schulung.spring.blog.domain;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.HashMap;
Expand All @@ -9,6 +12,7 @@
import java.util.UUID;
import java.util.stream.Stream;

@Validated
@Service
public class BlogPostService {

Expand All @@ -24,7 +28,7 @@ public Optional<BlogPost> findById(UUID id) {
);
}

public void create(BlogPost blogPost) {
public void create(@Valid @NotNull BlogPost blogPost) {
final var id = UUID.randomUUID();
blogPost.setId(id);
blogPost.setTimestamp(LocalDateTime.now());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ void shouldCreateBlogPostSuccessfully() throws Exception {

}

@Test
void shouldNotCreateInvalidBlogPost() throws Exception {
mvc.perform(
post("/blogposts")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"content": "Das ist ein Test"
}
""")
)
.andExpect(status().isBadRequest());

}

/*
* Testfall:
* - Lege BlogPost an (title: "test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.util.Optional;
import java.util.UUID;

import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -58,6 +60,23 @@ void shouldReturnSingleBlogPostSuccessfully() throws Exception {
.andExpect(jsonPath("$.timestamp").exists());
}

@Test
void shouldNotCreateInvalidBlogPost() throws Exception {
mvc.perform(
post("/blogposts")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"content": "Das ist ein Test"
}
""")
)
.andExpect(status().isBadRequest());
verifyNoInteractions(service);

}


// TODO wenn ID im Request mitgeschickt wird -> ??

Expand Down

0 comments on commit 3895546

Please sign in to comment.