Search in sources :

Example 41 with Subscription

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

the class VmResourceTest method downloadHistoryReport.

@Test
public void downloadHistoryReport() throws Exception {
    final VmResource resource = new VmResource();
    applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    resource.locator = mockServicePluginLocator;
    final AtomicReference<VmOperation> operation = new AtomicReference<>(null);
    // The third call is skipped
    Mockito.doNothing().when(mockVmTool).execute(ArgumentMatchers.argThat(new ArgumentMatcher<VmExecution>() {

        @Override
        public boolean matches(final VmExecution argument) {
            argument.setOperation(operation.get());
            return true;
        }
    }));
    // Report without executions
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadHistoryReport(subscription, "file1").getEntity()).write(output);
    List<String> lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(1, lines.size());
    Assertions.assertEquals("subscription;project;projectKey;projectName;node;dateHMS;timestamp;operation;vm;trigger;succeed;statusText;errorText", lines.get(0));
    output.close();
    // Manual execution
    operation.set(VmOperation.OFF);
    Integer id = resource.execute(subscription, VmOperation.OFF);
    vmExecutionRepository.findOneExpected(id).setStatusText("status1");
    // Manual execution by schedule, by pass the security check
    securityHelper.setUserName(SecurityHelper.SYSTEM_USERNAME);
    final Subscription entity = subscriptionRepository.findOneExpected(subscription);
    operation.set(VmOperation.SHUTDOWN);
    id = resource.execute(entity, VmOperation.ON);
    vmExecutionRepository.findOneExpected(id).setVm("vm1");
    // This call will be skipped
    operation.set(null);
    Assertions.assertNull(resource.execute(entity, VmOperation.REBOOT));
    // Restore the current user
    initSpringSecurityContext(getAuthenticationName());
    // Report contains these executions (OFF/SHUTDOWN/[REBOOT = skipped])
    output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadHistoryReport(subscription, "file1").getEntity()).write(output);
    lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(3, lines.size());
    Assertions.assertTrue(lines.get(2).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;.+;.+;OFF;;fdaugan;true;status1;"));
    Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;.+;.+;SHUTDOWN;vm1;_system;true;;"));
    Assertions.assertEquals(2, vmExecutionRepository.findAllBy("subscription.id", subscription).size());
    Assertions.assertEquals(subscription, vmExecutionRepository.findAllBy("subscription.id", subscription).get(0).getSubscription().getId().intValue());
    // Delete includes executions
    resource.delete(subscription, true);
    Assertions.assertEquals(0, vmExecutionRepository.findAllBy("subscription.id", subscription).size());
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) VmOperation(org.ligoj.app.plugin.vm.model.VmOperation) ArgumentMatcher(org.mockito.ArgumentMatcher) Subscription(org.ligoj.app.model.Subscription) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 42 with Subscription

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

the class VmScheduleResourceTest method deleteInvalidSubscription.

@Test
public void deleteInvalidSubscription() throws Exception {
    Assertions.assertEquals(1, repository.findAll().size());
    final Subscription entity = subscriptionRepository.findOneExpected(subscription);
    // Persist another VM schedule for another subscription
    final Subscription otherEntity = new Subscription();
    otherEntity.setProject(entity.getProject());
    otherEntity.setNode(entity.getNode());
    subscriptionRepository.saveAndFlush(otherEntity);
    final VmScheduleVo schedule2 = newSchedule("0 0 0 1 1 ? 2050", VmOperation.OFF);
    final int schedule = resource.create(otherEntity.getId(), schedule2);
    Assertions.assertEquals(2, repository.findAll().size());
    Assertions.assertThrows(EntityNotFoundException.class, () -> resource.delete(subscription, schedule));
}
Also used : Subscription(org.ligoj.app.model.Subscription) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 43 with Subscription

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

the class VmResource method getConfiguration.

@GET
@Path("{subscription:\\d+}")
@Override
@Transactional
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public VmConfigurationVo getConfiguration(@PathParam("subscription") final int subscription) throws ParseException {
    // Check the subscription is visible
    final Subscription entity = subscriptionResource.checkVisibleSubscription(subscription);
    // Get the details
    final VmConfigurationVo result = new VmConfigurationVo();
    result.setSchedules(scheduleResource.findAll(subscription));
    // Add snapshot capability
    result.setSupportSnapshot(locator.getResource(entity.getNode().getId(), Snapshotting.class) != null);
    return result;
}
Also used : Subscription(org.ligoj.app.model.Subscription) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) Transactional(javax.transaction.Transactional)

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