Skip to content

Commit

Permalink
feat(rest): Create new endpoint for schedule CVE and schedule attachm…
Browse files Browse the repository at this point in the history
…ent deletion.

Signed-off-by: Nikesh kumar <kumar.nikesh@simens.com>
  • Loading branch information
Nikesh kumar committed Jan 11, 2024
1 parent 3e59dc7 commit 786c7f5
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 12 deletions.
69 changes: 68 additions & 1 deletion rest/resource-server/src/docs/asciidoc/schedule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// SPDX-License-Identifier: EPL-2.0
//


[[resources-schedule]]
=== Schedule

Expand All @@ -22,4 +23,70 @@ A `DELETE` request will cancel all the services.
include::{snippets}/should_document_cancel_all_schedule/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_cancel_all_schedule/http-response.adoc[]
include::{snippets}/should_document_cancel_all_schedule/http-response.adoc[]

[[schedule-cve]]
==== Schedule cve service

A `POST` request will schedule the cve service.

===== Example request
include::{snippets}/should_document_schedule_cve_service/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_schedule_cve_service/http-response.adoc[]

[[unschedule-cve]]
==== Unschedule cve search

A `DELETE` request will unschedule the cve search.

===== Example request
include::{snippets}/should_document_unschedule_cve_search/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_unschedule_cve_search/http-response.adoc[]

[[schedule-service]]
==== Schedule service for attachment deletion from local FS.

A `POST` request will schedule attachment deletion.

===== Example request
include::{snippets}/should_document_schedule_service_from_local/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_schedule_service_from_local/http-response.adoc[]

[[cancel-schedule]]
==== Cancel schedule attachment from local fs.

A `DELETE` request will schedule attachment deletion.

===== Example request
include::{snippets}/should_document_cancel_schedule_attachment/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_cancel_schedule_attachment/http-response.adoc[]

[[delete-attachment]]
==== Delete old attachment from local fs.

A `DELETE` request will schedule attachment deletion.

===== Example request
include::{snippets}/should_document_delete_old_attachment_from_local/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_delete_old_attachment_from_local/http-response.adoc[]

[[cve-search]]
==== Schedule cve search.

A `POST` request will schedule the cve search.

===== Example request
include::{snippets}/should_document_schedule_cve_search/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_schedule_cve_search/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
Expand All @@ -30,20 +31,20 @@
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 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) {
Expand All @@ -58,4 +59,52 @@ public ResponseEntity<?> unscheduleAllServices()throws TException {
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}

@RequestMapping(value = SCHEDULE_URL + "/cveService", method = RequestMethod.POST)
public ResponseEntity<?> scheduleCve()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestSummary requestSummary = scheduleService.scheduleCveSearch(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestSummary, status);
}

@RequestMapping(value = SCHEDULE_URL + "/unscheduleCve", method = RequestMethod.DELETE)
public ResponseEntity<?> unscheduleCveSearch()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.cancelCveSearch(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}

@RequestMapping(value = SCHEDULE_URL + "/deleteAttachment", method = RequestMethod.POST)
public ResponseEntity<?> scheduleDeleteAttachment()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestSummary requestSummary = scheduleService.deleteAttachmentService(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestSummary, status);
}

@RequestMapping(value = SCHEDULE_URL + "/unscheduleService", method = RequestMethod.DELETE)
public ResponseEntity<?> unscheduleDeleteAttachment()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.cancelDeleteAttachment(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}

@RequestMapping(value = SCHEDULE_URL + "/DeleteOldAttachment", method = RequestMethod.DELETE)
public ResponseEntity<?> attachmentDeleteLocalFS()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.cancelAttachmentDeletionLocalFS(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}

@RequestMapping(value = SCHEDULE_URL + "/cveSearch", method = RequestMethod.POST)
public ResponseEntity<?> cveSearch()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.triggerCveSearch(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
Expand All @@ -22,10 +23,8 @@
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Sw360ScheduleService {

Expand All @@ -42,4 +41,84 @@ public RequestStatus cancelAllServices(User sw360User) throws TException {
}
}

public RequestSummary scheduleCveSearch(User sw360User) throws TException{
String serviceName = ThriftClients.CVESEARCH_SERVICE;
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestSummary requestSummary = new ThriftClients().makeScheduleClient().scheduleService(serviceName);
return requestSummary;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus cancelCveSearch(User sw360User) throws TException{
String serviceName = ThriftClients.CVESEARCH_SERVICE;
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeScheduleClient().unscheduleService(serviceName, sw360User);
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestSummary deleteAttachmentService(User sw360User) throws TException{
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestSummary requestSummary = new ThriftClients().makeScheduleClient().scheduleService(ThriftClients.DELETE_ATTACHMENT_SERVICE);
return requestSummary;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus cancelDeleteAttachment(User sw360User) throws TException{
String serviceName = ThriftClients.DELETE_ATTACHMENT_SERVICE;
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeScheduleClient().unscheduleService(serviceName, sw360User);
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus cancelAttachmentDeletionLocalFS(User sw360User) throws TException {
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeAttachmentClient().deleteOldAttachmentFromFileSystem();
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus triggerCveSearch(User sw360User) throws TException {
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeCvesearchClient().update();
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

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 static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;

import org.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.rest.resourceserver.TestHelper;
import org.eclipse.sw360.rest.resourceserver.schedule.Sw360ScheduleService;
Expand All @@ -47,15 +47,23 @@ public class ScheduleSpecTest extends TestRestDocsSpecBase {
@MockBean
private Sw360ScheduleService scheduleServiceMock;

private RequestSummary requestSummary = new RequestSummary();

@Before
public void before() throws TException {

User sw360User = new User();
sw360User.setId("123456789");
sw360User.setEmail("admin@sw360.org");
sw360User.setFullname("John Doe");
given(this.userServiceMock.getUserByEmailOrExternalId("admin@sw360.org")).willReturn(sw360User);
given(this.scheduleServiceMock.cancelAllServices(any())).willReturn(RequestStatus.SUCCESS);
given(this.scheduleServiceMock.scheduleCveSearch(any())).willReturn(requestSummary);
given(this.scheduleServiceMock.cancelCveSearch(any())).willReturn(RequestStatus.SUCCESS);
given(this.scheduleServiceMock.deleteAttachmentService(any())).willReturn(requestSummary);
given(this.scheduleServiceMock.cancelDeleteAttachment(any())).willReturn(RequestStatus.SUCCESS);
given(this.scheduleServiceMock.cancelAttachmentDeletionLocalFS(any())).willReturn(RequestStatus.SUCCESS);
given(this.scheduleServiceMock.triggerCveSearch(any())).willReturn(RequestStatus.SUCCESS);

}

Expand All @@ -67,4 +75,59 @@ public void should_document_cancel_all_schedule() throws Exception {
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_schedule_cve_service() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(post("/api/schedule/cveService")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_unschedule_cve_search() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(delete("/api/schedule/unscheduleCve")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_schedule_service_from_local() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(post("/api/schedule/deleteAttachment")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_schedule_cve_search() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(post("/api/schedule/cveSearch")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_cancel_schedule_attachment() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(delete("/api/schedule/unscheduleService")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

@Test
public void should_document_delete_old_attachment_from_local() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(delete("/api/schedule/DeleteOldAttachment")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

}

0 comments on commit 786c7f5

Please sign in to comment.