Skip to content

Commit

Permalink
Set the "reserved" timestamp when stealing a lock as well (#637)
Browse files Browse the repository at this point in the history
Co-authored-by: Bas van Erp <bas.van.erp@politie.nl>
  • Loading branch information
PayBas and Bas van Erp committed Mar 18, 2024
1 parent e2ac86a commit b05f8a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -939,6 +940,11 @@ public boolean steal(List<LockableResource> resources, String userName) {
r.setStolen();
}
unlock(resources, null, false);
// unlock() nulls resource.reservedTimestamp via resource.setBuild(null), so set it afterwards
Date date = new Date();
for (LockableResource r : resources) {
r.setReservedTimestamp(date);
}
save();
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -330,8 +332,10 @@ public void testDoSteal() throws IOException, ServletException {
// system must be permitted to create resource
resource = this.createResource("resource1");
// when the resource is not reserved, the doSteal action reserve it for you
assertNull(resource.getReservedTimestamp());
action.doSteal(req, rsp);
assertTrue("is reserved by system", resource.isReserved());
assertNotNull(resource.getReservedTimestamp());

// somebody
SecurityContextHolder.getContext().setAuthentication(this.reserve_user1.impersonate2());
Expand All @@ -344,15 +348,18 @@ public void testDoSteal() throws IOException, ServletException {
SecurityContextHolder.getContext().setAuthentication(this.admin.impersonate2());
action.doReset(req, rsp);
assertFalse("unreserved", resource.isReserved());
assertNull(resource.getReservedTimestamp());

// switch to user1 and reserve it
action.doReserve(req, rsp);
assertTrue("is reserved by user1", resource.isReserved());
assertNotNull(resource.getReservedTimestamp());

// switch to user with STEAL permission
SecurityContextHolder.getContext().setAuthentication(this.steal_user.impersonate2());
action.doSteal(req, rsp);
assertEquals("reserved by user", this.steal_user.getId(), resource.getReservedBy());
assertNotNull(resource.getReservedTimestamp());

// invalid params. Just check if it crash here
SecurityContextHolder.getContext().setAuthentication(this.admin.impersonate2());
Expand Down

0 comments on commit b05f8a4

Please sign in to comment.