Skip to content
Open
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 @@ -39,10 +39,10 @@ class ZooControllerTest {

@BeforeEach
void setUp() {
testZoo = new Zoo(null, "Manila Zoo", "Manila, Philippines",
"A beautiful zoo in the heart of Manila", new ArrayList<>(), new ArrayList<>());
createdZoo = new Zoo(1L, "Manila Zoo", "Manila, Philippines",
"A beautiful zoo in the heart of Manila", new ArrayList<>(), new ArrayList<>());
testZoo = new Zoo(null, "Manila Zoo", "Manila, Philippines",
"A beautiful zoo in the heart of Manila", new ArrayList<>(), new ArrayList<>());
createdZoo = new Zoo(1L, "Manila Zoo", "Manila, Philippines",
"A beautiful zoo in the heart of Manila", new ArrayList<>(), new ArrayList<>());
}

@Test
Expand Down Expand Up @@ -88,13 +88,17 @@ void shouldReturnZooWhenItExists() throws Exception {
// - jsonPath("$.name").value("Manila Zoo")
// - jsonPath("$.location").value("Manila, Philippines")
// - jsonPath("$.description").value("A beautiful zoo in the heart of Manila")

// Your code here:
// when(zooService.getZooById(1L)).thenReturn(createdZoo);
// mockMvc.perform(get("/api/zoos/1"))
// .andExpect(...)
// .andExpect(...)
// .andExpect(...);
when(zooService.getZooById(1L)).thenReturn(createdZoo);

mockMvc.perform(get("/api/zoos/1"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("Manila Zoo"))
.andExpect(jsonPath("$.location").value("Manila, Philippines"))
.andExpect(jsonPath("$.description").value("A beautiful zoo in the heart of Manila"));
}

@Test
Expand Down Expand Up @@ -140,16 +144,20 @@ void shouldUpdateZooSuccessfully() throws Exception {
// - jsonPath("$.name").value("Updated Zoo Name")
// - jsonPath("$.location").value("Updated Location")
// - jsonPath("$.description").value("Updated description")

// Your code here:
// Zoo updatedZoo = new Zoo(1L, "Updated Zoo Name", "Updated Location", "Updated description", new ArrayList<>(), new ArrayList<>());
// when(zooService.updateZoo(eq(1L), any(Zoo.class))).thenReturn(updatedZoo);
// mockMvc.perform(put("/api/zoos/1")
// .contentType(MediaType.APPLICATION_JSON)
// .content(objectMapper.writeValueAsString(testZoo)))
// .andExpect(...)
// .andExpect(...)
// .andExpect(...);
Zoo updatedZoo = new Zoo(1L, "Updated Zoo Name", "Updated Location", "Updated description", new ArrayList<>(), new ArrayList<>());
when(zooService.updateZoo(eq(1L), any(Zoo.class))).thenReturn(updatedZoo);

mockMvc.perform(put("/api/zoos/1")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(testZoo)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.name").value("Updated Zoo Name"))
.andExpect(jsonPath("$.location").value("Updated Location"))
.andExpect(jsonPath("$.description").value("Updated description"));
}

@Test
Expand All @@ -159,13 +167,13 @@ void shouldReturn404WhenUpdatingNonExistentZoo() throws Exception {
// 1. Set up the mock to throw an exception: when(zooService.updateZoo(eq(999L), any(Zoo.class))).thenThrow(new IllegalArgumentException("Zoo not found with id: 999"));
// 2. Use mockMvc.perform(put("/api/zoos/999").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(testZoo)))
// 3. Add expectation for status().isNotFound()

// Your code here:
// when(zooService.updateZoo(eq(999L), any(Zoo.class))).thenThrow(new IllegalArgumentException("Zoo not found with id: 999"));
// mockMvc.perform(put("/api/zoos/999")
// .contentType(MediaType.APPLICATION_JSON)
// .content(objectMapper.writeValueAsString(testZoo)))
// .andExpect(...);
when(zooService.updateZoo(eq(999L), any(Zoo.class))).thenThrow(new IllegalArgumentException("Zoo not found with id: 999"));
mockMvc.perform(put("/api/zoos/999")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(testZoo)))
.andExpect(status().isNotFound());
}

@Test
Expand All @@ -175,11 +183,11 @@ void shouldDeleteZooSuccessfully() throws Exception {
// 1. Set up the mock: doNothing().when(zooService).deleteZoo(1L);
// 2. Use mockMvc.perform(delete("/api/zoos/1")) to make a DELETE request
// 3. Add expectation for status().isNoContent()

// Your code here:
// doNothing().when(zooService).deleteZoo(1L);
// mockMvc.perform(delete("/api/zoos/1"))
// .andExpect(...);
doNothing().when(zooService).deleteZoo(1L);
mockMvc.perform(delete("/api/zoos/1"))
.andExpect(status().isNoContent());
}

@Test
Expand All @@ -189,11 +197,11 @@ void shouldReturn404WhenDeletingNonExistentZoo() throws Exception {
// 1. Set up the mock to throw an exception: doThrow(new IllegalArgumentException("Zoo not found with id: 999")).when(zooService).deleteZoo(999L);
// 2. Use mockMvc.perform(delete("/api/zoos/999")) to make a DELETE request
// 3. Add expectation for status().isNotFound()

// Your code here:
// doThrow(new IllegalArgumentException("Zoo not found with id: 999")).when(zooService).deleteZoo(999L);
// mockMvc.perform(delete("/api/zoos/999"))
// .andExpect(...);
doThrow(new IllegalArgumentException("Zoo not found with id: 999")).when(zooService).deleteZoo(999L);
mockMvc.perform(delete("/api/zoos/999"))
.andExpect(status().isNotFound());
}

@Test
Expand All @@ -212,12 +220,12 @@ void shouldHandleMalformedJsonInPutRequest() throws Exception {
// TODO: Complete this test
// 1. Use mockMvc.perform(put("/api/zoos/1").contentType(MediaType.APPLICATION_JSON).content("{ invalid json }"))
// 2. Add expectation for status().isBadRequest()

// Your code here:
// mockMvc.perform(put("/api/zoos/1")
// .contentType(MediaType.APPLICATION_JSON)
// .content("{ invalid json }"))
// .andExpect(...);
mockMvc.perform(put("/api/zoos/1")
.contentType(MediaType.APPLICATION_JSON)
.content("{ invalid json }"))
.andExpect(status().isBadRequest());
}

@Test
Expand Down Expand Up @@ -250,4 +258,4 @@ void shouldReturnProperContentTypeForAllResponses() throws Exception {
mockMvc.perform(delete("/api/zoos/1"))
.andExpect(status().isNoContent());
}
}
}
70 changes: 35 additions & 35 deletions lab3/src/test/java/com/tddacademy/zoo/service/ZooServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ void shouldUpdateZooWhenExists() {
// 7. Verify that zooRepository.save was called once

// Your code here:
// Long zooId = 1L;
// manilaZoo.setId(zooId);
// Zoo updatedZoo = new Zoo("Updated Manila Zoo", "Updated Location", "Updated description");
//
// when(zooRepository.findById(zooId)).thenReturn(Optional.of(manilaZoo));
// when(zooRepository.save(any(Zoo.class))).thenReturn(updatedZoo);
//
// Zoo result = zooService.updateZoo(zooId, updatedZoo);
//
// assertEquals("Updated Manila Zoo", result.getName());
// verify(zooRepository, times(1)).save(any(Zoo.class));
Long zooId = 1L;
manilaZoo.setId(zooId);
Zoo updatedZoo = new Zoo("Updated Manila Zoo", "Updated Location", "Updated description");

when(zooRepository.findById(zooId)).thenReturn(Optional.of(manilaZoo));
when(zooRepository.save(any(Zoo.class))).thenReturn(updatedZoo);

Zoo result = zooService.updateZoo(zooId, updatedZoo);

assertEquals("Updated Manila Zoo", result.getName());
verify(zooRepository, times(1)).save(any(Zoo.class));
}

@Test
Expand All @@ -134,16 +134,16 @@ void shouldThrowExceptionWhenUpdatingNonExistentZoo() {
// 4. Verify the exception message contains "Zoo not found with id: 999"

// Your code here:
// Long zooId = 999L;
// Zoo updatedZoo = new Zoo("Updated Zoo", "Updated Location", "Updated description");
//
// when(zooRepository.findById(zooId)).thenReturn(Optional.empty());
//
// IllegalArgumentException exception = assertThrows(
// IllegalArgumentException.class,
// () -> zooService.updateZoo(zooId, updatedZoo)
// );
// assertTrue(exception.getMessage().contains("Zoo not found with id: 999"));
Long zooId = 999L;
Zoo updatedZoo = new Zoo("Updated Zoo", "Updated Location", "Updated description");

when(zooRepository.findById(zooId)).thenReturn(Optional.empty());

IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> zooService.updateZoo(zooId, updatedZoo)
);
assertTrue(exception.getMessage().contains("Zoo not found with id: 999"));
}

@Test
Expand All @@ -155,12 +155,12 @@ void shouldDeleteZooWhenExists() {
// 3. Verify that zooRepository.deleteById(1L) was called once

// Your code here:
// Long zooId = 1L;
// when(zooRepository.existsById(zooId)).thenReturn(true);
//
// zooService.deleteZoo(zooId);
//
// verify(zooRepository, times(1)).deleteById(zooId);
Long zooId = 1L;
when(zooRepository.existsById(zooId)).thenReturn(true);

zooService.deleteZoo(zooId);

verify(zooRepository, times(1)).deleteById(zooId);
}

@Test
Expand All @@ -172,14 +172,14 @@ void shouldThrowExceptionWhenDeletingNonExistentZoo() {
// 3. Verify the exception message contains "Zoo not found with id: 999"

// Your code here:
// Long zooId = 999L;
// when(zooRepository.existsById(zooId)).thenReturn(false);
//
// IllegalArgumentException exception = assertThrows(
// IllegalArgumentException.class,
// () -> zooService.deleteZoo(zooId)
// );
// assertTrue(exception.getMessage().contains("Zoo not found with id: 999"));
Long zooId = 999L;
when(zooRepository.existsById(zooId)).thenReturn(false);

IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> zooService.deleteZoo(zooId)
);
assertTrue(exception.getMessage().contains("Zoo not found with id: 999"));
}

@Test
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading