Search in sources :

Example 1 with IamConfiguration

use of org.ligoj.app.iam.IamConfiguration 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 IamConfiguration

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

the class ToVoConverterTest method apply.

@Test
public void apply() {
    // Sub user repository
    final IamProvider iamProvider = Mockito.mock(IamProvider.class);
    final IUserRepository userRepository = Mockito.mock(IUserRepository.class);
    final IamConfiguration configuration = new IamConfiguration();
    configuration.setUserRepository(userRepository);
    Mockito.when(iamProvider.getConfiguration()).thenReturn(configuration);
    Mockito.when(userRepository.findById(ArgumentMatchers.anyString())).then(invocation -> toUser((String) invocation.getArguments()[0]));
    // Stub subscriptions
    final List<Object[]> subscriptions = new ArrayList<>();
    final Parameter parameter1 = new Parameter();
    parameter1.setId(IdentityResource.PARAMETER_GROUP);
    parameter1.setType(ParameterType.TEXT);
    parameter1.setOwner(new Node());
    final Parameter parameter2 = new Parameter();
    parameter2.setId(IdentityResource.PARAMETER_GROUP);
    parameter2.setType(ParameterType.TEXT);
    parameter2.setOwner(new Node());
    final Parameter parameter3 = new Parameter();
    parameter3.setId("any");
    parameter3.setType(ParameterType.TEXT);
    parameter3.setOwner(new Node());
    final ParameterValue value1 = new ParameterValue();
    value1.setId(1);
    value1.setParameter(parameter1);
    value1.setData("G");
    final ParameterValue value2 = new ParameterValue();
    value2.setId(2);
    value2.setParameter(parameter2);
    value2.setData("any");
    final ParameterValue value3 = new ParameterValue();
    value3.setId(3);
    value3.setParameter(parameter3);
    value3.setData("any");
    final Subscription subscription = new Subscription();
    subscription.setId(1);
    final Node node = new Node();
    node.setId("service:n2");
    subscription.setNode(node);
    subscriptions.add(new Object[] { subscription, value1 });
    subscriptions.add(new Object[] { subscription, value2 });
    subscriptions.add(new Object[] { subscription, value3 });
    // Subscription without status
    final Subscription subscription2 = new Subscription();
    subscription2.setId(-1);
    final Node node2 = new Node();
    node2.setId("service:n1");
    subscription2.setNode(node2);
    subscriptions.add(new Object[] { subscription2, value3 });
    // Stub events
    final Map<Integer, EventVo> events = new HashMap<>();
    final EventVo event = new EventVo();
    event.setSubscription(1);
    event.setNode(new NodeVo());
    event.setValue("UP");
    events.put(1, event);
    // Call
    final ToVoConverter converter = new ToVoConverter(this::toUser, subscriptions, events);
    final Project entity = new Project();
    entity.setId(1);
    entity.setName("N");
    entity.setDescription("D");
    entity.setLastModifiedBy("U1");
    entity.setLastModifiedDate(new Date());
    entity.setCreatedBy("U2");
    entity.setCreatedDate(new Date());
    entity.setPkey("PK");
    entity.setTeamLeader("U3");
    entity.setSubscriptions(Arrays.asList(new Subscription[] { subscription, subscription2 }));
    final ProjectVo vo = converter.apply(entity);
    // Check
    Assertions.assertEquals("N", vo.getName());
    Assertions.assertEquals("D", vo.getDescription());
    Assertions.assertEquals("PK", vo.getPkey());
    Assertions.assertEquals("U2", vo.getCreatedBy().getId());
    Assertions.assertNotNull(vo.getCreatedDate());
    Assertions.assertEquals("U1", vo.getLastModifiedBy().getId());
    Assertions.assertNotNull(vo.getLastModifiedDate());
    Assertions.assertEquals("U3", vo.getTeamLeader().getId());
    Assertions.assertEquals(1, vo.getId().intValue());
    Assertions.assertEquals(2, vo.getSubscriptions().size());
    // Check the statuses and their order by node
    final List<SubscriptionVo> subscriptionsVo = vo.getSubscriptions();
    Assertions.assertNull(subscriptionsVo.get(0).getStatus());
    Assertions.assertEquals("service:n1", subscriptionsVo.get(0).getNode().getId());
    Assertions.assertEquals(NodeStatus.UP, subscriptionsVo.get(1).getStatus());
    Assertions.assertEquals("service:n2", subscriptionsVo.get(1).getNode().getId());
}
Also used : ParameterValue(org.ligoj.app.model.ParameterValue) HashMap(java.util.HashMap) Node(org.ligoj.app.model.Node) ArrayList(java.util.ArrayList) IUserRepository(org.ligoj.app.iam.IUserRepository) IamProvider(org.ligoj.app.iam.IamProvider) Date(java.util.Date) Project(org.ligoj.app.model.Project) Parameter(org.ligoj.app.model.Parameter) IamConfiguration(org.ligoj.app.iam.IamConfiguration) Subscription(org.ligoj.app.model.Subscription) SubscriptionVo(org.ligoj.app.resource.subscription.SubscriptionVo) NodeVo(org.ligoj.app.api.NodeVo) EventVo(org.ligoj.app.resource.node.EventVo) Test(org.junit.jupiter.api.Test)

Example 3 with IamConfiguration

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

the class EmptyIamProvider method refreshConfiguration.

private IamConfiguration refreshConfiguration() {
    final IamConfiguration configuration = new IamConfiguration();
    final EmptyCompanyRepository companyRepository = new EmptyCompanyRepository();
    configuration.setCompanyRepository(companyRepository);
    configuration.setGroupRepository(new EmptyGroupRepository());
    final EmptyUserRepository userRepository = new EmptyUserRepository();
    configuration.setUserRepository(userRepository);
    // Also link user/company repositories
    userRepository.setCompanyRepository(companyRepository);
    this.iamConfiguration = configuration;
    return configuration;
}
Also used : IamConfiguration(org.ligoj.app.iam.IamConfiguration)

Example 4 with IamConfiguration

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

the class EmptyIamProviderTest method getConfigurationFindById.

@Test
public void getConfigurationFindById() {
    final IamConfiguration configuration = provider.getConfiguration();
    Assertions.assertEquals("any", configuration.getUserRepository().findById("any").getId());
    Assertions.assertNull(configuration.getGroupRepository().findById("any"));
    Assertions.assertNull(configuration.getCompanyRepository().findById("any"));
    Assertions.assertNull(new MockUserRepository2().findById("any"));
}
Also used : IamConfiguration(org.ligoj.app.iam.IamConfiguration) Test(org.junit.jupiter.api.Test)

Example 5 with IamConfiguration

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

the class EmptyIamProviderTest method getConfigurationFindByIdExpected.

@Test
public void getConfigurationFindByIdExpected() {
    final IamConfiguration configuration = provider.getConfiguration();
    Assertions.assertEquals("any", configuration.getUserRepository().findByIdExpected("any").getId());
}
Also used : IamConfiguration(org.ligoj.app.iam.IamConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

IamConfiguration (org.ligoj.app.iam.IamConfiguration)7 Test (org.junit.jupiter.api.Test)5 IamProvider (org.ligoj.app.iam.IamProvider)2 Node (org.ligoj.app.model.Node)2 Parameter (org.ligoj.app.model.Parameter)2 ParameterValue (org.ligoj.app.model.ParameterValue)2 Project (org.ligoj.app.model.Project)2 Subscription (org.ligoj.app.model.Subscription)2 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 HashMap (java.util.HashMap)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 EntityManager (javax.persistence.EntityManager)1