-
Notifications
You must be signed in to change notification settings - Fork 0
Spring Boot alignment: DTOs, MapStruct, GlobalExceptionHandler, MockMvc tests, CI #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
13
commits into
main
Choose a base branch
from
copilot/audit-spring-boot-dependencies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
361ad8b
Initial plan for Spring Boot alignment task
Copilot 6877ac8
Add DTOs, MapStruct mappers, GlobalExceptionHandler, @Validated contr…
Copilot 45d7590
Rewrite EntityControllerTest to use MockMvc with @SpringBootTest
Copilot 78ceb14
Rewrite GridControllerTest to use MockMvc with @SpringBootTest
Copilot ed7b9e0
Rewrite LocationControllerTest to use MockMvc with @SpringBootTest
Copilot 88767e7
Rewrite EnvironmentControllerTest to use MockMvc integration testing
Copilot 10f806c
Rewrite DebugControllerTest with comprehensive MockMvc-based tests
Copilot 71ce4b8
Return DTOs directly from controllers, add existence checks for delet…
Copilot d584877
Fix @PathVariable binding, log levels, validation message, revert mvn…
Copilot 01de1b9
Address review: WARN for 400s, private final entityNamePool, empty gr…
Copilot de0f187
Use consistent getFirst() for grid list access in DebugController
Copilot 57bb7e7
Add CI workflow for compilation and test checks
Copilot 22af906
Log all 400 handlers at WARN, use ./mvnw in CI, fix location error me…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up JDK 21 | ||
| uses: actions/setup-java@v4 | ||
| with: | ||
| java-version: '21' | ||
| distribution: 'temurin' | ||
| cache: maven | ||
|
|
||
| - name: Compile | ||
| run: ./mvnw compile -B | ||
|
|
||
| - name: Run tests | ||
| run: ./mvnw test -B |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.14/apache-maven-3.9.14-bin.zip | ||
| wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.0/maven-wrapper-3.3.0.jar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,120 +2,93 @@ | |
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import preponderous.viron.exceptions.EntityCreationException; | ||
| import preponderous.viron.dto.EntityDto; | ||
| import preponderous.viron.exceptions.NotFoundException; | ||
| import preponderous.viron.exceptions.ServiceException; | ||
| import preponderous.viron.factories.EntityFactory; | ||
| import preponderous.viron.mappers.EntityMapper; | ||
| import preponderous.viron.models.Entity; | ||
| import preponderous.viron.repositories.EntityRepository; | ||
|
|
||
| import jakarta.validation.constraints.Min; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/entities") | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| @Validated | ||
| public class EntityController { | ||
| private final EntityRepository entityRepository; | ||
| private final EntityFactory entityFactory; | ||
| private final EntityMapper entityMapper; | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<List<Entity>> getAllEntities() { | ||
| try { | ||
| return ResponseEntity.ok(entityRepository.findAll()); | ||
| } catch (Exception e) { | ||
| log.error("Error fetching all entities: {}", e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| } | ||
| public List<EntityDto> getAllEntities() { | ||
| List<Entity> entities = entityRepository.findAll(); | ||
| return entityMapper.toDtoList(entities); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Entity> getEntityById(@PathVariable int id) { | ||
| try { | ||
| return entityRepository.findById(id) | ||
| .map(ResponseEntity::ok) | ||
| .orElse(ResponseEntity.notFound().build()); | ||
| } catch (Exception e) { | ||
| log.error("Error fetching entity by id {}: {}", id, e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| } | ||
| public EntityDto getEntityById(@PathVariable @Min(1) int id) { | ||
| Entity entity = entityRepository.findById(id) | ||
| .orElseThrow(() -> new NotFoundException("Entity not found with id: " + id)); | ||
| return entityMapper.toDto(entity); | ||
| } | ||
|
|
||
| @GetMapping("/environment/{environmentId}") | ||
| public ResponseEntity<List<Entity>> getEntitiesInEnvironment(@PathVariable int environmentId) { | ||
| try { | ||
| return ResponseEntity.ok(entityRepository.findByEnvironmentId(environmentId)); | ||
| } catch (Exception e) { | ||
| log.error("Error fetching entities in environment {}: {}", environmentId, e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| } | ||
| public List<EntityDto> getEntitiesInEnvironment(@PathVariable @Min(1) int environmentId) { | ||
| List<Entity> entities = entityRepository.findByEnvironmentId(environmentId); | ||
| return entityMapper.toDtoList(entities); | ||
| } | ||
|
|
||
| @GetMapping("/grid/{gridId}") | ||
| public ResponseEntity<List<Entity>> getEntitiesInGrid(@PathVariable int gridId) { | ||
| try { | ||
| return ResponseEntity.ok(entityRepository.findByGridId(gridId)); | ||
| } catch (Exception e) { | ||
| log.error("Error fetching entities in grid {}: {}", gridId, e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| } | ||
| public List<EntityDto> getEntitiesInGrid(@PathVariable @Min(1) int gridId) { | ||
| List<Entity> entities = entityRepository.findByGridId(gridId); | ||
| return entityMapper.toDtoList(entities); | ||
| } | ||
|
|
||
| @GetMapping("/location/{locationId}") | ||
| public ResponseEntity<List<Entity>> getEntitiesInLocation(@PathVariable int locationId) { | ||
| try { | ||
| return ResponseEntity.ok(entityRepository.findByLocationId(locationId)); | ||
| } catch (Exception e) { | ||
| log.error("Error fetching entities in location {}: {}", locationId, e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| } | ||
| public List<EntityDto> getEntitiesInLocation(@PathVariable @Min(1) int locationId) { | ||
| List<Entity> entities = entityRepository.findByLocationId(locationId); | ||
| return entityMapper.toDtoList(entities); | ||
| } | ||
|
|
||
| @GetMapping("/unassigned") | ||
| public ResponseEntity<List<Entity>> getEntitiesNotInAnyLocation() { | ||
| try { | ||
| return ResponseEntity.ok(entityRepository.findEntitiesNotInAnyLocation()); | ||
| } catch (Exception e) { | ||
| log.error("Error fetching unassigned entities: {}", e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| } | ||
| public List<EntityDto> getEntitiesNotInAnyLocation() { | ||
| List<Entity> entities = entityRepository.findEntitiesNotInAnyLocation(); | ||
| return entityMapper.toDtoList(entities); | ||
| } | ||
|
|
||
| @PostMapping("/{name}") | ||
| public ResponseEntity<Entity> createEntity(@PathVariable String name) { | ||
| try { | ||
| Entity newEntity = entityFactory.createEntity(name); | ||
| return ResponseEntity.ok(newEntity); | ||
| } catch (EntityCreationException e) { | ||
| log.error("Error creating entity with name {}: {}", name, e.getMessage()); | ||
| return ResponseEntity.badRequest().build(); | ||
| } catch (Exception e) { | ||
| log.error("Unexpected error creating entity: {}", e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| } | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public EntityDto createEntity(@PathVariable @NotBlank String name) { | ||
| Entity newEntity = entityFactory.createEntity(name); | ||
| return entityMapper.toDto(newEntity); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Void> deleteEntity(@PathVariable int id) { | ||
| try { | ||
| return entityRepository.deleteById(id) | ||
| ? ResponseEntity.ok().build() | ||
| : ResponseEntity.notFound().build(); | ||
| } catch (Exception e) { | ||
| log.error("Error deleting entity {}: {}", id, e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| public void deleteEntity(@PathVariable @Min(1) int id) { | ||
| if (entityRepository.findById(id).isEmpty()) { | ||
| throw new NotFoundException("Entity not found with id: " + id); | ||
| } | ||
| if (!entityRepository.deleteById(id)) { | ||
| throw new ServiceException("Failed to delete entity with id: " + id); | ||
| } | ||
| } | ||
|
Comment on lines
74
to
83
|
||
|
|
||
| @PatchMapping("/{id}/name/{name}") | ||
| public ResponseEntity<Void> updateEntityName(@PathVariable int id, @PathVariable String name) { | ||
| try { | ||
| return entityRepository.updateName(id, name) | ||
| ? ResponseEntity.ok().build() | ||
| : ResponseEntity.notFound().build(); | ||
| } catch (Exception e) { | ||
| log.error("Error updating name for entity {}: {}", id, e.getMessage()); | ||
| return ResponseEntity.internalServerError().build(); | ||
| public void updateEntityName(@PathVariable @Min(1) int id, @PathVariable @NotBlank String name) { | ||
| if (entityRepository.findById(id).isEmpty()) { | ||
| throw new NotFoundException("Entity not found with id: " + id); | ||
| } | ||
| if (!entityRepository.updateName(id, name)) { | ||
| throw new ServiceException("Failed to update name for entity " + id); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This endpoint assumes a grid was created/fetched successfully. A few lines below it does
Grid grid = grids.get(0);, which will throw ifgridService.getGridsInEnvironment(...)returns an empty list and will surface as a generic 500. Consider explicitly checking for an empty result and throwing a controlled exception/message before indexing.