Search in sources :

Example 36 with Subscription

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

the class TerraformResourceTest method applyTerraformError.

/**
 * IOException during the asynchronous execution
 */
@Test
public void applyTerraformError() {
    final TerraformResource resource = new TerraformResource() {

        @Override
        protected File applyTerraform(final Subscription entity, final Terraforming terra, final QuoteVo configuration) throws IOException {
            throw new IOException();
        }
    };
    super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    // Mock to disable inner transactions for this test
    resource.resource = Mockito.mock(ProvResource.class);
    final ServicePluginLocator locator = Mockito.mock(ServicePluginLocator.class);
    // Replace the plugin locator
    resource.locator = locator;
    Mockito.when(locator.getResource("service:prov:test:account", Terraforming.class)).thenReturn(Mockito.mock(Terraforming.class));
    resource.applyTerraform(subscription);
}
Also used : ProvResource(org.ligoj.app.plugin.prov.ProvResource) QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) IOException(java.io.IOException) Subscription(org.ligoj.app.model.Subscription) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 37 with Subscription

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

the class VmScheduleResource method checkAndSave.

private VmSchedule checkAndSave(final int subscription, final VmScheduleVo schedule, final VmSchedule entity) {
    // Check the subscription is visible
    final Subscription subscriptionEntity = subscriptionResource.checkVisibleSubscription(subscription);
    if (schedule.getCron().split(" ").length == 6) {
        // Add the missing "seconds" part
        schedule.setCron(schedule.getCron() + " *");
    }
    // Check expressions first
    if (!CronExpression.isValidExpression(schedule.getCron())) {
        throw new ValidationJsonException("cron", "vm-cron");
    }
    // Every second is not accepted
    if (schedule.getCron().startsWith("* ")) {
        throw new ValidationJsonException("cron", "vm-cron-second");
    }
    entity.setSubscription(subscriptionEntity);
    entity.setOperation(schedule.getOperation());
    entity.setCron(schedule.getCron());
    // Persist the new schedules for each provided CRON
    repository.saveAndFlush(entity);
    return entity;
}
Also used : ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) Subscription(org.ligoj.app.model.Subscription)

Example 38 with Subscription

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

the class VmResourceTest method delete.

@Test
public void delete() throws SchedulerException {
    final Project project = new Project();
    project.setName("TEST");
    project.setPkey("test");
    em.persist(project);
    final Subscription subscription = new Subscription();
    subscription.setProject(project);
    subscription.setNode(nodeRepository.findOneExpected("service:vm"));
    em.persist(subscription);
    Assertions.assertEquals(1, subscriptionRepository.findAllByProject(project.getId()).size());
    em.flush();
    em.clear();
    resource.delete(subscription.getId(), false);
    subscriptionRepository.delete(subscription);
    em.flush();
    em.clear();
    Assertions.assertEquals(0, subscriptionRepository.findAllByProject(project.getId()).size());
    Assertions.assertEquals(0, vmScheduleRepository.findBySubscription(subscription.getId()).size());
    Assertions.assertEquals(0, vmExecutionRepository.findAllBy("subscription.id", subscription.getId()).size());
}
Also used : Project(org.ligoj.app.model.Project) Subscription(org.ligoj.app.model.Subscription) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 39 with Subscription

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

the class VmResourceTest method executeDefault.

/**
 * Coverage only
 */
@Test
public void executeDefault() throws Exception {
    final VmExecution execution = new VmExecution();
    final Subscription subscription = new Subscription();
    subscription.setId(1);
    execution.setSubscription(subscription);
    new VmServicePlugin() {

        @Override
        public String getKey() {
            return null;
        }

        @Override
        public Vm getVmDetails(Map<String, String> parameters) throws Exception {
            return null;
        }
    }.execute(execution);
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) Subscription(org.ligoj.app.model.Subscription) ParseException(java.text.ParseException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 40 with Subscription

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

the class VmResourceTest method executeUnavailablePlugin.

@Test
public void executeUnavailablePlugin() throws Exception {
    final VmResource resource = new VmResource();
    final Subscription entity = subscriptionRepository.findOneExpected(subscription);
    final Node node = new Node();
    node.setId("_deleted_plugin_");
    node.setName("any");
    nodeRepository.saveAndFlush(node);
    entity.setNode(node);
    subscriptionRepository.saveAndFlush(entity);
    applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    resource.locator = mockServicePluginLocator;
    final Integer id = resource.execute(entity, VmOperation.OFF);
    // Execution is logged but failed
    final VmExecution execution = vmExecutionRepository.findOneExpected(id);
    Assertions.assertNull(execution.getVm());
    Assertions.assertNull(execution.getVm());
    Assertions.assertFalse(execution.isSucceed());
    Assertions.assertEquals("fdaugan", execution.getTrigger());
    Assertions.assertEquals(VmOperation.OFF, execution.getOperation());
    Assertions.assertEquals("Plugin issue for _deleted_plugin_:Not found", execution.getError());
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) Node(org.ligoj.app.model.Node) Subscription(org.ligoj.app.model.Subscription) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

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