### Question:
FactHelper { public static FactRecord withAcl(FactRecord fact, UUID currentUserId, List<UUID> acl) { if (fact == null) return null; if (fact.getAccessMode() == FactRecord.AccessMode.Public) return fact; Set<UUID> existingAcl = set(fact.getAcl(), FactAclEntryRecord::getSubjectID); Set<UUID> subjectsToAdd = set(acl) .stream() .filter(subject -> !existingAcl.contains(subject)) .collect(Collectors.toSet()); if (fact.getAccessMode() == FactRecord.AccessMode.Explicit && !existingAcl.contains(currentUserId)) { subjectsToAdd.add(currentUserId); } for (UUID subject : subjectsToAdd) { fact.addAclEntry(new FactAclEntryRecord() .setId(UUID.randomUUID()) .setOriginID(fact.getOriginID()) .setSubjectID(subject) .setTimestamp(System.currentTimeMillis()) ); } return fact; }  static FactRecord withComment(FactRecord fact, String comment); static FactRecord withAcl(FactRecord fact, UUID currentUserId, List<UUID> acl);  }### Answer:
@Test public void testWithAclNullFact() { assertNull(withAcl(null, UUID.randomUUID(), list(UUID.randomUUID()))); }
@Test public void testWithAclSkipsEmptyAcl() { assertTrue(CollectionUtils.isEmpty(withAcl(new FactRecord(), UUID.randomUUID(), null).getAcl())); assertTrue(CollectionUtils.isEmpty(withAcl(new FactRecord(), UUID.randomUUID(), list()).getAcl())); }
@Test public void testWithAclAddsEntryToEmptyAcl() { UUID subjectID = UUID.randomUUID(); FactRecord fact = new FactRecord().setOriginID(UUID.randomUUID()); fact = withAcl(fact, UUID.randomUUID(), list(subjectID)); assertEquals(1, fact.getAcl().size()); assertNotNull(fact.getAcl().get(0).getId()); assertEquals(fact.getOriginID(), fact.getAcl().get(0).getOriginID()); assertEquals(subjectID, fact.getAcl().get(0).getSubjectID()); assertTrue(fact.getAcl().get(0).getTimestamp() > 0); }
@Test public void testWithAclAddsEntryToExistingAcl() { UUID subjectID = UUID.randomUUID(); FactRecord fact = new FactRecord().addAclEntry(new FactAclEntryRecord().setSubjectID(UUID.randomUUID())); fact = withAcl(fact, UUID.randomUUID(), list(subjectID)); assertEquals(2, fact.getAcl().size()); }
@Test public void testWithAclSkipsExistingEntry() { UUID subjectID = UUID.randomUUID(); FactRecord fact = new FactRecord().addAclEntry(new FactAclEntryRecord().setSubjectID(subjectID)); fact = withAcl(fact, UUID.randomUUID(), list(subjectID)); assertEquals(1, fact.getAcl().size()); }
@Test public void testWithAclAccessModeExplicitAddsCurrentUser() { UUID currentUser = UUID.randomUUID(); FactRecord fact = new FactRecord().setAccessMode(FactRecord.AccessMode.Explicit); fact = withAcl(fact, currentUser, null); assertEquals(1, fact.getAcl().size()); assertEquals(currentUser, fact.getAcl().get(0).getSubjectID()); }
@Test public void testWithAclAccessModeExplicitSkipsExistingCurrentUser() { UUID currentUser = UUID.randomUUID(); FactRecord fact = new FactRecord() .setAccessMode(FactRecord.AccessMode.Explicit) .addAclEntry(new FactAclEntryRecord().setSubjectID(currentUser)); fact = withAcl(fact, currentUser, null); assertEquals(1, fact.getAcl().size()); assertEquals(currentUser, fact.getAcl().get(0).getSubjectID()); }