Skip to content

Commit

Permalink
chore: add delete customer endpoint (#15)
Browse files Browse the repository at this point in the history
resolves: #6
  • Loading branch information
alvinmarshall authored Jul 28, 2024
1 parent e24b1f8 commit a8a3772
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
- 'POSTGRES_PASSWORD=postgres'
- 'POSTGRES_USER=postgres'
ports:
- '5432'
- '5432:5432'
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ ResponseEntity<Object> updateCustomer(@PathVariable("id") Long id, @RequestBody
return ResponseEntity.ok(CustomerDto.toCustomer(customer));
}

@DeleteMapping("{id}")
ResponseEntity<Object> deleteCustomer(@PathVariable("id") Long id) {
customerService.deleteCustomer(id);
return ResponseEntity.noContent().build();
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/cheise_proj/auditing/CustomerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ Customer updateCustomer(Long customerId, CustomerDto.UpdateCustomer updateCustom
return customerRepository.save(customer);
}

void deleteCustomer(Long id) {
customerRepository.deleteById(id);
}

}
18 changes: 18 additions & 0 deletions src/test/java/com/cheise_proj/auditing/CustomerControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,22 @@ void updateCustomer_returns_404() throws Exception {
).andExpectAll(MockMvcResultMatchers.status().isNotFound())
.andDo(result -> log.info("result: {}", result.getResponse().getContentAsString()));
}

@Test
void deleteCustomer_returns_204() throws Exception {
String customerLocation = (String) mockMvc.perform(MockMvcRequestBuilders.post("/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(CustomerFixture.createCustomerWithAddress(objectMapper))

).andExpectAll(MockMvcResultMatchers.status().isCreated())
.andDo(result -> log.info("result: {}", result.getResponse().getHeaderValue("location")))
.andReturn().getResponse().getHeaderValue("location");

assert customerLocation != null;
mockMvc.perform(MockMvcRequestBuilders.delete(customerLocation)
.contentType(MediaType.APPLICATION_JSON)

).andExpectAll(MockMvcResultMatchers.status().isNoContent())
.andDo(result -> log.info("result: {}", result.getResponse().getContentAsString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,10 @@ void updateCustomer_throw_if_customer_not_found() {
assertEquals("Customer with id 1 not found", exception.getMessage());
}

@Test
void deleteCustomer() {
Mockito.doNothing().when(customerRepository).deleteById(ArgumentMatchers.anyLong());
sut.deleteCustomer(1L);
Mockito.verify(customerRepository, Mockito.atMostOnce()).deleteById(ArgumentMatchers.anyLong());
}
}

0 comments on commit a8a3772

Please sign in to comment.