Skip to content

Commit

Permalink
Merge pull request #20 from neuefische/ISSUE-11
Browse files Browse the repository at this point in the history
ISSUE-11: Create a POST create new actor endpoint and cover it with tests.
  • Loading branch information
pavlo-bystrytskyi authored Oct 16, 2024
2 parents 63937ed + 5fdb40e commit 7c2aef3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.example.backend.dto;

import com.example.backend.model.Actor;
import lombok.NonNull;

public record CreateActorRequest(
String name
) {
@NonNull String name
) {
public Actor toActor() {
return Actor.builder().name(name).build();
}
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/com/example/backend/model/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class Actor {
@GeneratedValue
private Long id;

@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
private Set<Movie> movies;

@Column(nullable = false)
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Director {
@GeneratedValue
private Long id;

@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
private Set<Movie> movies;

private String name;
Expand Down
8 changes: 4 additions & 4 deletions backend/src/main/java/com/example/backend/model/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class Movie {

private Double rating;

@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
Set<Actor> actors;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
private Set<Actor> actors;

@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
Set<Director> directors;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
private Set<Director> directors;

private String name;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.backend.service;

import com.example.backend.dto.CreateActorRequest;
import com.example.backend.model.Actor;
import com.example.backend.model.ActorRepository;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -27,7 +28,60 @@ class ActorControllerTest {

@Test
@DirtiesContext
void save() throws Exception {
void saveTest_correct() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/api/actor")
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"name": "John Doe"
}
"""
))
.andExpect(MockMvcResultMatchers.status().isOk());

List<Actor> actual = repository.findAll();
List<Actor> expected = List.of(Actor.builder().id(1L).movies(Set.of()).name("John Doe").build());
assertEquals(expected, actual);
}

@Test
@DirtiesContext
void saveTest_correct_idIgnored() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/api/actor")
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "2",
"name": "Jane Doe"
}
"""
))
.andExpect(MockMvcResultMatchers.status().isOk());

List<Actor> actual = repository.findAll();
List<Actor> expected = List.of(Actor.builder().id(1L).movies(Set.of()).name("Jane Doe").build());
assertEquals(expected, actual);
}

@Test
@DirtiesContext
void saveTest_incorrectNoName() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/api/actor")
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "2"
}
"""
))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());

List<Actor> actual = repository.findAll();
List<Actor> expected = List.of();
assertEquals(expected, actual);
}

@Test
Expand Down

0 comments on commit 7c2aef3

Please sign in to comment.