diff --git a/src/main/java/org/lucoenergia/conluz/domain/admin/supply/Supply.java b/src/main/java/org/lucoenergia/conluz/domain/admin/supply/Supply.java index a4132f1..a5ab6ba 100644 --- a/src/main/java/org/lucoenergia/conluz/domain/admin/supply/Supply.java +++ b/src/main/java/org/lucoenergia/conluz/domain/admin/supply/Supply.java @@ -12,9 +12,6 @@ public class Supply { - private static final Integer DEFAULT_POINT_TYPE = 5; - private static final String DEFAULT_DISTRIBUTOR_CODE = DistributorCode.E_DISTRIBUCION; - @NotNull @ValidUUID private UUID id; @@ -197,11 +194,11 @@ public String getDistributor() { } public String getDistributorCode() { - return distributorCode != null ? distributorCode : DEFAULT_DISTRIBUTOR_CODE; + return distributorCode; } public Integer getPointType() { - return pointType != null ? pointType : DEFAULT_POINT_TYPE; + return pointType; } public void setAddress(String address) { diff --git a/src/main/java/org/lucoenergia/conluz/domain/admin/supply/update/UpdateSupplyService.java b/src/main/java/org/lucoenergia/conluz/domain/admin/supply/update/UpdateSupplyService.java new file mode 100644 index 0000000..8583612 --- /dev/null +++ b/src/main/java/org/lucoenergia/conluz/domain/admin/supply/update/UpdateSupplyService.java @@ -0,0 +1,18 @@ +package org.lucoenergia.conluz.domain.admin.supply.update; + +import org.lucoenergia.conluz.domain.admin.supply.Supply; +import org.springframework.stereotype.Service; + +@Service +public class UpdateSupplyService { + + private final UpdateSupplyRepository repository; + + public UpdateSupplyService(UpdateSupplyRepository repository) { + this.repository = repository; + } + + public Supply update(Supply supply) { + return repository.update(supply); + } +} diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyEntityMapper.java b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyEntityMapper.java index 942c3b7..9174282 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyEntityMapper.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyEntityMapper.java @@ -21,10 +21,10 @@ public Supply map(SupplyEntity entity) { return new Supply.Builder() .withId(entity.getId()) .withCode(entity.getCode()) + .withName(entity.getName()) .withAddress(entity.getAddress()) .withPartitionCoefficient(entity.getPartitionCoefficient()) .withEnabled(entity.getEnabled()) - .withName(entity.getName()) .withUser(userEntityMapper.map(entity.getUser())) .withValidDateFrom(entity.getValidDateFrom()) diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBody.java b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBody.java new file mode 100644 index 0000000..3ae0623 --- /dev/null +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBody.java @@ -0,0 +1,147 @@ +package org.lucoenergia.conluz.infrastructure.admin.supply.update; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.Positive; +import org.lucoenergia.conluz.domain.admin.supply.Supply; +import org.lucoenergia.conluz.domain.admin.user.User; +import org.lucoenergia.conluz.infrastructure.shared.time.DateConverter; + +import java.text.DateFormat; +import java.util.UUID; + +@Schema(requiredProperties = { + "code", "address", "partitionCoefficient" +}) +public class UpdateSupplyBody { + + @NotEmpty + private String code; + private String name; + @NotEmpty + private String address; + @Positive + private Float partitionCoefficient; + private Boolean enabled; + private String validDateFrom; + private String distributor; + private String distributorCode; + private Integer pointType; + private String shellyMac; + private String shellyId; + private String shellyMqttPrefix; + + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Float getPartitionCoefficient() { + return partitionCoefficient; + } + + public void setPartitionCoefficient(Float partitionCoefficient) { + this.partitionCoefficient = partitionCoefficient; + } + + public Boolean getEnabled() { + return enabled; + } + + public void setEnabled(Boolean enabled) { + this.enabled = enabled; + } + + public String getValidDateFrom() { + return validDateFrom; + } + + public void setValidDateFrom(String validDateFrom) { + this.validDateFrom = validDateFrom; + } + + public String getDistributor() { + return distributor; + } + + public void setDistributor(String distributor) { + this.distributor = distributor; + } + + public String getDistributorCode() { + return distributorCode; + } + + public void setDistributorCode(String distributorCode) { + this.distributorCode = distributorCode; + } + + public Integer getPointType() { + return pointType; + } + + public void setPointType(Integer pointType) { + this.pointType = pointType; + } + + public String getShellyMac() { + return shellyMac; + } + + public void setShellyMac(String shellyMac) { + this.shellyMac = shellyMac; + } + + public String getShellyId() { + return shellyId; + } + + public void setShellyId(String shellyId) { + this.shellyId = shellyId; + } + + public String getShellyMqttPrefix() { + return shellyMqttPrefix; + } + + public void setShellyMqttPrefix(String shellyMqttPrefix) { + this.shellyMqttPrefix = shellyMqttPrefix; + } + + public Supply mapToSupply(UUID supplyId) { + Supply.Builder builder = new Supply.Builder(); + builder.withId(supplyId) + .withCode(code) + .withName(name) + .withAddress(address) + .withPartitionCoefficient(partitionCoefficient) + .withValidDateFrom(validDateFrom != null ? DateConverter.convertStringToLocalDate(validDateFrom, "yyyy-MM-dd") : null) + .withDistributor(distributor) + .withDistributorCode(distributorCode) + .withPointType(pointType) + .withShellyMac(shellyMac) + .withShellyId(shellyId) + .withShellyMqttPrefix(shellyMqttPrefix); + return builder.build(); + } +} diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyController.java b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyController.java new file mode 100644 index 0000000..e5ae85d --- /dev/null +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyController.java @@ -0,0 +1,59 @@ +package org.lucoenergia.conluz.infrastructure.admin.supply.update; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import jakarta.validation.Valid; +import org.lucoenergia.conluz.domain.admin.supply.update.UpdateSupplyService; +import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyResponse; +import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.ApiTag; +import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.response.*; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.UUID; + +/** + * Updates an existing supply + */ +@RestController +@RequestMapping("/api/v1") +@Validated +public class UpdateSupplyController { + + private final UpdateSupplyService service; + + public UpdateSupplyController(UpdateSupplyService service) { + this.service = service; + } + + @PutMapping("/supplies/{id}") + @Operation( + summary = "Updates supply information", + description = """ + This endpoint enables the update of supply information by specifying the supply's unique identifier in the endpoint path. + + Clients send a request containing the updated supply details, and authentication, through an authentication token, is required for secure access. + + A successful update results in an HTTP status code of 200, indicating that the supply information has been successfully modified. In cases where the update encounters errors, the server responds with an appropriate error status code along with a descriptive error message to assist clients in addressing and resolving the issue. + + If you don't provide some of the optional parameters, they will be considered as null value so their values will be updated with a null value.""", + tags = ApiTag.SUPPLIES, + operationId = "updateSupply" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Supply successfully updated.", + useReturnTypeSchema = true + ) + }) + @ForbiddenErrorResponse + @UnauthorizedErrorResponse + @BadRequestErrorResponse + @InternalServerErrorResponse + @NotFoundErrorResponse + public SupplyResponse updateSupply(@PathVariable("id") UUID supplyId, @Valid @RequestBody UpdateSupplyBody body) { + return new SupplyResponse(service.update(body.mapToSupply(supplyId))); + } +} diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyRepositoryDatabase.java b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyRepositoryDatabase.java index ad17e0a..1e5037d 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyRepositoryDatabase.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyRepositoryDatabase.java @@ -37,12 +37,16 @@ public Supply update(Supply supply) { currentSupply.setName(supply.getName()); currentSupply.setAddress(supply.getAddress()); currentSupply.setPartitionCoefficient(supply.getPartitionCoefficient()); - currentSupply.setEnabled(supply.getEnabled()); + currentSupply.setValidDateFrom(supply.getValidDateFrom()); currentSupply.setDistributor(supply.getDistributor()); currentSupply.setDistributorCode(supply.getDistributorCode()); currentSupply.setPointType(supply.getPointType()); + currentSupply.setShellyMac(supply.getShellyMac()); + currentSupply.setShellyId(supply.getShellyId()); + currentSupply.setShellyMqttPrefix(supply.getShellyMqttPrefix()); + return mapper.map(repository.save(currentSupply)); } } diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/user/update/UpdateUserRepositoryImpl.java b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/user/update/UpdateUserRepositoryDatabase.java similarity index 90% rename from src/main/java/org/lucoenergia/conluz/infrastructure/admin/user/update/UpdateUserRepositoryImpl.java rename to src/main/java/org/lucoenergia/conluz/infrastructure/admin/user/update/UpdateUserRepositoryDatabase.java index 9e764ca..3a6c392 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/admin/user/update/UpdateUserRepositoryImpl.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/admin/user/update/UpdateUserRepositoryDatabase.java @@ -13,12 +13,12 @@ import java.util.UUID; @Repository -public class UpdateUserRepositoryImpl implements UpdateUserRepository { +public class UpdateUserRepositoryDatabase implements UpdateUserRepository { private final UserRepository repository; private final UserEntityMapper mapper; - public UpdateUserRepositoryImpl(UserRepository repository, UserEntityMapper mapper) { + public UpdateUserRepositoryDatabase(UserRepository repository, UserEntityMapper mapper) { this.repository = repository; this.mapper = mapper; } diff --git a/src/main/java/org/lucoenergia/conluz/infrastructure/shared/time/DateConverter.java b/src/main/java/org/lucoenergia/conluz/infrastructure/shared/time/DateConverter.java index ffb4d05..532d9e9 100644 --- a/src/main/java/org/lucoenergia/conluz/infrastructure/shared/time/DateConverter.java +++ b/src/main/java/org/lucoenergia/conluz/infrastructure/shared/time/DateConverter.java @@ -36,8 +36,12 @@ public OffsetDateTime convertInstantToOffsetDateTime(Instant instant) { return instant.atOffset(zonedDateTime.getOffset()); } - public LocalDate convertStringToLocalDate(String dateString) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); + public static LocalDate convertStringToLocalDate(String dateString) { + return convertStringToLocalDate(dateString, "yyyy/MM/dd"); + } + + public static LocalDate convertStringToLocalDate(String dateString, String pattern) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); return LocalDate.parse(dateString, formatter); } diff --git a/src/test/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBodyTest.java b/src/test/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBodyTest.java new file mode 100644 index 0000000..5ae497d --- /dev/null +++ b/src/test/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBodyTest.java @@ -0,0 +1,46 @@ +package org.lucoenergia.conluz.infrastructure.admin.supply.update; + +import org.junit.jupiter.api.Test; +import org.lucoenergia.conluz.domain.admin.supply.Supply; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class UpdateSupplyBodyTest { + + @Test + void testMapToSupply() { + // Arrange + UUID supplyId = UUID.randomUUID(); + UpdateSupplyBody updateSupplyBody = new UpdateSupplyBody(); + updateSupplyBody.setCode("code"); + updateSupplyBody.setName("name"); + updateSupplyBody.setAddress("address"); + updateSupplyBody.setPartitionCoefficient(0.5f); + updateSupplyBody.setValidDateFrom("2022-01-01"); + updateSupplyBody.setDistributor("distributor"); + updateSupplyBody.setDistributorCode("distributorCode"); + updateSupplyBody.setPointType(6); + updateSupplyBody.setShellyMac("shellyMac"); + updateSupplyBody.setShellyId("shellyId"); + updateSupplyBody.setShellyMqttPrefix("shellyMqttPrefix"); + + // Act + Supply supply = updateSupplyBody.mapToSupply(supplyId); + + // Assert + assertEquals(supplyId, supply.getId()); + assertEquals("code", supply.getCode()); + assertEquals("name", supply.getName()); + assertEquals("address", supply.getAddress()); + assertEquals(0.5f, supply.getPartitionCoefficient()); + assertEquals("2022-01-01", supply.getValidDateFrom().toString()); + assertEquals("distributor", supply.getDistributor()); + assertEquals("distributorCode", supply.getDistributorCode()); + assertEquals(6, supply.getPointType()); + assertEquals("shellyMac", supply.getShellyMac()); + assertEquals("shellyId", supply.getShellyId()); + assertEquals("shellyMqttPrefix", supply.getShellyMqttPrefix()); + } +} \ No newline at end of file diff --git a/src/test/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyControllerTest.java b/src/test/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyControllerTest.java new file mode 100644 index 0000000..b92b81c --- /dev/null +++ b/src/test/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyControllerTest.java @@ -0,0 +1,329 @@ +package org.lucoenergia.conluz.infrastructure.admin.supply.update; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.lucoenergia.conluz.domain.admin.supply.Supply; +import org.lucoenergia.conluz.domain.admin.supply.SupplyMother; +import org.lucoenergia.conluz.domain.admin.supply.create.CreateSupplyRepository; +import org.lucoenergia.conluz.domain.admin.user.Role; +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.shared.UserId; +import org.lucoenergia.conluz.infrastructure.admin.user.update.UpdateUserBody; +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.time.LocalDate; +import java.util.List; +import java.util.UUID; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +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 UpdateSupplyControllerTest extends BaseControllerTest { + + private static final String PATH = "/api/v1/supplies"; + + @Autowired + private CreateUserRepository createUserRepository; + @Autowired + private CreateSupplyRepository createSupplyRepository; + + @Test + void testUpdateSupplyModifyingAll() throws Exception { + + String authHeader = loginAsDefaultAdmin(); + + // Creates two users + User userOne = UserMother.randomUser(); + userOne = createUserRepository.create(userOne); + + // Creates one supply + Supply supply = SupplyMother.random().build(); + supply = createSupplyRepository.create(supply, UserId.of(userOne.getId())); + + // Modify data of the supply + UpdateSupplyBody supplyModified = new UpdateSupplyBody(); + supplyModified.setCode("code"); + supplyModified.setName("name"); + supplyModified.setAddress("address"); + supplyModified.setPartitionCoefficient(1.2f); + supplyModified.setEnabled(false); + supplyModified.setValidDateFrom("2023-04-26"); + supplyModified.setDistributor("2"); + supplyModified.setDistributorCode("EDISTRIBUCION"); + supplyModified.setPointType(5); + supplyModified.setShellyId("shelly-id"); + supplyModified.setShellyMac("shelly-mac"); + supplyModified.setShellyMqttPrefix("shelly-mqtt-prefix"); + + String bodyAsString = objectMapper.writeValueAsString(supplyModified); + + mockMvc.perform(put(String.format("%s/%s", PATH, supply.getId())) + .header(HttpHeaders.AUTHORIZATION, authHeader) + .contentType(MediaType.APPLICATION_JSON) + .content(bodyAsString)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(supplyModified.getCode())) + .andExpect(jsonPath("$.name").value(supplyModified.getName())) + .andExpect(jsonPath("$.address").value(supplyModified.getAddress())) + .andExpect(jsonPath("$.partitionCoefficient").value(supplyModified.getPartitionCoefficient())) + .andExpect(jsonPath("$.enabled").value(supply.getEnabled())) + .andExpect(jsonPath("$.validDateFrom").value(supplyModified.getValidDateFrom())) + .andExpect(jsonPath("$.distributor").value(supplyModified.getDistributor())) + .andExpect(jsonPath("$.distributorCode").value(supplyModified.getDistributorCode())) + .andExpect(jsonPath("$.pointType").value(supplyModified.getPointType())) + .andExpect(jsonPath("$.shellyMac").value(supplyModified.getShellyMac())) + .andExpect(jsonPath("$.shellyId").value(supplyModified.getShellyId())) + .andExpect(jsonPath("$.shellyMqttPrefix").value(supplyModified.getShellyMqttPrefix())) + .andExpect(jsonPath("$.user.personalId").value(userOne.getPersonalId())); + } + + @Test + void testWithMissingNotRequiredFields() throws Exception { + + String authHeader = loginAsDefaultAdmin(); + + // Creates two users + User userOne = UserMother.randomUser(); + userOne = createUserRepository.create(userOne); + + // Creates one supply + Supply supply = SupplyMother.random().build(); + supply = createSupplyRepository.create(supply, UserId.of(userOne.getId())); + + // Modify data of the supply + UpdateSupplyBody supplyModified = new UpdateSupplyBody(); + supplyModified.setCode("code"); + supplyModified.setAddress("address"); + supplyModified.setPartitionCoefficient(1.2f); + + String bodyAsString = objectMapper.writeValueAsString(supplyModified); + + mockMvc.perform(put(String.format("%s/%s", PATH, supply.getId())) + .header(HttpHeaders.AUTHORIZATION, authHeader) + .contentType(MediaType.APPLICATION_JSON) + .content(bodyAsString)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(supplyModified.getCode())) + .andExpect(jsonPath("$.name").isEmpty()) + .andExpect(jsonPath("$.address").value(supplyModified.getAddress())) + .andExpect(jsonPath("$.partitionCoefficient").value(supplyModified.getPartitionCoefficient())) + .andExpect(jsonPath("$.enabled").value(supply.getEnabled())) + .andExpect(jsonPath("$.validDateFrom").isEmpty()) + .andExpect(jsonPath("$.distributor").isEmpty()) + .andExpect(jsonPath("$.distributorCode").isEmpty()) + .andExpect(jsonPath("$.pointType").isEmpty()) + .andExpect(jsonPath("$.shellyMac").isEmpty()) + .andExpect(jsonPath("$.shellyId").isEmpty()) + .andExpect(jsonPath("$.shellyMqttPrefix").isEmpty()) + .andExpect(jsonPath("$.user.personalId").value(supply.getUser().getPersonalId())); + } + + @Test + void testWithUnknownSupply() throws Exception { + + String authHeader = loginAsDefaultAdmin(); + + final String supplyId = UUID.randomUUID().toString(); + + String body = """ + { + "code": "code", + "address": "Fake Street 123, + "partitionCoefficient": 1.2 + } + """; + + mockMvc.perform(put(PATH + "/" + supplyId) + .header(HttpHeaders.AUTHORIZATION, authHeader) + .contentType(MediaType.APPLICATION_JSON) + .content(body)) + .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 testWithUnknownFields() throws Exception { + + final String body = """ + { + "unknown": 1, + "code": "code", + "address": "Fake Street 123, + "partitionCoefficient": 1.2 + } + """; + + final String authHeader = loginAsDefaultAdmin(); + + final String supplyId = UUID.randomUUID().toString(); + + mockMvc.perform(put(PATH + "/" + supplyId) + .header(HttpHeaders.AUTHORIZATION, authHeader) + .contentType(MediaType.APPLICATION_JSON) + .content(body)) + .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()); + } + + @ParameterizedTest + @MethodSource("getBodyWithMissingRequiredFields") + void testMissingRequiredFields(String body) throws Exception { + + final String authHeader = loginAsDefaultAdmin(); + + final String supplyId = UUID.randomUUID().toString(); + + mockMvc.perform(put(PATH + "/" + supplyId) + .header(HttpHeaders.AUTHORIZATION, authHeader) + .contentType(MediaType.APPLICATION_JSON) + .content(body)) + .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()); + } + + static List getBodyWithMissingRequiredFields() { + return List.of( + """ + { + "address": "Fake Street 123, + "partitionCoefficient": 1.2 + } + """, + """ + { + "code": "code", + "partitionCoefficient": 1.2 + } + """, + """ + { + "code": "code", + "address": "Fake Street 123, + } + """); + } + + @ParameterizedTest + @MethodSource("getBodyWithInvalidFormatValues") + void testWithInvalidFormatValues(String body) throws Exception { + + final String authHeader = loginAsDefaultAdmin(); + + final String supplyId = UUID.randomUUID().toString(); + + mockMvc.perform(put(PATH + "/" + supplyId) + .header(HttpHeaders.AUTHORIZATION, authHeader) + .contentType(MediaType.APPLICATION_JSON) + .content(body)) + .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()); + } + + static List getBodyWithInvalidFormatValues() { + return List.of(""" + { + "code": "ES0031300326337001WS0F", + "address": "MAYOR 1", + "partitionCoefficient": 0.021255, + "personalId": "personalId", + "enabled": true, + "validDateFrom": "2023-05-01" + } + """, + """ + { + "code": "ES0031300326337001WS0F", + "address": "MAYOR 1", + "partitionCoefficient": 0.021255, + "personalId": "personalId", + "enabled": true, + "pointType": "foo" + } + """, + """ + { + "code": "ES0031300326337001WS0F", + "address": "MAYOR 1", + "partitionCoefficient": 0.021255, + "personalId": "personalId", + "enabled": "foo" + } + """); + } + + @Test + void + testWithoutBody() throws Exception { + final String authHeader = loginAsDefaultAdmin(); + + final String supplyId = UUID.randomUUID().toString(); + + mockMvc.perform(put(PATH + "/" + supplyId) + .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(put(PATH) + .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(put(PATH) + .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()); + } +}