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

Commit

Permalink
add POST /blogposts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Dec 18, 2023
1 parent 8938544 commit 9e597dc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
2 changes: 1 addition & 1 deletion http-requests/GET-blogposts.http
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Accept: application/json
###

GET http://localhost:9080/blogposts
Accept: text/plain
Accept: application/xml

> {%
client.test("BlogPosts returned", () => {
Expand Down
14 changes: 14 additions & 0 deletions http-requests/POST-blogposts.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
POST http://localhost:9080/blogposts
Accept: application/json
Content-Type: application/json

{
"title": "test",
"content": "Das ist ein Test"
}

> {%
client.test("BlogPosts returned", () => {
client.assert(response.status === 201);
});
%}
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package de.sample.schulung.spring.blog;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Controller
import java.time.LocalDateTime;
import java.util.UUID;

@RestController
@RequestMapping("/blogposts")
public class BlogPostController {

@GetMapping
@ResponseBody
@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE
)
String sayHello() {
return "Hello World";
}

@PostMapping(
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseStatus(HttpStatus.CREATED)
BlogPostDto createBlogPost(@RequestBody BlogPostDto blogPostDto) {
blogPostDto.setId(UUID.randomUUID());
blogPostDto.setTimestamp(LocalDateTime.now());
return blogPostDto;
}

}
19 changes: 19 additions & 0 deletions src/main/java/de/sample/schulung/spring/blog/BlogPostDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.sample.schulung.spring.blog;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.UUID;

@Data
public class BlogPostDto {

@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private UUID id;
private String title;
private String content;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private LocalDateTime timestamp;

}
47 changes: 45 additions & 2 deletions src/test/java/de/sample/schulung/spring/blog/BlogPostApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
Expand All @@ -23,9 +24,51 @@ void shouldReturn200WhenGetBlogPosts() throws Exception {
get("/blogposts")
.accept(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}

/*
* Testfall:
* - Lege BlogPost an (title: "test")
* - Frage BlogPost ab und prüfe, ob Titel == "test"
*/

@Test
void shouldReturn406WhenGetBlogPostsWithInvalidMediaType() throws Exception {
mvc.perform(
get("/blogposts")
.accept(MediaType.APPLICATION_XML)
)
.andExpect(
status().isOk()
status().isNotAcceptable()
);
}

// TODO wenn zu komplex -> @Nested

@Test
void shouldCreateBlogPostSuccessfully() throws Exception {
mvc.perform(
post("/blogposts")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "test",
"content": "Das ist ein Test"
}
""")

)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.title").value("test"))
.andExpect(jsonPath("$.id").exists())
.andExpect(jsonPath("$.timestamp").exists());
// .title = "test"
// Location-Header
}

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

}

0 comments on commit 9e597dc

Please sign in to comment.