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());
}
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));
}
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;
}
Aggregations