Skip to content

Commit

Permalink
fix(api-owner): add constraint validation on dpeDate for post property
Browse files Browse the repository at this point in the history
Now the propertyForm will have a validation constraint on the dpeDate to force the format yyyy-MM-dd.
  • Loading branch information
nicolasSagon committed Feb 10, 2025
1 parent 881162a commit 4aff341
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fr.dossierfacile.common.exceptions.NotFoundException;
import fr.dossierfacile.common.service.interfaces.LogService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.utils.URIBuilder;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class PropertyController {
private final PropertyMapper propertyMapper;

@PostMapping
public ResponseEntity<PropertyModel> createOrUpdate(HttpServletResponse response, @RequestBody PropertyForm Property) throws HttpResponseException, InterruptedException {
public ResponseEntity<PropertyModel> createOrUpdate(HttpServletResponse response, @Valid @RequestBody PropertyForm Property) throws HttpResponseException, InterruptedException {
try {
PropertyModel propertyModel;
propertyModel = propertyService.createOrUpdate(Property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.dossierfacile.common.enums.PropertyFurniture;
import fr.dossierfacile.common.enums.PropertyType;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -16,7 +17,6 @@ public class PropertyForm {

private Long id;

@NotBlank
private String name;

private Double rentCost;
Expand All @@ -37,6 +37,7 @@ public class PropertyForm {

private Integer energyConsumption;

@Pattern(regexp = "^d{4}-d{2}-d{2}$", message = "Date must be in the format yyyy-MM-dd")
private String dpeDate;

private String ademeNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.dossierfacile.api.dossierfacileapiowner;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.test.context.ActiveProfiles;

@SpringBootApplication
@ActiveProfiles(value = "test")
public class TestApplication {

public static void main(String[] args) {
SpringApplication.run(DossierfacileApiOwnerApplication.class, args);
}

@Bean
@Profile("test")
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().anyRequest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package fr.dossierfacile.api.dossierfacileapiowner.property;

import com.fasterxml.jackson.databind.ObjectMapper;
import fr.dossierfacile.api.dossierfacileapiowner.TestApplication;
import fr.dossierfacile.api.dossierfacileapiowner.register.AuthenticationFacade;
import fr.dossierfacile.api.dossierfacileapiowner.user.OwnerMapper;
import fr.dossierfacile.common.service.interfaces.LogService;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(PropertyController.class)
@ContextConfiguration(classes = {TestApplication.class})
public class PropertyControllerTest {

@Autowired
private ObjectMapper objectMapper;

@Autowired
private MockMvc mockMvc;

@MockBean
private LogService logService;

@MockBean
private PropertyService propertyService;

@MockBean
private PropertyApartmentSharingService propertyApartmentSharingService;

@MockBean
private AuthenticationFacade authenticationFacade;

@MockBean
private OwnerMapper ownerMapper;

@MockBean
private PropertyMapper propertyMapper;

@Test
void createOrUpdatePropertyWithEmptyBody() throws Exception {
mockMvc.perform(post("/api/property")
.contentType("application/json")).andExpect(status().isBadRequest());
}

@Test
void createOrUpdatePropertyWithValidBody() throws Exception {
var returnedPropertyModel = new PropertyModel();
returnedPropertyModel.setId(1L);
returnedPropertyModel.setName("Test");
returnedPropertyModel.setDpeDate("2021-08-01");
when(propertyService.createOrUpdate(any())).thenReturn(returnedPropertyModel);

var propertyForm = new PropertyForm();
propertyForm.setName("Test");
propertyForm.setDpeDate("2021-08-01");

mockMvc.perform(post("/api/property")
.contentType("application/json")
.content(objectMapper.writeValueAsString(propertyForm)))
.andExpect(status().isOk());
}

@Test
void createOrUpdatePropertyWithInvalidBodyDate() throws Exception {

var returnedPropertyModel = new PropertyModel();
returnedPropertyModel.setId(1L);
returnedPropertyModel.setName("Test");
returnedPropertyModel.setDpeDate("2021-08-01");
when(propertyService.createOrUpdate(any())).thenReturn(returnedPropertyModel);

var propertyForm = new PropertyForm();
propertyForm.setName("Test");
propertyForm.setDpeDate("Lundi 15 mars 2021");

mockMvc.perform(post("/api/property")
.contentType("application/json")
.content(objectMapper.writeValueAsString(propertyForm)))
.andExpect(status().isBadRequest());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.profiles.active=test,mockOvh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<!-- CONSOLE -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(%-5level) [%thread] %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<root>
<appender-ref ref="STDOUT"/>
</root>

</configuration>

0 comments on commit 4aff341

Please sign in to comment.