-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2238 from siemens/feat/CancleAllScheduleServices
feat(rest): Create new endpoint to cancel all scheduled services. Reviewed by: [email protected] Tested by: [email protected]
- Loading branch information
Showing
6 changed files
with
203 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Copyright Siemens AG, 2023. Part of the SW360 Portal Project. | ||
// | ||
// This program and the accompanying materials are made | ||
// available under the terms of the Eclipse Public License 2.0 | ||
// which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 | ||
// | ||
|
||
[[resources-schedule]] | ||
=== Schedule | ||
|
||
The Schedule resource is used to get and list the Schedule requests. | ||
|
||
[[unschedule-services]] | ||
==== Cancel all schedule services. | ||
|
||
A `DELETE` request will cancel all the services. | ||
|
||
===== Example request | ||
include::{snippets}/should_document_cancel_all_schedule/curl-request.adoc[] | ||
|
||
===== Example response | ||
include::{snippets}/should_document_cancel_all_schedule/http-response.adoc[] |
61 changes: 61 additions & 0 deletions
61
...src/main/java/org/eclipse/sw360/rest/resourceserver/schedule/ScheduleAdminController.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,61 @@ | ||
/* | ||
* Copyright Siemens AG, 2023-2024. | ||
* Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.sw360.rest.resourceserver.schedule; | ||
|
||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; | ||
|
||
import org.apache.thrift.TException; | ||
import org.eclipse.sw360.datahandler.thrift.RequestStatus; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.rest.webmvc.BasePathAwareController; | ||
import org.springframework.data.rest.webmvc.RepositoryLinksResource; | ||
import org.springframework.hateoas.server.RepresentationModelProcessor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@BasePathAwareController | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
@RestController | ||
@SecurityRequirement(name = "tokenAuth") | ||
public class ScheduleAdminController implements RepresentationModelProcessor<RepositoryLinksResource> { | ||
public static final String SCHEDULE_URL = "/schedule"; | ||
|
||
@NonNull | ||
private final RestControllerHelper restControllerHelper; | ||
|
||
@NonNull | ||
private Sw360ScheduleService scheduleService; | ||
|
||
|
||
@Override | ||
public RepositoryLinksResource process(RepositoryLinksResource resource) { | ||
resource.add(linkTo(ScheduleAdminController.class).slash("api/schedule").withRel("schedule")); | ||
return resource; | ||
} | ||
|
||
@RequestMapping(value = SCHEDULE_URL + "/unscheduleAllServices", method = RequestMethod.DELETE) | ||
public ResponseEntity<?> unscheduleAllServices()throws TException { | ||
User sw360User = restControllerHelper.getSw360UserFromAuthentication(); | ||
RequestStatus requestStatus = scheduleService.cancelAllServices(sw360User); | ||
HttpStatus status = HttpStatus.OK; | ||
return new ResponseEntity<>(requestStatus, status); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...er/src/main/java/org/eclipse/sw360/rest/resourceserver/schedule/Sw360ScheduleService.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,45 @@ | ||
/* | ||
* Copyright Siemens AG, 2023-2024. | ||
* Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.sw360.rest.resourceserver.schedule; | ||
|
||
import org.apache.thrift.TException; | ||
import org.eclipse.sw360.datahandler.permissions.PermissionUtils; | ||
import org.eclipse.sw360.datahandler.thrift.RequestStatus; | ||
import org.eclipse.sw360.datahandler.thrift.ThriftClients; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.eclipse.sw360.datahandler.thrift.users.UserGroup; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) | ||
public class Sw360ScheduleService { | ||
|
||
public RequestStatus cancelAllServices(User sw360User) throws TException { | ||
try { | ||
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { | ||
RequestStatus requestStatus = new ThriftClients().makeScheduleClient().unscheduleAllServices(sw360User); | ||
return requestStatus; | ||
} else { | ||
throw new HttpMessageNotReadableException("User is not admin"); | ||
} | ||
} catch (TException e) { | ||
throw new TException(e.getMessage()); | ||
} | ||
} | ||
|
||
} |
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
70 changes: 70 additions & 0 deletions
70
...server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ScheduleSpecTest.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,70 @@ | ||
/* | ||
* Copyright Siemens AG, 2023-2024. | ||
* Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.sw360.rest.resourceserver.restdocs; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.thrift.TException; | ||
import org.eclipse.sw360.datahandler.thrift.RequestStatus; | ||
import org.eclipse.sw360.datahandler.thrift.users.User; | ||
import org.eclipse.sw360.rest.resourceserver.TestHelper; | ||
import org.eclipse.sw360.rest.resourceserver.schedule.Sw360ScheduleService; | ||
import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.hateoas.MediaTypes; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
public class ScheduleSpecTest extends TestRestDocsSpecBase { | ||
@Value("${sw360.test-user-id}") | ||
private String testUserId; | ||
|
||
@Value("${sw360.test-user-password}") | ||
private String testUserPassword; | ||
|
||
@MockBean | ||
private Sw360UserService userServiceMock; | ||
|
||
@MockBean | ||
private Sw360ScheduleService scheduleServiceMock; | ||
|
||
@Before | ||
public void before() throws TException { | ||
|
||
User sw360User = new User(); | ||
sw360User.setId("123456789"); | ||
sw360User.setEmail("[email protected]"); | ||
sw360User.setFullname("John Doe"); | ||
given(this.userServiceMock.getUserByEmailOrExternalId("[email protected]")).willReturn(sw360User); | ||
given(this.scheduleServiceMock.cancelAllServices(any())).willReturn(RequestStatus.SUCCESS); | ||
|
||
} | ||
|
||
@Test | ||
public void should_document_cancel_all_schedule() throws Exception { | ||
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); | ||
mockMvc.perform(delete("/api/schedule/unscheduleAllServices") | ||
.header("Authorization", "Bearer " + accessToken) | ||
.accept(MediaTypes.HAL_JSON)) | ||
.andExpect(status().isOk()); | ||
} | ||
} |