Skip to content

Commit

Permalink
fix: Allowing merge when saving without explicitly setting semantic i…
Browse files Browse the repository at this point in the history
…d field
  • Loading branch information
antonioT90 committed Feb 10, 2025
1 parent 02805b0 commit 11aa405
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions openapi/p4pa-classification.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ components:
enum:
- CLASSIFICATION_NOT_FOUND
- CLASSIFICATION_BAD_REQUEST
- CLASSIFICATION_CONFLICT
- CLASSIFICATION_GENERIC_ERROR
message:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Object generate(SharedSessionContractImplementor session, Object o, Objec
}
}

private String buildSemanticId(PaymentsReporting paymentsReporting) {
public static String buildSemanticId(PaymentsReporting paymentsReporting) {
return paymentsReporting.getIuf() + "/" +
paymentsReporting.getIuv() + "/" +
paymentsReporting.getTransferIndex() + "/" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Object generate(SharedSessionContractImplementor session, Object o, Objec
}
}

private String buildSemanticId(Treasury treasury) {
public static String buildSemanticId(Treasury treasury) {
return treasury.getBillCode() + "-" +
treasury.getBillYear() + "-" +
treasury.getOrganizationId() ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,6 +29,16 @@
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ClassificationExceptionHandler {

@ExceptionHandler({ResourceNotFoundException.class})
public ResponseEntity<ClassificationErrorDTO> handleResourceNotFoundException(RuntimeException ex, HttpServletRequest request){
return handleException(ex, request, HttpStatus.NOT_FOUND, ClassificationErrorDTO.CodeEnum.NOT_FOUND);
}

@ExceptionHandler({DataIntegrityViolationException.class})
public ResponseEntity<ClassificationErrorDTO> handleDataIntegrityViolationException(Exception ex, HttpServletRequest request) {
return handleException(ex, request, HttpStatus.CONFLICT, ClassificationErrorDTO.CodeEnum.CONFLICT);
}

@ExceptionHandler({ValidationException.class, HttpMessageNotReadableException.class, MethodArgumentNotValidException.class, MethodArgumentTypeMismatchException.class})
public ResponseEntity<ClassificationErrorDTO> handleViolationException(Exception ex, HttpServletRequest request) {
return handleException(ex, request, HttpStatus.BAD_REQUEST, ClassificationErrorDTO.CodeEnum.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,30 @@ public class PaymentsReporting extends BaseEntity implements Serializable{
@NotNull
private LocalDate acquiringDate;
private String bicCodePouringBank;

//region keep updated semanticId
private void setSemanticId() {
this.paymentsReportingId = PaymentsReportingSemanticIdGenerator.PaymentsReportingSemanticIdGeneratorStrategy.buildSemanticId(this);
}

public void setOrganizationId(Long organizationId) {
this.organizationId = organizationId;
setSemanticId();
}

public void setIuv(String iuv) {
this.iuv = iuv;
setSemanticId();
}

public void setIuf(String iuf) {
this.iuf = iuf;
setSemanticId();
}

public void setTransferIndex(Integer transferIndex) {
this.transferIndex = transferIndex;
setSemanticId();
}
//endregion
}
21 changes: 21 additions & 0 deletions src/main/java/it/gov/pagopa/pu/classification/model/Treasury.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,25 @@ public class Treasury extends BaseEntity implements Serializable{
private LocalDate actualSuspensionDate;
private String managementProvisionalCode;
private String endToEndId;

//region keep updated semanticId
private void setSemanticId() {
this.treasuryId = TreasurySemanticIdGenerator.TreasurySemanticIdGeneratorStrategy.buildSemanticId(this);
}

public void setOrganizationId(Long organizationId) {
this.organizationId = organizationId;
setSemanticId();
}

public void setBillCode(String billCode) {
this.billCode = billCode;
setSemanticId();
}

public void setBillYear(String billYear) {
this.billYear = billYear;
setSemanticId();
}
//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
Expand Down Expand Up @@ -145,6 +147,28 @@ void handleUrlNotFound() throws Exception {
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("No static resource NOTEXISTENTURL."));
}

@Test
void handleResourceNotFoundException() throws Exception {
doThrow(new ResourceNotFoundException("Error"))
.when(requestMappingHandlerAdapterSpy).handle(any(), any(), any());

performRequest(DATA, MediaType.APPLICATION_JSON)
.andExpect(MockMvcResultMatchers.status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("CLASSIFICATION_NOT_FOUND"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Error"));
}

@Test
void handleDataIntegrityViolationException() throws Exception {
doThrow(new DataIntegrityViolationException("Error"))
.when(requestMappingHandlerAdapterSpy).handle(any(), any(), any());

performRequest(DATA, MediaType.APPLICATION_JSON)
.andExpect(MockMvcResultMatchers.status().isConflict())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("CLASSIFICATION_CONFLICT"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Error"));
}

@Test
void handleNoBodyException() throws Exception {
performRequest(DATA, MediaType.APPLICATION_JSON, null)
Expand Down

0 comments on commit 11aa405

Please sign in to comment.