Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-10: Create a GET existing actors by name comparison endpoint and cover it with tests. #21

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.backend.model.Actor;
import com.example.backend.service.ActorService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

Expand All @@ -20,14 +21,13 @@ public class ActorController {
@PostMapping
public ActorResponse save(@RequestBody @NotNull CreateActorRequest request) {
Actor actor = request.toActor();
actorService.createActor(actor);

return ActorResponse.from(actor);
return ActorResponse.from(actorService.createActor(actor));
}

@GetMapping("/{id}")
public void get() {
// TODO
@GetMapping("/autocompletion/{prefix}")
public List<ActorResponse> getByName(@PathVariable @NonNull String prefix) {
return actorService.getActorsByPrefix(prefix).stream().map(ActorResponse::from).toList();
}

@PutMapping
Expand Down
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 @@ -3,6 +3,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ActorRepository extends JpaRepository<Actor, Long> {
List<Actor> findByNameStartingWith(String prefix);
}
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 All @@ -20,4 +19,8 @@ public Actor createActor(Actor actor) {
public List<Actor> getAllActors() {
return actorRepository.findAll();
}

public List<Actor> getActorsByPrefix(String prefix) {
return actorRepository.findByNameStartingWith(prefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,33 @@
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.*;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@SpringBootTest
@AutoConfigureMockMvc
class ActorControllerTest {
private static final String URL_BASE = "/api/actor";
private static final String URL_AUTOCOMPLETION = "/api/actor/autocompletion/{prefix}";

private static final long ID_FIRST = 1L;
private static final long ID_SECOND = 2L;

private static final String NAME_JANE = "Jane Doe";
private static final String NAME_JIM = "Jim Doe";
private static final String NAME_JOE = "Joe Doe";
private static final String NAME_JOHN = "John Doe";

@Autowired
private MockMvc mvc;

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

@Test
@DirtiesContext
void save() throws Exception {
void saveTest_correct() throws Exception {
mvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"name": "%s"
}
""".formatted(NAME_JOHN)
))
.andExpect(MockMvcResultMatchers.status().isOk());

List<Actor> actual = repository.findAll();
List<Actor> expected = List.of(Actor.builder().id(ID_FIRST).movies(Set.of()).name(NAME_JOHN).build());
assertEquals(expected, actual);
}

@Test
@DirtiesContext
void saveTest_correct_idIgnored() throws Exception {
mvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "%s",
"name": "%s"
}
""".formatted(ID_SECOND, NAME_JANE)
))
.andExpect(MockMvcResultMatchers.status().isOk());

List<Actor> actual = repository.findAll();
List<Actor> expected = List.of(Actor.builder().id(ID_FIRST).movies(Set.of()).name(NAME_JANE).build());
assertEquals(expected, actual);
}

@Test
@DirtiesContext
void saveTest_incorrectNoName() throws Exception {
mvc.perform(MockMvcRequestBuilders.post(URL_BASE)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "%s"
}
""".formatted(ID_FIRST)
))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());

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

@Test
Expand All @@ -45,4 +112,55 @@ void getAll() {
@Test
void delete() {
}

@Test
@DirtiesContext
void getByNameTest_multipleMatch() throws Exception {

repository.saveAll(
List.of(
Actor.builder().name(NAME_JANE).build(),
Actor.builder().name(NAME_JIM).build(),
Actor.builder().name(NAME_JOE).build(),
Actor.builder().name(NAME_JOHN).build()
)
);

mvc.perform(MockMvcRequestBuilders.get(URL_AUTOCOMPLETION, "Jo"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].name").value(NAME_JOE))
.andExpect(jsonPath("$[1].name").value(NAME_JOHN));
}

@Test
@DirtiesContext
void getByNameTest_noMatch() throws Exception {
repository.saveAll(
List.of(
Actor.builder().name(NAME_JANE).build(),
Actor.builder().name(NAME_JOE).build(),
Actor.builder().name(NAME_JOHN).build()
)
);

mvc.perform(MockMvcRequestBuilders.get(URL_AUTOCOMPLETION, "Ji"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$", hasSize(0)));
}

@Test
@DirtiesContext
void getByNameTest_emptyRequest() throws Exception {
repository.saveAll(
List.of(
Actor.builder().name(NAME_JANE).build(),
Actor.builder().name(NAME_JIM).build(),
Actor.builder().name(NAME_JOHN).build()
)
);

mvc.perform(MockMvcRequestBuilders.get(URL_AUTOCOMPLETION, ""))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}
}