Search in sources :

Example 1 with EmptyGroupRepository

use of org.ligoj.app.iam.empty.EmptyGroupRepository 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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Parameter(org.ligoj.app.model.Parameter) Arrays(java.util.Arrays) EmptyIamProvider(org.ligoj.app.iam.empty.EmptyIamProvider) Autowired(org.springframework.beans.factory.annotation.Autowired) Function(java.util.function.Function) CacheUser(org.ligoj.app.iam.model.CacheUser) HashSet(java.util.HashSet) GroupOrg(org.ligoj.app.iam.GroupOrg) Map(java.util.Map) CacheCompany(org.ligoj.app.iam.model.CacheCompany) Subscription(org.ligoj.app.model.Subscription) Normalizer(org.ligoj.app.api.Normalizer) ParameterValue(org.ligoj.app.model.ParameterValue) CacheMembership(org.ligoj.app.iam.model.CacheMembership) Node(org.ligoj.app.model.Node) AbstractAppTest(org.ligoj.app.AbstractAppTest) CacheCompanyRepository(org.ligoj.app.iam.dao.CacheCompanyRepository) IOException(java.io.IOException) CacheProjectGroup(org.ligoj.app.model.CacheProjectGroup) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) DelegateOrg(org.ligoj.app.iam.model.DelegateOrg) IamConfiguration(org.ligoj.app.iam.IamConfiguration) EmptyUserRepository(org.ligoj.app.iam.empty.EmptyUserRepository) EmptyCompanyRepository(org.ligoj.app.iam.empty.EmptyCompanyRepository) EmptyGroupRepository(org.ligoj.app.iam.empty.EmptyGroupRepository) CompanyOrg(org.ligoj.app.iam.CompanyOrg) CacheGroup(org.ligoj.app.iam.model.CacheGroup) CacheContainer(org.ligoj.app.iam.model.CacheContainer) CacheGroupRepository(org.ligoj.app.iam.dao.CacheGroupRepository) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) UserOrg(org.ligoj.app.iam.UserOrg) Project(org.ligoj.app.model.Project) CacheGroup(org.ligoj.app.iam.model.CacheGroup) UserOrg(org.ligoj.app.iam.UserOrg) CacheCompany(org.ligoj.app.iam.model.CacheCompany) CompanyOrg(org.ligoj.app.iam.CompanyOrg) EmptyUserRepository(org.ligoj.app.iam.empty.EmptyUserRepository) EmptyCompanyRepository(org.ligoj.app.iam.empty.EmptyCompanyRepository) GroupOrg(org.ligoj.app.iam.GroupOrg) CacheMembership(org.ligoj.app.iam.model.CacheMembership) EmptyGroupRepository(org.ligoj.app.iam.empty.EmptyGroupRepository) IamConfiguration(org.ligoj.app.iam.IamConfiguration) Map(java.util.Map) CacheUser(org.ligoj.app.iam.model.CacheUser) EmptyIamProvider(org.ligoj.app.iam.empty.EmptyIamProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with EmptyGroupRepository

use of org.ligoj.app.iam.empty.EmptyGroupRepository in project ligoj-api by ligoj.

the class EmptyGroupRepositoryTest method create.

@Test
public void create() {
    final GroupOrg groupLdap = new EmptyGroupRepository().create("Cn=Some", "Name");
    Assertions.assertEquals("Cn=Some", groupLdap.getDn());
    Assertions.assertEquals("Name", groupLdap.getName());
    Assertions.assertEquals("name", groupLdap.getId());
}
Also used : EmptyGroupRepository(org.ligoj.app.iam.empty.EmptyGroupRepository) GroupOrg(org.ligoj.app.iam.GroupOrg) Test(org.junit.jupiter.api.Test)

Aggregations

GroupOrg (org.ligoj.app.iam.GroupOrg)2 EmptyGroupRepository (org.ligoj.app.iam.empty.EmptyGroupRepository)2 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Assertions (org.junit.jupiter.api.Assertions)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 AbstractAppTest (org.ligoj.app.AbstractAppTest)1 Normalizer (org.ligoj.app.api.Normalizer)1 CompanyOrg (org.ligoj.app.iam.CompanyOrg)1 IamConfiguration (org.ligoj.app.iam.IamConfiguration)1 UserOrg (org.ligoj.app.iam.UserOrg)1 CacheCompanyRepository (org.ligoj.app.iam.dao.CacheCompanyRepository)1 CacheGroupRepository (org.ligoj.app.iam.dao.CacheGroupRepository)1