Skip to content

Commit 9c37c6a

Browse files
authored
feature/conluz-108 to main (#117)
* [conluz-108] Added field addressRef to supply. Added new exception filter to catch unexpected errors * [conluz-108] Changed position of global exception handler to delegate invalid token errors to the specific filter. Fixed tests. * [conluz-108] Configured error builder to add same trace ID for logs and API error responses
1 parent 5578eda commit 9c37c6a

File tree

41 files changed

+441
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+441
-226
lines changed

src/main/java/org/lucoenergia/conluz/domain/admin/supply/Supply.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Supply {
2323
private String name;
2424
@NotBlank
2525
private String address;
26+
private String addressRef;
2627
@NotNull
2728
@PositiveOrZero
2829
private Float partitionCoefficient;
@@ -44,6 +45,7 @@ private Supply(Builder builder) {
4445
this.user = builder.user;
4546
this.name = builder.name;
4647
this.address = builder.address;
48+
this.addressRef = builder.addressRef;
4749
this.partitionCoefficient = builder.partitionCoefficient;
4850
this.enabled = builder.enabled;
4951
this.validDateFrom = builder.validDateFrom;
@@ -70,6 +72,7 @@ public static class Builder {
7072
private User user;
7173
private String name;
7274
private String address;
75+
private String addressRef;
7376
private Float partitionCoefficient;
7477
private Boolean enabled;
7578
private LocalDate validDateFrom;
@@ -106,6 +109,11 @@ public Builder withAddress(String address) {
106109
return this;
107110
}
108111

112+
public Builder withAddressRef(String addressRef) {
113+
this.addressRef = addressRef;
114+
return this;
115+
}
116+
109117
public Builder withPartitionCoefficient(Float partitionCoefficient) {
110118
this.partitionCoefficient = partitionCoefficient;
111119
return this;
@@ -185,6 +193,10 @@ public String getAddress() {
185193
return address;
186194
}
187195

196+
public String getAddressRef() {
197+
return addressRef;
198+
}
199+
188200
public Float getPartitionCoefficient() {
189201
return partitionCoefficient;
190202
}
@@ -217,6 +229,10 @@ public void setAddress(String address) {
217229
this.address = address;
218230
}
219231

232+
public void setAddressRef(String addressRef) {
233+
this.addressRef = addressRef;
234+
}
235+
220236
public void setValidDateFrom(LocalDate validDateFrom) {
221237
this.validDateFrom = validDateFrom;
222238
}

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SharingAgreementExceptionHandler.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.lucoenergia.conluz.infrastructure.admin.supply;
22

33
import org.lucoenergia.conluz.domain.admin.supply.SharingAgreementNotFoundException;
4+
import org.lucoenergia.conluz.infrastructure.shared.error.ErrorBuilder;
45
import org.lucoenergia.conluz.infrastructure.shared.web.error.RestError;
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
76
import org.springframework.context.MessageSource;
87
import org.springframework.context.i18n.LocaleContextHolder;
98
import org.springframework.http.HttpStatus;
@@ -16,13 +15,12 @@
1615
@RestControllerAdvice
1716
public class SharingAgreementExceptionHandler {
1817

19-
20-
private static final Logger LOGGER = LoggerFactory.getLogger(SharingAgreementExceptionHandler.class);
21-
2218
private final MessageSource messageSource;
19+
private final ErrorBuilder errorBuilder;
2320

24-
public SharingAgreementExceptionHandler(MessageSource messageSource) {
21+
public SharingAgreementExceptionHandler(MessageSource messageSource, ErrorBuilder errorBuilder) {
2522
this.messageSource = messageSource;
23+
this.errorBuilder = errorBuilder;
2624
}
2725

2826
@ExceptionHandler(SharingAgreementNotFoundException.class)
@@ -35,7 +33,6 @@ public ResponseEntity<RestError> handleException(SharingAgreementNotFoundExcepti
3533
Collections.singletonList(sharingAgreementId).toArray(),
3634
LocaleContextHolder.getLocale()
3735
);
38-
LOGGER.error(message);
39-
return new ResponseEntity<>(new RestError(HttpStatus.BAD_REQUEST.value(), message), HttpStatus.BAD_REQUEST);
36+
return errorBuilder.build(message, HttpStatus.BAD_REQUEST);
4037
}
4138
}

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyEntity.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class SupplyEntity {
2121

2222
private String name;
2323
private String address;
24+
private String addressRef;
2425
private Float partitionCoefficient;
2526
private Boolean enabled;
2627
private LocalDate validDateFrom;
@@ -58,6 +59,7 @@ public static class Builder {
5859
private UserEntity user;
5960
private String name;
6061
private String address;
62+
private String addressRef;
6163
private Float partitionCoefficient;
6264
private Boolean enabled;
6365

@@ -95,6 +97,11 @@ public Builder withAddress(String address) {
9597
return this;
9698
}
9799

100+
public Builder withAddressRef(String addressRef) {
101+
this.addressRef = addressRef;
102+
return this;
103+
}
104+
98105
public Builder withPartitionCoefficient(Float partitionCoefficient) {
99106
this.partitionCoefficient = partitionCoefficient;
100107
return this;
@@ -152,6 +159,7 @@ public SupplyEntity build() {
152159
entity.user = this.user;
153160
entity.name = this.name;
154161
entity.address = this.address;
162+
entity.addressRef = this.addressRef;
155163
entity.partitionCoefficient = this.partitionCoefficient;
156164
entity.enabled = this.enabled;
157165
entity.validDateFrom = this.validDateFrom;
@@ -206,11 +214,18 @@ public String getAddress() {
206214
return address;
207215
}
208216

217+
public String getAddressRef() {
218+
return addressRef;
219+
}
209220

210221
public void setAddress(String address) {
211222
this.address = address;
212223
}
213224

225+
public void setAddressRef(String addressRef) {
226+
this.addressRef = addressRef;
227+
}
228+
214229
public Float getPartitionCoefficient() {
215230
return partitionCoefficient;
216231
}

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyEntityMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public Supply map(SupplyEntity entity) {
2323
.withCode(entity.getCode())
2424
.withName(entity.getName())
2525
.withAddress(entity.getAddress())
26+
.withAddressRef(entity.getAddressRef())
2627
.withPartitionCoefficient(entity.getPartitionCoefficient())
2728
.withEnabled(entity.getEnabled())
2829
.withUser(userEntityMapper.map(entity.getUser()))

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyExceptionHandler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.lucoenergia.conluz.domain.admin.supply.SupplyAlreadyExistsException;
44
import org.lucoenergia.conluz.domain.admin.supply.SupplyNotFoundException;
55
import org.lucoenergia.conluz.domain.admin.user.UserAlreadyExistsException;
6+
import org.lucoenergia.conluz.infrastructure.shared.error.ErrorBuilder;
67
import org.lucoenergia.conluz.infrastructure.shared.web.error.RestError;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
@@ -22,9 +23,11 @@ public class SupplyExceptionHandler {
2223
private static final Logger LOGGER = LoggerFactory.getLogger(SupplyExceptionHandler.class);
2324

2425
private final MessageSource messageSource;
26+
private final ErrorBuilder errorBuilder;
2527

26-
public SupplyExceptionHandler(MessageSource messageSource) {
28+
public SupplyExceptionHandler(MessageSource messageSource, ErrorBuilder errorBuilder) {
2729
this.messageSource = messageSource;
30+
this.errorBuilder = errorBuilder;
2831
}
2932

3033
@ExceptionHandler(SupplyNotFoundException.class)
@@ -37,8 +40,7 @@ public ResponseEntity<RestError> handleException(SupplyNotFoundException e) {
3740
Collections.singletonList(supplyId).toArray(),
3841
LocaleContextHolder.getLocale()
3942
);
40-
LOGGER.error(message);
41-
return new ResponseEntity<>(new RestError(HttpStatus.NOT_FOUND.value(), message), HttpStatus.NOT_FOUND);
43+
return errorBuilder.build(message, HttpStatus.NOT_FOUND);
4244
}
4345

4446
@ExceptionHandler(SupplyAlreadyExistsException.class)
@@ -49,7 +51,6 @@ public ResponseEntity<RestError> handleException(SupplyAlreadyExistsException e)
4951
List.of(e.getCode().getCode()).toArray(),
5052
LocaleContextHolder.getLocale()
5153
);
52-
LOGGER.error(message);
53-
return new ResponseEntity<>(new RestError(HttpStatus.BAD_REQUEST.value(), message), HttpStatus.BAD_REQUEST);
54+
return errorBuilder.build(message, HttpStatus.BAD_REQUEST);
5455
}
5556
}

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/SupplyResponse.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class SupplyResponse {
1919
private final String name;
2020
@Schema(description = "Address of the supply", example = "Fake Street 123")
2121
private final String address;
22+
@Schema(description = "Reference ID of the address", example = "4ASDF654ASDF89ASD")
23+
private final String addressRef;
2224
@Schema(description = "Address of the supply", example = "2.403561")
2325
private final Float partitionCoefficient;
2426
@Schema(description = "Whether the supply is enabled or disabled", example = "true")
@@ -46,6 +48,7 @@ public SupplyResponse(Supply supply) {
4648
this.code = supply.getCode();
4749
this.name = supply.getName();
4850
this.address = supply.getAddress();
51+
this.addressRef = supply.getAddressRef();
4952
this.partitionCoefficient = supply.getPartitionCoefficient();
5053
this.enabled = supply.getEnabled();
5154
this.user = supply.getUser() != null ? new UserResponse(supply.getUser()) : null;
@@ -78,6 +81,10 @@ public String getAddress() {
7881
return address;
7982
}
8083

84+
public String getAddressRef() {
85+
return addressRef;
86+
}
87+
8188
public Float getPartitionCoefficient() {
8289
return partitionCoefficient;
8390
}

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/create/CreateSuppliesPartitionsWithFileController.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.lucoenergia.conluz.domain.admin.supply.SupplyPartition;
1313
import org.lucoenergia.conluz.domain.admin.supply.create.CreateSupplyPartitionService;
1414
import org.lucoenergia.conluz.infrastructure.admin.supply.InvalidSupplyPartitionCoefficientException;
15+
import org.lucoenergia.conluz.infrastructure.shared.error.ErrorBuilder;
1516
import org.lucoenergia.conluz.infrastructure.shared.io.CsvFileParser;
1617
import org.lucoenergia.conluz.infrastructure.shared.io.CsvFileRequestValidator;
1718
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.ApiTag;
@@ -62,15 +63,17 @@ public class CreateSuppliesPartitionsWithFileController {
6263
private final MessageSource messageSource;
6364
private final CreateSupplyPartitionService createSupplyPartitionService;
6465
private final CsvParseExceptionHandler csvParseExceptionHandler;
66+
private final ErrorBuilder errorBuilder;
6567

6668
public CreateSuppliesPartitionsWithFileController(CsvFileRequestValidator csvFileRequestValidator,
6769
CsvFileParser csvFileParser, MessageSource messageSource,
68-
CreateSupplyPartitionService createSupplyPartitionService, CsvParseExceptionHandler csvParseExceptionHandler) {
70+
CreateSupplyPartitionService createSupplyPartitionService, CsvParseExceptionHandler csvParseExceptionHandler, ErrorBuilder errorBuilder) {
6971
this.csvFileRequestValidator = csvFileRequestValidator;
7072
this.csvFileParser = csvFileParser;
7173
this.messageSource = messageSource;
7274
this.createSupplyPartitionService = createSupplyPartitionService;
7375
this.csvParseExceptionHandler = csvParseExceptionHandler;
76+
this.errorBuilder = errorBuilder;
7477
}
7578

7679
@PostMapping
@@ -127,14 +130,10 @@ public ResponseEntity importSuppliesWithFile(
127130
try {
128131
suppliesPartitions = csvFileParser.parse(file.getInputStream(), CreateSupplyPartitionDto.class, ';');
129132
} catch (NumberFormatException ex) {
130-
LOGGER.error("Invalid number format.", ex);
131133
String message = messageSource.getMessage("error.supply.partitions.invalid.number.format",
132134
Collections.emptyList().toArray(),
133135
LocaleContextHolder.getLocale());
134-
return new ResponseEntity<>(
135-
new RestError(HttpStatus.BAD_REQUEST.value(), message),
136-
HttpStatus.BAD_REQUEST
137-
);
136+
return errorBuilder.build(message, HttpStatus.BAD_REQUEST);
138137
} catch (Exception ex) {
139138
return csvParseExceptionHandler.handleCsvParsingError(ex);
140139
}
@@ -143,19 +142,12 @@ public ResponseEntity importSuppliesWithFile(
143142
try {
144143
response = createSupplyPartitionService.createInBulk(suppliesPartitions, SharingAgreementId.of(sharingAgreementId));
145144
} catch (InvalidSupplyPartitionCoefficientException e) {
146-
return new ResponseEntity<>(
147-
new RestError(HttpStatus.BAD_REQUEST.value(), e.getMessage()),
148-
HttpStatus.BAD_REQUEST
149-
);
145+
return errorBuilder.build(e.getMessage(), HttpStatus.BAD_REQUEST);
150146
} catch (SharingAgreementNotFoundException e) {
151-
LOGGER.error("Sharing agreement with id {} not found", sharingAgreementId, e);
152147
String message = messageSource.getMessage("error.sharing.agreement.not.found",
153148
Collections.singletonList(sharingAgreementId).toArray(),
154149
LocaleContextHolder.getLocale());
155-
return new ResponseEntity<>(
156-
new RestError(HttpStatus.BAD_REQUEST.value(), message),
157-
HttpStatus.BAD_REQUEST
158-
);
150+
return errorBuilder.build(message, HttpStatus.NOT_FOUND);
159151
}
160152

161153
return new ResponseEntity<>(response, HttpStatus.OK);

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/create/CreateSupplyBody.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class CreateSupplyBody {
1717
private String personalId;
1818
@NotEmpty
1919
private String address;
20+
@NotEmpty
21+
private String addressRef;
2022
@Positive
2123
private Float partitionCoefficient;
2224
private String name;
@@ -37,6 +39,14 @@ public void setAddress(String address) {
3739
this.address = address;
3840
}
3941

42+
public String getAddressRef() {
43+
return addressRef;
44+
}
45+
46+
public void setAddressRef(String addressRef) {
47+
this.addressRef = addressRef;
48+
}
49+
4050
public Float getPartitionCoefficient() {
4151
return partitionCoefficient;
4252
}
@@ -65,12 +75,16 @@ public Supply mapToSupply() {
6575
Supply.Builder builder = new Supply.Builder();
6676
builder.withCode(code.trim())
6777
.withAddress(address.trim())
78+
.withAddressRef(addressRef.trim())
6879
.withPartitionCoefficient(partitionCoefficient)
6980
.withUser(new User.Builder().personalId(personalId.trim()).build());
7081

7182
if (name != null && !name.isBlank()) {
7283
builder.withName(name.trim());
7384
}
85+
if (addressRef != null && !addressRef.isBlank()) {
86+
builder.withAddressRef(addressRef.trim());
87+
}
7488

7589
return builder.build();
7690
}

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/create/CreateSupplyRepositoryDatabase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public Supply create(Supply supply, UserId id) {
5252
.withCode(supply.getCode())
5353
.withName(supply.getName())
5454
.withAddress(supply.getAddress())
55+
.withAddressRef(supply.getAddressRef())
5556
.withPartitionCoefficient(supply.getPartitionCoefficient())
5657
.withEnabled(supply.getEnabled())
5758

src/main/java/org/lucoenergia/conluz/infrastructure/admin/supply/create/CreateSupplyServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public Supply create(Supply supply, UserId id) {
3939
.withUser(supply.getUser())
4040
.withName(supply.getAddress())
4141
.withAddress(supply.getAddress())
42+
.withAddressRef(supply.getAddressRef())
4243
.withPartitionCoefficient(supply.getPartitionCoefficient())
4344
.withEnabled(supply.getEnabled())
4445
.withValidDateFrom(supply.getValidDateFrom())

0 commit comments

Comments
 (0)