Search in sources :

Example 1 with SubscriptionVo

use of org.ligoj.app.resource.subscription.SubscriptionVo 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 2 with SubscriptionVo

use of org.ligoj.app.resource.subscription.SubscriptionVo in project ligoj-api by ligoj.

the class ToVoConverter method apply.

@Override
public ProjectVo apply(final Project entity) {
    final ProjectVo vo = new ProjectVo();
    vo.copyAuditData(entity, userConverter);
    DescribedBean.copy(entity, vo);
    vo.setPkey(entity.getPkey());
    vo.setTeamLeader(userConverter.apply(entity.getTeamLeader()));
    // Build the subscriptions
    final Map<Integer, SubscriptionVo> subscriptions = new TreeMap<>();
    for (final Object[] resultSet : this.subscriptionsAndParam) {
        // Add subscription value
        final ParameterValue parameterValue = (ParameterValue) resultSet[1];
        addVo(subscriptions, (Subscription) resultSet[0]).getParameters().put(parameterValue.getParameter().getId(), ParameterValueResource.parseValue(parameterValue, new ParameterValueVo()));
    }
    // Merge with subscription without parameters
    entity.getSubscriptions().forEach(s -> addVo(subscriptions, s));
    // Return the subscription to order by the related node
    vo.setSubscriptions(subscriptions.values().stream().sorted(Comparator.comparing(s -> s.getNode().getId(), String::compareTo)).collect(Collectors.toList()));
    return vo;
}
Also used : ParameterValueResource(org.ligoj.app.resource.node.ParameterValueResource) NodeStatus(org.ligoj.app.api.NodeStatus) SubscriptionVo(org.ligoj.app.resource.subscription.SubscriptionVo) HashMap(java.util.HashMap) EventVo(org.ligoj.app.resource.node.EventVo) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ParameterValueVo(org.ligoj.app.resource.node.ParameterValueVo) List(java.util.List) NodeResource(org.ligoj.app.resource.node.NodeResource) TreeMap(java.util.TreeMap) Map(java.util.Map) UserOrg(org.ligoj.app.iam.UserOrg) Subscription(org.ligoj.app.model.Subscription) Comparator(java.util.Comparator) ParameterValue(org.ligoj.app.model.ParameterValue) Project(org.ligoj.app.model.Project) DescribedBean(org.ligoj.bootstrap.core.DescribedBean) ParameterValueVo(org.ligoj.app.resource.node.ParameterValueVo) ParameterValue(org.ligoj.app.model.ParameterValue) TreeMap(java.util.TreeMap) SubscriptionVo(org.ligoj.app.resource.subscription.SubscriptionVo)

Example 3 with SubscriptionVo

use of org.ligoj.app.resource.subscription.SubscriptionVo in project ligoj-api by ligoj.

the class ToVoConverter method addVo.

/**
 * Convert a {@link Subscription} to a {@link SubscriptionVo} with status, and put it in the target map if not
 * existing.
 *
 * @param subscriptions
 *            The map of already converted entities.
 * @param entity
 *            The {@link Subscription}
 * @return The related converted {@link SubscriptionVo} newly created or existing one.
 */
private SubscriptionVo addVo(final Map<Integer, SubscriptionVo> subscriptions, final Subscription entity) {
    return subscriptions.computeIfAbsent(entity.getId(), id -> {
        // Build the subscription root instance
        final SubscriptionVo vo = new SubscriptionVo();
        vo.copyAuditData(entity, userConverter);
        vo.setId(entity.getId());
        vo.setNode(NodeResource.toVo(entity.getNode()));
        vo.setParameters(new HashMap<>());
        subscriptions.put(entity.getId(), vo);
        // Add subscription status
        final EventVo lastEvent = subscriptionStatus.get(entity.getId());
        if (lastEvent != null) {
            vo.setStatus(NodeStatus.valueOf(lastEvent.getValue()));
        }
        return vo;
    });
}
Also used : SubscriptionVo(org.ligoj.app.resource.subscription.SubscriptionVo) EventVo(org.ligoj.app.resource.node.EventVo)

Example 4 with SubscriptionVo

use of org.ligoj.app.resource.subscription.SubscriptionVo in project ligoj-api by ligoj.

the class ProjectResourceTest method checkProject.

private void checkProject(final ProjectVo project) {
    checkProject((BasicProjectVo) project);
    Assertions.assertTrue(project.isManageSubscriptions());
    // Check subscription
    Assertions.assertEquals(1, project.getSubscriptions().size());
    final SubscriptionVo subscription = project.getSubscriptions().iterator().next();
    Assertions.assertNotNull(subscription.getCreatedDate());
    Assertions.assertNotNull(subscription.getLastModifiedDate());
    Assertions.assertNotNull(subscription.getId());
    Assertions.assertEquals(DEFAULT_USER, subscription.getCreatedBy().getId());
    Assertions.assertEquals(DEFAULT_USER, subscription.getLastModifiedBy().getId());
    // Check service (ordered by id)
    final NodeVo service = subscription.getNode();
    Assertions.assertNotNull(service);
    Assertions.assertEquals("JIRA 4", service.getName());
    Assertions.assertNotNull(service.getId());
    Assertions.assertEquals("service:bt:jira", service.getRefined().getId());
    Assertions.assertEquals("service:bt", service.getRefined().getRefined().getId());
    Assertions.assertNull(service.getRefined().getRefined().getRefined());
    // Check subscription values
    Assertions.assertEquals(3, subscription.getParameters().size());
    Assertions.assertEquals("http://localhost:8120", subscription.getParameters().get("service:bt:jira:url"));
    Assertions.assertEquals(10074, ((Integer) subscription.getParameters().get("service:bt:jira:project")).intValue());
    Assertions.assertEquals("MDA", subscription.getParameters().get("service:bt:jira:pkey"));
}
Also used : SubscriptionVo(org.ligoj.app.resource.subscription.SubscriptionVo) NodeVo(org.ligoj.app.api.NodeVo)

Example 5 with SubscriptionVo

use of org.ligoj.app.resource.subscription.SubscriptionVo in project ligoj-api by ligoj.

the class ProjectResourceTest method findByIdNoParameter.

/**
 * Test {@link ProjectResource#findById(int)} when a subscription has no parameter.
 */
@Test
public void findByIdNoParameter() {
    // Pre check
    initSpringSecurityContext("fdaugan");
    Assertions.assertEquals(1, resource.findById(testProject.getId()).getSubscriptions().size());
    em.flush();
    em.clear();
    final Subscription subscription = new Subscription();
    subscription.setProject(testProject);
    subscription.setNode(nodeRepository.findOneExpected("service:build:jenkins"));
    subscriptionRepository.saveAndFlush(subscription);
    em.flush();
    em.clear();
    // Post check
    final List<SubscriptionVo> subscriptions = resource.findById(testProject.getId()).getSubscriptions();
    Assertions.assertEquals(2, subscriptions.size());
    Assertions.assertEquals("service:bt:jira:4", subscriptions.get(0).getNode().getId());
    Assertions.assertEquals("service:build:jenkins", subscriptions.get(1).getNode().getId());
    Assertions.assertEquals(0, subscriptions.get(1).getParameters().size());
}
Also used : Subscription(org.ligoj.app.model.Subscription) SubscriptionVo(org.ligoj.app.resource.subscription.SubscriptionVo) AbstractOrgTest(org.ligoj.app.resource.AbstractOrgTest) Test(org.junit.jupiter.api.Test)

Aggregations

SubscriptionVo (org.ligoj.app.resource.subscription.SubscriptionVo)6 Test (org.junit.jupiter.api.Test)3 Project (org.ligoj.app.model.Project)3 Subscription (org.ligoj.app.model.Subscription)3 EventVo (org.ligoj.app.resource.node.EventVo)3 HashMap (java.util.HashMap)2 NodeVo (org.ligoj.app.api.NodeVo)2 ParameterValue (org.ligoj.app.model.ParameterValue)2 AbstractOrgTest (org.ligoj.app.resource.AbstractOrgTest)2 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 NodeStatus (org.ligoj.app.api.NodeStatus)1 IUserRepository (org.ligoj.app.iam.IUserRepository)1 IamConfiguration (org.ligoj.app.iam.IamConfiguration)1