mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-15 20:15:03 +00:00
🤖 format everything with pre-commit by stirlingbot (#3697)
Auto-generated by [create-pull-request][1] with **stirlingbot** [1]: https://github.com/peter-evans/create-pull-request Signed-off-by: stirlingbot[bot] <stirlingbot[bot]@users.noreply.github.com> Co-authored-by: stirlingbot[bot] <195170888+stirlingbot[bot]@users.noreply.github.com>
This commit is contained in:
parent
5393ae24cb
commit
a49eb3a629
24
.github/scripts/check_language_properties.py
vendored
24
.github/scripts/check_language_properties.py
vendored
@ -196,7 +196,9 @@ def check_for_differences(reference_file, file_list, branch, actor):
|
|||||||
|
|
||||||
if len(file_list) == 1:
|
if len(file_list) == 1:
|
||||||
file_arr = file_list[0].split()
|
file_arr = file_list[0].split()
|
||||||
base_dir = os.path.abspath(os.path.join(os.getcwd(), "stirling-pdf", "src", "main", "resources"))
|
base_dir = os.path.abspath(
|
||||||
|
os.path.join(os.getcwd(), "stirling-pdf", "src", "main", "resources")
|
||||||
|
)
|
||||||
|
|
||||||
for file_path in file_arr:
|
for file_path in file_arr:
|
||||||
file_normpath = os.path.normpath(file_path)
|
file_normpath = os.path.normpath(file_path)
|
||||||
@ -216,10 +218,19 @@ def check_for_differences(reference_file, file_list, branch, actor):
|
|||||||
or (
|
or (
|
||||||
# only local windows command
|
# only local windows command
|
||||||
not file_normpath.startswith(
|
not file_normpath.startswith(
|
||||||
os.path.join("", "stirling-pdf", "src", "main", "resources", "messages_")
|
os.path.join(
|
||||||
|
"", "stirling-pdf", "src", "main", "resources", "messages_"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
and not file_normpath.startswith(
|
and not file_normpath.startswith(
|
||||||
os.path.join(os.getcwd(), "stirling-pdf", "src", "main", "resources", "messages_")
|
os.path.join(
|
||||||
|
os.getcwd(),
|
||||||
|
"stirling-pdf",
|
||||||
|
"src",
|
||||||
|
"main",
|
||||||
|
"resources",
|
||||||
|
"messages_",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
or not file_normpath.endswith(".properties")
|
or not file_normpath.endswith(".properties")
|
||||||
@ -377,7 +388,12 @@ if __name__ == "__main__":
|
|||||||
else:
|
else:
|
||||||
file_list = glob.glob(
|
file_list = glob.glob(
|
||||||
os.path.join(
|
os.path.join(
|
||||||
os.getcwd(), "stirling-pdf", "src", "main", "resources", "messages_*.properties"
|
os.getcwd(),
|
||||||
|
"stirling-pdf",
|
||||||
|
"src",
|
||||||
|
"main",
|
||||||
|
"resources",
|
||||||
|
"messages_*.properties",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
update_missing_keys(args.reference_file, file_list)
|
update_missing_keys(args.reference_file, file_list)
|
||||||
|
@ -10,11 +10,11 @@ public class TeamWithUserCountDTO {
|
|||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private Long userCount;
|
private Long userCount;
|
||||||
|
|
||||||
// Constructor for JPQL projection
|
// Constructor for JPQL projection
|
||||||
public TeamWithUserCountDTO(Long id, String name, Long userCount) {
|
public TeamWithUserCountDTO(Long id, String name, Long userCount) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.userCount = userCount;
|
this.userCount = userCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,7 +228,7 @@ public class AccountWebController {
|
|||||||
User user = iterator.next();
|
User user = iterator.next();
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
boolean shouldRemove = false;
|
boolean shouldRemove = false;
|
||||||
|
|
||||||
// Check if user is an INTERNAL_API_USER
|
// Check if user is an INTERNAL_API_USER
|
||||||
for (Authority authority : user.getAuthorities()) {
|
for (Authority authority : user.getAuthorities()) {
|
||||||
if (authority.getAuthority().equals(Role.INTERNAL_API_USER.getRoleId())) {
|
if (authority.getAuthority().equals(Role.INTERNAL_API_USER.getRoleId())) {
|
||||||
@ -237,12 +237,12 @@ public class AccountWebController {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also check if user is part of the Internal team
|
// Also check if user is part of the Internal team
|
||||||
if (user.getTeam() != null && user.getTeam().getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
if (user.getTeam() != null && user.getTeam().getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
||||||
shouldRemove = true;
|
shouldRemove = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the user if either condition is true
|
// Remove the user if either condition is true
|
||||||
if (shouldRemove) {
|
if (shouldRemove) {
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
|
@ -56,12 +56,12 @@ public class TeamController {
|
|||||||
return new RedirectView("/adminSettings?messageType=teamNameExists");
|
return new RedirectView("/adminSettings?messageType=teamNameExists");
|
||||||
}
|
}
|
||||||
Team team = existing.get();
|
Team team = existing.get();
|
||||||
|
|
||||||
// Prevent renaming the Internal team
|
// Prevent renaming the Internal team
|
||||||
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
||||||
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible");
|
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible");
|
||||||
}
|
}
|
||||||
|
|
||||||
team.setName(newName);
|
team.setName(newName);
|
||||||
teamRepository.save(team);
|
teamRepository.save(team);
|
||||||
return new RedirectView("/adminSettings?messageType=teamRenamed");
|
return new RedirectView("/adminSettings?messageType=teamRenamed");
|
||||||
@ -77,12 +77,12 @@ public class TeamController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Team team = teamOpt.get();
|
Team team = teamOpt.get();
|
||||||
|
|
||||||
// Prevent deleting the Internal team
|
// Prevent deleting the Internal team
|
||||||
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
||||||
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible");
|
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible");
|
||||||
}
|
}
|
||||||
|
|
||||||
long memberCount = userRepository.countByTeam(team);
|
long memberCount = userRepository.countByTeam(team);
|
||||||
if (memberCount > 0) {
|
if (memberCount > 0) {
|
||||||
return new RedirectView("/adminSettings?messageType=teamHasUsers");
|
return new RedirectView("/adminSettings?messageType=teamHasUsers");
|
||||||
@ -91,36 +91,36 @@ public class TeamController {
|
|||||||
teamRepository.delete(team);
|
teamRepository.delete(team);
|
||||||
return new RedirectView("/adminSettings?messageType=teamDeleted");
|
return new RedirectView("/adminSettings?messageType=teamDeleted");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||||
@PostMapping("/addUser")
|
@PostMapping("/addUser")
|
||||||
@Transactional
|
@Transactional
|
||||||
public RedirectView addUserToTeam(
|
public RedirectView addUserToTeam(
|
||||||
@RequestParam("teamId") Long teamId,
|
@RequestParam("teamId") Long teamId,
|
||||||
@RequestParam("userId") Long userId) {
|
@RequestParam("userId") Long userId) {
|
||||||
|
|
||||||
// Find the team
|
// Find the team
|
||||||
Team team = teamRepository.findById(teamId)
|
Team team = teamRepository.findById(teamId)
|
||||||
.orElseThrow(() -> new RuntimeException("Team not found"));
|
.orElseThrow(() -> new RuntimeException("Team not found"));
|
||||||
|
|
||||||
// Prevent adding users to the Internal team
|
// Prevent adding users to the Internal team
|
||||||
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
||||||
return new RedirectView("/teams?error=internalTeamNotAccessible");
|
return new RedirectView("/teams?error=internalTeamNotAccessible");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the user
|
// Find the user
|
||||||
User user = userRepository.findById(userId)
|
User user = userRepository.findById(userId)
|
||||||
.orElseThrow(() -> new RuntimeException("User not found"));
|
.orElseThrow(() -> new RuntimeException("User not found"));
|
||||||
|
|
||||||
// Check if user is in the Internal team - prevent moving them
|
// Check if user is in the Internal team - prevent moving them
|
||||||
if (user.getTeam() != null && user.getTeam().getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
if (user.getTeam() != null && user.getTeam().getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
||||||
return new RedirectView("/teams/" + teamId + "?error=cannotMoveInternalUsers");
|
return new RedirectView("/teams/" + teamId + "?error=cannotMoveInternalUsers");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign user to team
|
// Assign user to team
|
||||||
user.setTeam(team);
|
user.setTeam(team);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
// Redirect back to team details page
|
// Redirect back to team details page
|
||||||
return new RedirectView("/teams/" + teamId + "?messageType=userAdded");
|
return new RedirectView("/teams/" + teamId + "?messageType=userAdded");
|
||||||
}
|
}
|
||||||
|
@ -246,7 +246,7 @@ public class UserController {
|
|||||||
// If the role ID is not valid, redirect with an error message
|
// If the role ID is not valid, redirect with an error message
|
||||||
return new RedirectView("/adminSettings?messageType=invalidRole", true);
|
return new RedirectView("/adminSettings?messageType=invalidRole", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use teamId if provided, otherwise use default team
|
// Use teamId if provided, otherwise use default team
|
||||||
Long effectiveTeamId = teamId;
|
Long effectiveTeamId = teamId;
|
||||||
if (effectiveTeamId == null) {
|
if (effectiveTeamId == null) {
|
||||||
@ -261,7 +261,7 @@ public class UserController {
|
|||||||
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible", true);
|
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authType.equalsIgnoreCase(AuthenticationType.SSO.toString())) {
|
if (authType.equalsIgnoreCase(AuthenticationType.SSO.toString())) {
|
||||||
userService.saveUser(username, AuthenticationType.SSO, effectiveTeamId, role);
|
userService.saveUser(username, AuthenticationType.SSO, effectiveTeamId, role);
|
||||||
} else {
|
} else {
|
||||||
@ -309,7 +309,7 @@ public class UserController {
|
|||||||
return new RedirectView("/adminSettings?messageType=invalidRole", true);
|
return new RedirectView("/adminSettings?messageType=invalidRole", true);
|
||||||
}
|
}
|
||||||
User user = userOpt.get();
|
User user = userOpt.get();
|
||||||
|
|
||||||
// Update the team if a teamId is provided
|
// Update the team if a teamId is provided
|
||||||
if (teamId != null) {
|
if (teamId != null) {
|
||||||
Team team = teamRepository.findById(teamId).orElse(null);
|
Team team = teamRepository.findById(teamId).orElse(null);
|
||||||
@ -318,17 +318,17 @@ public class UserController {
|
|||||||
if (TeamService.INTERNAL_TEAM_NAME.equals(team.getName())) {
|
if (TeamService.INTERNAL_TEAM_NAME.equals(team.getName())) {
|
||||||
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible", true);
|
return new RedirectView("/adminSettings?messageType=internalTeamNotAccessible", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent moving users from Internal team
|
// Prevent moving users from Internal team
|
||||||
if (user.getTeam() != null && TeamService.INTERNAL_TEAM_NAME.equals(user.getTeam().getName())) {
|
if (user.getTeam() != null && TeamService.INTERNAL_TEAM_NAME.equals(user.getTeam().getName())) {
|
||||||
return new RedirectView("/adminSettings?messageType=cannotMoveInternalUsers", true);
|
return new RedirectView("/adminSettings?messageType=cannotMoveInternalUsers", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
user.setTeam(team);
|
user.setTeam(team);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
userService.changeRole(user, role);
|
userService.changeRole(user, role);
|
||||||
return new RedirectView(
|
return new RedirectView(
|
||||||
"/adminSettings", // Redirect to account page after adding the user
|
"/adminSettings", // Redirect to account page after adding the user
|
||||||
|
@ -38,7 +38,7 @@ public class TeamWebController {
|
|||||||
public String listTeams(Model model) {
|
public String listTeams(Model model) {
|
||||||
// Get teams with user counts using a DTO projection
|
// Get teams with user counts using a DTO projection
|
||||||
List<TeamWithUserCountDTO> allTeamsWithCounts = teamRepository.findAllTeamsWithUserCount();
|
List<TeamWithUserCountDTO> allTeamsWithCounts = teamRepository.findAllTeamsWithUserCount();
|
||||||
|
|
||||||
// Filter out the Internal team
|
// Filter out the Internal team
|
||||||
List<TeamWithUserCountDTO> teamsWithCounts = allTeamsWithCounts.stream()
|
List<TeamWithUserCountDTO> teamsWithCounts = allTeamsWithCounts.stream()
|
||||||
.filter(team -> !team.getName().equals(TeamService.INTERNAL_TEAM_NAME))
|
.filter(team -> !team.getName().equals(TeamService.INTERNAL_TEAM_NAME))
|
||||||
@ -58,7 +58,7 @@ public class TeamWebController {
|
|||||||
// Add data to the model
|
// Add data to the model
|
||||||
model.addAttribute("teamsWithCounts", teamsWithCounts);
|
model.addAttribute("teamsWithCounts", teamsWithCounts);
|
||||||
model.addAttribute("teamLastRequest", teamLastRequest);
|
model.addAttribute("teamLastRequest", teamLastRequest);
|
||||||
|
|
||||||
return "accounts/teams";
|
return "accounts/teams";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,23 +68,23 @@ public class TeamWebController {
|
|||||||
// Get the team
|
// Get the team
|
||||||
Team team = teamRepository.findById(id)
|
Team team = teamRepository.findById(id)
|
||||||
.orElseThrow(() -> new RuntimeException("Team not found"));
|
.orElseThrow(() -> new RuntimeException("Team not found"));
|
||||||
|
|
||||||
// Prevent access to Internal team
|
// Prevent access to Internal team
|
||||||
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
if (team.getName().equals(TeamService.INTERNAL_TEAM_NAME)) {
|
||||||
return "redirect:/teams?error=internalTeamNotAccessible";
|
return "redirect:/teams?error=internalTeamNotAccessible";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get users for this team directly using the direct query
|
// Get users for this team directly using the direct query
|
||||||
List<User> teamUsers = userRepository.findAllByTeamId(id);
|
List<User> teamUsers = userRepository.findAllByTeamId(id);
|
||||||
|
|
||||||
// Get all users not in this team for the Add User to Team dropdown
|
// Get all users not in this team for the Add User to Team dropdown
|
||||||
// Exclude users that are in the Internal team
|
// Exclude users that are in the Internal team
|
||||||
List<User> allUsers = userRepository.findAllWithTeam();
|
List<User> allUsers = userRepository.findAllWithTeam();
|
||||||
List<User> availableUsers = allUsers.stream()
|
List<User> availableUsers = allUsers.stream()
|
||||||
.filter(user -> (user.getTeam() == null || !user.getTeam().getId().equals(id)) &&
|
.filter(user -> (user.getTeam() == null || !user.getTeam().getId().equals(id)) &&
|
||||||
(user.getTeam() == null || !user.getTeam().getName().equals(TeamService.INTERNAL_TEAM_NAME)))
|
(user.getTeam() == null || !user.getTeam().getName().equals(TeamService.INTERNAL_TEAM_NAME)))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
// Get the latest session for each user in the team
|
// Get the latest session for each user in the team
|
||||||
List<Object[]> userSessions = sessionRepository.findLatestSessionByTeamId(id);
|
List<Object[]> userSessions = sessionRepository.findLatestSessionByTeamId(id);
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
|||||||
|
|
||||||
@Query(value = "SELECT u FROM User u LEFT JOIN FETCH u.team")
|
@Query(value = "SELECT u FROM User u LEFT JOIN FETCH u.team")
|
||||||
List<User> findAllWithTeam();
|
List<User> findAllWithTeam();
|
||||||
|
|
||||||
@Query("SELECT u FROM User u JOIN FETCH u.authorities JOIN FETCH u.team WHERE u.team.id = :teamId")
|
@Query("SELECT u FROM User u JOIN FETCH u.authorities JOIN FETCH u.team WHERE u.team.id = :teamId")
|
||||||
List<User> findAllByTeamId(@Param("teamId") Long teamId);
|
List<User> findAllByTeamId(@Param("teamId") Long teamId);
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ class UserServiceTest {
|
|||||||
// Given
|
// Given
|
||||||
String username = "testuser";
|
String username = "testuser";
|
||||||
AuthenticationType authType = AuthenticationType.WEB;
|
AuthenticationType authType = AuthenticationType.WEB;
|
||||||
|
|
||||||
when(teamRepository.findByName("Default")).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findByName("Default")).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
doNothing().when(databaseService).exportDatabase();
|
doNothing().when(databaseService).exportDatabase();
|
||||||
@ -99,7 +99,7 @@ class UserServiceTest {
|
|||||||
String password = "password123";
|
String password = "password123";
|
||||||
Long teamId = 1L;
|
Long teamId = 1L;
|
||||||
String encodedPassword = "encodedPassword123";
|
String encodedPassword = "encodedPassword123";
|
||||||
|
|
||||||
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
||||||
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
@ -124,7 +124,7 @@ class UserServiceTest {
|
|||||||
String role = Role.ADMIN.getRoleId();
|
String role = Role.ADMIN.getRoleId();
|
||||||
boolean firstLogin = true;
|
boolean firstLogin = true;
|
||||||
String encodedPassword = "encodedPassword123";
|
String encodedPassword = "encodedPassword123";
|
||||||
|
|
||||||
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
doNothing().when(databaseService).exportDatabase();
|
doNothing().when(databaseService).exportDatabase();
|
||||||
@ -150,7 +150,7 @@ class UserServiceTest {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> userService.saveUser(invalidUsername, authType)
|
() -> userService.saveUser(invalidUsername, authType)
|
||||||
);
|
);
|
||||||
|
|
||||||
verify(userRepository, never()).save(any(User.class));
|
verify(userRepository, never()).save(any(User.class));
|
||||||
verify(databaseService, never()).exportDatabase();
|
verify(databaseService, never()).exportDatabase();
|
||||||
}
|
}
|
||||||
@ -160,7 +160,7 @@ class UserServiceTest {
|
|||||||
// Given
|
// Given
|
||||||
String username = "testuser";
|
String username = "testuser";
|
||||||
Long teamId = 1L;
|
Long teamId = 1L;
|
||||||
|
|
||||||
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
doNothing().when(databaseService).exportDatabase();
|
doNothing().when(databaseService).exportDatabase();
|
||||||
@ -181,7 +181,7 @@ class UserServiceTest {
|
|||||||
String username = "testuser";
|
String username = "testuser";
|
||||||
String emptyPassword = "";
|
String emptyPassword = "";
|
||||||
Long teamId = 1L;
|
Long teamId = 1L;
|
||||||
|
|
||||||
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
doNothing().when(databaseService).exportDatabase();
|
doNothing().when(databaseService).exportDatabase();
|
||||||
@ -201,7 +201,7 @@ class UserServiceTest {
|
|||||||
// Given
|
// Given
|
||||||
String emailUsername = "test@example.com";
|
String emailUsername = "test@example.com";
|
||||||
AuthenticationType authType = AuthenticationType.SSO;
|
AuthenticationType authType = AuthenticationType.SSO;
|
||||||
|
|
||||||
when(teamRepository.findByName("Default")).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findByName("Default")).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
doNothing().when(databaseService).exportDatabase();
|
doNothing().when(databaseService).exportDatabase();
|
||||||
@ -225,7 +225,7 @@ class UserServiceTest {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> userService.saveUser(reservedUsername, authType)
|
() -> userService.saveUser(reservedUsername, authType)
|
||||||
);
|
);
|
||||||
|
|
||||||
verify(userRepository, never()).save(any(User.class));
|
verify(userRepository, never()).save(any(User.class));
|
||||||
verify(databaseService, never()).exportDatabase();
|
verify(databaseService, never()).exportDatabase();
|
||||||
}
|
}
|
||||||
@ -241,7 +241,7 @@ class UserServiceTest {
|
|||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> userService.saveUser(anonymousUsername, authType)
|
() -> userService.saveUser(anonymousUsername, authType)
|
||||||
);
|
);
|
||||||
|
|
||||||
verify(userRepository, never()).save(any(User.class));
|
verify(userRepository, never()).save(any(User.class));
|
||||||
verify(databaseService, never()).exportDatabase();
|
verify(databaseService, never()).exportDatabase();
|
||||||
}
|
}
|
||||||
@ -253,7 +253,7 @@ class UserServiceTest {
|
|||||||
String password = "password123";
|
String password = "password123";
|
||||||
Long teamId = 1L;
|
Long teamId = 1L;
|
||||||
String encodedPassword = "encodedPassword123";
|
String encodedPassword = "encodedPassword123";
|
||||||
|
|
||||||
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
||||||
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
@ -261,7 +261,7 @@ class UserServiceTest {
|
|||||||
|
|
||||||
// When & Then
|
// When & Then
|
||||||
assertThrows(SQLException.class, () -> userService.saveUser(username, password, teamId));
|
assertThrows(SQLException.class, () -> userService.saveUser(username, password, teamId));
|
||||||
|
|
||||||
// Verify user was still saved before the exception
|
// Verify user was still saved before the exception
|
||||||
verify(userRepository).save(any(User.class));
|
verify(userRepository).save(any(User.class));
|
||||||
verify(databaseService).exportDatabase();
|
verify(databaseService).exportDatabase();
|
||||||
@ -276,7 +276,7 @@ class UserServiceTest {
|
|||||||
boolean firstLogin = true;
|
boolean firstLogin = true;
|
||||||
boolean enabled = false;
|
boolean enabled = false;
|
||||||
String encodedPassword = "encodedPassword123";
|
String encodedPassword = "encodedPassword123";
|
||||||
|
|
||||||
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
||||||
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
@ -299,7 +299,7 @@ class UserServiceTest {
|
|||||||
Long teamId = 1L;
|
Long teamId = 1L;
|
||||||
String customRole = Role.LIMITED_API_USER.getRoleId();
|
String customRole = Role.LIMITED_API_USER.getRoleId();
|
||||||
String encodedPassword = "encodedPassword123";
|
String encodedPassword = "encodedPassword123";
|
||||||
|
|
||||||
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
when(passwordEncoder.encode(password)).thenReturn(encodedPassword);
|
||||||
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
when(teamRepository.findById(teamId)).thenReturn(Optional.of(mockTeam));
|
||||||
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
when(userRepository.save(any(User.class))).thenReturn(mockUser);
|
||||||
|
@ -50,7 +50,7 @@ class MergeControllerTest {
|
|||||||
mockFile1 = new MockMultipartFile("file1", "document1.pdf", "application/pdf", "PDF content 1".getBytes());
|
mockFile1 = new MockMultipartFile("file1", "document1.pdf", "application/pdf", "PDF content 1".getBytes());
|
||||||
mockFile2 = new MockMultipartFile("file2", "document2.pdf", "application/pdf", "PDF content 2".getBytes());
|
mockFile2 = new MockMultipartFile("file2", "document2.pdf", "application/pdf", "PDF content 2".getBytes());
|
||||||
mockFile3 = new MockMultipartFile("file3", "chapter3.pdf", "application/pdf", "PDF content 3".getBytes());
|
mockFile3 = new MockMultipartFile("file3", "chapter3.pdf", "application/pdf", "PDF content 3".getBytes());
|
||||||
|
|
||||||
mockDocument = mock(PDDocument.class);
|
mockDocument = mock(PDDocument.class);
|
||||||
mockMergedDocument = mock(PDDocument.class);
|
mockMergedDocument = mock(PDDocument.class);
|
||||||
mockCatalog = mock(PDDocumentCatalog.class);
|
mockCatalog = mock(PDDocumentCatalog.class);
|
||||||
@ -63,23 +63,23 @@ class MergeControllerTest {
|
|||||||
void testAddTableOfContents_WithMultipleFiles_Success() throws Exception {
|
void testAddTableOfContents_WithMultipleFiles_Success() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
MultipartFile[] files = {mockFile1, mockFile2, mockFile3};
|
MultipartFile[] files = {mockFile1, mockFile2, mockFile3};
|
||||||
|
|
||||||
// Mock the merged document setup
|
// Mock the merged document setup
|
||||||
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
||||||
when(mockMergedDocument.getNumberOfPages()).thenReturn(6);
|
when(mockMergedDocument.getNumberOfPages()).thenReturn(6);
|
||||||
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
|
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
|
||||||
when(mockMergedDocument.getPage(2)).thenReturn(mockPage2);
|
when(mockMergedDocument.getPage(2)).thenReturn(mockPage2);
|
||||||
when(mockMergedDocument.getPage(4)).thenReturn(mockPage1);
|
when(mockMergedDocument.getPage(4)).thenReturn(mockPage1);
|
||||||
|
|
||||||
// Mock individual document loading for page count
|
// Mock individual document loading for page count
|
||||||
PDDocument doc1 = mock(PDDocument.class);
|
PDDocument doc1 = mock(PDDocument.class);
|
||||||
PDDocument doc2 = mock(PDDocument.class);
|
PDDocument doc2 = mock(PDDocument.class);
|
||||||
PDDocument doc3 = mock(PDDocument.class);
|
PDDocument doc3 = mock(PDDocument.class);
|
||||||
|
|
||||||
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
||||||
when(pdfDocumentFactory.load(mockFile2)).thenReturn(doc2);
|
when(pdfDocumentFactory.load(mockFile2)).thenReturn(doc2);
|
||||||
when(pdfDocumentFactory.load(mockFile3)).thenReturn(doc3);
|
when(pdfDocumentFactory.load(mockFile3)).thenReturn(doc3);
|
||||||
|
|
||||||
when(doc1.getNumberOfPages()).thenReturn(2);
|
when(doc1.getNumberOfPages()).thenReturn(2);
|
||||||
when(doc2.getNumberOfPages()).thenReturn(2);
|
when(doc2.getNumberOfPages()).thenReturn(2);
|
||||||
when(doc3.getNumberOfPages()).thenReturn(2);
|
when(doc3.getNumberOfPages()).thenReturn(2);
|
||||||
@ -92,15 +92,15 @@ class MergeControllerTest {
|
|||||||
// Then
|
// Then
|
||||||
ArgumentCaptor<PDDocumentOutline> outlineCaptor = ArgumentCaptor.forClass(PDDocumentOutline.class);
|
ArgumentCaptor<PDDocumentOutline> outlineCaptor = ArgumentCaptor.forClass(PDDocumentOutline.class);
|
||||||
verify(mockCatalog).setDocumentOutline(outlineCaptor.capture());
|
verify(mockCatalog).setDocumentOutline(outlineCaptor.capture());
|
||||||
|
|
||||||
PDDocumentOutline capturedOutline = outlineCaptor.getValue();
|
PDDocumentOutline capturedOutline = outlineCaptor.getValue();
|
||||||
assertNotNull(capturedOutline);
|
assertNotNull(capturedOutline);
|
||||||
|
|
||||||
// Verify that documents were loaded for page count
|
// Verify that documents were loaded for page count
|
||||||
verify(pdfDocumentFactory).load(mockFile1);
|
verify(pdfDocumentFactory).load(mockFile1);
|
||||||
verify(pdfDocumentFactory).load(mockFile2);
|
verify(pdfDocumentFactory).load(mockFile2);
|
||||||
verify(pdfDocumentFactory).load(mockFile3);
|
verify(pdfDocumentFactory).load(mockFile3);
|
||||||
|
|
||||||
// Verify document closing
|
// Verify document closing
|
||||||
verify(doc1).close();
|
verify(doc1).close();
|
||||||
verify(doc2).close();
|
verify(doc2).close();
|
||||||
@ -111,11 +111,11 @@ class MergeControllerTest {
|
|||||||
void testAddTableOfContents_WithSingleFile_Success() throws Exception {
|
void testAddTableOfContents_WithSingleFile_Success() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
MultipartFile[] files = {mockFile1};
|
MultipartFile[] files = {mockFile1};
|
||||||
|
|
||||||
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
||||||
when(mockMergedDocument.getNumberOfPages()).thenReturn(3);
|
when(mockMergedDocument.getNumberOfPages()).thenReturn(3);
|
||||||
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
|
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
|
||||||
|
|
||||||
PDDocument doc1 = mock(PDDocument.class);
|
PDDocument doc1 = mock(PDDocument.class);
|
||||||
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
||||||
when(doc1.getNumberOfPages()).thenReturn(3);
|
when(doc1.getNumberOfPages()).thenReturn(3);
|
||||||
@ -152,25 +152,25 @@ class MergeControllerTest {
|
|||||||
void testAddTableOfContents_WithIOException_HandlesGracefully() throws Exception {
|
void testAddTableOfContents_WithIOException_HandlesGracefully() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
MultipartFile[] files = {mockFile1, mockFile2};
|
MultipartFile[] files = {mockFile1, mockFile2};
|
||||||
|
|
||||||
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
||||||
when(mockMergedDocument.getNumberOfPages()).thenReturn(4);
|
when(mockMergedDocument.getNumberOfPages()).thenReturn(4);
|
||||||
when(mockMergedDocument.getPage(anyInt())).thenReturn(mockPage1); // Use anyInt() to avoid stubbing conflicts
|
when(mockMergedDocument.getPage(anyInt())).thenReturn(mockPage1); // Use anyInt() to avoid stubbing conflicts
|
||||||
|
|
||||||
// First document loads successfully
|
// First document loads successfully
|
||||||
PDDocument doc1 = mock(PDDocument.class);
|
PDDocument doc1 = mock(PDDocument.class);
|
||||||
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
||||||
when(doc1.getNumberOfPages()).thenReturn(2);
|
when(doc1.getNumberOfPages()).thenReturn(2);
|
||||||
|
|
||||||
// Second document throws IOException
|
// Second document throws IOException
|
||||||
when(pdfDocumentFactory.load(mockFile2)).thenThrow(new IOException("Failed to load document"));
|
when(pdfDocumentFactory.load(mockFile2)).thenThrow(new IOException("Failed to load document"));
|
||||||
|
|
||||||
// When
|
// When
|
||||||
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
|
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
|
||||||
addTableOfContentsMethod.setAccessible(true);
|
addTableOfContentsMethod.setAccessible(true);
|
||||||
|
|
||||||
// Should not throw exception
|
// Should not throw exception
|
||||||
assertDoesNotThrow(() ->
|
assertDoesNotThrow(() ->
|
||||||
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files)
|
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -186,11 +186,11 @@ class MergeControllerTest {
|
|||||||
// Given
|
// Given
|
||||||
MockMultipartFile fileWithoutExtension = new MockMultipartFile("file", "document_no_ext", "application/pdf", "PDF content".getBytes());
|
MockMultipartFile fileWithoutExtension = new MockMultipartFile("file", "document_no_ext", "application/pdf", "PDF content".getBytes());
|
||||||
MultipartFile[] files = {fileWithoutExtension};
|
MultipartFile[] files = {fileWithoutExtension};
|
||||||
|
|
||||||
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
||||||
when(mockMergedDocument.getNumberOfPages()).thenReturn(1);
|
when(mockMergedDocument.getNumberOfPages()).thenReturn(1);
|
||||||
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
|
when(mockMergedDocument.getPage(0)).thenReturn(mockPage1);
|
||||||
|
|
||||||
PDDocument doc = mock(PDDocument.class);
|
PDDocument doc = mock(PDDocument.class);
|
||||||
when(pdfDocumentFactory.load(fileWithoutExtension)).thenReturn(doc);
|
when(pdfDocumentFactory.load(fileWithoutExtension)).thenReturn(doc);
|
||||||
when(doc.getNumberOfPages()).thenReturn(1);
|
when(doc.getNumberOfPages()).thenReturn(1);
|
||||||
@ -209,10 +209,10 @@ class MergeControllerTest {
|
|||||||
void testAddTableOfContents_PageIndexExceedsDocumentPages_HandlesGracefully() throws Exception {
|
void testAddTableOfContents_PageIndexExceedsDocumentPages_HandlesGracefully() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
MultipartFile[] files = {mockFile1};
|
MultipartFile[] files = {mockFile1};
|
||||||
|
|
||||||
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
when(mockMergedDocument.getDocumentCatalog()).thenReturn(mockCatalog);
|
||||||
when(mockMergedDocument.getNumberOfPages()).thenReturn(0); // No pages in merged document
|
when(mockMergedDocument.getNumberOfPages()).thenReturn(0); // No pages in merged document
|
||||||
|
|
||||||
PDDocument doc1 = mock(PDDocument.class);
|
PDDocument doc1 = mock(PDDocument.class);
|
||||||
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
when(pdfDocumentFactory.load(mockFile1)).thenReturn(doc1);
|
||||||
when(doc1.getNumberOfPages()).thenReturn(3);
|
when(doc1.getNumberOfPages()).thenReturn(3);
|
||||||
@ -220,9 +220,9 @@ class MergeControllerTest {
|
|||||||
// When
|
// When
|
||||||
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
|
Method addTableOfContentsMethod = MergeController.class.getDeclaredMethod("addTableOfContents", PDDocument.class, MultipartFile[].class);
|
||||||
addTableOfContentsMethod.setAccessible(true);
|
addTableOfContentsMethod.setAccessible(true);
|
||||||
|
|
||||||
// Should not throw exception
|
// Should not throw exception
|
||||||
assertDoesNotThrow(() ->
|
assertDoesNotThrow(() ->
|
||||||
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files)
|
addTableOfContentsMethod.invoke(mergeController, mockMergedDocument, files)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -238,12 +238,12 @@ class MergeControllerTest {
|
|||||||
PDDocument doc1 = mock(PDDocument.class);
|
PDDocument doc1 = mock(PDDocument.class);
|
||||||
PDDocument doc2 = mock(PDDocument.class);
|
PDDocument doc2 = mock(PDDocument.class);
|
||||||
List<PDDocument> documents = Arrays.asList(doc1, doc2);
|
List<PDDocument> documents = Arrays.asList(doc1, doc2);
|
||||||
|
|
||||||
PDPageTree pages1 = mock(PDPageTree.class);
|
PDPageTree pages1 = mock(PDPageTree.class);
|
||||||
PDPageTree pages2 = mock(PDPageTree.class);
|
PDPageTree pages2 = mock(PDPageTree.class);
|
||||||
PDPage page1 = mock(PDPage.class);
|
PDPage page1 = mock(PDPage.class);
|
||||||
PDPage page2 = mock(PDPage.class);
|
PDPage page2 = mock(PDPage.class);
|
||||||
|
|
||||||
when(pdfDocumentFactory.createNewDocument()).thenReturn(mockMergedDocument);
|
when(pdfDocumentFactory.createNewDocument()).thenReturn(mockMergedDocument);
|
||||||
when(doc1.getPages()).thenReturn(pages1);
|
when(doc1.getPages()).thenReturn(pages1);
|
||||||
when(doc2.getPages()).thenReturn(pages2);
|
when(doc2.getPages()).thenReturn(pages2);
|
||||||
@ -264,7 +264,7 @@ class MergeControllerTest {
|
|||||||
void testMergeDocuments_EmptyList_ReturnsEmptyDocument() throws IOException {
|
void testMergeDocuments_EmptyList_ReturnsEmptyDocument() throws IOException {
|
||||||
// Given
|
// Given
|
||||||
List<PDDocument> documents = Arrays.asList();
|
List<PDDocument> documents = Arrays.asList();
|
||||||
|
|
||||||
when(pdfDocumentFactory.createNewDocument()).thenReturn(mockMergedDocument);
|
when(pdfDocumentFactory.createNewDocument()).thenReturn(mockMergedDocument);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
|
Loading…
x
Reference in New Issue
Block a user