use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.
the class ImportCatalogResourceTest method findAll.
@Test
public void findAll() {
final ImportCatalogResource resource = newResource();
// Add importable provider
resource.locator = Mockito.mock(ServicePluginLocator.class);
final ImportCatalogService service = Mockito.mock(ImportCatalogService.class);
Mockito.when(resource.locator.getResource("service:prov:test", ImportCatalogService.class)).thenReturn(service);
// Add not updatable provider node
final Node notImportNode = new Node();
notImportNode.setId("service:prov:any");
notImportNode.setName("Cannot import");
notImportNode.setRefined(nodeRepository.findOneExpected("service:prov"));
nodeRepository.saveAndFlush(notImportNode);
final List<CatalogVo> catalogs = resource.findAll();
Assertions.assertEquals(3, catalogs.size());
// This provider does not support catalog update
Assertions.assertEquals(0, catalogs.get(0).getStatus().getNbInstancePrices().intValue());
Assertions.assertNull(catalogs.get(0).getStatus().getEnd());
Assertions.assertNull(catalogs.get(0).getStatus().getStart());
Assertions.assertEquals("service:prov:any", catalogs.get(0).getNode().getId());
Assertions.assertFalse(catalogs.get(0).isCanImport());
Assertions.assertEquals(0, catalogs.get(0).getNbQuotes());
// This provider supports catalog update
Assertions.assertNotNull(catalogs.get(1).getStatus());
Assertions.assertEquals("service:prov:test", catalogs.get(1).getNode().getId());
Assertions.assertTrue(catalogs.get(1).isCanImport());
Assertions.assertEquals(2, catalogs.get(1).getNbQuotes());
// This provider does not support catalog update
Assertions.assertEquals("service:prov:x", catalogs.get(2).getNode().getId());
Assertions.assertFalse(catalogs.get(2).isCanImport());
Assertions.assertNull(catalogs.get(2).getStatus().getEnd());
Assertions.assertNull(catalogs.get(2).getStatus().getStart());
Assertions.assertEquals(1, catalogs.get(2).getNbQuotes());
final ImportCatalogStatus status = catalogs.get(1).getStatus();
Assertions.assertEquals(DEFAULT_USER, status.getAuthor());
Assertions.assertNull(status.getEnd());
Assertions.assertNull(status.getLocation());
Assertions.assertEquals("service:prov:test", status.getLocked().getId());
Assertions.assertNotNull(status.getStart());
Assertions.assertEquals(0, status.getDone());
Assertions.assertEquals(0, status.getWorkload());
}
use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.
the class ImportCatalogResourceTest method cancelNoStartedTask.
@Test
public void cancelNoStartedTask() {
final ImportCatalogResource resource = new ImportCatalogResource();
applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
final ImportCatalogStatus status = newStatus();
status.setEnd(new Date());
Assertions.assertEquals("Already finished", Assertions.assertThrows(BusinessException.class, () -> {
resource.cancel("service:prov:test");
}).getMessage());
}
use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.
the class ImportCatalogResourceTest method updateCatalog.
@Test
public void updateCatalog() throws Exception {
final ImportCatalogResource resource = new ImportCatalogResource() {
@Override
public ImportCatalogStatus getTask(@PathParam("node") final String node) {
return getTaskRepository().findBy("locked.id", node);
}
};
applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
// Replace the locator for the custom provider
resource.locator = Mockito.mock(ServicePluginLocator.class);
final ImportCatalogService service = Mockito.mock(ImportCatalogService.class);
Mockito.when(resource.locator.getResource("service:prov:test", ImportCatalogService.class)).thenReturn(service);
final ImportCatalogStatus status = resource.updateCatalog("service:prov:test:account");
Assertions.assertEquals(DEFAULT_USER, status.getAuthor());
Assertions.assertNull(status.getEnd());
Assertions.assertNull(status.getLocation());
Assertions.assertEquals("service:prov:test", status.getLocked().getId());
Assertions.assertNotNull(status.getStart());
Assertions.assertNull(status.getLocation());
Assertions.assertEquals(0, status.getDone());
Assertions.assertEquals(0, status.getWorkload());
Assertions.assertFalse(resource.getTask("service:prov:test").isFinished());
Thread.sleep(100);
Mockito.verify(service).updateCatalog("service:prov:test");
}
use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.
the class ImportCatalogResourceTest method updateCatalogSynchronous.
@Test
public void updateCatalogSynchronous() throws Exception {
initSpringSecurityContext(DEFAULT_USER);
final ImportCatalogResource resource = newResource();
final ImportCatalogService service = Mockito.mock(ImportCatalogService.class);
resource.updateCatalog(service, "service:prov:test");
final ImportCatalogStatus status = repository.findBy("locked.id", "service:prov:test");
Assertions.assertEquals(DEFAULT_USER, status.getAuthor());
Assertions.assertNotNull(status.getEnd());
Assertions.assertNull(status.getLocation());
Assertions.assertEquals("service:prov:test", status.getLocked().getId());
Assertions.assertNotNull(status.getStart());
Assertions.assertNull(status.getLocation());
Assertions.assertEquals(0, status.getDone());
Assertions.assertNull(status.getPhase());
Assertions.assertEquals(0, status.getWorkload());
Assertions.assertTrue(status.isFinished());
Assertions.assertFalse(status.isFailed());
Assertions.assertNotEquals(0, status.getLastSuccess().getTime());
Assertions.assertEquals(101, status.getNbInstancePrices().intValue());
Assertions.assertEquals(13, status.getNbInstanceTypes().intValue());
Assertions.assertEquals(3, status.getNbLocations().intValue());
Assertions.assertEquals(4, status.getNbStorageTypes().intValue());
Mockito.verify(service).updateCatalog("service:prov:test");
}
use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.
the class ImportCatalogResource method findAll.
/**
* Return the nodes and their catalog status.
*
* @return The nodes and their catalog status.
*/
@GET
@Path("catalog")
public List<CatalogVo> findAll() {
// Get all catalogs
final Map<String, ImportCatalogStatus> statuses = taskRepository.findAllVisible(securityHelper.getLogin()).stream().collect(Collectors.toMap(s -> s.getLocked().getId(), Function.identity()));
// Complete with nodes without populated catalog
final Page<Node> providers = nodeRepository.findAllVisible(securityHelper.getLogin(), "", ProvResource.SERVICE_KEY, null, 1, PageRequest.of(0, 100));
return providers.getContent().stream().sorted().map(NodeResource::toVo).map(n -> new CatalogVo(Optional.ofNullable(statuses.get(n.getId())).orElseGet(() -> {
// Create a mock catalog status
final ImportCatalogStatus status = new ImportCatalogStatus();
updateStats(status, n.getId());
return status;
}), n, locator.getResource(n.getId(), ImportCatalogService.class) != null, (int) repository.countByNode(n.getId()))).collect(Collectors.toList());
}
Aggregations