Skip to content

Commit

Permalink
Merge pull request #21 from neuefische/ISSUE-10
Browse files Browse the repository at this point in the history
ISSUE-10: Create a GET existing actors by name comparison endpoint and cover it with tests.
  • Loading branch information
Krisssssssssssssssssssssss authored Oct 16, 2024
2 parents 7c2aef3 + 1b2a9d3 commit d912172
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 35 deletions.
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
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 @@ -19,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 @@ -15,11 +15,24 @@
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 @@ -29,54 +42,54 @@ class ActorControllerTest {
@Test
@DirtiesContext
void saveTest_correct() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/api/actor")
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"name": "John Doe"
}
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(1L).movies(Set.of()).name("John Doe").build());
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("/api/actor")
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "2",
"name": "Jane Doe"
}
"""
))
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(1L).movies(Set.of()).name("Jane Doe").build());
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("/api/actor")
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"id": "2"
}
"""
))
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();
Expand All @@ -99,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());
}
}

0 comments on commit d912172

Please sign in to comment.