Skip to content

Commit c10f732

Browse files
committed
sonarcloud.yml added
1 parent e43e8b6 commit c10f732

8 files changed

Lines changed: 80 additions & 37 deletions

File tree

.github/workflows/sonarcloud.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: SonarCloud
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
types: [opened, synchronize, reopened]
8+
9+
jobs:
10+
build:
11+
name: Build and Analyze
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Set up JDK 17
16+
uses: actions/setup-java@v4
17+
with:
18+
java-version: '17'
19+
distribution: 'temurin'
20+
- name: Cache SonarCloud packages
21+
uses: actions/cache@v4
22+
with:
23+
path: ~/.sonar/cache
24+
key: ${{ runner.os }}-sonar
25+
restore-keys: ${{ runner.os }}-sonar
26+
- name: SonarCloud Scan
27+
uses: SonarSource/sonarcloud-github-action@v2
28+
with:
29+
projectBaseDir: .
30+
env:
31+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
32+
SONAR_HOST_URL: "https://sonarcloud.io"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
[![Build Status](https://github.com/vijayagopalsb/SpringCleanArch/actions/workflows/ci.yml/badge.svg)](https://github.com/vijayagopalsb/SpringCleanArch/actions)
55
![Java](https://img.shields.io/badge/Java-17+-blue)
6+
[![SonarCloud Status](https://sonarcloud.io/api/project_badges/measure?project=vijayagopalsb_CleanArchitecture&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=vijayagopalsb_CleanArchitecture)
67
![License](https://img.shields.io/badge/license-MIT-green)
78

89
---

src/main/java/com/example/app/DemoApplication.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import static java.lang.System.out;
65

76
@SpringBootApplication
87
public class DemoApplication {
98

109
public static void main(String[] args) {
11-
out.println ("Starting application...");
10+
1211
SpringApplication.run(DemoApplication.class, args);
1312

1413

src/main/java/com/example/app/api/UserController.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ public AppUser createUser(@RequestBody AppUser user) {
5858
@GetMapping("/{id}")
5959
public AppUser getUser(@PathVariable Long id) {
6060
logger.info("Fetching user with id: {}", id);
61-
// return userService.getUserById(id).orElseThrow(() -> new
62-
// RuntimeException("User not Found!"));
63-
// return userService.getUserById(id).orElseThrow(() -> new
64-
// ResponseStatusException(HttpStatus.NOT_FOUND, "User not Found"));
6561
return userService.getUserById(id).orElseThrow(() -> new UserNotFoundException(id));
6662
}
6763

@@ -102,7 +98,7 @@ public AppUser assignRoles(@PathVariable Long id, @RequestBody Map<String, List<
10298
}
10399

104100
@PostMapping("/{id}/photo")
105-
public ResponseEntity<?> uploadPhoto(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
101+
public ResponseEntity<Map<String, String>> uploadPhoto(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
106102
String uploadDir = "uploads/";
107103
String fileName = id + "_" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
108104
Path filePath = Paths.get(uploadDir, fileName);
@@ -114,26 +110,25 @@ public ResponseEntity<?> uploadPhoto(@PathVariable Long id, @RequestParam("file"
114110
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", "Photo upload failed"));
115111
}
116112

117-
// AppUser user = userService.getUserById(id).orElseThrow(() -> new
118-
// UserNotFoundException(id));
119-
// user.setPhotoPath(filePath.toString());
120-
userService.updateUserPhoto(id, fileName.toString());
113+
userService.updateUserPhoto(id, fileName);
121114

122115
// Return the file name/path as response
123-
return ResponseEntity.ok(Map.of("message", "Photo uploaded successfully", "photoPath", fileName.toString()));
116+
return ResponseEntity.ok(Map.of("message", "Photo uploaded successfully", "photoPath", fileName));
124117
}
125118

126119
@GetMapping("/photos/{filename:.+}") // allows for extensions (like .jpg, .png)
127120
public ResponseEntity<Resource> getPhoto(@PathVariable String filename) {
128-
121+
129122
logger.info("Received request to fetch photo: {}", filename);
130123

131124
try {
132125
String uploadDir = System.getProperty("user.dir") + "/uploads/";
133126
Path filePath = Paths.get(uploadDir, filename).normalize();
134-
135-
logger.info("--- Looking for photo at: {}", filePath.toString());
136-
127+
128+
if (logger.isInfoEnabled()) {
129+
logger.info("Looking for photo at: {}", filePath);
130+
}
131+
137132
Resource resource = new UrlResource(filePath.toUri());
138133

139134
if (!resource.exists()) {
@@ -152,6 +147,6 @@ public ResponseEntity<Resource> getPhoto(@PathVariable String filename) {
152147
logger.error("Error serving photo {}: {}", filename, exception.getMessage());
153148
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
154149
}
155-
150+
156151
}
157152
}

src/main/java/com/example/app/api/exceptions/ApiExceptionHandler.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.springframework.web.bind.annotation.ControllerAdvice;
1111
import org.springframework.web.bind.annotation.ExceptionHandler;
1212

13+
import com.example.app.constants.ApiConstants;
14+
1315
import jakarta.servlet.http.HttpServletRequest;
1416

1517
@ControllerAdvice
@@ -19,13 +21,12 @@ public class ApiExceptionHandler {
1921
@ExceptionHandler(UserNotFoundException.class)
2022
public ResponseEntity<Object> handleUserNotFound(UserNotFoundException ex, HttpServletRequest request) {
2123
Map<String, Object> body = new HashMap<>();
22-
body.put("timestamp", ZonedDateTime.now());
23-
body.put("status", HttpStatus.NOT_FOUND.value());
24-
body.put("error", "Not Found");
25-
body.put("message", ex.getMessage());
26-
body.put("path", request.getRequestURI());
27-
// Optionally, add more fields, e.g., "path"
28-
return new ResponseEntity<Object>(body, HttpStatus.NOT_FOUND);
24+
body.put(ApiConstants.TIMESTAMP, ZonedDateTime.now());
25+
body.put(ApiConstants.STATUS, HttpStatus.NOT_FOUND.value());
26+
body.put(ApiConstants.ERROR, "Not Found");
27+
body.put(ApiConstants.MESSAGE, ex.getMessage());
28+
body.put(ApiConstants.PATH, request.getRequestURI());
29+
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
2930
}
3031

3132
// 2. Custom Duplicate Email Exception - More Specific
@@ -34,11 +35,11 @@ public ResponseEntity<Map<String, Object>> handleDuplicateEmail(DuplicateEmailEx
3435
HttpServletRequest request) {
3536

3637
Map<String, Object> body = new HashMap<>();
37-
body.put("timestamp", ZonedDateTime.now());
38-
body.put("status", 409);
39-
body.put("error", "Conflict");
40-
body.put("message", duplicateEmailException.getMessage());
41-
body.put("path", request.getRequestURI());
38+
body.put(ApiConstants.TIMESTAMP, ZonedDateTime.now());
39+
body.put(ApiConstants.STATUS, 409);
40+
body.put(ApiConstants.ERROR, "Conflict");
41+
body.put(ApiConstants.MESSAGE, duplicateEmailException.getMessage());
42+
body.put(ApiConstants.PATH, request.getRequestURI());
4243
return new ResponseEntity<>(body, HttpStatus.CONFLICT);
4344

4445
}
@@ -60,11 +61,11 @@ public ResponseEntity<Map<String, Object>> handleAll(Exception ex, HttpServletRe
6061
// --- Utility Method for Response Construction ---
6162
private ResponseEntity<Map<String, Object>> buildResponse(int status, String error, String message, String path) {
6263
Map<String, Object> body = new HashMap<>();
63-
body.put("timestamp", ZonedDateTime.now());
64-
body.put("status", status);
65-
body.put("error", error);
66-
body.put("message", message);
67-
body.put("path", path);
64+
body.put(ApiConstants.TIMESTAMP, ZonedDateTime.now());
65+
body.put(ApiConstants.STATUS, status);
66+
body.put( ApiConstants.ERROR, error);
67+
body.put(ApiConstants.MESSAGE, message);
68+
body.put(ApiConstants.PATH, path);
6869
return new ResponseEntity<>(body, HttpStatus.valueOf(status));
6970
}
7071

src/main/java/com/example/app/application/UserService.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ public AppUser assignRolesToUser(Long userId, List<String> roleNames) {
8383
return userRepository.save(user);
8484
}
8585

86-
// public AppUser saveUser(AppUser user) {
87-
// return userRepository.save(user);
88-
// }
8986

9087
// Update User with Photo
9188
public AppUser updateUserPhoto(Long id, String photoPath) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.app.constants;
2+
3+
public class ApiConstants {
4+
5+
private ApiConstants() {} // To prevent instantiation
6+
7+
public static final String TIMESTAMP = "timestamp";
8+
9+
public static final String STATUS = "status";
10+
11+
public static final String ERROR = "error";
12+
13+
public static final String MESSAGE = "message";
14+
15+
public static final String PATH = "path";
16+
17+
}

src/test/java/com/example/app/SpringCleanArchApplicationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class SpringCleanArchApplicationTests {
88

99
@Test
1010
void contextLoads() {
11+
// This test is intentionally empty to verify the application context loads successfully.
1112
}
1213

1314
}

0 commit comments

Comments
 (0)