use of org.ligoj.app.iam.model.CacheContainer in project ligoj-api by ligoj.
the class AbstractOrgTest method setUpEntities.
@BeforeEach
public void setUpEntities() throws IOException {
// Prepare the standard data
persistEntities("csv", new Class[] { DelegateOrg.class }, StandardCharsets.UTF_8.name());
persistEntities("csv", new Class[] { Node.class, Parameter.class, Project.class, Subscription.class, ParameterValue.class }, StandardCharsets.UTF_8.name());
// Add the IAM data
csvForJpa.cleanup(CacheCompany.class, CacheUser.class, CacheGroup.class, CacheMembership.class, CacheProjectGroup.class);
final Map<String, CompanyOrg> companies = csvForJpa.insert("csv", CacheCompany.class, StandardCharsets.UTF_8.name()).stream().map(c -> new CompanyOrg(c.getDescription(), c.getName())).collect(Collectors.toMap(CompanyOrg::getId, Function.identity()));
final Map<String, UserOrg> users = csvForJpa.insert("csv", CacheUser.class, StandardCharsets.UTF_8.name()).stream().map(c -> {
final UserOrg user = new UserOrg();
user.setId(c.getId());
user.setDn("uid=" + c.getId() + "," + companies.get(c.getCompany().getId()).getDn());
user.setCompany(c.getCompany().getId());
user.setFirstName(c.getFirstName());
user.setLastName(c.getLastName());
user.setMails(Arrays.asList(Optional.ofNullable(c.getMails()).orElse("").split(",")));
return user;
}).collect(Collectors.toMap(UserOrg::getId, Function.identity()));
final Map<String, GroupOrg> groups = csvForJpa.insert("csv", CacheGroup.class, StandardCharsets.UTF_8.name()).stream().map(c -> new GroupOrg(c.getDescription(), c.getName(), new HashSet<>())).collect(Collectors.toMap(GroupOrg::getId, Function.identity()));
CacheMembership cacheMembership = csvForJpa.insert("csv", CacheMembership.class, StandardCharsets.UTF_8.name()).get(0);
csvForJpa.insert("csv", CacheProjectGroup.class, StandardCharsets.UTF_8.name());
// Coverage required here only there because of JPA bean
Assertions.assertNotNull(cacheMembership.getGroup());
Assertions.assertNotNull(cacheMembership.getUser());
Assertions.assertNull(cacheMembership.getSubGroup());
cacheMembership.setSubGroup(null);
// Plug-in the IAMProvider to the database
final IamConfiguration configuration = new IamConfiguration();
final EmptyUserRepository userRepository = new EmptyUserRepository() {
@Override
public Map<String, UserOrg> findAll() {
return users;
}
@Override
public UserOrg findById(final String login) {
return findAll().get(login);
}
@Override
public UserOrg findOneBy(final String attribute, final String value) {
return findAllBy(attribute, value).stream().findFirst().orElse(null);
}
};
configuration.setUserRepository(userRepository);
configuration.setCompanyRepository(new EmptyCompanyRepository() {
@Override
public Map<String, CompanyOrg> findAll() {
return companies;
}
@Override
public CompanyOrg findById(final String user, final String id) {
// Check the container exists and return the in memory object.
return Optional.ofNullable(cacheCompanyRepository.findById(user, Normalizer.normalize(id))).map(CacheContainer::getId).map(this::findById).orElse(null);
}
});
configuration.setGroupRepository(new EmptyGroupRepository() {
@Override
public Map<String, GroupOrg> findAll() {
return groups;
}
@Override
public GroupOrg findById(final String user, final String id) {
// Check the container exists and return the in memory object.
return Optional.ofNullable(cacheGroupRepository.findById(user, Normalizer.normalize(id))).map(CacheContainer::getId).map(this::findById).orElse(null);
}
});
userRepository.setCompanyRepository(configuration.getCompanyRepository());
iamProvider = new EmptyIamProvider() {
@Override
public IamConfiguration getConfiguration() {
return configuration;
}
};
em.flush();
em.clear();
}
Aggregations