Search in sources :

Example 1 with DashboardEntity

use of io.gravitee.rest.api.model.DashboardEntity in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceTest method shouldCreate.

@Test
public void shouldCreate() throws TechnicalException {
    final NewDashboardEntity newDashboardEntity = new NewDashboardEntity();
    newDashboardEntity.setName("NAME");
    newDashboardEntity.setDefinition("DEFINITION");
    newDashboardEntity.setReferenceId("REF_ID");
    newDashboardEntity.setReferenceType(PLATFORM);
    newDashboardEntity.setQueryFilter("QUERY FILTER");
    final Dashboard createdDashboard = new Dashboard();
    createdDashboard.setId(DASHBOARD_ID);
    createdDashboard.setName("NAME");
    createdDashboard.setDefinition("DEFINITION");
    createdDashboard.setOrder(1);
    createdDashboard.setReferenceId("REF_ID");
    createdDashboard.setReferenceType(PLATFORM.name());
    createdDashboard.setQueryFilter("QUERY FILTER");
    createdDashboard.setCreatedAt(new Date());
    createdDashboard.setUpdatedAt(new Date());
    when(dashboardRepository.create(any())).thenReturn(createdDashboard);
    final Dashboard d = new Dashboard();
    d.setOrder(0);
    when(dashboardRepository.findByReferenceType(PLATFORM.name())).thenReturn(singletonList(d));
    final DashboardEntity dashboardEntity = dashboardService.create(newDashboardEntity);
    assertNotNull(dashboardEntity.getId());
    assertEquals("NAME", dashboardEntity.getName());
    assertEquals("DEFINITION", dashboardEntity.getDefinition());
    assertEquals(1, dashboardEntity.getOrder());
    assertEquals("REF_ID", dashboardEntity.getReferenceId());
    assertEquals(PLATFORM.name(), dashboardEntity.getReferenceType());
    assertEquals("QUERY FILTER", dashboardEntity.getQueryFilter());
    assertNotNull(dashboardEntity.getCreatedAt());
    assertNotNull(dashboardEntity.getUpdatedAt());
    final Dashboard dashboard = new Dashboard();
    dashboard.setName("NAME");
    dashboard.setDefinition("DEFINITION");
    dashboard.setOrder(1);
    dashboard.setReferenceId("REF_ID");
    dashboard.setReferenceType(PLATFORM.name());
    dashboard.setQueryFilter("QUERY FILTER");
    verify(dashboardRepository, times(1)).create(argThat(argument -> "NAME".equals(argument.getName()) && "DEFINITION".equals(argument.getDefinition()) && "REF_ID".equals(argument.getReferenceId()) && PLATFORM.name().equals(argument.getReferenceType()) && "QUERY FILTER".equals(argument.getQueryFilter()) && Integer.valueOf(1).equals(argument.getOrder()) && !argument.getId().isEmpty() && argument.getCreatedAt() != null && argument.getUpdatedAt() != null));
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(DASHBOARD, DASHBOARD_ID)), eq(Dashboard.AuditEvent.DASHBOARD_CREATED), any(Date.class), isNull(), any());
}
Also used : Optional.empty(java.util.Optional.empty) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Date(java.util.Date) Mock(org.mockito.Mock) Optional.of(java.util.Optional.of) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) DASHBOARD(io.gravitee.repository.management.model.Audit.AuditProperties.DASHBOARD) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) PLATFORM(io.gravitee.rest.api.model.DashboardReferenceType.PLATFORM) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardRepository(io.gravitee.repository.management.api.DashboardRepository) DashboardServiceImpl(io.gravitee.rest.api.service.impl.DashboardServiceImpl) InjectMocks(org.mockito.InjectMocks) Dashboard(io.gravitee.repository.management.model.Dashboard) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito(org.mockito.Mockito) List(java.util.List) DashboardNotFoundException(io.gravitee.rest.api.service.exceptions.DashboardNotFoundException) DashboardReferenceType(io.gravitee.repository.management.model.DashboardReferenceType) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) Dashboard(io.gravitee.repository.management.model.Dashboard) Date(java.util.Date) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) Test(org.junit.Test)

Example 2 with DashboardEntity

use of io.gravitee.rest.api.model.DashboardEntity in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceTest method shouldUpdate.

@Test
public void shouldUpdate() throws TechnicalException {
    final UpdateDashboardEntity updateDashboardEntity = new UpdateDashboardEntity();
    updateDashboardEntity.setId(DASHBOARD_ID);
    updateDashboardEntity.setName("NAME");
    updateDashboardEntity.setDefinition("DEFINITION");
    updateDashboardEntity.setOrder(1);
    updateDashboardEntity.setReferenceId("REF_ID");
    updateDashboardEntity.setReferenceType(PLATFORM);
    updateDashboardEntity.setQueryFilter("QUERY FILTER");
    final Dashboard updatedDashboard = new Dashboard();
    updatedDashboard.setId(DASHBOARD_ID);
    updatedDashboard.setName("NAME");
    updatedDashboard.setDefinition("DEFINITION");
    updatedDashboard.setOrder(1);
    updatedDashboard.setReferenceId("REF_ID");
    updatedDashboard.setReferenceType(PLATFORM.name());
    updatedDashboard.setQueryFilter("QUERY FILTER");
    updatedDashboard.setCreatedAt(new Date());
    updatedDashboard.setUpdatedAt(new Date());
    when(dashboardRepository.update(any())).thenReturn(updatedDashboard);
    when(dashboardRepository.findById(DASHBOARD_ID)).thenReturn(of(updatedDashboard));
    final DashboardEntity dashboardEntity = dashboardService.update(updateDashboardEntity);
    assertNotNull(dashboardEntity.getId());
    assertEquals("NAME", dashboardEntity.getName());
    assertEquals("DEFINITION", dashboardEntity.getDefinition());
    assertEquals(1, dashboardEntity.getOrder());
    assertEquals("REF_ID", dashboardEntity.getReferenceId());
    assertEquals(PLATFORM.name(), dashboardEntity.getReferenceType());
    assertEquals("QUERY FILTER", dashboardEntity.getQueryFilter());
    assertNotNull(dashboardEntity.getCreatedAt());
    assertNotNull(dashboardEntity.getUpdatedAt());
    final Dashboard dashboard = new Dashboard();
    dashboard.setName("NAME");
    dashboard.setDefinition("DEFINITION");
    dashboard.setOrder(1);
    dashboard.setReferenceId("REF_ID");
    dashboard.setReferenceType(PLATFORM.name());
    dashboard.setQueryFilter("QUERY FILTER");
    verify(dashboardRepository, times(1)).update(argThat(argument -> "NAME".equals(argument.getName()) && "DEFINITION".equals(argument.getDefinition()) && "REF_ID".equals(argument.getReferenceId()) && PLATFORM.name().equals(argument.getReferenceType()) && "QUERY FILTER".equals(argument.getQueryFilter()) && Integer.valueOf(1).equals(argument.getOrder()) && DASHBOARD_ID.equals(argument.getId()) && argument.getCreatedAt() == null && argument.getUpdatedAt() != null));
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(DASHBOARD, DASHBOARD_ID)), eq(Dashboard.AuditEvent.DASHBOARD_UPDATED), any(Date.class), any(), any());
}
Also used : Optional.empty(java.util.Optional.empty) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Date(java.util.Date) Mock(org.mockito.Mock) Optional.of(java.util.Optional.of) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) DASHBOARD(io.gravitee.repository.management.model.Audit.AuditProperties.DASHBOARD) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) PLATFORM(io.gravitee.rest.api.model.DashboardReferenceType.PLATFORM) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardRepository(io.gravitee.repository.management.api.DashboardRepository) DashboardServiceImpl(io.gravitee.rest.api.service.impl.DashboardServiceImpl) InjectMocks(org.mockito.InjectMocks) Dashboard(io.gravitee.repository.management.model.Dashboard) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito(org.mockito.Mockito) List(java.util.List) DashboardNotFoundException(io.gravitee.rest.api.service.exceptions.DashboardNotFoundException) DashboardReferenceType(io.gravitee.repository.management.model.DashboardReferenceType) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) Dashboard(io.gravitee.repository.management.model.Dashboard) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) Date(java.util.Date) Test(org.junit.Test)

Example 3 with DashboardEntity

use of io.gravitee.rest.api.model.DashboardEntity in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method reorderAndSaveDashboards.

private DashboardEntity reorderAndSaveDashboards(final Dashboard dashboardToReorder, final boolean deleted) throws TechnicalException {
    final Collection<Dashboard> dashboards = dashboardRepository.findByReferenceType(dashboardToReorder.getReferenceType());
    final Dashboard[] dashboardsToReorder = dashboards.stream().filter(d -> !Objects.equals(d.getId(), dashboardToReorder.getId())).toArray(Dashboard[]::new);
    if (dashboardToReorder.getOrder() < 1) {
        dashboardToReorder.setOrder(1);
    } else if (dashboardToReorder.getOrder() > dashboardsToReorder.length + 1) {
        dashboardToReorder.setOrder(dashboardsToReorder.length + 1);
    }
    try {
        for (int i = 0; i < dashboardsToReorder.length; i++) {
            if (deleted) {
                if (dashboardsToReorder[i].getOrder() > dashboardToReorder.getOrder()) {
                    dashboardsToReorder[i].setOrder(dashboardsToReorder[i].getOrder() - 1);
                    dashboardRepository.update(dashboardsToReorder[i]);
                }
            } else {
                int newOrder = (i + 1) < dashboardToReorder.getOrder() ? (i + 1) : (i + 2);
                if (dashboardsToReorder[i].getOrder() != newOrder) {
                    dashboardsToReorder[i].setOrder(newOrder);
                    dashboardRepository.update(dashboardsToReorder[i]);
                }
            }
        }
        if (deleted) {
            return null;
        }
        return convert(dashboardRepository.update(dashboardToReorder));
    } catch (final TechnicalException ex) {
        final String error = "An error occurs while trying to update dashboard " + dashboardToReorder.getId();
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : Charset.defaultCharset(java.nio.charset.Charset.defaultCharset) java.util(java.util) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) DASHBOARD(io.gravitee.repository.management.model.Audit.AuditProperties.DASHBOARD) Value(org.springframework.beans.factory.annotation.Value) AuditEvent(io.gravitee.repository.management.model.Dashboard.AuditEvent) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) JsonNode(com.fasterxml.jackson.databind.JsonNode) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardService(io.gravitee.rest.api.service.DashboardService) Comparator.comparing(java.util.Comparator.comparing) DashboardRepository(io.gravitee.repository.management.api.DashboardRepository) UuidString(io.gravitee.rest.api.service.common.UuidString) Comparator.comparingInt(java.util.Comparator.comparingInt) Logger(org.slf4j.Logger) Dashboard(io.gravitee.repository.management.model.Dashboard) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AuditService(io.gravitee.rest.api.service.AuditService) IOException(java.io.IOException) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) DashboardNotFoundException(io.gravitee.rest.api.service.exceptions.DashboardNotFoundException) DashboardReferenceType(io.gravitee.repository.management.model.DashboardReferenceType) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Dashboard(io.gravitee.repository.management.model.Dashboard) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with DashboardEntity

use of io.gravitee.rest.api.model.DashboardEntity in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method update.

@Override
public DashboardEntity update(UpdateDashboardEntity dashboardEntity) {
    try {
        final Optional<Dashboard> dashboardOptional = dashboardRepository.findById(dashboardEntity.getId());
        if (dashboardOptional.isPresent()) {
            final Dashboard dashboard = convert(dashboardEntity);
            final DashboardEntity savedDashboard;
            if (dashboard.getOrder() != dashboardOptional.get().getOrder()) {
                savedDashboard = reorderAndSaveDashboards(dashboard, false);
            } else {
                savedDashboard = convert(dashboardRepository.update(dashboard));
            }
            auditService.createEnvironmentAuditLog(Collections.singletonMap(DASHBOARD, dashboard.getId()), DASHBOARD_UPDATED, new Date(), dashboardOptional.get(), dashboard);
            return savedDashboard;
        } else {
            throw new DashboardNotFoundException(dashboardEntity.getId());
        }
    } catch (TechnicalException ex) {
        final String error = "An error occurred while trying to update dashboard " + dashboardEntity;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : DashboardNotFoundException(io.gravitee.rest.api.service.exceptions.DashboardNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) Dashboard(io.gravitee.repository.management.model.Dashboard) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 5 with DashboardEntity

use of io.gravitee.rest.api.model.DashboardEntity in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method convert.

private DashboardEntity convert(final Dashboard dashboard) {
    final DashboardEntity dashboardEntity = new DashboardEntity();
    dashboardEntity.setId(dashboard.getId());
    dashboardEntity.setReferenceId(dashboard.getReferenceId());
    dashboardEntity.setReferenceType(dashboard.getReferenceType());
    dashboardEntity.setName(dashboard.getName());
    dashboardEntity.setQueryFilter(dashboard.getQueryFilter());
    dashboardEntity.setOrder(dashboard.getOrder());
    dashboardEntity.setEnabled(dashboard.isEnabled());
    dashboardEntity.setDefinition(dashboard.getDefinition());
    dashboardEntity.setCreatedAt(dashboard.getCreatedAt());
    dashboardEntity.setUpdatedAt(dashboard.getUpdatedAt());
    return dashboardEntity;
}
Also used : UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity)

Aggregations

DashboardEntity (io.gravitee.rest.api.model.DashboardEntity)9 NewDashboardEntity (io.gravitee.rest.api.model.NewDashboardEntity)9 UpdateDashboardEntity (io.gravitee.rest.api.model.UpdateDashboardEntity)9 Dashboard (io.gravitee.repository.management.model.Dashboard)7 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)5 Date (java.util.Date)5 Test (org.junit.Test)5 DashboardNotFoundException (io.gravitee.rest.api.service.exceptions.DashboardNotFoundException)4 DashboardRepository (io.gravitee.repository.management.api.DashboardRepository)3 DASHBOARD (io.gravitee.repository.management.model.Audit.AuditProperties.DASHBOARD)3 DashboardReferenceType (io.gravitee.repository.management.model.DashboardReferenceType)3 UuidString (io.gravitee.rest.api.service.common.UuidString)3 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 PLATFORM (io.gravitee.rest.api.model.DashboardReferenceType.PLATFORM)2 DashboardServiceImpl (io.gravitee.rest.api.service.impl.DashboardServiceImpl)2 Collections.singleton (java.util.Collections.singleton)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Optional.empty (java.util.Optional.empty)2