-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature 3789 user can cancel job (#3799)
* Added REST API for user cancel job #3789 * Applied spotless #3789 * Added 404 to openapi and replaced notauthorized by notfound #3789 * Added change requests #3789 * Added 204 to restDoc test #3789 * Added new management URL #3789 * Handle nullpointer #3789 * Refactored exception #3789 * Changed API URL #3789
- Loading branch information
1 parent
a70efe5
commit 6b8f419
Showing
13 changed files
with
1,134 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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
37 changes: 37 additions & 0 deletions
37
...on/src/main/java/com/mercedesbenz/sechub/domain/administration/job/JobRestController.java
This file contains 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,37 @@ | ||
// SPDX-License-Identifier: MIT | ||
package com.mercedesbenz.sechub.domain.administration.job; | ||
|
||
import java.util.UUID; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import com.mercedesbenz.sechub.domain.administration.AdministrationAPIConstants; | ||
import com.mercedesbenz.sechub.sharedkernel.Step; | ||
import com.mercedesbenz.sechub.sharedkernel.security.RoleConstants; | ||
import com.mercedesbenz.sechub.sharedkernel.security.UserContextService; | ||
import com.mercedesbenz.sechub.sharedkernel.usecases.job.UseCaseUserCancelsJob; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
|
||
@RestController | ||
public class JobRestController { | ||
|
||
private final UserContextService userContextService; | ||
private final JobCancelService jobCancelService; | ||
|
||
public JobRestController(UserContextService userContextService, JobCancelService jobCancelService) { | ||
this.userContextService = userContextService; | ||
this.jobCancelService = jobCancelService; | ||
} | ||
|
||
@UseCaseUserCancelsJob(@Step(number = 1, name = "Rest call", description = "Triggers job cancellation request, owners of project will be informed", needsRestDoc = true)) | ||
@PostMapping(path = AdministrationAPIConstants.API_USER_CANCEL_JOB, produces = { MediaType.APPLICATION_JSON_VALUE }) | ||
@RolesAllowed({ RoleConstants.ROLE_USER, RoleConstants.ROLE_SUPERADMIN, RoleConstants.ROLE_OWNER }) | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void userCancelJob(@PathVariable(name = "jobUUID") UUID jobUUID) { | ||
String userId = userContextService.getUserId(); | ||
jobCancelService.userCancelJob(jobUUID, userId); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...src/test/java/com/mercedesbenz/sechub/domain/administration/job/JobCancelServiceTest.java
This file contains 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,92 @@ | ||
// SPDX-License-Identifier: MIT | ||
package com.mercedesbenz.sechub.domain.administration.job; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import org.junit.Test; | ||
|
||
import com.mercedesbenz.sechub.domain.administration.project.Project; | ||
import com.mercedesbenz.sechub.domain.administration.user.User; | ||
import com.mercedesbenz.sechub.domain.administration.user.UserRepository; | ||
import com.mercedesbenz.sechub.sharedkernel.error.NotFoundException; | ||
import com.mercedesbenz.sechub.sharedkernel.logging.AuditLogService; | ||
import com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService; | ||
import com.mercedesbenz.sechub.sharedkernel.validation.UserInputAssertion; | ||
|
||
public class JobCancelServiceTest { | ||
|
||
private static final AuditLogService auditLogService = mock(); | ||
private static final UserInputAssertion userInputAssertion = mock(); | ||
private static final DomainMessageService eventBusService = mock(); | ||
private static final JobInformationRepository jobInformationRepository = mock(); | ||
private static final UserRepository userRepository = mock(); | ||
private static final JobCancelService serviceToTest = new JobCancelService(auditLogService, userInputAssertion, eventBusService, jobInformationRepository, | ||
userRepository); | ||
|
||
@Test | ||
public void userCancelJob_receives_not_found_exception_when_job_not_found() { | ||
/* prepare */ | ||
UUID jobUUID = UUID.randomUUID(); | ||
String userId = "user1"; | ||
when(jobInformationRepository.findById(jobUUID)).thenReturn(Optional.empty()); | ||
|
||
/* execute + test */ | ||
assertThatThrownBy(() -> serviceToTest.userCancelJob(jobUUID, userId)).isInstanceOf(NotFoundException.class); | ||
verify(eventBusService, never()).sendAsynchron(any()); | ||
|
||
} | ||
|
||
@Test | ||
public void userCancelJob_receives_not_found_exception_when_project_not_assigned() { | ||
/* prepare */ | ||
UUID jobUUID = UUID.randomUUID(); | ||
String userId = "user1"; | ||
String projectId = "project1"; | ||
String otherProjectId = "project2"; | ||
|
||
JobInformation jobInformation = mock(JobInformation.class); | ||
when(jobInformation.getProjectId()).thenReturn(projectId); | ||
Project project = mock(Project.class); | ||
when(project.getId()).thenReturn(otherProjectId); | ||
|
||
User user = mock(User.class); | ||
when(user.getProjects()).thenReturn(Set.of(project)); | ||
|
||
when(userRepository.findOrFailUser(userId)).thenReturn(user); | ||
when(jobInformationRepository.findById(jobUUID)).thenReturn(Optional.of(jobInformation)); | ||
|
||
/* execute + test */ | ||
assertThatThrownBy(() -> serviceToTest.userCancelJob(jobUUID, userId)).isInstanceOf(NotFoundException.class); | ||
verify(eventBusService, never()).sendAsynchron(any()); | ||
} | ||
|
||
@Test | ||
public void userCancelJob_receives_no_exception_when_job_found_and_authorized() { | ||
/* prepare */ | ||
UUID jobUUID = UUID.randomUUID(); | ||
String userId = "user1"; | ||
String projectId = "project1"; | ||
|
||
JobInformation jobInformation = mock(JobInformation.class); | ||
when(jobInformation.getProjectId()).thenReturn(projectId); | ||
Project project = mock(Project.class); | ||
when(project.getId()).thenReturn(projectId); | ||
|
||
User user = mock(User.class); | ||
when(user.getProjects()).thenReturn(Set.of(project)); | ||
|
||
when(userRepository.findOrFailUser(userId)).thenReturn(user); | ||
when(jobInformationRepository.findById(jobUUID)).thenReturn(Optional.of(jobInformation)); | ||
|
||
/* execute + test */ | ||
assertThatCode(() -> serviceToTest.userCancelJob(jobUUID, userId)).doesNotThrowAnyException(); | ||
verify(eventBusService, times(1)).sendAsynchron(any()); | ||
} | ||
|
||
} |
Oops, something went wrong.