Search in sources :

Example 6 with Dashboard

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

the class DashboardServiceImpl method convert.

private Dashboard convert(final NewDashboardEntity dashboardEntity, final List<Dashboard> dashboards) {
    final Dashboard dashboard = new Dashboard();
    dashboard.setId(UuidString.generateRandom());
    dashboard.setReferenceId(dashboardEntity.getReferenceId());
    dashboard.setReferenceType(dashboardEntity.getReferenceType().name());
    dashboard.setName(dashboardEntity.getName());
    dashboard.setQueryFilter(dashboardEntity.getQueryFilter());
    dashboard.setEnabled(dashboardEntity.isEnabled());
    dashboard.setDefinition(dashboardEntity.getDefinition());
    if (dashboards == null || dashboards.isEmpty()) {
        dashboard.setOrder(1);
    } else {
        dashboard.setOrder(dashboards.stream().max(comparingInt(Dashboard::getOrder)).map(Dashboard::getOrder).get() + 1);
    }
    final Date now = new Date();
    dashboard.setCreatedAt(now);
    dashboard.setUpdatedAt(now);
    return dashboard;
}
Also used : Dashboard(io.gravitee.repository.management.model.Dashboard)

Example 7 with Dashboard

use of io.gravitee.repository.management.model.Dashboard 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 8 with Dashboard

use of io.gravitee.repository.management.model.Dashboard 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 9 with Dashboard

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

the class DashboardServiceImpl method findById.

@Override
public DashboardEntity findById(String dashboardId) {
    try {
        LOGGER.debug("Find dashboard by ID: {}", dashboardId);
        Optional<Dashboard> optDashboard = dashboardRepository.findById(dashboardId);
        if (!optDashboard.isPresent()) {
            throw new DashboardNotFoundException(dashboardId);
        }
        return convert(optDashboard.get());
    } catch (TechnicalException ex) {
        final String error = "An error occurs while trying to find dashboard by ID";
        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) Dashboard(io.gravitee.repository.management.model.Dashboard) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 10 with Dashboard

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

the class DashboardServiceTest method shouldFindByReferenceType.

@Test
public void shouldFindByReferenceType() throws TechnicalException {
    final Dashboard dashboard = mock(Dashboard.class);
    when(dashboard.getId()).thenReturn(DASHBOARD_ID);
    when(dashboard.getName()).thenReturn("NAME");
    when(dashboard.getDefinition()).thenReturn("DEFINITION");
    when(dashboard.getOrder()).thenReturn(1);
    when(dashboard.getReferenceId()).thenReturn("REF_ID");
    when(dashboard.getReferenceType()).thenReturn(PLATFORM.name());
    when(dashboard.getQueryFilter()).thenReturn("QUERY FILTER");
    when(dashboard.getCreatedAt()).thenReturn(new Date(1));
    when(dashboard.getUpdatedAt()).thenReturn(new Date(2));
    when(dashboardRepository.findByReferenceType(PLATFORM.name())).thenReturn(singletonList(dashboard));
    final List<DashboardEntity> dashboards = dashboardService.findByReferenceType(DashboardReferenceType.PLATFORM);
    final DashboardEntity dashboardEntity = dashboards.iterator().next();
    assertEquals(DASHBOARD_ID, 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());
    assertEquals(new Date(1), dashboardEntity.getCreatedAt());
    assertEquals(new Date(2), dashboardEntity.getUpdatedAt());
}
Also used : 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) Test(org.junit.Test)

Aggregations

Dashboard (io.gravitee.repository.management.model.Dashboard)13 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)7 DashboardEntity (io.gravitee.rest.api.model.DashboardEntity)7 NewDashboardEntity (io.gravitee.rest.api.model.NewDashboardEntity)7 UpdateDashboardEntity (io.gravitee.rest.api.model.UpdateDashboardEntity)7 Date (java.util.Date)6 Test (org.junit.Test)6 UuidString (io.gravitee.rest.api.service.common.UuidString)5 DashboardNotFoundException (io.gravitee.rest.api.service.exceptions.DashboardNotFoundException)5 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)5 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 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