Skip to content

Commit

Permalink
ISSUE-12: Create a POST create new relation actor-movie-relation endp…
Browse files Browse the repository at this point in the history
…oint and cover it with tests.
  • Loading branch information
pavlo-bystrytskyi committed Oct 17, 2024
1 parent 7b41344 commit 0f90841
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.backend.controller;

import com.example.backend.dto.ActorResponse;
import com.example.backend.dto.IdRequest;
import com.example.backend.service.ActorMovieService;
import com.example.backend.dto.MovieActorRequest;
import com.example.backend.service.MovieActorService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.http.HttpMethod;
Expand All @@ -13,19 +13,19 @@

@RestController
@AllArgsConstructor
@RequestMapping("/api/actor-movie")
public class ActorMovieController {
@RequestMapping("/api/movie-actor")
public class MovieActorController {

private final ActorMovieService actorMovieService;
private final MovieActorService movieActorService;

@PostMapping("/{movieId}")
public void addActorById(@PathVariable @NonNull Long movieId, @RequestBody IdRequest idRequest) {
actorMovieService.addActor(movieId, idRequest.id());
@PostMapping()
public void addActorById(@RequestBody @NonNull MovieActorRequest movieActorRequest) {
movieActorService.addActor(movieActorRequest.movieId(), movieActorRequest.actorId());
}

@GetMapping("/{movieId}")
public List<ActorResponse> getActorsByMovieId(@PathVariable @NonNull Long movieId) {
return actorMovieService.getActorsByMovieId(movieId).stream().map(ActorResponse::from).toList();
return movieActorService.getActorsByMovieId(movieId).stream().map(ActorResponse::from).toList();
}

/**
Expand Down
6 changes: 0 additions & 6 deletions backend/src/main/java/com/example/backend/dto/IdRequest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.backend.dto;

import lombok.NonNull;

public record MovieActorRequest(@NonNull Long movieId, @NonNull Long actorId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@UniqueConstraint(columnNames = {"actor_id", "movie_id"})
}
)
public class ActorMovieRelation {
public class MovieActorRelation {
@Id
@GeneratedValue
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
import java.util.List;

@Repository
public interface ActorMovieRelationRepository extends JpaRepository<ActorMovieRelation, Long> {
List<ActorMovieRelation> findByMovieId(Long movieId);
public interface MovieActorRelationRepository extends JpaRepository<MovieActorRelation, Long> {
List<MovieActorRelation> findByMovieId(Long movieId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.backend.service;

import com.example.backend.model.Actor;
import com.example.backend.model.ActorMovieRelation;
import com.example.backend.model.ActorMovieRelationRepository;
import com.example.backend.model.ActorRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@

@Service
@RequiredArgsConstructor
public class ActorMovieService {
private final ActorMovieRelationRepository actorMovieRelationRepository;
public class MovieActorService {
private final MovieActorRelationRepository movieActorRelationRepository;

private final ActorRepository actorRepository;

private final MovieRepository movieRepository;

public List<Actor> getActorsByMovieId(Long movieId) {
return actorMovieRelationRepository.findByMovieId(movieId)
return movieActorRelationRepository.findByMovieId(movieId)
.stream()
.map(ActorMovieRelation::getActor)
.map(MovieActorRelation::getActor)
.toList();
}

public void addActor(Long movieId, Long actorId) {
Movie movie = movieRepository.findById(movieId).orElseThrow();
Actor actor = actorRepository.findById(actorId).orElseThrow();
actorMovieRelationRepository.save(ActorMovieRelation.builder().actor(actor).movie(movie).actor(actor).build());
movieActorRelationRepository.save(MovieActorRelation.builder().actor(actor).movie(movie).actor(actor).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ActorControllerTest {
private MovieRepository movieRepository;

@Autowired
private ActorMovieRelationRepository actorMovieRelationRepository;
private MovieActorRelationRepository movieActorRelationRepository;

@Test
@DirtiesContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

@SpringBootTest
@AutoConfigureMockMvc
class ActorMovieControllerTest {
private static final String URL_ACTOR_MOVIE_BASE = "/api/actor-movie/{movieId}";
class MovieActorControllerTest {
private static final String URL_BASE = "/api/movie-actor";
private static final String URL_WITH_ID = "/api/movie-actor/{movieId}";

private static final String ACTOR_NAME_JANE = "Jane Doe";
private static final String ACTOR_NAME_JIM = "Jim Doe";
Expand All @@ -41,7 +42,7 @@ class ActorMovieControllerTest {
private ActorRepository actorRepository;

@Autowired
private ActorMovieRelationRepository actorMovieRelationRepository;
private MovieActorRelationRepository actorMovieRelationRepository;

@Test
@DirtiesContext
Expand All @@ -56,14 +57,14 @@ void getByMovieIdTest_multipleMatch() throws Exception {
actorRepository.saveAll(List.of(actorJane, actorJim, actorJoe, actorJohn));
actorMovieRelationRepository.saveAll(
List.of(
ActorMovieRelation.builder().actor(actorJane).movie(movieFirst).build(),
ActorMovieRelation.builder().actor(actorJoe).movie(movieSecond).build(),
ActorMovieRelation.builder().actor(actorJim).movie(movieSecond).build(),
ActorMovieRelation.builder().actor(actorJane).movie(movieSecond).build()
MovieActorRelation.builder().actor(actorJane).movie(movieFirst).build(),
MovieActorRelation.builder().actor(actorJoe).movie(movieSecond).build(),
MovieActorRelation.builder().actor(actorJim).movie(movieSecond).build(),
MovieActorRelation.builder().actor(actorJane).movie(movieSecond).build()
)
);

mockMvc.perform(MockMvcRequestBuilders.get(URL_ACTOR_MOVIE_BASE, movieSecond.getId()))
mockMvc.perform(MockMvcRequestBuilders.get(URL_WITH_ID, movieSecond.getId()))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[*].name", containsInAnyOrder(
Expand All @@ -86,21 +87,21 @@ void getByMovieIdTest_noMatch() throws Exception {
actorRepository.saveAll(List.of(actorJane, actorJim, actorJoe, actorJohn));
actorMovieRelationRepository.saveAll(
List.of(
ActorMovieRelation.builder().actor(actorJane).movie(movieFirst).build(),
ActorMovieRelation.builder().actor(actorJoe).movie(movieFirst).build(),
ActorMovieRelation.builder().actor(actorJim).movie(movieFirst).build(),
ActorMovieRelation.builder().actor(actorJohn).movie(movieFirst).build()
MovieActorRelation.builder().actor(actorJane).movie(movieFirst).build(),
MovieActorRelation.builder().actor(actorJoe).movie(movieFirst).build(),
MovieActorRelation.builder().actor(actorJim).movie(movieFirst).build(),
MovieActorRelation.builder().actor(actorJohn).movie(movieFirst).build()
)
);

mockMvc.perform(MockMvcRequestBuilders.get(URL_ACTOR_MOVIE_BASE, movieSecond.getId()))
mockMvc.perform(MockMvcRequestBuilders.get(URL_WITH_ID, movieSecond.getId()))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$", hasSize(0)));
}

@Test
void getByMovieIdTest_emptyRequest() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(URL_ACTOR_MOVIE_BASE, ""))
mockMvc.perform(MockMvcRequestBuilders.get(URL_WITH_ID, ""))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}

Expand All @@ -112,19 +113,20 @@ void addActorById_successFull() throws Exception {
movieRepository.save(movie);
actorRepository.save(actor);

mockMvc.perform(MockMvcRequestBuilders.post(URL_ACTOR_MOVIE_BASE, movie.getId())
mockMvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "%s"
"actorId": "%s",
"movieId": "%s"
}
""".formatted(actor.getId())
""".formatted(actor.getId(), movie.getId())
)
)
.andExpect(MockMvcResultMatchers.status().isOk());

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

mockMvc.perform(MockMvcRequestBuilders.post(URL_ACTOR_MOVIE_BASE, movie.getId())
mockMvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "%s"
"actorId": "%s",
"movieId": "%s"
}
""".formatted(actor.getId())
""".formatted(actor.getId(), movie.getId())
))
.andExpect(MockMvcResultMatchers.status().is5xxServerError());
}
Expand All @@ -159,14 +162,15 @@ void addActorById_noSuchActor() throws Exception {
actorRepository.save(actor);
Long nonExistentActorId = actor.getId() + 1;

mockMvc.perform(MockMvcRequestBuilders.post(URL_ACTOR_MOVIE_BASE, movie.getId())
mockMvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "%s"
"actorId": "%s",
"movieId": "%s",
}
""".formatted(nonExistentActorId)
""".formatted(nonExistentActorId, movie.getId())
))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}
Expand All @@ -180,14 +184,15 @@ void addActorById_noSuchMovie() throws Exception {
actorRepository.save(actor);
Long nonExistentMovieId = movie.getId() + 1L;

mockMvc.perform(MockMvcRequestBuilders.post(URL_ACTOR_MOVIE_BASE, nonExistentMovieId)
mockMvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "%s"
"actorId": "%s",
"movieId": "%s"
}
""".formatted(actor.getId())
""".formatted(actor.getId(), nonExistentMovieId)
))

.andExpect(MockMvcResultMatchers.status().is4xxClientError());
Expand Down

0 comments on commit 0f90841

Please sign in to comment.