ScopedFileAccessService implements FileAccessService { @Override public Iterable<Result<AccessControlEntry, Boolean>> setAccess(@NotNull final AccessControlList accessControlList) { final List<AccessControlEntry> newAcl = new ArrayList<AccessControlEntry>(); for (AccessControlEntry ace : accessControlList) { switch (ace.getScope()) { case Global: if (!myGlobalFileAccessCache.tryAddEntry(ace)) { LOG.info("Skipping setting an access for " + ace + ", because it has being done previously"); continue; } break; case Build: if (!myBuildFileAccessCache.tryAddEntry(ace)) { LOG.info("Skipping setting an access for " + ace + ", because it has being done on the previous step"); continue; } break; } newAcl.add(ace); } if(newAcl.size() == 0) { return Collections.emptyList(); } myLoggerService.onMessage(new Message(MESSAGE_GRANTING_PERMISSIONS, "NORMAL", null)); List<Result<AccessControlEntry, Boolean>> results = new ArrayList<Result<AccessControlEntry, Boolean>>(); boolean hasError = false; for (Result<AccessControlEntry, Boolean> result: myFileAccessService.setAccess(new AccessControlList(newAcl))) { results.add(result); hasError |= !result.isSuccessful() || (result.getValue() != null && !result.getValue()); } if(hasError) { LOG.info(WARNING_PERMISSIONS_ERRORS); myLoggerService.onMessage(new Message(WARNING_PERMISSIONS_ERRORS, "WARNING", null)); } return results; } ScopedFileAccessService(
    @NotNull final FileAccessService fileAccessService,
    @NotNull final LoggerService loggerService,
    @NotNull final FileAccessCache globalFileAccessCache,
    @NotNull final FileAccessCache buildFileAccessCache); @Override Iterable<Result<AccessControlEntry, Boolean>> setAccess(@NotNull final AccessControlList accessControlList);  }
@Test(dataProvider = "getSetPermissionsCases") public void shouldSetPermissions( @NotNull final Iterable<AccessControlList> accessControlLists, @NotNull final Iterable<AccessControlList> expectedAccessControlLists, final int expectedGlobalCacheSize, final int expectedBuildCacheSize) throws ExecutionException { final List<AccessControlList> actualAccessControlLists = new ArrayList<AccessControlList>(); final FileAccessService instance = createInstance(); myCtx.checking(new Expectations() {{ allowing(myLoggerService).onMessage(with(any(ServiceMessage.class))); allowing(myFileAccessService).setAccess(with(any(AccessControlList.class))); will(new CustomAction("setAccess") { @Override public Object invoke(final Invocation invocation) throws Throwable { actualAccessControlLists.add((AccessControlList)invocation.getParameter(0)); return Lists.emptyList(); } }); }}); for (AccessControlList acl: accessControlLists) { instance.setAccess(acl); } myCtx.assertIsSatisfied(); then(actualAccessControlLists).isEqualTo(expectedAccessControlLists); then(myGlobalFileAccessCache.size()).isEqualTo(expectedGlobalCacheSize); then(myBuildFileAccessCache.size()).isEqualTo(expectedBuildCacheSize); }
@Test public void shouldShowGrantingPermMessage() throws ExecutionException { final FileAccessService instance = createInstance(); myCtx.checking(new Expectations() {{ oneOf(myLoggerService).onMessage(with(new BaseMatcher<Message>() { @Override public boolean matches(final Object o) { return "NORMAL".equals(((Message)o).getStatus()) && ScopedFileAccessService.MESSAGE_GRANTING_PERMISSIONS.equals(((Message)o).getText()); } @Override public void describeTo(final Description description) { } })); allowing(myFileAccessService).setAccess(with(any(AccessControlList.class))); will(returnValue(Lists.emptyList())); }}); instance.setAccess(new AccessControlList(Arrays.asList(createAce("my_file", AccessControlScope.Build)))); myCtx.assertIsSatisfied(); }
@Test(dataProvider = "warnCases") public void shouldShowWarningMessage( @NotNull final List<Result<AccessControlEntry, Boolean>> results) throws ExecutionException { final FileAccessService instance = createInstance(); myCtx.checking(new Expectations() {{ one(myLoggerService).onMessage(with(any(Message.class))); oneOf(myLoggerService).onMessage(with(new BaseMatcher<Message>() { @Override public boolean matches(final Object o) { return "WARNING".equals(((Message)o).getStatus()) && ScopedFileAccessService.WARNING_PERMISSIONS_ERRORS.equals(((Message)o).getText()); } @Override public void describeTo(final Description description) { } })); allowing(myFileAccessService).setAccess(with(any(AccessControlList.class))); will(returnValue(results)); }}); instance.setAccess(new AccessControlList(Arrays.asList(createAce("my_file", AccessControlScope.Build)))); myCtx.assertIsSatisfied(); }