Skip to content

Commit

Permalink
Merge pull request #30 from neuefische/ISSUE-13
Browse files Browse the repository at this point in the history
ISSUE-13: Create a DELETE delete relation actor-movie-relation endpoint and cover it with tests.
  • Loading branch information
Krisssssssssssssssssssssss authored Oct 17, 2024
2 parents a136757 + d665088 commit b443315
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ build/
### VS Code ###
.vscode/

.env
### Local ###
.env
./docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ public class MovieActorController {
private final MovieActorService movieActorService;

@PostMapping()
public void addActorById(@RequestBody @NonNull MovieActorRequest movieActorRequest) {
public void addRelation(@RequestBody @NonNull MovieActorRequest movieActorRequest) {
movieActorService.addActor(movieActorRequest.movieId(), movieActorRequest.actorId());
}

@DeleteMapping()
public void deleteRelation(@RequestBody @NonNull MovieActorRequest movieActorRequest) {
movieActorService.removeActor(movieActorRequest.movieId(), movieActorRequest.actorId());
}

@GetMapping("/{movieId}")
public List<ActorResponse> getActorsByMovieId(@PathVariable @NonNull Long movieId) {
return movieActorService.getActorsByMovieId(movieId).stream().map(ActorResponse::from).toList();
Expand Down
1 change: 0 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 @@ -18,6 +18,5 @@ public class Actor {
@GeneratedValue
private Long id;

@Column(nullable = false)
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface MovieActorRelationRepository extends JpaRepository<MovieActorRelation, Long> {
List<MovieActorRelation> findByMovieId(Long movieId);
Optional<MovieActorRelation> findByMovieIdAndActorId(Long movieId, Long actorId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public void addActor(Long movieId, Long actorId) {
Actor actor = actorRepository.findById(actorId).orElseThrow();
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actor).movie(movie).actor(actor).build());
}

public void removeActor(Long movieId, Long actorId) {
MovieActorRelation relation = movieActorRelationRepository
.findByMovieIdAndActorId(actorId, movieId)
.orElseThrow();

movieActorRelationRepository.delete(relation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@SpringBootTest
Expand All @@ -42,7 +43,7 @@ class MovieActorControllerTest {
private ActorRepository actorRepository;

@Autowired
private MovieActorRelationRepository actorMovieRelationRepository;
private MovieActorRelationRepository movieActorRelationRepository;

@Test
@DirtiesContext
Expand All @@ -55,7 +56,7 @@ void getByMovieIdTest_multipleMatch() throws Exception {
Actor actorJohn = Actor.builder().name(ACTOR_NAME_JOHN).build();
movieRepository.saveAll(List.of(movieFirst, movieSecond));
actorRepository.saveAll(List.of(actorJane, actorJim, actorJoe, actorJohn));
actorMovieRelationRepository.saveAll(
movieActorRelationRepository.saveAll(
List.of(
MovieActorRelation.builder().actor(actorJane).movie(movieFirst).build(),
MovieActorRelation.builder().actor(actorJoe).movie(movieSecond).build(),
Expand Down Expand Up @@ -85,7 +86,7 @@ void getByMovieIdTest_noMatch() throws Exception {
Actor actorJohn = Actor.builder().name(ACTOR_NAME_JOHN).build();
movieRepository.saveAll(List.of(movieFirst, movieSecond));
actorRepository.saveAll(List.of(actorJane, actorJim, actorJoe, actorJohn));
actorMovieRelationRepository.saveAll(
movieActorRelationRepository.saveAll(
List.of(
MovieActorRelation.builder().actor(actorJane).movie(movieFirst).build(),
MovieActorRelation.builder().actor(actorJoe).movie(movieFirst).build(),
Expand Down Expand Up @@ -126,7 +127,7 @@ void addActorById_successFull() throws Exception {
)
.andExpect(MockMvcResultMatchers.status().isOk());

List<MovieActorRelation> actualActorIdList = actorMovieRelationRepository.findByMovieId(movie.getId());
List<MovieActorRelation> actualActorIdList = movieActorRelationRepository.findByMovieId(movie.getId());
assertEquals(1, actualActorIdList.size());
assertEquals(actor.getId(), actualActorIdList.getFirst().getActor().getId());
}
Expand All @@ -138,7 +139,7 @@ void addActorById_alreadyAdded() throws Exception {
Actor actor = Actor.builder().name(ACTOR_NAME_JANE).build();
movieRepository.save(movie);
actorRepository.save(actor);
actorMovieRelationRepository.save(MovieActorRelation.builder().actor(actor).movie(movie).build());
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actor).movie(movie).build());

mockMvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -197,4 +198,69 @@ void addActorById_noSuchMovie() throws Exception {

.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}

@Test
@DirtiesContext
void removeActor_successful() throws Exception {
Movie movieFirst = Movie.builder().name(MOVIE_NAME_MEMENTO).build();
Movie movieSecond = Movie.builder().name(MOVIE_NAME_MEMENTO).build();
Actor actorJane = Actor.builder().name(ACTOR_NAME_JANE).build();
Actor actorJim = Actor.builder().name(ACTOR_NAME_JANE).build();
movieRepository.save(movieFirst);
movieRepository.save(movieSecond);
actorRepository.save(actorJane);
actorRepository.save(actorJim);
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actorJane).movie(movieFirst).build());
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actorJim).movie(movieFirst).build());
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actorJane).movie(movieSecond).build());
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actorJim).movie(movieSecond).build());

mockMvc.perform(MockMvcRequestBuilders.delete(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"actorId": "%s",
"movieId": "%s"
}
""".formatted(actorJane.getId(), movieFirst.getId())
))
.andExpect(MockMvcResultMatchers.status().isOk());

assertTrue(
movieActorRelationRepository.findByMovieIdAndActorId(movieFirst.getId(), actorJane.getId()).isEmpty()
);
assertEquals(
3,
movieActorRelationRepository.findAll().size()
);
}

@Test
@DirtiesContext
void removeActor_noSuchRelation() throws Exception {
Movie movieFirst = Movie.builder().name(MOVIE_NAME_MEMENTO).build();
Movie movieSecond = Movie.builder().name(MOVIE_NAME_MEMENTO).build();
Actor actorJane = Actor.builder().name(ACTOR_NAME_JANE).build();
Actor actorJim = Actor.builder().name(ACTOR_NAME_JANE).build();
movieRepository.save(movieFirst);
movieRepository.save(movieSecond);
actorRepository.save(actorJane);
actorRepository.save(actorJim);
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actorJim).movie(movieFirst).build());
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actorJane).movie(movieSecond).build());
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actorJim).movie(movieSecond).build());

mockMvc.perform(MockMvcRequestBuilders.delete(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"actorId": "%s",
"movieId": "%s"
}
""".formatted(actorJane.getId(), movieFirst.getId())
))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}
}

0 comments on commit b443315

Please sign in to comment.