@Test public void addAuthorizationIgnoresTheSecondAdditionForUserToAVre() throws Exception { String unknownUser = "unknownUser"; instance.getOrCreateAuthorization(VRE, unknownUser, UNVERIFIED_USER_ROLE); VreAuthorization authorization2 = instance.getOrCreateAuthorization(VRE, unknownUser, ADMIN_ROLE); assertThat(authorization2.getRoles(), contains(UNVERIFIED_USER_ROLE)); }
@Override public VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole) throws AuthorizationUnavailableException { Optional<VreAuthorization> authOptional = getAuthorization(vreId, userId); if (authOptional.isPresent()) { return authOptional.get(); } else { try { synchronized (authorizationsFolder) { File file = getFileOfVre(vreId); List<VreAuthorization> authorizations = Lists.newArrayList(); if (file.exists()) { authorizations = objectMapper.readValue(file, new TypeReference<List<VreAuthorization>>() { }); } VreAuthorization vreAuthorization = VreAuthorization.create(vreId, userId, userRole); authorizations.add(vreAuthorization); objectMapper.writeValue(file, authorizations.toArray(new VreAuthorization[authorizations.size()])); return vreAuthorization; } } catch (IOException e) { LOG.error("Error creating VRE Authorization", e); throw new AuthorizationUnavailableException(e.getMessage()); } } }
LocalFileVreAuthorizationAccess implements VreAuthorizationAccess { @Override public VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole) throws AuthorizationUnavailableException { Optional<VreAuthorization> authOptional = getAuthorization(vreId, userId); if (authOptional.isPresent()) { return authOptional.get(); } else { try { synchronized (authorizationsFolder) { File file = getFileOfVre(vreId); List<VreAuthorization> authorizations = Lists.newArrayList(); if (file.exists()) { authorizations = objectMapper.readValue(file, new TypeReference<List<VreAuthorization>>() { }); } VreAuthorization vreAuthorization = VreAuthorization.create(vreId, userId, userRole); authorizations.add(vreAuthorization); objectMapper.writeValue(file, authorizations.toArray(new VreAuthorization[authorizations.size()])); return vreAuthorization; } } catch (IOException e) { LOG.error("Error creating VRE Authorization", e); throw new AuthorizationUnavailableException(e.getMessage()); } } } }
LocalFileVreAuthorizationAccess implements VreAuthorizationAccess { @Override public VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole) throws AuthorizationUnavailableException { Optional<VreAuthorization> authOptional = getAuthorization(vreId, userId); if (authOptional.isPresent()) { return authOptional.get(); } else { try { synchronized (authorizationsFolder) { File file = getFileOfVre(vreId); List<VreAuthorization> authorizations = Lists.newArrayList(); if (file.exists()) { authorizations = objectMapper.readValue(file, new TypeReference<List<VreAuthorization>>() { }); } VreAuthorization vreAuthorization = VreAuthorization.create(vreId, userId, userRole); authorizations.add(vreAuthorization); objectMapper.writeValue(file, authorizations.toArray(new VreAuthorization[authorizations.size()])); return vreAuthorization; } } catch (IOException e) { LOG.error("Error creating VRE Authorization", e); throw new AuthorizationUnavailableException(e.getMessage()); } } } LocalFileVreAuthorizationAccess(Path authorizationsFolder); }
LocalFileVreAuthorizationAccess implements VreAuthorizationAccess { @Override public VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole) throws AuthorizationUnavailableException { Optional<VreAuthorization> authOptional = getAuthorization(vreId, userId); if (authOptional.isPresent()) { return authOptional.get(); } else { try { synchronized (authorizationsFolder) { File file = getFileOfVre(vreId); List<VreAuthorization> authorizations = Lists.newArrayList(); if (file.exists()) { authorizations = objectMapper.readValue(file, new TypeReference<List<VreAuthorization>>() { }); } VreAuthorization vreAuthorization = VreAuthorization.create(vreId, userId, userRole); authorizations.add(vreAuthorization); objectMapper.writeValue(file, authorizations.toArray(new VreAuthorization[authorizations.size()])); return vreAuthorization; } } catch (IOException e) { LOG.error("Error creating VRE Authorization", e); throw new AuthorizationUnavailableException(e.getMessage()); } } } 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); }
LocalFileVreAuthorizationAccess implements VreAuthorizationAccess { @Override public VreAuthorization getOrCreateAuthorization(String vreId, String userId, String userRole) throws AuthorizationUnavailableException { Optional<VreAuthorization> authOptional = getAuthorization(vreId, userId); if (authOptional.isPresent()) { return authOptional.get(); } else { try { synchronized (authorizationsFolder) { File file = getFileOfVre(vreId); List<VreAuthorization> authorizations = Lists.newArrayList(); if (file.exists()) { authorizations = objectMapper.readValue(file, new TypeReference<List<VreAuthorization>>() { }); } VreAuthorization vreAuthorization = VreAuthorization.create(vreId, userId, userRole); authorizations.add(vreAuthorization); objectMapper.writeValue(file, authorizations.toArray(new VreAuthorization[authorizations.size()])); return vreAuthorization; } } catch (IOException e) { LOG.error("Error creating VRE Authorization", e); throw new AuthorizationUnavailableException(e.getMessage()); } } } 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; }