From da57832924778aff1b46f84b7af05a5ff5d4ac18 Mon Sep 17 00:00:00 2001 From: diya17 Date: Fri, 10 Mar 2023 16:29:44 -0700 Subject: [PATCH 01/24] [GECO-140] Adding new delete document controller to the v2 API --- .../api/v2/V2DeleteDocumentController.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java new file mode 100644 index 00000000..f7bda1cb --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -0,0 +1,33 @@ +package edu.asu.diging.gilesecosystem.web.api.v2; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import edu.asu.diging.gilesecosystem.web.core.model.IDocument; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; +import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; + +@Controller +public class V2DeleteDocumentController { + + @Autowired + private IDeleteDocumentService deleteDocumentService; + + @Autowired + private ITransactionalDocumentService documentService; + + @RequestMapping(value = "/api/v2/resources/documents/{documentId}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8") + public ResponseEntity reprocessDocument(@PathVariable("documentId") String documentId) { + IDocument document = documentService.getDocument(documentId); + if (document == null) { + return new ResponseEntity(HttpStatus.NOT_FOUND); + } + deleteDocumentService.deleteDocument(document); + return new ResponseEntity(HttpStatus.OK); + } +} From 7e07f0e16e5e8f6eb3e04cbf80b6beff508c5c5c Mon Sep 17 00:00:00 2001 From: diya17 Date: Thu, 16 Mar 2023 11:43:12 -0700 Subject: [PATCH 02/24] [GECO-140] Making changes to the controller endpoint response and adding tests --- .../api/v2/V2DeleteDocumentController.java | 4 +- .../v2/V2DeleteDocumentControllerTest.java | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index f7bda1cb..f68f30b7 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -22,12 +22,12 @@ public class V2DeleteDocumentController { private ITransactionalDocumentService documentService; @RequestMapping(value = "/api/v2/resources/documents/{documentId}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8") - public ResponseEntity reprocessDocument(@PathVariable("documentId") String documentId) { + public ResponseEntity deleteDocument(@PathVariable("documentId") String documentId) { IDocument document = documentService.getDocument(documentId); if (document == null) { return new ResponseEntity(HttpStatus.NOT_FOUND); } deleteDocumentService.deleteDocument(document); - return new ResponseEntity(HttpStatus.OK); + return new ResponseEntity(HttpStatus.ACCEPTED); } } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java new file mode 100644 index 00000000..338afa98 --- /dev/null +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java @@ -0,0 +1,68 @@ +package edu.asu.diging.gilesecosystem.web.api.v2; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import edu.asu.diging.gilesecosystem.web.core.model.DocumentAccess; +import edu.asu.diging.gilesecosystem.web.core.model.IDocument; +import edu.asu.diging.gilesecosystem.web.core.model.impl.Document; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; +import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; + +public class V2DeleteDocumentControllerTest { + + @Mock + private IDeleteDocumentService deleteDocumentService; + + @Mock + private ITransactionalDocumentService documentService; + + @InjectMocks + private V2DeleteDocumentController v2DeleteDocumentController; + + private String DOCUMENT_ID = "documentId"; + private DocumentAccess access = DocumentAccess.PRIVATE; + private String FILE_ID = "fileId"; + private String UPLOAD_ID = "uploadId"; + IDocument document; + + @Before + public void setUp() { + document = createDocument(); + v2DeleteDocumentController = new V2DeleteDocumentController(); + MockitoAnnotations.initMocks(this); + } + + @Test + public void test_deleteDocument_success() { + Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); + ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID); + Assert.assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + } + + @Test + public void test_deleteDocument_notFound() { + Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(null); + ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID); + Assert.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + } + + private Document createDocument() { + Document document = new Document(); + document.setId(DOCUMENT_ID); + document.setAccess(access); + document.setFileIds(Arrays.asList(FILE_ID)); + document.setUploadId(UPLOAD_ID); + document.setUploadedFileId(FILE_ID); + return document; + } +} From 1dc8fcb9a6b43343b82fbcd21e78fcd00c43af96 Mon Sep 17 00:00:00 2001 From: diya17 Date: Fri, 26 May 2023 17:00:28 -0700 Subject: [PATCH 03/24] [GECO-140] : Addressing review comments --- .../web/api/util/IResponseHelper.java | 10 ++++ .../web/api/util/impl/ResponseHelper.java | 48 +++++++++++++++++ .../api/v2/V2DeleteDocumentController.java | 54 ++++++++++++++++++- .../src/main/resources/config.properties | 1 + 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java new file mode 100644 index 00000000..f5ee4b04 --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java @@ -0,0 +1,10 @@ +package edu.asu.diging.gilesecosystem.web.api.util; + +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +public interface IResponseHelper { + public abstract ResponseEntity generateResponse(Map msgs, HttpStatus status); +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java new file mode 100644 index 00000000..bb7f7059 --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java @@ -0,0 +1,48 @@ +package edu.asu.diging.gilesecosystem.web.api.util.impl; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import edu.asu.diging.gilesecosystem.septemberutil.properties.MessageType; +import edu.asu.diging.gilesecosystem.septemberutil.service.ISystemMessageHandler; +import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; + +@Component +public class ResponseHelper implements IResponseHelper { + + @Autowired + private ISystemMessageHandler messageHandler; + + @Override + public ResponseEntity generateResponse(Map msgs, + HttpStatus status) { + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + ObjectNode root = mapper.createObjectNode(); + for (String key : msgs.keySet()) { + root.put(key, msgs.get(key)); + } + + StringWriter sw = new StringWriter(); + try { + mapper.writeValue(sw, root); + } catch (IOException e) { + messageHandler.handleMessage("Could not write json.", e, MessageType.ERROR); + return new ResponseEntity( + "{\"errorMsg\": \"Could not write json result.\", \"errorCode\": \"errorCode\": \"500\" }", + HttpStatus.INTERNAL_SERVER_ERROR); + } + + return new ResponseEntity(sw.toString(), status); + } +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index f68f30b7..8e21cb37 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -1,6 +1,12 @@ package edu.asu.diging.gilesecosystem.web.api.v2; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; @@ -8,9 +14,15 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; +import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; +import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; +import edu.asu.diging.gilesecosystem.web.config.IUserHelper; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; +import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; +import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; @Controller public class V2DeleteDocumentController { @@ -21,13 +33,51 @@ public class V2DeleteDocumentController { @Autowired private ITransactionalDocumentService documentService; + @Autowired + private IResponseHelper responseHelper; + + @Autowired + private IPropertiesManager propertyManager; + + @Autowired + private IUserHelper userHelper; + + @Value("${giles_check_deletion_endpoint_v2}") + private String deleteEndpoint; + @RequestMapping(value = "/api/v2/resources/documents/{documentId}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8") public ResponseEntity deleteDocument(@PathVariable("documentId") String documentId) { IDocument document = documentService.getDocument(documentId); + Map msgs = new HashMap(); if (document == null) { - return new ResponseEntity(HttpStatus.NOT_FOUND); + msgs.put("errorMsg", "Document Id: " + documentId + " does not exist."); + msgs.put("errorCode", "404"); + return responseHelper.generateResponse(msgs, HttpStatus.NOT_FOUND); } deleteDocumentService.deleteDocument(document); - return new ResponseEntity(HttpStatus.ACCEPTED); + msgs.put("checkUrl", propertyManager.getProperty(Properties.GILES_URL) + deleteEndpoint + documentId); + + return responseHelper.generateResponse(msgs, HttpStatus.OK); + } + + @RequestMapping(value = "/api/v2/files/deletion/check/{documentId}", method = RequestMethod.GET) + public ResponseEntity checkDocumentDeletion(HttpServletRequest request, + @PathVariable String documentId, CitesphereToken citesphereToken) { + IDocument document = documentService.getDocument(documentId); + String username = document.getUsername(); + Map msgs = new HashMap(); + CitesphereUser user = (CitesphereUser) citesphereToken.getPrincipal(); + String usernameInSystem = userHelper.createUsername(user.getUsername(), user.getAuthorizingClient()); + if (!usernameInSystem.equals(username)) { + msgs.put("errorMsg", "User is not authorized to check status."); + msgs.put("errorCode", "401"); + + return responseHelper.generateResponse(msgs, HttpStatus.UNAUTHORIZED); + } else if (document == null) { + msgs.put("successMessage", "Document Id: " + documentId + " is deleted."); + return responseHelper.generateResponse(msgs, HttpStatus.OK); + } + msgs.put("progressInfo", "Deletion in progress. Please check back later."); + return responseHelper.generateResponse(msgs, HttpStatus.OK); } } diff --git a/giles-eco/src/main/resources/config.properties b/giles-eco/src/main/resources/config.properties index e2ba21cd..c4c878e7 100755 --- a/giles-eco/src/main/resources/config.properties +++ b/giles-eco/src/main/resources/config.properties @@ -52,6 +52,7 @@ giles_digilib_endpoint=/rest/digilib giles_file_endpoint=/rest/files/ giles_check_upload_endpoint=/rest/files/upload/check/ giles_check_upload_endpoint_v2=/api/v2/files/upload/check/ +giles_check_deletion_endpoint_v2=/api/v2/files/deletion/check/ giles_file_content_suffix=/content # file uploads From 8fed2933b76b8a973e20c389211fadef6b92374e Mon Sep 17 00:00:00 2001 From: diya17 Date: Tue, 30 May 2023 15:28:23 -0700 Subject: [PATCH 04/24] [GECO-140] Changing the status for the deletion endpoint and adding an endpoint for polling the status --- .../api/v2/V2DeleteDocumentController.java | 25 ++++++++-------- .../web/config/IUserHelper.java | 8 ++++- .../web/config/impl/UserHelper.java | 29 +++++++++++++++++++ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index 8e21cb37..37877fce 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -46,7 +46,7 @@ public class V2DeleteDocumentController { private String deleteEndpoint; @RequestMapping(value = "/api/v2/resources/documents/{documentId}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8") - public ResponseEntity deleteDocument(@PathVariable("documentId") String documentId) { + public ResponseEntity deleteDocument(@PathVariable("documentId") String documentId, CitesphereToken citesphereToken) { IDocument document = documentService.getDocument(documentId); Map msgs = new HashMap(); if (document == null) { @@ -54,6 +54,9 @@ public ResponseEntity deleteDocument(@PathVariable("documentId") String msgs.put("errorCode", "404"); return responseHelper.generateResponse(msgs, HttpStatus.NOT_FOUND); } + if (!userHelper.checkUserPermission(document, citesphereToken)) { + return userHelper.generateUnauthorizedUserResponse(); + } deleteDocumentService.deleteDocument(document); msgs.put("checkUrl", propertyManager.getProperty(Properties.GILES_URL) + deleteEndpoint + documentId); @@ -64,19 +67,15 @@ public ResponseEntity deleteDocument(@PathVariable("documentId") String public ResponseEntity checkDocumentDeletion(HttpServletRequest request, @PathVariable String documentId, CitesphereToken citesphereToken) { IDocument document = documentService.getDocument(documentId); - String username = document.getUsername(); - Map msgs = new HashMap(); - CitesphereUser user = (CitesphereUser) citesphereToken.getPrincipal(); - String usernameInSystem = userHelper.createUsername(user.getUsername(), user.getAuthorizingClient()); - if (!usernameInSystem.equals(username)) { - msgs.put("errorMsg", "User is not authorized to check status."); - msgs.put("errorCode", "401"); - - return responseHelper.generateResponse(msgs, HttpStatus.UNAUTHORIZED); - } else if (document == null) { - msgs.put("successMessage", "Document Id: " + documentId + " is deleted."); - return responseHelper.generateResponse(msgs, HttpStatus.OK); + if (document == null) { + Map successMsgs = new HashMap(); + successMsgs.put("successMessage", "Document Id: " + documentId + " is deleted."); + return responseHelper.generateResponse(successMsgs, HttpStatus.OK); + } + if (!userHelper.checkUserPermission(document, citesphereToken)) { + return userHelper.generateUnauthorizedUserResponse(); } + Map msgs = new HashMap(); msgs.put("progressInfo", "Deletion in progress. Please check back later."); return responseHelper.generateResponse(msgs, HttpStatus.OK); } diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java index 237c07ba..a954cc32 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java @@ -1,7 +1,9 @@ package edu.asu.diging.gilesecosystem.web.config; +import org.springframework.http.ResponseEntity; import org.springframework.social.connect.Connection; +import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.users.User; public interface IUserHelper { @@ -10,4 +12,8 @@ public interface IUserHelper { String createUsername(String username, String providerId); -} \ No newline at end of file + public abstract boolean checkUserPermission(IDocument document, CitesphereToken citesphereToken); + + public abstract ResponseEntity generateUnauthorizedUserResponse(); + +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java index c515fab1..1646afdf 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java @@ -1,12 +1,21 @@ package edu.asu.diging.gilesecosystem.web.config.impl; +import java.util.HashMap; +import java.util.Map; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.social.connect.Connection; import org.springframework.social.connect.UserProfile; import org.springframework.stereotype.Service; +import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; +import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; +import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.users.AccountStatus; +import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; import edu.asu.diging.gilesecosystem.web.core.users.IUserManager; import edu.asu.diging.gilesecosystem.web.core.users.User; @@ -15,6 +24,9 @@ public class UserHelper implements IUserHelper { @Autowired private IUserManager userManager; + + @Autowired + private IResponseHelper responseHelper; /* (non-Javadoc) * @see edu.asu.giles.config.IUserHelper#createUser(org.springframework.social.connect.Connection) @@ -49,4 +61,21 @@ public User createUser(Connection connection) { public String createUsername(String username, String providerId) { return username + "_" + providerId; } + + @Override + public boolean checkUserPermission(IDocument document, CitesphereToken citesphereToken) { + String username = document.getUsername(); + CitesphereUser user = (CitesphereUser) citesphereToken.getPrincipal(); + String usernameInSystem = createUsername(user.getUsername(), user.getAuthorizingClient()); + + return username.equals(usernameInSystem); + } + + @Override + public ResponseEntity generateUnauthorizedUserResponse() { + Map unauthorizedMsgs = new HashMap(); + unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); + unauthorizedMsgs.put("errorCode", "401"); + return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED); + } } From 1235a6b6eba33fba04512c66ed9c35b1f2570743 Mon Sep 17 00:00:00 2001 From: diya17 Date: Tue, 30 May 2023 16:59:39 -0700 Subject: [PATCH 05/24] [GECO-140] Adding javadocs and test case changes --- .../web/config/IUserHelper.java | 14 +++++++-- .../v2/V2DeleteDocumentControllerTest.java | 29 +++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java index a954cc32..1bd4fa17 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java @@ -11,9 +11,19 @@ public interface IUserHelper { public abstract User createUser(Connection connection); String createUsername(String username, String providerId); - + + /** + Checks whether the user associated with the provided Citesphere token has permission to access the given document. + @param document The document for which to check the user permission. + @param citesphereToken The Citesphere token for the user. + @return {@code true} if the user has permission to access the document, {@code false} otherwise. + */ public abstract boolean checkUserPermission(IDocument document, CitesphereToken citesphereToken); - + + /** + Generates a ResponseEntity with an unauthorized status code (401) and a corresponding error message. + @return The generated ResponseEntity containing the unauthorized error message. + */ public abstract ResponseEntity generateUnauthorizedUserResponse(); } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java index 338afa98..0d9dee73 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java @@ -12,6 +12,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; +import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; +import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; +import edu.asu.diging.gilesecosystem.web.config.IUserHelper; import edu.asu.diging.gilesecosystem.web.core.model.DocumentAccess; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.model.impl.Document; @@ -26,6 +30,15 @@ public class V2DeleteDocumentControllerTest { @Mock private ITransactionalDocumentService documentService; + @Mock + private IUserHelper userHelper; + + @Mock + private IPropertiesManager propertyManager; + + @Mock + private IResponseHelper responseHelper; + @InjectMocks private V2DeleteDocumentController v2DeleteDocumentController; @@ -33,6 +46,7 @@ public class V2DeleteDocumentControllerTest { private DocumentAccess access = DocumentAccess.PRIVATE; private String FILE_ID = "fileId"; private String UPLOAD_ID = "uploadId"; + private CitesphereToken CITESPHERE_TOKEN = new CitesphereToken("71b9cc36-939d-4e28-89e9-e5bfca1b26c3"); IDocument document; @Before @@ -45,14 +59,23 @@ public void setUp() { @Test public void test_deleteDocument_success() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID); - Assert.assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); + Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(true); + ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + public void test_deleteDocument_whenUserIsNotAuthorized() { + Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); + Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(false); + ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); + Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); } @Test public void test_deleteDocument_notFound() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(null); - ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID); + ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); Assert.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } From 5b0ccf3578f1cc39b2944d20dcf9a4fa124bbc4f Mon Sep 17 00:00:00 2001 From: diya17 Date: Thu, 1 Jun 2023 14:07:34 -0400 Subject: [PATCH 06/24] [GECO-140] Adding and fixing tests --- .../api/v2/V2DeleteDocumentController.java | 7 +- .../v2/V2DeleteDocumentControllerTest.java | 76 +++++++++++++ .../web/config/impl/UserHelperTest.java | 106 ++++++++++++++++-- 3 files changed, 176 insertions(+), 13 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index 37877fce..1489fdfe 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -22,7 +22,6 @@ import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; -import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; @Controller public class V2DeleteDocumentController { @@ -43,7 +42,7 @@ public class V2DeleteDocumentController { private IUserHelper userHelper; @Value("${giles_check_deletion_endpoint_v2}") - private String deleteEndpoint; + private String checkDeleteEndpoint; @RequestMapping(value = "/api/v2/resources/documents/{documentId}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8") public ResponseEntity deleteDocument(@PathVariable("documentId") String documentId, CitesphereToken citesphereToken) { @@ -58,8 +57,8 @@ public ResponseEntity deleteDocument(@PathVariable("documentId") String return userHelper.generateUnauthorizedUserResponse(); } deleteDocumentService.deleteDocument(document); - msgs.put("checkUrl", propertyManager.getProperty(Properties.GILES_URL) + deleteEndpoint + documentId); - + msgs.put("checkUrl", propertyManager.getProperty(Properties.GILES_URL) + checkDeleteEndpoint + documentId); + return responseHelper.generateResponse(msgs, HttpStatus.OK); } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java index 0d9dee73..696c7295 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java @@ -1,6 +1,10 @@ package edu.asu.diging.gilesecosystem.web.api.v2; +import java.io.IOException; +import java.io.StringWriter; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import org.junit.Assert; import org.junit.Before; @@ -12,6 +16,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.ObjectNode; + import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; @@ -21,6 +29,7 @@ import edu.asu.diging.gilesecosystem.web.core.model.impl.Document; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; +import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; public class V2DeleteDocumentControllerTest { @@ -54,20 +63,29 @@ public void setUp() { document = createDocument(); v2DeleteDocumentController = new V2DeleteDocumentController(); MockitoAnnotations.initMocks(this); + Mockito.when(propertyManager.getProperty(Properties.GILES_URL)).thenReturn("http://localhost:8085/giles/"); } @Test public void test_deleteDocument_success() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(true); + Map msgs = new HashMap(); + msgs.put("checkUrl", "http://localhost:8085/giles/api/v2/files/deletion/check/" + DOCUMENT_ID); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.OK)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + Assert.assertTrue(response.getBody().contains("http://localhost:8085/giles/api/v2/files/deletion/check/documentId")); } @Test public void test_deleteDocument_whenUserIsNotAuthorized() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(false); + Map unauthorizedMsgs = new HashMap(); + unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); + unauthorizedMsgs.put("errorCode", "401"); + Mockito.when(userHelper.generateUnauthorizedUserResponse()).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); } @@ -75,10 +93,47 @@ public void test_deleteDocument_whenUserIsNotAuthorized() { @Test public void test_deleteDocument_notFound() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(null); + Map msgs = new HashMap(); + msgs.put("errorMsg", "Document Id: " + DOCUMENT_ID + " does not exist."); + msgs.put("errorCode", "404"); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.NOT_FOUND)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); Assert.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } + @Test + public void test_checkDocumentDeletion_whenDocumentIsDeleted() { + Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(null); + Map successMsgs = new HashMap(); + successMsgs.put("successMessage", "Document Id: " + DOCUMENT_ID + " is deleted."); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(successMsgs, HttpStatus.OK)); + ResponseEntity response = v2DeleteDocumentController.checkDocumentDeletion(null, DOCUMENT_ID, CITESPHERE_TOKEN); + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + public void test_checkDocumentDeletion_whenDocumentIsNotDeleted() { + Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); + Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(true); + Map msgs = new HashMap(); + msgs.put("progressInfo", "Deletion in progress. Please check back later."); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.OK)); + ResponseEntity response = v2DeleteDocumentController.checkDocumentDeletion(null, DOCUMENT_ID, CITESPHERE_TOKEN); + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + public void test_checkDocumentDeletion_whenUserDoesNotHavePermissionToCheckDocumentStatus() { + Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); + Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(false); + Map unauthorizedMsgs = new HashMap(); + unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); + unauthorizedMsgs.put("errorCode", "401"); + Mockito.when(userHelper.generateUnauthorizedUserResponse()).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); + ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); + Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + private Document createDocument() { Document document = new Document(); document.setId(DOCUMENT_ID); @@ -88,4 +143,25 @@ private Document createDocument() { document.setUploadedFileId(FILE_ID); return document; } + + private ResponseEntity generateResponse(Map msgs, + HttpStatus status) { + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + ObjectNode root = mapper.createObjectNode(); + for (String key : msgs.keySet()) { + root.put(key, msgs.get(key)); + } + + StringWriter sw = new StringWriter(); + try { + mapper.writeValue(sw, root); + } catch (IOException e) { + return new ResponseEntity( + "{\"errorMsg\": \"Could not write json result.\", \"errorCode\": \"errorCode\": \"500\" }", + HttpStatus.INTERNAL_SERVER_ERROR); + } + + return new ResponseEntity(sw.toString(), status); + } } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java index dbbdcdb8..3dc9cd41 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java @@ -1,5 +1,11 @@ package edu.asu.diging.gilesecosystem.web.config.impl; +import java.io.IOException; +import java.io.StringWriter; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -7,12 +13,24 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionKey; import org.springframework.social.connect.UserProfile; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; +import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; +import edu.asu.diging.gilesecosystem.web.core.model.DocumentAccess; +import edu.asu.diging.gilesecosystem.web.core.model.IDocument; +import edu.asu.diging.gilesecosystem.web.core.model.impl.Document; import edu.asu.diging.gilesecosystem.web.core.users.AccountStatus; +import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; import edu.asu.diging.gilesecosystem.web.core.users.IUserManager; import edu.asu.diging.gilesecosystem.web.core.users.User; @@ -22,9 +40,26 @@ public class UserHelperTest { @Mock private Connection connection; + @Mock + private IResponseHelper responseHelper; + @InjectMocks private IUserHelper helperToTest; private String USER_ID = "user_id"; + private CitesphereToken citesphereToken = new CitesphereToken("71b9cc36-939d-4e28-89e9-e5bfca1b26c3"); + private String NAME = "name"; + private String FIRST_NAME = "firstname"; + private String LAST_NAME = "lastName"; + private String EMAIL = "email"; + private String USERNAME = "username"; + private String PROVIDER_ID = "providerId"; + private String PROVIDER_USER_ID = "providerUserId"; + private String EXISTING_USERNAME = USERNAME + "_" + PROVIDER_ID; + private String NEW_USERNAME = "newuser_providerId"; + private String DOCUMENT_ID = "documentId"; + private DocumentAccess access = DocumentAccess.PRIVATE; + private String FILE_ID = "fileId"; + private String UPLOAD_ID = "uploadId"; @Before public void setUp() { @@ -61,16 +96,7 @@ public void test_createUser_userNotExisting() { @Test public void test_createUser_userExists() { - String NAME = "name"; - String FIRST_NAME = "firstname"; - String LAST_NAME = "lastName"; - String EMAIL = "email"; - String USERNAME = "username"; - String PROVIDER_ID = "providerId"; - String PROVIDER_USER_ID = "providerUserId"; - String EXISTING_USERNAME = USERNAME + "_" + PROVIDER_ID; - String NEW_USERNAME = "newuser_providerId"; UserProfile profile = new UserProfile(USER_ID, NAME, FIRST_NAME, LAST_NAME, EMAIL, USERNAME); @@ -95,4 +121,66 @@ public void test_createUser_userExists() { Assert.assertEquals(PROVIDER_USER_ID, createdUser.getUserIdOfProvider()); Assert.assertEquals(AccountStatus.ADDED, createdUser.getAccountStatus()); } + + @Test + public void test_checkUserPermission_whenUserHasPermission() { + CitesphereUser user = new CitesphereUser(); + user.setUsername("newuser"); + user.setAuthorizingClient("providerId"); + citesphereToken.setPrincipal(user); + IDocument document = createDocument(NEW_USERNAME); + Assert.assertTrue(helperToTest.checkUserPermission(document, citesphereToken)); + } + + @Test + public void test_checkUserPermission_whenUserDoesNotHavePermission() { + CitesphereUser user = new CitesphereUser(); + user.setUsername("wrongUser"); + user.setAuthorizingClient("providerId"); + citesphereToken.setPrincipal(user); + IDocument document = createDocument(NEW_USERNAME); + Assert.assertFalse(helperToTest.checkUserPermission(document, citesphereToken)); + } + + @Test + public void test_generateUnauthorizedUserResponse() { + Map unauthorizedMsgs = new HashMap(); + unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); + unauthorizedMsgs.put("errorCode", "401"); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); + ResponseEntity response = helperToTest.generateUnauthorizedUserResponse(); + Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + + private Document createDocument(String username) { + Document document = new Document(); + document.setId(DOCUMENT_ID); + document.setAccess(access); + document.setFileIds(Arrays.asList(FILE_ID)); + document.setUploadId(UPLOAD_ID); + document.setUploadedFileId(FILE_ID); + document.setUsername(username); + return document; + } + + private ResponseEntity generateResponse(Map msgs, + HttpStatus status) { + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + ObjectNode root = mapper.createObjectNode(); + for (String key : msgs.keySet()) { + root.put(key, msgs.get(key)); + } + + StringWriter sw = new StringWriter(); + try { + mapper.writeValue(sw, root); + } catch (IOException e) { + return new ResponseEntity( + "{\"errorMsg\": \"Could not write json result.\", \"errorCode\": \"errorCode\": \"500\" }", + HttpStatus.INTERNAL_SERVER_ERROR); + } + + return new ResponseEntity(sw.toString(), status); + } } From 6c74ab9baaf5f3db038eb1731dd1913d47e3669b Mon Sep 17 00:00:00 2001 From: diya17 Date: Thu, 15 Jun 2023 16:42:30 -0400 Subject: [PATCH 07/24] [GECO-140] Removing generateUnauthorizedUserResponse from userHelper --- .../web/api/util/IResponseHelper.java | 9 ++++++ .../web/api/util/impl/ResponseHelper.java | 3 ++ .../api/v2/V2DeleteDocumentController.java | 10 ++++-- .../web/config/IUserHelper.java | 7 ----- .../web/config/impl/UserHelper.java | 8 ----- .../v2/V2DeleteDocumentControllerTest.java | 8 ++--- .../web/config/impl/UserHelperTest.java | 31 ------------------- 7 files changed, 24 insertions(+), 52 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java index f5ee4b04..90ba3287 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/IResponseHelper.java @@ -6,5 +6,14 @@ import org.springframework.http.ResponseEntity; public interface IResponseHelper { + /** + Generates a ResponseEntity with a JSON body based on the given map of messages and HTTP status. + + @param msgs a map of messages where the keys represent message identifiers and the values represent message content + + @param status the HTTP status code to be set in the response + + @return a ResponseEntity object with JSON content and the specified HTTP status + */ public abstract ResponseEntity generateResponse(Map msgs, HttpStatus status); } diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java index bb7f7059..768c72e8 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/util/impl/ResponseHelper.java @@ -17,6 +17,9 @@ import edu.asu.diging.gilesecosystem.septemberutil.service.ISystemMessageHandler; import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; +/** +A helper class that generates HTTP responses with JSON content. +*/ @Component public class ResponseHelper implements IResponseHelper { diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index 1489fdfe..6878da7e 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -54,7 +54,10 @@ public ResponseEntity deleteDocument(@PathVariable("documentId") String return responseHelper.generateResponse(msgs, HttpStatus.NOT_FOUND); } if (!userHelper.checkUserPermission(document, citesphereToken)) { - return userHelper.generateUnauthorizedUserResponse(); + Map unauthorizedMsgs = new HashMap(); + unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document."); + unauthorizedMsgs.put("errorCode", "401"); + return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED); } deleteDocumentService.deleteDocument(document); msgs.put("checkUrl", propertyManager.getProperty(Properties.GILES_URL) + checkDeleteEndpoint + documentId); @@ -72,7 +75,10 @@ public ResponseEntity checkDocumentDeletion(HttpServletRequest request, return responseHelper.generateResponse(successMsgs, HttpStatus.OK); } if (!userHelper.checkUserPermission(document, citesphereToken)) { - return userHelper.generateUnauthorizedUserResponse(); + Map unauthorizedMsgs = new HashMap(); + unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); + unauthorizedMsgs.put("errorCode", "401"); + return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED); } Map msgs = new HashMap(); msgs.put("progressInfo", "Deletion in progress. Please check back later."); diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java index 1bd4fa17..5ba844d0 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java @@ -19,11 +19,4 @@ public interface IUserHelper { @return {@code true} if the user has permission to access the document, {@code false} otherwise. */ public abstract boolean checkUserPermission(IDocument document, CitesphereToken citesphereToken); - - /** - Generates a ResponseEntity with an unauthorized status code (401) and a corresponding error message. - @return The generated ResponseEntity containing the unauthorized error message. - */ - public abstract ResponseEntity generateUnauthorizedUserResponse(); - } diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java index 1646afdf..9920aeb8 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java @@ -70,12 +70,4 @@ public boolean checkUserPermission(IDocument document, CitesphereToken citespher return username.equals(usernameInSystem); } - - @Override - public ResponseEntity generateUnauthorizedUserResponse() { - Map unauthorizedMsgs = new HashMap(); - unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); - unauthorizedMsgs.put("errorCode", "401"); - return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED); - } } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java index 696c7295..b243fc06 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java @@ -83,9 +83,9 @@ public void test_deleteDocument_whenUserIsNotAuthorized() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(false); Map unauthorizedMsgs = new HashMap(); - unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); + unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document."); unauthorizedMsgs.put("errorCode", "401"); - Mockito.when(userHelper.generateUnauthorizedUserResponse()).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); } @@ -128,8 +128,8 @@ public void test_checkDocumentDeletion_whenUserDoesNotHavePermissionToCheckDocum Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(false); Map unauthorizedMsgs = new HashMap(); unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); - unauthorizedMsgs.put("errorCode", "401"); - Mockito.when(userHelper.generateUnauthorizedUserResponse()).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); + unauthorizedMsgs.put("errorCode", "401"); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java index 3dc9cd41..b293f6c2 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java @@ -142,16 +142,6 @@ public void test_checkUserPermission_whenUserDoesNotHavePermission() { Assert.assertFalse(helperToTest.checkUserPermission(document, citesphereToken)); } - @Test - public void test_generateUnauthorizedUserResponse() { - Map unauthorizedMsgs = new HashMap(); - unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); - unauthorizedMsgs.put("errorCode", "401"); - Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); - ResponseEntity response = helperToTest.generateUnauthorizedUserResponse(); - Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); - } - private Document createDocument(String username) { Document document = new Document(); document.setId(DOCUMENT_ID); @@ -162,25 +152,4 @@ private Document createDocument(String username) { document.setUsername(username); return document; } - - private ResponseEntity generateResponse(Map msgs, - HttpStatus status) { - ObjectMapper mapper = new ObjectMapper(); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - ObjectNode root = mapper.createObjectNode(); - for (String key : msgs.keySet()) { - root.put(key, msgs.get(key)); - } - - StringWriter sw = new StringWriter(); - try { - mapper.writeValue(sw, root); - } catch (IOException e) { - return new ResponseEntity( - "{\"errorMsg\": \"Could not write json result.\", \"errorCode\": \"errorCode\": \"500\" }", - HttpStatus.INTERNAL_SERVER_ERROR); - } - - return new ResponseEntity(sw.toString(), status); - } } From f9e97720b5c0a12ecb2e61a2406094710f6cf5b7 Mon Sep 17 00:00:00 2001 From: diya17 Date: Fri, 23 Jun 2023 12:28:29 -0400 Subject: [PATCH 08/24] [GECO-140] Refactoring code and and testcases --- .../api/v2/V2DeleteDocumentController.java | 6 +- .../web/config/IUserHelper.java | 2 +- .../web/config/impl/UserHelper.java | 11 +-- .../v2/V2DeleteDocumentControllerTest.java | 10 +-- .../web/config/impl/UserHelperTest.java | 67 ++++++++----------- 5 files changed, 37 insertions(+), 59 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index 6878da7e..ef93513d 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -49,11 +49,11 @@ public ResponseEntity deleteDocument(@PathVariable("documentId") String IDocument document = documentService.getDocument(documentId); Map msgs = new HashMap(); if (document == null) { - msgs.put("errorMsg", "Document Id: " + documentId + " does not exist."); + msgs.put("errorMsg", "Document with id: " + documentId + " does not exist."); msgs.put("errorCode", "404"); return responseHelper.generateResponse(msgs, HttpStatus.NOT_FOUND); } - if (!userHelper.checkUserPermission(document, citesphereToken)) { + if (!userHelper.isUserPermittedToAccessDocument(document, citesphereToken)) { Map unauthorizedMsgs = new HashMap(); unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document."); unauthorizedMsgs.put("errorCode", "401"); @@ -74,7 +74,7 @@ public ResponseEntity checkDocumentDeletion(HttpServletRequest request, successMsgs.put("successMessage", "Document Id: " + documentId + " is deleted."); return responseHelper.generateResponse(successMsgs, HttpStatus.OK); } - if (!userHelper.checkUserPermission(document, citesphereToken)) { + if (!userHelper.isUserPermittedToAccessDocument(document, citesphereToken)) { Map unauthorizedMsgs = new HashMap(); unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); unauthorizedMsgs.put("errorCode", "401"); diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java index 5ba844d0..fa17d26a 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java @@ -18,5 +18,5 @@ public interface IUserHelper { @param citesphereToken The Citesphere token for the user. @return {@code true} if the user has permission to access the document, {@code false} otherwise. */ - public abstract boolean checkUserPermission(IDocument document, CitesphereToken citesphereToken); + public abstract boolean isUserPermittedToAccessDocument(IDocument document, CitesphereToken citesphereToken); } diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java index 9920aeb8..4fe7d005 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java @@ -1,16 +1,10 @@ package edu.asu.diging.gilesecosystem.web.config.impl; -import java.util.HashMap; -import java.util.Map; - import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.social.connect.Connection; import org.springframework.social.connect.UserProfile; import org.springframework.stereotype.Service; -import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; @@ -24,9 +18,6 @@ public class UserHelper implements IUserHelper { @Autowired private IUserManager userManager; - - @Autowired - private IResponseHelper responseHelper; /* (non-Javadoc) * @see edu.asu.giles.config.IUserHelper#createUser(org.springframework.social.connect.Connection) @@ -63,7 +54,7 @@ public String createUsername(String username, String providerId) { } @Override - public boolean checkUserPermission(IDocument document, CitesphereToken citesphereToken) { + public boolean isUserPermittedToAccessDocument(IDocument document, CitesphereToken citesphereToken) { String username = document.getUsername(); CitesphereUser user = (CitesphereUser) citesphereToken.getPrincipal(); String usernameInSystem = createUsername(user.getUsername(), user.getAuthorizingClient()); diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java index b243fc06..f7e2451b 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java @@ -69,7 +69,7 @@ public void setUp() { @Test public void test_deleteDocument_success() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(true); + Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(true); Map msgs = new HashMap(); msgs.put("checkUrl", "http://localhost:8085/giles/api/v2/files/deletion/check/" + DOCUMENT_ID); Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.OK)); @@ -81,7 +81,7 @@ public void test_deleteDocument_success() { @Test public void test_deleteDocument_whenUserIsNotAuthorized() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(false); + Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(false); Map unauthorizedMsgs = new HashMap(); unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document."); unauthorizedMsgs.put("errorCode", "401"); @@ -94,7 +94,7 @@ public void test_deleteDocument_whenUserIsNotAuthorized() { public void test_deleteDocument_notFound() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(null); Map msgs = new HashMap(); - msgs.put("errorMsg", "Document Id: " + DOCUMENT_ID + " does not exist."); + msgs.put("errorMsg", "Document with id: " + DOCUMENT_ID + " does not exist."); msgs.put("errorCode", "404"); Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.NOT_FOUND)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); @@ -114,7 +114,7 @@ public void test_checkDocumentDeletion_whenDocumentIsDeleted() { @Test public void test_checkDocumentDeletion_whenDocumentIsNotDeleted() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(true); + Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(true); Map msgs = new HashMap(); msgs.put("progressInfo", "Deletion in progress. Please check back later."); Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.OK)); @@ -125,7 +125,7 @@ public void test_checkDocumentDeletion_whenDocumentIsNotDeleted() { @Test public void test_checkDocumentDeletion_whenUserDoesNotHavePermissionToCheckDocumentStatus() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.checkUserPermission(document, CITESPHERE_TOKEN)).thenReturn(false); + Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(false); Map unauthorizedMsgs = new HashMap(); unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); unauthorizedMsgs.put("errorCode", "401"); diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java index b293f6c2..e8a9dbde 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java @@ -1,10 +1,6 @@ package edu.asu.diging.gilesecosystem.web.config.impl; -import java.io.IOException; -import java.io.StringWriter; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; import org.junit.Assert; import org.junit.Before; @@ -13,17 +9,10 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionKey; import org.springframework.social.connect.UserProfile; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.node.ObjectNode; - -import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; import edu.asu.diging.gilesecosystem.web.core.model.DocumentAccess; @@ -40,26 +29,14 @@ public class UserHelperTest { @Mock private Connection connection; - @Mock - private IResponseHelper responseHelper; - @InjectMocks private IUserHelper helperToTest; private String USER_ID = "user_id"; - private CitesphereToken citesphereToken = new CitesphereToken("71b9cc36-939d-4e28-89e9-e5bfca1b26c3"); - private String NAME = "name"; - private String FIRST_NAME = "firstname"; - private String LAST_NAME = "lastName"; - private String EMAIL = "email"; - private String USERNAME = "username"; - private String PROVIDER_ID = "providerId"; - private String PROVIDER_USER_ID = "providerUserId"; - private String EXISTING_USERNAME = USERNAME + "_" + PROVIDER_ID; - private String NEW_USERNAME = "newuser_providerId"; private String DOCUMENT_ID = "documentId"; private DocumentAccess access = DocumentAccess.PRIVATE; private String FILE_ID = "fileId"; private String UPLOAD_ID = "uploadId"; + private CitesphereToken citesphereToken = new CitesphereToken("71b9cc36-939d-4e28-89e9-e5bfca1b26c3"); @Before public void setUp() { @@ -96,7 +73,16 @@ public void test_createUser_userNotExisting() { @Test public void test_createUser_userExists() { + String NAME = "name"; + String FIRST_NAME = "firstname"; + String LAST_NAME = "lastName"; + String EMAIL = "email"; + String USERNAME = "username"; + String PROVIDER_ID = "providerId"; + String PROVIDER_USER_ID = "providerUserId"; + String EXISTING_USERNAME = USERNAME + "_" + PROVIDER_ID; + String NEW_USERNAME = "newuser_providerId"; UserProfile profile = new UserProfile(USER_ID, NAME, FIRST_NAME, LAST_NAME, EMAIL, USERNAME); @@ -123,33 +109,34 @@ public void test_createUser_userExists() { } @Test - public void test_checkUserPermission_whenUserHasPermission() { - CitesphereUser user = new CitesphereUser(); - user.setUsername("newuser"); - user.setAuthorizingClient("providerId"); - citesphereToken.setPrincipal(user); - IDocument document = createDocument(NEW_USERNAME); - Assert.assertTrue(helperToTest.checkUserPermission(document, citesphereToken)); + public void test_isUserPermittedToAccessDocument_whenUserIsAllowedToAccessDocument() { + IDocument document = createDocument(); + citesphereToken.setPrincipal(createCitesphereUser("dab")); + Assert.assertTrue(helperToTest.isUserPermittedToAccessDocument(document, citesphereToken)); } @Test - public void test_checkUserPermission_whenUserDoesNotHavePermission() { - CitesphereUser user = new CitesphereUser(); - user.setUsername("wrongUser"); - user.setAuthorizingClient("providerId"); - citesphereToken.setPrincipal(user); - IDocument document = createDocument(NEW_USERNAME); - Assert.assertFalse(helperToTest.checkUserPermission(document, citesphereToken)); + public void test_isUserPermittedToAccessDocument_whenUserIsNotAllowedToAccessDocument() { + IDocument document = createDocument(); + citesphereToken.setPrincipal(createCitesphereUser("test1")); + Assert.assertFalse(helperToTest.isUserPermittedToAccessDocument(document, citesphereToken)); } - private Document createDocument(String username) { + private Document createDocument() { Document document = new Document(); document.setId(DOCUMENT_ID); document.setAccess(access); document.setFileIds(Arrays.asList(FILE_ID)); document.setUploadId(UPLOAD_ID); document.setUploadedFileId(FILE_ID); - document.setUsername(username); + document.setUsername("dab_citesphere"); return document; } + + private CitesphereUser createCitesphereUser(String userName) { + CitesphereUser user = new CitesphereUser(); + user.setAuthorizingClient("citesphere"); + user.setUsername(userName); + return user; + } } From e515ac62b89a2419071e07807832f2e1b1122573 Mon Sep 17 00:00:00 2001 From: diya17 Date: Fri, 30 Jun 2023 14:51:25 -0400 Subject: [PATCH 09/24] [GECO-140] Making changes to check user access using citesphere --- .../api/v2/V2DeleteDocumentController.java | 21 ++++++++----- .../web/config/IUserHelper.java | 11 +------ .../web/config/impl/UserHelper.java | 12 -------- .../v2/V2DeleteDocumentControllerTest.java | 30 ++++++++++--------- 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index ef93513d..8e6b859d 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -18,10 +18,12 @@ import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; +import edu.asu.diging.gilesecosystem.web.core.citesphere.ICitesphereConnector; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; +import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; @Controller public class V2DeleteDocumentController { @@ -41,6 +43,9 @@ public class V2DeleteDocumentController { @Autowired private IUserHelper userHelper; + @Autowired + private ICitesphereConnector citesphereConnector; + @Value("${giles_check_deletion_endpoint_v2}") private String checkDeleteEndpoint; @@ -53,11 +58,11 @@ public ResponseEntity deleteDocument(@PathVariable("documentId") String msgs.put("errorCode", "404"); return responseHelper.generateResponse(msgs, HttpStatus.NOT_FOUND); } - if (!userHelper.isUserPermittedToAccessDocument(document, citesphereToken)) { + if (!citesphereConnector.hasAccess(document.getId(), ((CitesphereUser)citesphereToken.getPrincipal()).getUsername())) { Map unauthorizedMsgs = new HashMap(); - unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document."); - unauthorizedMsgs.put("errorCode", "401"); - return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED); + unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document with id " + document.getId()); + unauthorizedMsgs.put("errorCode", "403"); + return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.FORBIDDEN); } deleteDocumentService.deleteDocument(document); msgs.put("checkUrl", propertyManager.getProperty(Properties.GILES_URL) + checkDeleteEndpoint + documentId); @@ -74,11 +79,11 @@ public ResponseEntity checkDocumentDeletion(HttpServletRequest request, successMsgs.put("successMessage", "Document Id: " + documentId + " is deleted."); return responseHelper.generateResponse(successMsgs, HttpStatus.OK); } - if (!userHelper.isUserPermittedToAccessDocument(document, citesphereToken)) { + if (!citesphereConnector.hasAccess(document.getId(), ((CitesphereUser)citesphereToken.getPrincipal()).getUsername())) { Map unauthorizedMsgs = new HashMap(); - unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); - unauthorizedMsgs.put("errorCode", "401"); - return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED); + unauthorizedMsgs.put("errorMsg", "User is not authorized to check status for document id " + document.getId()); + unauthorizedMsgs.put("errorCode", "403"); + return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.FORBIDDEN); } Map msgs = new HashMap(); msgs.put("progressInfo", "Deletion in progress. Please check back later."); diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java index fa17d26a..b981db55 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java @@ -1,9 +1,7 @@ package edu.asu.diging.gilesecosystem.web.config; -import org.springframework.http.ResponseEntity; import org.springframework.social.connect.Connection; -import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.users.User; public interface IUserHelper { @@ -11,12 +9,5 @@ public interface IUserHelper { public abstract User createUser(Connection connection); String createUsername(String username, String providerId); - - /** - Checks whether the user associated with the provided Citesphere token has permission to access the given document. - @param document The document for which to check the user permission. - @param citesphereToken The Citesphere token for the user. - @return {@code true} if the user has permission to access the document, {@code false} otherwise. - */ - public abstract boolean isUserPermittedToAccessDocument(IDocument document, CitesphereToken citesphereToken); + } diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java index 4fe7d005..c515fab1 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelper.java @@ -5,11 +5,8 @@ import org.springframework.social.connect.UserProfile; import org.springframework.stereotype.Service; -import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; -import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.users.AccountStatus; -import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; import edu.asu.diging.gilesecosystem.web.core.users.IUserManager; import edu.asu.diging.gilesecosystem.web.core.users.User; @@ -52,13 +49,4 @@ public User createUser(Connection connection) { public String createUsername(String username, String providerId) { return username + "_" + providerId; } - - @Override - public boolean isUserPermittedToAccessDocument(IDocument document, CitesphereToken citesphereToken) { - String username = document.getUsername(); - CitesphereUser user = (CitesphereUser) citesphereToken.getPrincipal(); - String usernameInSystem = createUsername(user.getUsername(), user.getAuthorizingClient()); - - return username.equals(usernameInSystem); - } } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java index f7e2451b..0f7c9510 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.StringWriter; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -23,13 +24,14 @@ import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; -import edu.asu.diging.gilesecosystem.web.config.IUserHelper; +import edu.asu.diging.gilesecosystem.web.core.citesphere.ICitesphereConnector; import edu.asu.diging.gilesecosystem.web.core.model.DocumentAccess; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.model.impl.Document; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; +import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; public class V2DeleteDocumentControllerTest { @@ -39,15 +41,15 @@ public class V2DeleteDocumentControllerTest { @Mock private ITransactionalDocumentService documentService; - @Mock - private IUserHelper userHelper; - @Mock private IPropertiesManager propertyManager; @Mock private IResponseHelper responseHelper; + @Mock + private ICitesphereConnector citesphereConnector; + @InjectMocks private V2DeleteDocumentController v2DeleteDocumentController; @@ -64,12 +66,13 @@ public void setUp() { v2DeleteDocumentController = new V2DeleteDocumentController(); MockitoAnnotations.initMocks(this); Mockito.when(propertyManager.getProperty(Properties.GILES_URL)).thenReturn("http://localhost:8085/giles/"); + Mockito.when(citesphereConnector.hasAccess(Mockito.anyString(), Mockito.anyString())).thenReturn(true); + CITESPHERE_TOKEN.setPrincipal(new CitesphereUser("testUser", "oauth", new ArrayList<>())); } @Test public void test_deleteDocument_success() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(true); Map msgs = new HashMap(); msgs.put("checkUrl", "http://localhost:8085/giles/api/v2/files/deletion/check/" + DOCUMENT_ID); Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.OK)); @@ -81,13 +84,13 @@ public void test_deleteDocument_success() { @Test public void test_deleteDocument_whenUserIsNotAuthorized() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(false); + Mockito.when(citesphereConnector.hasAccess(Mockito.anyString(), Mockito.anyString())).thenReturn(false); Map unauthorizedMsgs = new HashMap(); - unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document."); + unauthorizedMsgs.put("errorMsg", "User is not authorized to delete the document with id " + document.getId()); unauthorizedMsgs.put("errorCode", "401"); - Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.FORBIDDEN)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); - Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); } @Test @@ -114,7 +117,6 @@ public void test_checkDocumentDeletion_whenDocumentIsDeleted() { @Test public void test_checkDocumentDeletion_whenDocumentIsNotDeleted() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(true); Map msgs = new HashMap(); msgs.put("progressInfo", "Deletion in progress. Please check back later."); Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.OK)); @@ -125,13 +127,13 @@ public void test_checkDocumentDeletion_whenDocumentIsNotDeleted() { @Test public void test_checkDocumentDeletion_whenUserDoesNotHavePermissionToCheckDocumentStatus() { Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); - Mockito.when(userHelper.isUserPermittedToAccessDocument(document, CITESPHERE_TOKEN)).thenReturn(false); + Mockito.when(citesphereConnector.hasAccess(Mockito.anyString(), Mockito.anyString())).thenReturn(false); Map unauthorizedMsgs = new HashMap(); - unauthorizedMsgs.put("errorMsg", "User is not authorized to check status."); + unauthorizedMsgs.put("errorMsg", "User is not authorized to check status for document id " + document.getId()); unauthorizedMsgs.put("errorCode", "401"); - Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.UNAUTHORIZED)); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(unauthorizedMsgs, HttpStatus.FORBIDDEN)); ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); - Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); } private Document createDocument() { From 93f7d139acb685a0b1fffa3fbd62d27c173056b4 Mon Sep 17 00:00:00 2001 From: diya17 Date: Mon, 3 Jul 2023 10:32:08 -0700 Subject: [PATCH 10/24] [GECO-140] Final changes after using citesphere connector --- .../edu/asu/diging/gilesecosystem/web/config/IUserHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java index b981db55..237c07ba 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/config/IUserHelper.java @@ -10,4 +10,4 @@ public interface IUserHelper { String createUsername(String username, String providerId); -} +} \ No newline at end of file From 753ad10dbfa0a69ff1fb5a334715520e31005980 Mon Sep 17 00:00:00 2001 From: diya17 Date: Tue, 25 Jul 2023 16:59:05 -0700 Subject: [PATCH 11/24] [GECO-140] Changing Giles Deletion API and also saving Processing requests --- .../api/v2/V2DeleteDocumentController.java | 17 ++++++++++++++++ ...kaDeletionCompletionProcessingListner.java | 19 +++++++++++++++++- .../delete/impl/DeleteDocumentService.java | 20 +++++++++++++------ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index 8e6b859d..5f27d6cb 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -1,6 +1,7 @@ package edu.asu.diging.gilesecosystem.web.api.v2; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -14,13 +15,16 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import edu.asu.diging.gilesecosystem.requests.RequestStatus; import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; import edu.asu.diging.gilesecosystem.web.core.citesphere.ICitesphereConnector; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; +import edu.asu.diging.gilesecosystem.web.core.model.IProcessingRequest; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalProcessingRequestService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; @@ -45,6 +49,9 @@ public class V2DeleteDocumentController { @Autowired private ICitesphereConnector citesphereConnector; + + @Autowired + private ITransactionalProcessingRequestService processingRequestService; @Value("${giles_check_deletion_endpoint_v2}") private String checkDeleteEndpoint; @@ -85,6 +92,16 @@ public ResponseEntity checkDocumentDeletion(HttpServletRequest request, unauthorizedMsgs.put("errorCode", "403"); return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.FORBIDDEN); } + if (!document.getRequestId().isEmpty()) { + List pRequests = processingRequestService.getProcRequestsByRequestId(document.getRequestId()); + for (IProcessingRequest pReq : pRequests) { + if (pReq.getRequestStatus().equals(RequestStatus.FAILED)) { + Map deletionErrorMsg = new HashMap(); + deletionErrorMsg.put("progressInfo", "There was an error in deleting the document. Please try again."); + return responseHelper.generateResponse(deletionErrorMsg, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + } Map msgs = new HashMap(); msgs.put("progressInfo", "Deletion in progress. Please check back later."); return responseHelper.generateResponse(msgs, HttpStatus.OK); diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/kafka/KafkaDeletionCompletionProcessingListner.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/kafka/KafkaDeletionCompletionProcessingListner.java index 6edcbf51..0fac3da9 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/kafka/KafkaDeletionCompletionProcessingListner.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/kafka/KafkaDeletionCompletionProcessingListner.java @@ -1,6 +1,7 @@ package edu.asu.diging.gilesecosystem.web.core.kafka; import java.io.IOException; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.PropertySource; @@ -15,7 +16,10 @@ import edu.asu.diging.gilesecosystem.requests.impl.CompletedStorageDeletionRequest; import edu.asu.diging.gilesecosystem.septemberutil.properties.MessageType; import edu.asu.diging.gilesecosystem.septemberutil.service.ISystemMessageHandler; +import edu.asu.diging.gilesecosystem.util.exceptions.UnstorableObjectException; +import edu.asu.diging.gilesecosystem.web.core.model.IProcessingRequest; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalProcessingRequestService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; @PropertySource("classpath:/config.properties") @@ -30,6 +34,9 @@ public class KafkaDeletionCompletionProcessingListner { @Autowired private ITransactionalDocumentService documentService; + @Autowired + private ITransactionalProcessingRequestService processingRequestService; + @KafkaListener(id = "giles.deletion.topic.listener", topics = "${topic_delete_storage_request_complete}") public void receiveDeletionCompletedMessageMessage(String message, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) { ObjectMapper mapper = new ObjectMapper(); @@ -40,7 +47,17 @@ public void receiveDeletionCompletedMessageMessage(String message, @Header(Kafka messageHandler.handleMessage("Could not unmarshall request.", e, MessageType.ERROR); return; } - if (request.getStatus() == RequestStatus.FAILED) { + if (request.getStatus().equals(RequestStatus.FAILED)) { + List pRequests = processingRequestService.getProcRequestsByRequestId(request.getRequestId()); + for (IProcessingRequest pReq : pRequests) { + pReq.setRequestStatus(request.getStatus()); + try { + processingRequestService.save(pReq); + } catch (UnstorableObjectException e) { + // should never happen + messageHandler.handleMessage("Could not store request.", e, MessageType.ERROR); + } + } return; } deleteDocumentService.deleteDocumentAfterStorageDeletion(documentService.getDocument(request.getDocumentId())); diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java index 4fec7082..c4175bf3 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java @@ -29,6 +29,8 @@ import edu.asu.diging.gilesecosystem.web.core.files.IFilesManager; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; import edu.asu.diging.gilesecosystem.web.core.model.IFile; +import edu.asu.diging.gilesecosystem.web.core.model.IProcessingRequest; +import edu.asu.diging.gilesecosystem.web.core.model.impl.ProcessingRequest; import edu.asu.diging.gilesecosystem.web.core.service.IFileTypeHandler; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalFileService; @@ -131,19 +133,21 @@ private IRequest createRequest(IDocument document) throws GilesProcessingExcepti IStorageDeletionRequest storageDeletionRequest = null; try { String requestId = documentService.generateRequestId(REQUEST_PREFIX); - storeRequestId(requestId, document); storageDeletionRequest = requestFactory.createRequest(requestId, document.getUploadId()); + storeRequestId(requestId, document); storageDeletionRequest.setDocumentId(document.getId()); } catch (InstantiationException | IllegalAccessException e) { throw new GilesProcessingException(e); - } + } + IProcessingRequest procReq = new ProcessingRequest(); + procReq.setRequestId(storageDeletionRequest.getRequestId()); + procReq.setDocumentId(document.getId()); + procReq.setSentRequest(storageDeletionRequest); + procReq.setRequestStatus(storageDeletionRequest.getStatus()); + processingRequestService.saveNewProcessingRequest(procReq); return storageDeletionRequest; } - private String getTopic() { - return propertyManager.getProperty(Properties.KAFKA_TOPIC_STORAGE_DELETION_REQUEST); - } - private void storeRequestId(String requestId, IDocument document) { document.setRequestId(requestId); try { @@ -152,4 +156,8 @@ private void storeRequestId(String requestId, IDocument document) { messageHandler.handleMessage("Could not store document.", e, MessageType.ERROR); } } + + private String getTopic() { + return propertyManager.getProperty(Properties.KAFKA_TOPIC_STORAGE_DELETION_REQUEST); + } } From 5a472cdb724a198e3d63ed75abb2b27c2a658180 Mon Sep 17 00:00:00 2001 From: diya17 Date: Fri, 28 Jul 2023 16:56:41 -0700 Subject: [PATCH 12/24] [GECO-140] Changes to API and processing requests --- .../gilesecosystem/web/api/v2/V2DeleteDocumentController.java | 2 +- .../web/core/service/delete/impl/DeleteDocumentService.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java index 5f27d6cb..69a6d54a 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentController.java @@ -104,6 +104,6 @@ public ResponseEntity checkDocumentDeletion(HttpServletRequest request, } Map msgs = new HashMap(); msgs.put("progressInfo", "Deletion in progress. Please check back later."); - return responseHelper.generateResponse(msgs, HttpStatus.OK); + return responseHelper.generateResponse(msgs, HttpStatus.ACCEPTED); } } diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java index c4175bf3..2ca191a2 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java @@ -140,6 +140,7 @@ private IRequest createRequest(IDocument document) throws GilesProcessingExcepti throw new GilesProcessingException(e); } IProcessingRequest procReq = new ProcessingRequest(); + procReq.setRequestId(storageDeletionRequest.getRequestId()); procReq.setDocumentId(document.getId()); procReq.setSentRequest(storageDeletionRequest); From bc32897a6106a05109af88402dd39202f31dae9f Mon Sep 17 00:00:00 2001 From: diya17 Date: Mon, 31 Jul 2023 17:00:03 -0700 Subject: [PATCH 13/24] [GECO-140] Adding storage deletion request to orm.xml --- giles-eco/src/main/resources/META-INF/hibernate/orm.xml | 3 +++ giles-eco/src/main/resources/META-INF/orm.xml | 2 ++ 2 files changed, 5 insertions(+) diff --git a/giles-eco/src/main/resources/META-INF/hibernate/orm.xml b/giles-eco/src/main/resources/META-INF/hibernate/orm.xml index 6accaf89..88b8686e 100644 --- a/giles-eco/src/main/resources/META-INF/hibernate/orm.xml +++ b/giles-eco/src/main/resources/META-INF/hibernate/orm.xml @@ -77,6 +77,9 @@ + + + diff --git a/giles-eco/src/main/resources/META-INF/orm.xml b/giles-eco/src/main/resources/META-INF/orm.xml index 875dd4ee..fbc00116 100644 --- a/giles-eco/src/main/resources/META-INF/orm.xml +++ b/giles-eco/src/main/resources/META-INF/orm.xml @@ -28,6 +28,8 @@ + + From 0379d099f9799c7825bbf4db94588f741ff3e63b Mon Sep 17 00:00:00 2001 From: diya17 Date: Tue, 1 Aug 2023 16:51:43 -0700 Subject: [PATCH 14/24] [GECO-140] Setting request status for storage deletion in processing request --- .../web/core/service/delete/impl/DeleteDocumentService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java index 2ca191a2..bdc96d42 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java @@ -17,6 +17,7 @@ import edu.asu.diging.gilesecosystem.requests.IRequest; import edu.asu.diging.gilesecosystem.requests.IRequestFactory; import edu.asu.diging.gilesecosystem.requests.IStorageDeletionRequest; +import edu.asu.diging.gilesecosystem.requests.RequestStatus; import edu.asu.diging.gilesecosystem.requests.exceptions.MessageCreationException; import edu.asu.diging.gilesecosystem.requests.impl.StorageDeletionRequest; import edu.asu.diging.gilesecosystem.requests.kafka.IRequestProducer; @@ -136,6 +137,7 @@ private IRequest createRequest(IDocument document) throws GilesProcessingExcepti storageDeletionRequest = requestFactory.createRequest(requestId, document.getUploadId()); storeRequestId(requestId, document); storageDeletionRequest.setDocumentId(document.getId()); + storageDeletionRequest.setStatus(RequestStatus.NEW); } catch (InstantiationException | IllegalAccessException e) { throw new GilesProcessingException(e); } From ebcdfd1bd6751f95bb02f1e4b622961f219f42c8 Mon Sep 17 00:00:00 2001 From: diya17 Date: Wed, 2 Aug 2023 16:59:30 -0700 Subject: [PATCH 15/24] [GECO-140] Deleting requests for document initial commit --- .../core/files/IRequestDatabaseClient.java | 5 +++++ .../files/impl/RequestDatabaseClient.java | 22 +++++++++++++++++++ .../core/repository/RequestRepository.java | 11 ++++++++++ .../core/ITransactionalRequestService.java | 5 +++++ .../impl/TranscationalRequestService.java | 18 +++++++++++++++ .../delete/impl/DeleteDocumentService.java | 9 ++++++++ 6 files changed, 70 insertions(+) create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/IRequestDatabaseClient.java create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/repository/RequestRepository.java create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/ITransactionalRequestService.java create mode 100644 giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/impl/TranscationalRequestService.java diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/IRequestDatabaseClient.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/IRequestDatabaseClient.java new file mode 100644 index 00000000..81526d7f --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/IRequestDatabaseClient.java @@ -0,0 +1,5 @@ +package edu.asu.diging.gilesecosystem.web.core.files; + +public interface IRequestDatabaseClient { + public void deleteRequestsByDocumentId(String documentId); +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java new file mode 100644 index 00000000..97132cb2 --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java @@ -0,0 +1,22 @@ +package edu.asu.diging.gilesecosystem.web.core.files.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import edu.asu.diging.gilesecosystem.web.core.files.IRequestDatabaseClient; +import edu.asu.diging.gilesecosystem.web.core.repository.RequestRepository; + +@Component +public class RequestDatabaseClient implements IRequestDatabaseClient { + @Autowired + private final RequestRepository requestRepository; + + @Autowired + public RequestDatabaseClient(RequestRepository requestRepository) { + this.requestRepository = requestRepository; + } + + public void deleteRequestsByDocumentId(String documentId) { + requestRepository.deleteByDocumentId(documentId); + } +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/repository/RequestRepository.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/repository/RequestRepository.java new file mode 100644 index 00000000..8e235b18 --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/repository/RequestRepository.java @@ -0,0 +1,11 @@ +package edu.asu.diging.gilesecosystem.web.core.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import edu.asu.diging.gilesecosystem.requests.impl.Request; + +@Repository +public interface RequestRepository extends JpaRepository { + void deleteByDocumentId(String documentId); +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/ITransactionalRequestService.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/ITransactionalRequestService.java new file mode 100644 index 00000000..5a2b888b --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/ITransactionalRequestService.java @@ -0,0 +1,5 @@ +package edu.asu.diging.gilesecosystem.web.core.service.core; + +public interface ITransactionalRequestService { + public void deleteRequestsByDocumentId(String documentId); +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/impl/TranscationalRequestService.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/impl/TranscationalRequestService.java new file mode 100644 index 00000000..73589c40 --- /dev/null +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/core/impl/TranscationalRequestService.java @@ -0,0 +1,18 @@ +package edu.asu.diging.gilesecosystem.web.core.service.core.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.asu.diging.gilesecosystem.web.core.files.IRequestDatabaseClient; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalRequestService; + +@Service +public class TranscationalRequestService implements ITransactionalRequestService { + @Autowired + private IRequestDatabaseClient requestDatabaseClient; + + @Override + public void deleteRequestsByDocumentId(String documentId) { + requestDatabaseClient.deleteRequestsByDocumentId(documentId); + } +} diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java index bdc96d42..3ede051a 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java @@ -36,6 +36,7 @@ import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalFileService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalProcessingRequestService; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalRequestService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalUploadService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; @@ -76,6 +77,9 @@ public class DeleteDocumentService implements IDeleteDocumentService { @Autowired private ITransactionalProcessingRequestService processingRequestService; + @Autowired + private ITransactionalRequestService requestService; + private Map fileTypes; @PostConstruct @@ -111,6 +115,7 @@ public void deleteDocument(IDocument document) { public void deleteDocumentAfterStorageDeletion(IDocument document) { processDeleteFilesOfDocument(document); processDeleteProcessingRequestsOfDocument(document); + processDeleteRequestsOfDocument(document); documentService.deleteDocument(document.getId()); processDeleteUploadOfDocument(document.getUploadId()); } @@ -163,4 +168,8 @@ private void storeRequestId(String requestId, IDocument document) { private String getTopic() { return propertyManager.getProperty(Properties.KAFKA_TOPIC_STORAGE_DELETION_REQUEST); } + + private void processDeleteRequestsOfDocument(IDocument document) { + requestService.deleteRequestsByDocumentId(document.getId()); + } } From 1001a557b0ea84db2e2a6bcc40cef19766421738 Mon Sep 17 00:00:00 2001 From: diya17 Date: Fri, 4 Aug 2023 16:44:54 -0700 Subject: [PATCH 16/24] [GECO-140] Deleting requests --- .../files/impl/RequestDatabaseClient.java | 12 ++++- .../v2/V2DeleteDocumentControllerTest.java | 24 ++++++++++ .../web/config/impl/UserHelperTest.java | 44 ------------------- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java index 97132cb2..d897237c 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java @@ -1,13 +1,23 @@ package edu.asu.diging.gilesecosystem.web.core.files.impl; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import edu.asu.diging.gilesecosystem.util.store.objectdb.DatabaseClient; import edu.asu.diging.gilesecosystem.web.core.files.IRequestDatabaseClient; import edu.asu.diging.gilesecosystem.web.core.repository.RequestRepository; +@Transactional @Component -public class RequestDatabaseClient implements IRequestDatabaseClient { +public class RequestDatabaseClient extends DatabaseClient implements IRequestDatabaseClient { + + @PersistenceContext(unitName="entityManagerFactory") + private EntityManager em; + @Autowired private final RequestRepository requestRepository; diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java index 0f7c9510..54b8e52b 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/api/v2/V2DeleteDocumentControllerTest.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.junit.Assert; @@ -21,14 +22,18 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.node.ObjectNode; +import edu.asu.diging.gilesecosystem.requests.RequestStatus; import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper; import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.core.citesphere.ICitesphereConnector; import edu.asu.diging.gilesecosystem.web.core.model.DocumentAccess; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; +import edu.asu.diging.gilesecosystem.web.core.model.IProcessingRequest; import edu.asu.diging.gilesecosystem.web.core.model.impl.Document; +import edu.asu.diging.gilesecosystem.web.core.model.impl.ProcessingRequest; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalProcessingRequestService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties; import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; @@ -50,6 +55,9 @@ public class V2DeleteDocumentControllerTest { @Mock private ICitesphereConnector citesphereConnector; + @Mock + private ITransactionalProcessingRequestService processingRequestService; + @InjectMocks private V2DeleteDocumentController v2DeleteDocumentController; @@ -136,6 +144,21 @@ public void test_checkDocumentDeletion_whenUserDoesNotHavePermissionToCheckDocum Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); } + @Test + public void test_checkDocumentDeletion_whenDeletionFailed() { + Mockito.when(documentService.getDocument(DOCUMENT_ID)).thenReturn(document); + Map msgs = new HashMap(); + msgs.put("progressInfo", "Deletion in progress. Please check back later."); + Mockito.when(responseHelper.generateResponse(Mockito.anyMap(), Mockito.any(HttpStatus.class))).thenReturn(generateResponse(msgs, HttpStatus.INTERNAL_SERVER_ERROR)); + IProcessingRequest procRequest = new ProcessingRequest(); + procRequest.setRequestStatus(RequestStatus.FAILED); + List procList = new ArrayList(); + procList.add(procRequest); + Mockito.when(processingRequestService.getProcRequestsByRequestId(Mockito.anyString())).thenReturn(procList); + ResponseEntity response = v2DeleteDocumentController.deleteDocument(DOCUMENT_ID, CITESPHERE_TOKEN); + Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + private Document createDocument() { Document document = new Document(); document.setId(DOCUMENT_ID); @@ -143,6 +166,7 @@ private Document createDocument() { document.setFileIds(Arrays.asList(FILE_ID)); document.setUploadId(UPLOAD_ID); document.setUploadedFileId(FILE_ID); + document.setRequestId("REQ123"); return document; } diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java index e8a9dbde..dbbdcdb8 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/config/impl/UserHelperTest.java @@ -1,7 +1,5 @@ package edu.asu.diging.gilesecosystem.web.config.impl; -import java.util.Arrays; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -13,13 +11,8 @@ import org.springframework.social.connect.ConnectionKey; import org.springframework.social.connect.UserProfile; -import edu.asu.diging.gilesecosystem.web.config.CitesphereToken; import edu.asu.diging.gilesecosystem.web.config.IUserHelper; -import edu.asu.diging.gilesecosystem.web.core.model.DocumentAccess; -import edu.asu.diging.gilesecosystem.web.core.model.IDocument; -import edu.asu.diging.gilesecosystem.web.core.model.impl.Document; import edu.asu.diging.gilesecosystem.web.core.users.AccountStatus; -import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser; import edu.asu.diging.gilesecosystem.web.core.users.IUserManager; import edu.asu.diging.gilesecosystem.web.core.users.User; @@ -32,11 +25,6 @@ public class UserHelperTest { @InjectMocks private IUserHelper helperToTest; private String USER_ID = "user_id"; - private String DOCUMENT_ID = "documentId"; - private DocumentAccess access = DocumentAccess.PRIVATE; - private String FILE_ID = "fileId"; - private String UPLOAD_ID = "uploadId"; - private CitesphereToken citesphereToken = new CitesphereToken("71b9cc36-939d-4e28-89e9-e5bfca1b26c3"); @Before public void setUp() { @@ -107,36 +95,4 @@ public void test_createUser_userExists() { Assert.assertEquals(PROVIDER_USER_ID, createdUser.getUserIdOfProvider()); Assert.assertEquals(AccountStatus.ADDED, createdUser.getAccountStatus()); } - - @Test - public void test_isUserPermittedToAccessDocument_whenUserIsAllowedToAccessDocument() { - IDocument document = createDocument(); - citesphereToken.setPrincipal(createCitesphereUser("dab")); - Assert.assertTrue(helperToTest.isUserPermittedToAccessDocument(document, citesphereToken)); - } - - @Test - public void test_isUserPermittedToAccessDocument_whenUserIsNotAllowedToAccessDocument() { - IDocument document = createDocument(); - citesphereToken.setPrincipal(createCitesphereUser("test1")); - Assert.assertFalse(helperToTest.isUserPermittedToAccessDocument(document, citesphereToken)); - } - - private Document createDocument() { - Document document = new Document(); - document.setId(DOCUMENT_ID); - document.setAccess(access); - document.setFileIds(Arrays.asList(FILE_ID)); - document.setUploadId(UPLOAD_ID); - document.setUploadedFileId(FILE_ID); - document.setUsername("dab_citesphere"); - return document; - } - - private CitesphereUser createCitesphereUser(String userName) { - CitesphereUser user = new CitesphereUser(); - user.setAuthorizingClient("citesphere"); - user.setUsername(userName); - return user; - } } From 8b50bf9a4d29ba7b588ad2eaf3c126a938f91aae Mon Sep 17 00:00:00 2001 From: diya17 Date: Mon, 7 Aug 2023 16:24:54 -0400 Subject: [PATCH 17/24] [GECO-140] Modifying request client to use spring data and modifying tests for service --- .../core/files/impl/RequestDatabaseClient.java | 16 ++-------------- .../delete/impl/DeleteDocumentServiceTest.java | 4 ++++ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java index d897237c..89d51056 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/files/impl/RequestDatabaseClient.java @@ -1,30 +1,18 @@ package edu.asu.diging.gilesecosystem.web.core.files.impl; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; -import edu.asu.diging.gilesecosystem.util.store.objectdb.DatabaseClient; import edu.asu.diging.gilesecosystem.web.core.files.IRequestDatabaseClient; import edu.asu.diging.gilesecosystem.web.core.repository.RequestRepository; @Transactional @Component -public class RequestDatabaseClient extends DatabaseClient implements IRequestDatabaseClient { - - @PersistenceContext(unitName="entityManagerFactory") - private EntityManager em; +public class RequestDatabaseClient implements IRequestDatabaseClient { @Autowired - private final RequestRepository requestRepository; - - @Autowired - public RequestDatabaseClient(RequestRepository requestRepository) { - this.requestRepository = requestRepository; - } + private RequestRepository requestRepository; public void deleteRequestsByDocumentId(String documentId) { requestRepository.deleteByDocumentId(documentId); diff --git a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/service/delete/impl/DeleteDocumentServiceTest.java b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/service/delete/impl/DeleteDocumentServiceTest.java index 7e236758..cb2c8652 100644 --- a/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/service/delete/impl/DeleteDocumentServiceTest.java +++ b/giles-eco/src/test/java/edu/asu/diging/gilesecosystem/web/service/delete/impl/DeleteDocumentServiceTest.java @@ -33,6 +33,7 @@ import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalFileService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalProcessingRequestService; +import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalRequestService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalUploadService; import edu.asu.diging.gilesecosystem.web.core.service.delete.IDeleteDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.delete.impl.DeleteDocumentService; @@ -69,6 +70,9 @@ public class DeleteDocumentServiceTest { @Mock private ITransactionalProcessingRequestService processingRequestService; + @Mock + private ITransactionalRequestService requestService; + @InjectMocks private IDeleteDocumentService factoryToTest; From 984f9f5948bc4b493a98ed0e23dbb4de336d31e0 Mon Sep 17 00:00:00 2001 From: Julia Damerow Date: Fri, 29 Mar 2024 16:08:15 -0400 Subject: [PATCH 18/24] Fix path to jsp --- .../gilesecosystem/web/web/admin/RegisteredAppController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java index 3b8d4788..17914b8d 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java @@ -36,6 +36,6 @@ public String showApp(Model model, @PathVariable String id) { model.addAttribute("providerName", providerRegistry.getProviderName(app.getProviderId())); model.addAttribute("app", app); - return "admin/apps/app"; + return "admin/app"; } } From da5e90c9338ca07b974b2d820a16b53c84c3a11a Mon Sep 17 00:00:00 2001 From: Julia Damerow Date: Fri, 29 Mar 2024 17:07:07 -0400 Subject: [PATCH 19/24] undo change --- .../gilesecosystem/web/web/admin/RegisteredAppController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java index 17914b8d..3b8d4788 100755 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/web/admin/RegisteredAppController.java @@ -36,6 +36,6 @@ public String showApp(Model model, @PathVariable String id) { model.addAttribute("providerName", providerRegistry.getProviderName(app.getProviderId())); model.addAttribute("app", app); - return "admin/app"; + return "admin/apps/app"; } } From 540321427a90422c1827e9563bb26e8bd670e257 Mon Sep 17 00:00:00 2001 From: diya17 Date: Wed, 10 Apr 2024 14:15:03 -0700 Subject: [PATCH 20/24] Merge branch 'story/GECO-139' into story/GECO-140 --- .../delete/impl/DeleteDocumentService.java | 47 +- .../WEB-INF/views/uploads/showDocument.jsp | 44 +- .../main/webapp/resources/notify/animate.css | 3623 +++++++++++++++++ .../resources/notify/bootstrap-notify.min.js | 2 + 4 files changed, 3659 insertions(+), 57 deletions(-) create mode 100644 giles-eco/src/main/webapp/resources/notify/animate.css create mode 100644 giles-eco/src/main/webapp/resources/notify/bootstrap-notify.min.js diff --git a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java index 93f5e1a0..92513f87 100644 --- a/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java +++ b/giles-eco/src/main/java/edu/asu/diging/gilesecosystem/web/core/service/delete/impl/DeleteDocumentService.java @@ -1,11 +1,5 @@ package edu.asu.diging.gilesecosystem.web.core.service.delete.impl; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; @@ -13,7 +7,6 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import edu.asu.diging.gilesecosystem.requests.FileType; import edu.asu.diging.gilesecosystem.requests.IRequest; import edu.asu.diging.gilesecosystem.requests.IRequestFactory; import edu.asu.diging.gilesecosystem.requests.IStorageDeletionRequest; @@ -26,13 +19,7 @@ import edu.asu.diging.gilesecosystem.util.exceptions.UnstorableObjectException; import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager; import edu.asu.diging.gilesecosystem.web.core.exceptions.GilesProcessingException; -import edu.asu.diging.gilesecosystem.web.core.files.IDocumentDatabaseClient; -import edu.asu.diging.gilesecosystem.web.core.files.IFilesManager; import edu.asu.diging.gilesecosystem.web.core.model.IDocument; -import edu.asu.diging.gilesecosystem.web.core.model.IFile; -import edu.asu.diging.gilesecosystem.web.core.model.IProcessingRequest; -import edu.asu.diging.gilesecosystem.web.core.model.impl.ProcessingRequest; -import edu.asu.diging.gilesecosystem.web.core.service.IFileTypeHandler; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalFileService; import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalProcessingRequestService; @@ -76,27 +63,9 @@ public class DeleteDocumentService implements IDeleteDocumentService { @Autowired private ITransactionalProcessingRequestService processingRequestService; - - @Autowired - private ITransactionalRequestService requestService; - - private Map fileTypes; - + @PostConstruct public void init() { - fileTypes = new HashMap(); - - // register what content types should be treated as what file type - Map ctxMap = ctx.getBeansOfType(IFileTypeHandler.class); - Iterator> iter = ctxMap.entrySet().iterator(); - - while(iter.hasNext()){ - Entry handlerEntry = iter.next(); - IFileTypeHandler handler = (IFileTypeHandler) handlerEntry.getValue(); - for (String type : handler.getHandledFileTypes()) { - fileTypes.put(type, handler.getHandledFileType()); - } - } requestFactory.config(StorageDeletionRequest.class); } @@ -104,25 +73,17 @@ public void init() { @Async public void initiateDeletion(IDocument document) throws GilesProcessingException, MessageCreationException { IRequest storageDeletionRequest = createRequest(document); - requestProducer.sendRequest(storageDeletionRequest, getTopic()); + requestProducer.sendRequest(storageDeletionRequest, propertyManager.getProperty(Properties.KAFKA_TOPIC_STORAGE_DELETION_REQUEST)); } @Override public void completeDeletion(IDocument document) { - deleteFiles(document); - deleteProcessingRequests(document); + fileService.deleteFiles(document.getId()); + processingRequestService.deleteRequestsByDocumentId(document.getId()); documentService.deleteDocument(document.getId()); deleteUpload(document.getUploadId()); } - private void deleteProcessingRequests(IDocument document) { - processingRequestService.deleteRequestsByDocumentId(document.getId()); - } - - private void deleteFiles(IDocument document) { - fileService.deleteFiles(document.getId()); - } - private void deleteUpload(String uploadId) { // if an upload has multiple documents and only one of the documents is deleted the upload does not have to be deleted. if(documentService.getDocumentsByUploadId(uploadId).isEmpty()) { diff --git a/giles-eco/src/main/webapp/WEB-INF/views/uploads/showDocument.jsp b/giles-eco/src/main/webapp/WEB-INF/views/uploads/showDocument.jsp index ae9d0442..6f43ba3c 100755 --- a/giles-eco/src/main/webapp/WEB-INF/views/uploads/showDocument.jsp +++ b/giles-eco/src/main/webapp/WEB-INF/views/uploads/showDocument.jsp @@ -1,5 +1,6 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> +