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 <[email protected]>
  • Loading branch information
Nikesh kumar committed May 2, 2024
1 parent 3e59dc7 commit 107bbe1
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 19 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 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,32 +31,80 @@
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) {
resource.add(linkTo(ScheduleAdminController.class).slash("api/schedule").withRel("schedule"));
return resource;
}

@RequestMapping(value = SCHEDULE_URL + "/unscheduleAllServices", method = RequestMethod.DELETE)
@RequestMapping(value = SCHEDULE_URL + "/unscheduleAllServices", method = RequestMethod.POST)
public ResponseEntity<?> unscheduleAllServices()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.cancelAllServices(sw360User);
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.POST)
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 + "/unScheduleDeleteAttachment", method = RequestMethod.POST)
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.POST)
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 @@ -11,35 +11,101 @@

package org.eclipse.sw360.rest.resourceserver.schedule;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.SW360Exception;
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.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Sw360ScheduleService {
private static final Logger log = LogManager.getLogger(Sw360ScheduleService.class);

private RequestSummary scheduleService(User sw360User, String serviceName) throws TException {
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
ThriftClients thriftClients = new ThriftClients();
return thriftClients.makeScheduleClient().scheduleService(serviceName);
} else {
throw new AccessDeniedException("User does not have admin access");
}
} catch (SW360Exception sw360Exp) {
if (sw360Exp.getErrorCode() == 403) {
throw new AccessDeniedException("User does not have admin access", sw360Exp);
} else {
throw sw360Exp;
}
}
}

private RequestStatus unscheduleService(User sw360User, String serviceName) throws TException {
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
ThriftClients thriftClients = new ThriftClients();
return thriftClients.makeScheduleClient().unscheduleService(serviceName, sw360User);
} else {
throw new AccessDeniedException("User does not have admin access");
}
} catch (SW360Exception sw360Exp) {
if (sw360Exp.getErrorCode() == 403) {
throw new AccessDeniedException("User does not have admin access", sw360Exp);
} else {
throw sw360Exp;
}
}
}

public RequestStatus cancelAllServices(User sw360User) throws TException {
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeScheduleClient().unscheduleAllServices(sw360User);
return requestStatus;
ThriftClients thriftClients = new ThriftClients();
return thriftClients.makeScheduleClient().unscheduleAllServices(sw360User);
} else {
throw new AccessDeniedException("User does not have admin access");
}
} catch (SW360Exception sw360Exp) {
if (sw360Exp.getErrorCode() == 403) {
throw new AccessDeniedException("User does not have admin access", sw360Exp);
} else {
throw new HttpMessageNotReadableException("User is not admin");
throw sw360Exp;
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}


public RequestSummary scheduleCveSearch(User sw360User) throws TException {
return scheduleService(sw360User, ThriftClients.CVESEARCH_SERVICE);
}

public RequestStatus cancelCveSearch(User sw360User) throws TException {
return unscheduleService(sw360User, ThriftClients.CVESEARCH_SERVICE);
}

public RequestSummary deleteAttachmentService(User sw360User) throws TException {
return scheduleService(sw360User, ThriftClients.DELETE_ATTACHMENT_SERVICE);
}

public RequestStatus cancelDeleteAttachment(User sw360User) throws TException {
return unscheduleService(sw360User, ThriftClients.DELETE_ATTACHMENT_SERVICE);
}

public RequestStatus cancelAttachmentDeletionLocalFS(User sw360User) throws TException {
return new ThriftClients().makeAttachmentClient().deleteOldAttachmentFromFileSystem();
}

public RequestStatus triggerCveSearch(User sw360User) throws TException {
return new ThriftClients().makeCvesearchClient().update();
}
}
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,24 +47,87 @@ 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("[email protected]");
sw360User.setFullname("John Doe");
given(this.userServiceMock.getUserByEmailOrExternalId("[email protected]")).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);

}

@Test
public void should_document_cancel_all_schedule() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(delete("/api/schedule/unscheduleAllServices")
mockMvc.perform(post("/api/schedule/unscheduleAllServices")
.header("Authorization", "Bearer " + accessToken)
.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(post("/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(post("/api/schedule/unScheduleDeleteAttachment")
.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(post("/api/schedule/DeleteOldAttachment")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

}

0 comments on commit 107bbe1

Please sign in to comment.