Skip to content

Commit

Permalink
Handle nullpointer #3789
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed Jan 16, 2025
1 parent da92765 commit 6036536
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
package com.mercedesbenz.sechub.domain.administration.job;

import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.mercedesbenz.sechub.domain.administration.project.Project;
import com.mercedesbenz.sechub.domain.administration.user.User;
Expand Down Expand Up @@ -45,7 +45,6 @@ public JobCancelService(AuditLogService auditLogService, UserInputAssertion user
this.userRepository = userRepository;
}

@Validated
@UseCaseAdminCancelsJob(@Step(number = 2, name = "Cancel job", description = "Will trigger event that job cancel requested"))
public void cancelJob(UUID jobUUID) {
userInputAssertion.assertIsValidJobUUID(jobUUID);
Expand Down Expand Up @@ -79,13 +78,18 @@ private void assertUserAllowedCancelJob(UUID jobUUID, String userId) {
});

User user = userRepository.findOrFailUser(userId);
for (Project project : user.getProjects()) {
if (project.getId().equals(jobInfo.getProjectId())) {
return;
}
Set<Project> projects = user.getProjects();

if (projects == null) {
throw new IllegalStateException("Projects fore user {} are null." + userId);
}

boolean isForbidden = projects.stream().noneMatch(project -> project.getId().equals(jobInfo.getProjectId()));

if (isForbidden) {
LOG.debug("User {} is not allowed to cancel job {}", userId, jobUUID);
throw new NotFoundException("Job not found: " + jobUUID);
}
LOG.debug("User {} is not allowed to cancel job {}", userId, jobUUID);
throw new NotFoundException("Job not found: " + jobUUID);
}

private JobMessage buildMessage(UUID jobUUID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void userCancelJob_receives_not_found_exception_when_job_not_found() {

/* execute + test */
assertThatThrownBy(() -> serviceToTest.userCancelJob(jobUUID, userId)).isInstanceOf(NotFoundException.class);
verify(eventBusService, never()).sendAsynchron(any());

}

@Test
Expand All @@ -61,6 +63,7 @@ public void userCancelJob_receives_not_found_exception_when_project_not_assigned

/* execute + test */
assertThatThrownBy(() -> serviceToTest.userCancelJob(jobUUID, userId)).isInstanceOf(NotFoundException.class);
verify(eventBusService, never()).sendAsynchron(any());
}

@Test
Expand Down

0 comments on commit 6036536

Please sign in to comment.