Search in sources :

Example 26 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class TestAbstractConfiguredServicePlugin method prepareMock.

@SuppressWarnings("unchecked")
@BeforeEach
public void prepareMock() {
    resource = new AbstractConfiguredServicePlugin<>() {

        @Override
        public String getKey() {
            return "key";
        }

        @Override
        public Object getConfiguration(int subscription) {
            return configuration;
        }
    };
    resource.subscriptionRepository = Mockito.mock(SubscriptionRepository.class);
    resource.projectRepository = Mockito.mock(ProjectRepository.class);
    resource.securityHelper = Mockito.mock(SecurityHelper.class);
    repository = Mockito.mock(RestRepository.class);
    configuration = Mockito.mock(PluginConfiguration.class);
    configurable = Mockito.mock(NamedConfigurable.class);
    subscription = new Subscription();
    subscription.setId(33);
    project = new Project();
    project.setId(44);
    subscription.setProject(project);
    Mockito.when(resource.securityHelper.getLogin()).thenReturn("junit");
    Mockito.when(configurable.getConfiguration()).thenReturn(configuration);
    Mockito.when(configurable.getId()).thenReturn(1);
    Mockito.when(configurable.getName()).thenReturn("my-name");
    Mockito.when(configuration.getSubscription()).thenReturn(subscription);
    Mockito.when(resource.subscriptionRepository.findOneExpected(33)).thenReturn(subscription);
    Mockito.when(resource.projectRepository.findOneVisible(44, "junit")).thenReturn(project);
    Mockito.when(repository.findOneExpected(1)).thenReturn(configurable);
    Mockito.when(repository.findAllBy("configuration.subscription.id", subscription.getId(), new String[] { "name" }, "my-name")).thenReturn(Collections.singletonList(configurable));
}
Also used : SubscriptionRepository(org.ligoj.app.dao.SubscriptionRepository) ProjectRepository(org.ligoj.app.dao.ProjectRepository) Project(org.ligoj.app.model.Project) PluginConfiguration(org.ligoj.app.model.PluginConfiguration) Subscription(org.ligoj.app.model.Subscription) RestRepository(org.ligoj.bootstrap.core.dao.RestRepository) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 27 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class ProjectResource method delete.

/**
 * Delete entity. Should be protected with RBAC.
 *
 * @param id
 *            The entity identifier.
 */
@DELETE
@Path("{id:\\d+}")
public void delete(@PathParam("id") final int id) throws Exception {
    final Project project = findOneVisible(id, Function.identity());
    for (final Subscription subscription : project.getSubscriptions()) {
        subscriptionResource.delete(subscription.getId());
    }
    repository.delete(project);
}
Also used : Project(org.ligoj.app.model.Project) Subscription(org.ligoj.app.model.Subscription) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 28 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class SubscriptionResource method getConfiguration.

/**
 * Return tools specific configuration. Only non secured parameters are returned.
 *
 * @param id
 *            The subscription identifier.
 * @return tools specific configuration.
 * @throws Exception
 *             When the configuration gathering fails. Managed at JAX-RS level.
 */
@GET
@Path("{id:\\d+}/configuration")
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public ConfigurationVo getConfiguration(@PathParam("id") final int id) throws Exception {
    // Copy subscription details
    final Subscription entity = checkVisibleSubscription(id);
    final ConfigurationVo vo = new ConfigurationVo();
    vo.setNode(NodeResource.toVo(entity.getNode()));
    vo.setParameters(getNonSecuredParameters(id));
    vo.setSubscription(id);
    vo.setProject(DescribedBean.clone(entity.getProject()));
    // Get specific configuration
    final ConfigurablePlugin servicePlugin = locator.getResource(vo.getNode().getId(), ConfigurablePlugin.class);
    if (servicePlugin != null) {
        // Specific configuration is available
        vo.setConfiguration(servicePlugin.getConfiguration(id));
    }
    return vo;
}
Also used : ConfigurablePlugin(org.ligoj.app.api.ConfigurablePlugin) ConfigurationVo(org.ligoj.app.api.ConfigurationVo) Subscription(org.ligoj.app.model.Subscription) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 29 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class NodeResource method checkNodeSubscriptions.

/**
 * Check the subscriptions of each subscription related to given node.
 */
private void checkNodeSubscriptions(final Node node, final Map<String, String> nodeParameters, final Map<Subscription, Map<String, String>> subscriptions) {
    int counter = 0;
    for (final Entry<Subscription, Map<String, String>> subscription : subscriptions.entrySet()) {
        // For each subscription, check status
        log.info("Check all subscriptions of node {} : {}/{} ...", node.getId(), counter + 1, subscriptions.size());
        final Map<String, String> parameters = new HashMap<>(nodeParameters);
        parameters.putAll(subscription.getValue());
        final NodeStatus subscriptionStatus = self.checkSubscriptionStatus(subscription.getKey(), parameters).getStatus();
        eventResource.registerEvent(subscription.getKey(), EventType.STATUS, subscriptionStatus.name());
        counter++;
    }
}
Also used : HashMap(java.util.HashMap) Subscription(org.ligoj.app.model.Subscription) Map(java.util.Map) HashMap(java.util.HashMap) NodeStatus(org.ligoj.app.api.NodeStatus)

Example 30 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class SubscriptionResource method create.

/**
 * Create subscription.
 *
 * @param vo
 *            the subscription.
 * @return the created {@link Subscription}.
 * @throws Exception
 *             When the creat fails. Managed at JAX-RS level.
 */
@POST
public int create(final SubscriptionEditionVo vo) throws Exception {
    // Validate entities
    final Project project = checkVisibleProject(vo.getProject());
    checkManagedProject(vo.getProject());
    final Node node = checkManagedNodeForSubscription(vo.getNode());
    final List<Parameter> acceptedParameters = checkInputParameters(vo);
    // Create subscription and parameters that would be removed in case of
    // roll-back because of invalid parameters
    final Subscription entity = toEntity(vo, project, node);
    // Expose the real entity for plug-in since we have loaded it
    entity.setProject(project);
    // Save this subscription in the transaction
    repository.saveAndFlush(entity);
    parameterValueResource.create(vo.getParameters(), entity);
    // Delegate to the related plug-in the next process
    delegateToPlugin(vo, entity);
    // Check again the parameters in the final state
    checkMandatoryParameters(vo.getParameters(), acceptedParameters, SubscriptionMode.CREATE);
    log.info("Subscription of project {} to service {}", vo.getProject(), vo.getNode());
    return entity.getId();
}
Also used : Project(org.ligoj.app.model.Project) Node(org.ligoj.app.model.Node) Parameter(org.ligoj.app.model.Parameter) Subscription(org.ligoj.app.model.Subscription) POST(javax.ws.rs.POST)

Aggregations

Subscription (org.ligoj.app.model.Subscription)43 Test (org.junit.jupiter.api.Test)22 Path (javax.ws.rs.Path)10 Node (org.ligoj.app.model.Node)10 AbstractServerTest (org.ligoj.app.AbstractServerTest)9 IOException (java.io.IOException)7 GET (javax.ws.rs.GET)7 POST (javax.ws.rs.POST)7 HashMap (java.util.HashMap)6 Transactional (javax.transaction.Transactional)6 Produces (javax.ws.rs.Produces)6 Map (java.util.Map)5 Optional (java.util.Optional)5 PathParam (javax.ws.rs.PathParam)5 MediaType (javax.ws.rs.core.MediaType)5 Slf4j (lombok.extern.slf4j.Slf4j)5 File (java.io.File)4 Date (java.util.Date)4 Function (java.util.function.Function)4 DELETE (javax.ws.rs.DELETE)4