-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[conluz-68] Implemented endponit to delete a plant
- Loading branch information
1 parent
c2f9a54
commit 74c5038
Showing
9 changed files
with
243 additions
and
2 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...ain/java/org/lucoenergia/conluz/domain/production/plant/delete/DeletePlantRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.lucoenergia.conluz.domain.production.plant.delete; | ||
|
||
import org.lucoenergia.conluz.domain.shared.PlantId; | ||
|
||
public interface DeletePlantRepository { | ||
|
||
void delete(PlantId id); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/lucoenergia/conluz/domain/production/plant/delete/DeletePlantService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.lucoenergia.conluz.domain.production.plant.delete; | ||
|
||
import org.lucoenergia.conluz.domain.shared.PlantId; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class DeletePlantService { | ||
|
||
private final DeletePlantRepository repository; | ||
|
||
public DeletePlantService(DeletePlantRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public void delete(PlantId id) { | ||
repository.delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
.../org/lucoenergia/conluz/infrastructure/production/plant/delete/DeletePlantController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.lucoenergia.conluz.infrastructure.production.plant.delete; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import org.lucoenergia.conluz.domain.production.plant.delete.DeletePlantService; | ||
import org.lucoenergia.conluz.domain.shared.PlantId; | ||
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.ApiTag; | ||
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.response.*; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.UUID; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1") | ||
public class DeletePlantController { | ||
|
||
private final DeletePlantService service; | ||
|
||
public DeletePlantController(DeletePlantService service) { | ||
this.service = service; | ||
} | ||
|
||
@DeleteMapping("/plants/{id}") | ||
@Operation( | ||
summary = "Removes a plant by ID", | ||
description = """ | ||
This endpoint enables the removal of a plant from the system by specifying the plant's unique identifier within the endpoint path. | ||
To utilize this endpoint, clients send a DELETE request with the targeted plant's ID, requiring authentication for secure access. | ||
Upon successful deletion, the server responds with an HTTP status code of 200, indicating that the plant has been successfully removed. | ||
In cases where the deletion process encounters errors, the server returns an appropriate error status code, along with a descriptive error message to guide clients in diagnosing and addressing the issue. | ||
""", | ||
tags = ApiTag.PLANTS, | ||
operationId = "deletePlant" | ||
) | ||
@ApiResponses(value = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "Plant deleted successfully" | ||
) | ||
}) | ||
@ForbiddenErrorResponse | ||
@UnauthorizedErrorResponse | ||
@BadRequestErrorResponse | ||
@InternalServerErrorResponse | ||
@NotFoundErrorResponse | ||
public void deletePlant(@PathVariable("id") UUID plantId) { | ||
service.delete(PlantId.of(plantId)); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...oenergia/conluz/infrastructure/production/plant/delete/DeletePlantRepositoryDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.lucoenergia.conluz.infrastructure.production.plant.delete; | ||
|
||
import org.lucoenergia.conluz.domain.production.plant.PlantNotFoundException; | ||
import org.lucoenergia.conluz.domain.production.plant.delete.DeletePlantRepository; | ||
import org.lucoenergia.conluz.domain.shared.PlantId; | ||
import org.lucoenergia.conluz.infrastructure.production.plant.PlantEntity; | ||
import org.lucoenergia.conluz.infrastructure.production.plant.PlantRepository; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Transactional | ||
@Repository | ||
public class DeletePlantRepositoryDatabase implements DeletePlantRepository { | ||
|
||
private final PlantRepository plantRepository; | ||
|
||
public DeletePlantRepositoryDatabase(PlantRepository plantRepository) { | ||
this.plantRepository = plantRepository; | ||
} | ||
|
||
@Override | ||
public void delete(PlantId id) { | ||
Optional<PlantEntity> entity = plantRepository.findById(id.getId()); | ||
if (entity.isEmpty()) { | ||
throw new PlantNotFoundException(id); | ||
} | ||
plantRepository.delete(entity.get()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
.../lucoenergia/conluz/infrastructure/production/plant/delete/DeletePlantControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.lucoenergia.conluz.infrastructure.production.plant.delete; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.lucoenergia.conluz.domain.admin.user.User; | ||
import org.lucoenergia.conluz.domain.admin.user.UserMother; | ||
import org.lucoenergia.conluz.domain.admin.user.create.CreateUserRepository; | ||
import org.lucoenergia.conluz.domain.production.plant.Plant; | ||
import org.lucoenergia.conluz.domain.production.plant.PlantMother; | ||
import org.lucoenergia.conluz.domain.production.plant.create.CreatePlantRepository; | ||
import org.lucoenergia.conluz.domain.production.plant.get.GetPlantRepository; | ||
import org.lucoenergia.conluz.domain.shared.PlantId; | ||
import org.lucoenergia.conluz.domain.shared.UserId; | ||
import org.lucoenergia.conluz.infrastructure.shared.BaseControllerTest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@Transactional | ||
class DeletePlantControllerTest extends BaseControllerTest { | ||
|
||
@Autowired | ||
private CreateUserRepository createUserRepository; | ||
@Autowired | ||
private CreatePlantRepository createPlantRepository; | ||
@Autowired | ||
private GetPlantRepository getPlantRepository; | ||
|
||
@Test | ||
void testDelete() throws Exception { | ||
|
||
// Create two users | ||
User userOne = UserMother.randomUser(); | ||
createUserRepository.create(userOne); | ||
User userTwo = UserMother.randomUser(); | ||
createUserRepository.create(userTwo); | ||
|
||
// Create three supplies | ||
Plant plantOne = PlantMother.random(userOne).withCode("TS-456789").build(); | ||
createPlantRepository.create(plantOne, UserId.of(userOne.getId())); | ||
Plant plantTwo = PlantMother.random(userOne).withCode("TS-123456").build(); | ||
plantTwo = createPlantRepository.create(plantTwo, UserId.of(userOne.getId())); | ||
Plant plantThree = PlantMother.random(userTwo).withCode("TS-789456").build(); | ||
createPlantRepository.create(plantThree, UserId.of(userTwo.getId())); | ||
|
||
// Login as default admin | ||
String authHeader = loginAsDefaultAdmin(); | ||
|
||
mockMvc.perform(delete(String.format("/api/v1/plants/%s", plantTwo.getId())) | ||
.header(HttpHeaders.AUTHORIZATION, authHeader) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()); | ||
Assertions.assertTrue(getPlantRepository.findById(PlantId.of(plantTwo.getId())).isEmpty()); | ||
} | ||
|
||
@Test | ||
void testWithUnknown() throws Exception { | ||
|
||
String authHeader = loginAsDefaultAdmin(); | ||
|
||
final String plantId = UUID.randomUUID().toString(); | ||
|
||
mockMvc.perform(delete("/api/v1/plants/" + plantId) | ||
.header(HttpHeaders.AUTHORIZATION, authHeader) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.timestamp").isNotEmpty()) | ||
.andExpect(jsonPath("$.status").value(HttpStatus.BAD_REQUEST.value())) | ||
.andExpect(jsonPath("$.message").isNotEmpty()) | ||
.andExpect(jsonPath("$.traceId").isNotEmpty()); | ||
} | ||
|
||
@Test | ||
void testWithoutIdInPath() throws Exception { | ||
final String authHeader = loginAsDefaultAdmin(); | ||
|
||
mockMvc.perform(delete("/api/v1/plants") | ||
.header(HttpHeaders.AUTHORIZATION, authHeader) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.timestamp").isNotEmpty()) | ||
.andExpect(jsonPath("$.status").value(HttpStatus.BAD_REQUEST.value())) | ||
.andExpect(jsonPath("$.message").isNotEmpty()) | ||
.andExpect(jsonPath("$.traceId").isNotEmpty()); | ||
} | ||
|
||
@Test | ||
void testWithoutToken() throws Exception { | ||
|
||
mockMvc.perform(delete("/api/v1/plants") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andDo(print()) | ||
.andExpect(status().isUnauthorized()) | ||
.andExpect(jsonPath("$.timestamp").isNotEmpty()) | ||
.andExpect(jsonPath("$.status").value(HttpStatus.UNAUTHORIZED.value())) | ||
.andExpect(jsonPath("$.message").isNotEmpty()) | ||
.andExpect(jsonPath("$.traceId").isNotEmpty()); | ||
} | ||
} |