Search in sources :

Example 1 with ImportCatalogStatus

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());
}
Also used : Node(org.ligoj.app.model.Node) ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 2 with ImportCatalogStatus

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());
}
Also used : ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) Date(java.util.Date) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 3 with ImportCatalogStatus

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");
}
Also used : ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) PathParam(javax.ws.rs.PathParam) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 4 with ImportCatalogStatus

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");
}
Also used : ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 5 with ImportCatalogStatus

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());
}
Also used : PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) Getter(lombok.Getter) GET(javax.ws.rs.GET) ProvInstancePriceRepository(org.ligoj.app.plugin.prov.dao.ProvInstancePriceRepository) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) Function(java.util.function.Function) Supplier(java.util.function.Supplier) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) MediaType(javax.ws.rs.core.MediaType) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) NodeResource(org.ligoj.app.resource.node.NodeResource) ProvInstanceTypeRepository(org.ligoj.app.plugin.prov.dao.ProvInstanceTypeRepository) Service(org.springframework.stereotype.Service) Map(java.util.Map) ProvStorageTypeRepository(org.ligoj.app.plugin.prov.dao.ProvStorageTypeRepository) LongTaskRunnerNode(org.ligoj.app.resource.node.LongTaskRunnerNode) DELETE(javax.ws.rs.DELETE) POST(javax.ws.rs.POST) NodeRepository(org.ligoj.app.dao.NodeRepository) Node(org.ligoj.app.model.Node) Transactional(javax.transaction.Transactional) ProvQuoteRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteRepository) PageRequest(org.springframework.data.domain.PageRequest) ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) OnNullReturn404(org.ligoj.bootstrap.core.resource.OnNullReturn404) ImportCatalogStatusRepository(org.ligoj.app.plugin.prov.dao.ImportCatalogStatusRepository) ProvLocationRepository(org.ligoj.app.plugin.prov.dao.ProvLocationRepository) Optional(java.util.Optional) ProvResource(org.ligoj.app.plugin.prov.ProvResource) NodeResource(org.ligoj.app.resource.node.NodeResource) LongTaskRunnerNode(org.ligoj.app.resource.node.LongTaskRunnerNode) Node(org.ligoj.app.model.Node) ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ImportCatalogStatus (org.ligoj.app.plugin.prov.model.ImportCatalogStatus)10 Test (org.junit.jupiter.api.Test)5 AbstractAppTest (org.ligoj.app.AbstractAppTest)5 ServicePluginLocator (org.ligoj.app.resource.ServicePluginLocator)4 Node (org.ligoj.app.model.Node)3 Date (java.util.Date)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 PathParam (javax.ws.rs.PathParam)2 LongTaskRunnerNode (org.ligoj.app.resource.node.LongTaskRunnerNode)2 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Executors (java.util.concurrent.Executors)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Transactional (javax.transaction.Transactional)1