### Question:
LocalFileVreAuthorizationAccess implements VreAuthorizationAccess { @Override public Optional<VreAuthorization> getAuthorization(String vreId, String userId) throws AuthorizationUnavailableException { Optional<VreAuthorization> authorizationValue = Optional.empty(); File file = getFileOfVre(vreId); authorizationValue = getAuthorization(userId, authorizationValue, file); if (!authorizationValue.isPresent()) { file = authorizationsFolder.resolve("authorizations.json").toFile(); authorizationValue = getAuthorization(userId, authorizationValue, file); } return authorizationValue; } LocalFileVreAuthorizationAccess(Path authorizationsFolder); @Override VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole); @Override Optional<VreAuthorization> getAuthorization(String vreId, String userId); @Override void deleteVreAuthorizations(String vreId); static final Logger LOG; }### Answer:
@Test public void authorizationForReturnsAnOptionalOfTheFoundVreAuthorization() throws Exception { Optional<VreAuthorization> vreAuthorization = instance.getAuthorization(VRE, USER_ID); assertThat(vreAuthorization, is(present())); }
@Test public void authorizationForReturnsAnEmptyOptionalIfTheVreAuthorizationIsNotFound() throws Exception { Optional<VreAuthorization> vreAuthorization = instance.getAuthorization(VRE, "unknownUser"); assertThat(vreAuthorization, is(not(present()))); }
@Test public void authorizationForReturnsAnEmptyOptionalIfTheVreAuthorizationsFileDoesNotExist() throws Exception { Optional<VreAuthorization> vreAuthorization = instance.getAuthorization("nonExisting", USER_ID); assertThat(vreAuthorization, is(not(present()))); }
@Test public void getAuthorizationCheckesTheGeneralAccessOfTheUserWhenHeHasNoAccessOnTheDataSet() throws Exception { File file = new File(authorizationsFolder.toFile(), "authorizations.json"); file.createNewFile(); String testAuthInfo = "[{\"vreId\":\"test_vre\",\"userId\":\"33707283d426f900d4d33707283d426f9testing\"," + "\"roles\":[\"ADMIN\"]}]\n"; Files.write(file.toPath(), testAuthInfo.getBytes()); String userId = "33707283d426f900d4d33707283d426f9testing"; Optional<VreAuthorization> authorization = instance.getAuthorization(VRE, userId); assertThat(authorization, is(present())); assertThat(authorization.get().hasAdminAccess(), is(true)); }
@Test public void getAuthorizationReturnsEmptyIfPermissionNotPresentInEitherFile() throws Exception { File file = new File(authorizationsFolder.toFile(), "authorizations.json"); file.createNewFile(); String testAuthInfo = "[{\"vreId\":\"test_vre\",\"userId\":\"33707283d426f900d4d33707283d4268testing2\"," + "\"roles\":[\"ADMIN\"]}]\n"; Files.write(file.toPath(), testAuthInfo.getBytes()); String userId = "33707283d426f900d4d33707283d426f9testing"; Optional<VreAuthorization> authorization = instance.getAuthorization(VRE, userId); assertThat(authorization, is(not(present()))); }