Skip to content

Commit ac76750

Browse files
authored
Merge pull request #59 from juherr/features/steve-1791
backport steve/steve-community#1791
2 parents ca35cd6 + 6453add commit ac76750

49 files changed

Lines changed: 336 additions & 327 deletions

File tree

Some content is hidden

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

steve-api/src/main/java/de/rwth/idsg/steve/config/ApiDocsConfiguration.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,17 @@ public class ApiDocsConfiguration {
6969
System.setProperty("springdoc.swagger-ui.tagsSorter", "alpha");
7070
// Sort endpoints (within a controller) alphabetically by their path
7171
System.setProperty("springdoc.swagger-ui.operationsSorter", "alpha");
72+
// Sort schemas/DTOs alphabetically
73+
System.setProperty("springdoc.writer-with-order-by-keys", "true");
7274
}
7375

7476
@Bean
7577
public OpenAPI apiDocs(SteveConfiguration config) {
76-
String title = "SteVe REST API Documentation";
78+
var title = "SteVe REST API Documentation";
7779

78-
String securityName = "basicAuth";
80+
var securityName = "basicAuth";
7981

80-
SecurityScheme securityScheme = new SecurityScheme()
82+
var securityScheme = new SecurityScheme()
8183
.type(SecurityScheme.Type.HTTP)
8284
.scheme("basic")
8385
.name(securityName);

steve-api/src/main/java/de/rwth/idsg/steve/web/api/ApiControllerAdvice.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
package de.rwth.idsg.steve.web.api;
2020

2121
import de.rwth.idsg.steve.SteveException;
22-
import de.rwth.idsg.steve.web.api.exception.BadRequestException;
23-
import de.rwth.idsg.steve.web.api.exception.NotFoundException;
2422
import lombok.Data;
2523
import lombok.extern.slf4j.Slf4j;
2624
import org.jspecify.annotations.Nullable;
@@ -59,9 +57,9 @@ public ApiErrorResponse handleBindException(HttpServletRequest req, BindExceptio
5957
return createResponse(url, HttpStatus.BAD_REQUEST, "Error understanding the request");
6058
}
6159

62-
@ExceptionHandler(NotFoundException.class)
60+
@ExceptionHandler(SteveException.NotFound.class)
6361
@ResponseStatus(value = HttpStatus.NOT_FOUND)
64-
public ApiErrorResponse handleNotFoundException(HttpServletRequest req, NotFoundException exception) {
62+
public ApiErrorResponse handleNotFoundException(HttpServletRequest req, SteveException.NotFound exception) {
6563
String url = req.getRequestURL().toString();
6664
log.error("Request: {} raised following exception.", url, exception);
6765
return createResponse(url, HttpStatus.NOT_FOUND, exception.getMessage());
@@ -76,9 +74,9 @@ public ApiErrorResponse handleAlreadyExistsException(
7674
return createResponse(url, HttpStatus.UNPROCESSABLE_ENTITY, exception.getMessage());
7775
}
7876

79-
@ExceptionHandler(BadRequestException.class)
77+
@ExceptionHandler(SteveException.BadRequest.class)
8078
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
81-
public ApiErrorResponse handleBadRequestException(HttpServletRequest req, BadRequestException exception) {
79+
public ApiErrorResponse handleBadRequestException(HttpServletRequest req, SteveException.BadRequest exception) {
8280
String url = req.getRequestURL().toString();
8381
log.error("Request: {} raised following exception.", url, exception);
8482
return createResponse(url, HttpStatus.BAD_REQUEST, exception.getMessage());

steve-api/src/main/java/de/rwth/idsg/steve/web/api/OcppTagsRestController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
package de.rwth.idsg.steve.web.api;
2020

21+
import de.rwth.idsg.steve.SteveException;
2122
import de.rwth.idsg.steve.repository.dto.OcppTag.OcppTagOverview;
2223
import de.rwth.idsg.steve.service.OcppTagsService;
2324
import de.rwth.idsg.steve.web.api.ApiControllerAdvice.ApiErrorResponse;
24-
import de.rwth.idsg.steve.web.api.exception.NotFoundException;
2525
import de.rwth.idsg.steve.web.dto.OcppTagForm;
2626
import de.rwth.idsg.steve.web.dto.OcppTagQueryForm.OcppTagQueryFormForApi;
2727
import io.swagger.v3.oas.annotations.Operation;
@@ -186,7 +186,7 @@ private OcppTagOverview getOneInternal(int ocppTagPk) {
186186

187187
var results = ocppTagsService.getOverview(params);
188188
if (results.isEmpty()) {
189-
throw new NotFoundException("Could not find ocppTag with id " + ocppTagPk);
189+
throw new SteveException.NotFound("Could not find ocppTag with id " + ocppTagPk);
190190
}
191191
return results.get(0);
192192
}

steve-api/src/main/java/de/rwth/idsg/steve/web/api/RemoteCommandsRestController.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package de.rwth.idsg.steve.web.api;
2020

21+
import de.rwth.idsg.steve.SteveException;
2122
import de.rwth.idsg.steve.ocpp.OcppVersion;
2223
import de.rwth.idsg.steve.repository.ChargePointRepository;
2324
import de.rwth.idsg.steve.repository.TaskStore;
@@ -29,7 +30,6 @@
2930
import de.rwth.idsg.steve.web.api.dto.ApiChargePointStart;
3031
import de.rwth.idsg.steve.web.api.dto.ApiChargePointStop;
3132
import de.rwth.idsg.steve.web.api.dto.ApiChargePointUnlock;
32-
import de.rwth.idsg.steve.web.api.exception.BadRequestException;
3333
import de.rwth.idsg.steve.web.dto.ocpp.RemoteStartTransactionParams;
3434
import de.rwth.idsg.steve.web.dto.ocpp.RemoteStopTransactionParams;
3535
import de.rwth.idsg.steve.web.dto.ocpp.UnlockConnectorParams;
@@ -143,7 +143,7 @@ public Integer postRemoteStartTx(@Valid ApiChargePointStart params) {
143143
// only one active task per charge box over api; to be discussed!
144144
if (activeTaskOnChargeBox(params.getChargeBoxId())) {
145145
var errMsg = String.format("Active task found on ChargeBox %s!", params.getChargeBoxId());
146-
throw new BadRequestException(errMsg);
146+
throw new SteveException.BadRequest(errMsg);
147147
}
148148

149149
var transactionParams = new RemoteStartTransactionParams();
@@ -156,15 +156,15 @@ public Integer postRemoteStartTx(@Valid ApiChargePointStart params) {
156156
var errMsg = String.format(
157157
"Active transaction found for connector %s at ChargeBox %s!",
158158
params.getConnectorId(), params.getChargeBoxId());
159-
throw new BadRequestException(errMsg);
159+
throw new SteveException.BadRequest(errMsg);
160160
}
161161
}
162162
transactionParams.setConnectorId(params.getConnectorId());
163163

164164
var chargePoint = chargePointRepository
165165
.getChargePointSelect(params.getChargeBoxId())
166-
.orElseThrow(() ->
167-
new BadRequestException(String.format("ChargeBox %s not found!", params.getChargeBoxId())));
166+
.orElseThrow(() -> new SteveException.BadRequest(
167+
String.format("ChargeBox %s not found!", params.getChargeBoxId())));
168168
transactionParams.setChargePointSelectList(Collections.singletonList(chargePoint));
169169

170170
// Check if OCPP-Tag is allowed to use the connector? To be discussed and t.b.d.!
@@ -179,32 +179,33 @@ public Integer postRemoteStopTx(@Valid ApiChargePointStop params) {
179179
// only one active task per charge box over api; to be discussed!
180180
if (activeTaskOnChargeBox(params.getChargeBoxId())) {
181181
var errMsg = String.format("Active task found on ChargeBox %s!", params.getChargeBoxId());
182-
throw new BadRequestException(errMsg);
182+
throw new SteveException.BadRequest(errMsg);
183183
}
184184

185185
var transactionParams = new RemoteStopTransactionParams();
186186

187187
// set the ChargPointSelectionList, ensure the length is exactly one
188188
var chargePoint = chargePointRepository
189189
.getChargePointSelect(params.getChargeBoxId())
190-
.orElseThrow(() ->
191-
new BadRequestException(String.format("ChargeBox %s not found!", params.getChargeBoxId())));
190+
.orElseThrow(() -> new SteveException.BadRequest(
191+
String.format("ChargeBox %s not found!", params.getChargeBoxId())));
192192
transactionParams.setChargePointSelectList(Collections.singletonList(chargePoint));
193193

194194
// Get the transactionId of the active transaction on the connector.
195195
// If no transaction active don't send RemoteStop
196196
var transactionId = transactionRepository
197197
.getActiveTransactionId(params.getChargeBoxId(), params.getConnectorId())
198-
.orElseThrow(() -> new BadRequestException(String.format(
198+
.orElseThrow(() -> new SteveException.BadRequest(String.format(
199199
"No active transaction found for connector %s at ChargeBox %s!",
200200
params.getConnectorId(), params.getChargeBoxId())));
201201
// check the user is allowed to stop this transaction (actual only the one who started it!)
202202
var ocppTag = transactionRepository
203203
.getTransaction(transactionId)
204-
.orElseThrow(() -> new BadRequestException(String.format("Transaction %s not found!", transactionId)))
204+
.orElseThrow(
205+
() -> new SteveException.BadRequest(String.format("Transaction %s not found!", transactionId)))
205206
.getOcppIdTag();
206207
if (!ocppTag.contentEquals(params.getOcppTag())) {
207-
throw new BadRequestException("The transaction was authorised with another OCPP Tag!");
208+
throw new SteveException.BadRequest("The transaction was authorised with another OCPP Tag!");
208209
}
209210
transactionParams.setTransactionId(transactionId);
210211

@@ -217,14 +218,14 @@ public Integer postUnlockCon(@Valid ApiChargePointUnlock params) {
217218
// only one active task per charge box over api; to be discussed!
218219
if (activeTaskOnChargeBox(params.getChargeBoxId())) {
219220
var errMsg = String.format("Active task found on ChargeBox %s!", params.getChargeBoxId());
220-
throw new BadRequestException(errMsg);
221+
throw new SteveException.BadRequest(errMsg);
221222
}
222223

223224
var transactionParams = new UnlockConnectorParams();
224225
var chargePoint = chargePointRepository
225226
.getChargePointSelect(params.getChargeBoxId())
226-
.orElseThrow(() ->
227-
new BadRequestException(String.format("ChargeBox %s not found!", params.getChargeBoxId())));
227+
.orElseThrow(() -> new SteveException.BadRequest(
228+
String.format("ChargeBox %s not found!", params.getChargeBoxId())));
228229
transactionParams.setChargePointSelectList(Collections.singletonList(chargePoint));
229230

230231
/* If a active transaction is found, don't unlock the connection. */
@@ -234,7 +235,7 @@ public Integer postUnlockCon(@Valid ApiChargePointUnlock params) {
234235
var errMsg = String.format(
235236
"Active transaction found for connector %s at ChargeBox %s!",
236237
params.getConnectorId(), params.getChargeBoxId());
237-
throw new BadRequestException(errMsg);
238+
throw new SteveException.BadRequest(errMsg);
238239
}
239240

240241
transactionParams.setConnectorId(params.getConnectorId());

steve-api/src/main/java/de/rwth/idsg/steve/web/api/ReservationsRestController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import de.rwth.idsg.steve.SteveException;
2222
import de.rwth.idsg.steve.repository.dto.Reservation;
2323
import de.rwth.idsg.steve.service.ReservationsService;
24-
import de.rwth.idsg.steve.web.api.exception.NotFoundException;
2524
import de.rwth.idsg.steve.web.dto.ReservationForm;
2625
import de.rwth.idsg.steve.web.dto.ReservationQueryForm;
2726
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -59,8 +58,8 @@ public ResponseEntity<Reservation> addReservation(@Valid @RequestBody Reservatio
5958
var id = reservationsService.addReservation(form);
6059
var body = reservationsService
6160
.getReservation(id)
62-
.orElseThrow(
63-
() -> new SteveException("Reservation not found after creation, this should never happen"));
61+
.orElseThrow(() -> new SteveException.InternalError(
62+
"Reservation not found after creation, this should never happen"));
6463
var location = ServletUriComponentsBuilder.fromCurrentRequest()
6564
.path("/{id}")
6665
.buildAndExpand(id)
@@ -72,7 +71,7 @@ public ResponseEntity<Reservation> addReservation(@Valid @RequestBody Reservatio
7271
public Reservation deleteReservation(@PathVariable int id) {
7372
var reservation = reservationsService
7473
.getReservation(id)
75-
.orElseThrow(() -> new NotFoundException(String.format("Reservation with id %d not found", id)));
74+
.orElseThrow(() -> new SteveException.NotFound("Reservation with id %d not found", id));
7675
reservationsService.deleteReservation(id);
7776
return reservation;
7877
}

steve-api/src/main/java/de/rwth/idsg/steve/web/api/TasksRestController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
package de.rwth.idsg.steve.web.api;
2020

21+
import de.rwth.idsg.steve.SteveException;
2122
import de.rwth.idsg.steve.repository.TaskStore;
2223
import de.rwth.idsg.steve.web.api.dto.ApiTaskInfo;
2324
import de.rwth.idsg.steve.web.api.dto.ApiTaskList;
24-
import de.rwth.idsg.steve.web.api.exception.NotFoundException;
2525
import io.swagger.v3.oas.annotations.tags.Tag;
2626
import lombok.RequiredArgsConstructor;
2727
import org.springframework.http.MediaType;
@@ -81,7 +81,7 @@ public ApiTaskList clearFinished() {
8181
public ApiTaskInfo getTaskDetails(@RequestParam(name = "id") @Valid @NotNull @Positive Integer taskId) {
8282
var task = taskStore.get(taskId);
8383
if (task == null) {
84-
throw new NotFoundException("Task not found: " + taskId);
84+
throw new SteveException.NotFound("Task not found: " + taskId);
8585
}
8686
return new ApiTaskInfo(taskId, task);
8787
}

steve-api/src/main/java/de/rwth/idsg/steve/web/api/TransactionsRestController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
package de.rwth.idsg.steve.web.api;
2020

21+
import de.rwth.idsg.steve.SteveException;
2122
import de.rwth.idsg.steve.repository.TransactionRepository;
2223
import de.rwth.idsg.steve.repository.dto.Transaction;
23-
import de.rwth.idsg.steve.web.api.exception.BadRequestException;
2424
import de.rwth.idsg.steve.web.dto.TransactionQueryForm;
2525
import io.swagger.v3.oas.annotations.Operation;
2626
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -62,7 +62,7 @@ public class TransactionsRestController {
6262
@GetMapping(value = "")
6363
public List<Transaction> get(@Valid @ParameterObject TransactionQueryForm.TransactionQueryFormForApi params) {
6464
if (params.isReturnCSV()) {
65-
throw new BadRequestException("returnCSV=true is not supported for API calls");
65+
throw new SteveException.BadRequest("returnCSV=true is not supported for API calls");
6666
}
6767

6868
return transactionRepository.getTransactions(params);

steve-api/src/main/java/de/rwth/idsg/steve/web/api/exception/BadRequestException.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

steve-api/src/main/java/de/rwth/idsg/steve/web/api/exception/NotFoundException.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

steve-api/src/test/java/de/rwth/idsg/steve/web/api/ChargePointsRestControllerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
package de.rwth.idsg.steve.web.api;
2020

21+
import de.rwth.idsg.steve.SteveException;
2122
import de.rwth.idsg.steve.repository.dto.ChargePoint;
2223
import de.rwth.idsg.steve.service.ChargePointsService;
23-
import de.rwth.idsg.steve.web.api.exception.NotFoundException;
2424
import de.rwth.idsg.steve.web.dto.ChargePointForm;
2525
import jooq.steve.db.tables.records.ChargeBoxRecord;
2626
import org.junit.jupiter.api.BeforeEach;
@@ -89,7 +89,7 @@ public void testGet_withOneResult() {
8989
@Test
9090
@DisplayName("GET one: Entity not found, expected 404")
9191
public void testGetOne_notFound() {
92-
when(chargePointsService.getDetails(anyInt())).thenThrow(new NotFoundException(""));
92+
when(chargePointsService.getDetails(anyInt())).thenThrow(new SteveException.NotFound(""));
9393

9494
assertThat(mockMvc.perform(get("/api/v1/chargeboxes/1"))).hasStatus(HttpStatus.NOT_FOUND);
9595
}

0 commit comments

Comments
 (0)