Skip to content

Commit

Permalink
Merge pull request #1861 from UNC-Libraries/bxc-4803
Browse files Browse the repository at this point in the history
BXC-4803 - Return an unchanged status if the mods was unchanged during an update
  • Loading branch information
sharonluong authored Jan 14, 2025
2 parents 8ff8357 + f9bb6c6 commit 67c570d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.servlet.http.HttpServletRequest;

import edu.unc.lib.boxc.operations.api.exceptions.StateUnmodifiedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -54,6 +55,10 @@ private ResponseEntity<Object> update(String id, HttpServletRequest request) {
AgentPrincipals agent = AgentPrincipalsImpl.createFromThread();
try (InputStream modsStream = request.getInputStream()) {
updateService.updateDescription(new UpdateDescriptionRequest(agent, pid, modsStream));
result.put("status", "updated");
} catch (StateUnmodifiedException e) {
log.info("No changes were made to {}", pid.getRepositoryPath());
result.put("status", "unchanged");
} catch (MetadataValidationException e) {
if (e.getMessage() != null) {
result.put("error", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import static edu.unc.lib.boxc.auth.api.Permission.editDescription;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand Down Expand Up @@ -82,6 +84,47 @@ public void testUpdateDescription() throws Exception {
Map<String, Object> respMap = MvcTestHelpers.getMapFromResponse(result);
assertEquals(objPid.getUUID(), respMap.get("pid"));
assertEquals("updateDescription", respMap.get("action"));
assertEquals("updated", respMap.get("status"));
}

@Test
public void testUpdateDescriptionNoChange() throws Exception {
doNothing().when(aclService)
.assertHasAccess(anyString(), any(PID.class), any(AccessGroupSetImpl.class), eq(editDescription));

File file = new File("src/test/resources/mods/valid-mods.xml");
var content = IOUtils.toByteArray(new FileInputStream(file));
PID objPid = makeWorkObject();

assertDescriptionNotUpdated(objPid);

mvc.perform(post("/edit/description/" + objPid.getUUID())
.content(content))
.andExpect(status().is2xxSuccessful())
.andReturn();

ContentObject obj1 = (ContentObject) repositoryObjectLoader.getRepositoryObject(objPid);
var modifiedAfterFirst = obj1.getDescription().getLastModified();

// Repeat the update, so there should be no changes the second time
MvcResult result2 = mvc.perform(post("/edit/description/" + objPid.getUUID())
.content(content))
.andExpect(status().is2xxSuccessful())
.andReturn();

// Invalid cache to make sure we get a fresh copy
repositoryObjectLoader.invalidate(objPid);
repositoryObjectLoader.invalidate(obj1.getDescription().getPid());

ContentObject obj2 = (ContentObject) repositoryObjectLoader.getRepositoryObject(objPid);
var modifiedAfterSecond = obj2.getDescription().getLastModified();

// Verify response from api
Map<String, Object> respMap = MvcTestHelpers.getMapFromResponse(result2);
assertEquals(objPid.getUUID(), respMap.get("pid"));
assertEquals("updateDescription", respMap.get("action"));
assertEquals("unchanged", respMap.get("status"));
assertEquals(modifiedAfterFirst, modifiedAfterSecond, "Description should not have been updated");
}

@Test
Expand Down

0 comments on commit 67c570d

Please sign in to comment.