Search in sources :

Example 1 with PortalNotificationConfig

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

the class PortalNotificationConfigService_FindByIdTest method shouldFindExistingConfig.

@Test
public void shouldFindExistingConfig() throws TechnicalException {
    PortalNotificationConfig cfg = mock(PortalNotificationConfig.class);
    when(cfg.getReferenceType()).thenReturn(NotificationReferenceType.API);
    when(cfg.getReferenceId()).thenReturn("123");
    when(cfg.getUser()).thenReturn("user");
    when(cfg.getHooks()).thenReturn(Arrays.asList("A", "B", "C"));
    when(portalNotificationConfigRepository.findById("user", NotificationReferenceType.API, "123")).thenReturn(of(cfg));
    final PortalNotificationConfigEntity entity = portalNotificationConfigService.findById("user", NotificationReferenceType.API, "123");
    assertNotNull(entity);
    assertEquals("referenceId", cfg.getReferenceId(), entity.getReferenceId());
    assertEquals("referenceType", cfg.getReferenceType().name(), entity.getReferenceType());
    assertEquals("user", cfg.getUser(), entity.getUser());
    assertEquals("hooks", cfg.getHooks(), entity.getHooks());
    verify(portalNotificationConfigRepository, times(1)).findById("user", NotificationReferenceType.API, "123");
}
Also used : PortalNotificationConfig(io.gravitee.repository.management.model.PortalNotificationConfig) PortalNotificationConfigEntity(io.gravitee.management.model.notification.PortalNotificationConfigEntity) Test(org.junit.Test)

Example 2 with PortalNotificationConfig

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

the class PortalNotificationConfigService_SaveTest method shouldUpdate.

@Test
public void shouldUpdate() throws TechnicalException {
    PortalNotificationConfig cfg = mock(PortalNotificationConfig.class);
    when(cfg.getReferenceType()).thenReturn(NotificationReferenceType.API);
    when(cfg.getReferenceId()).thenReturn("123");
    when(cfg.getUser()).thenReturn("user");
    when(cfg.getHooks()).thenReturn(Arrays.asList("A", "B", "C"));
    PortalNotificationConfigEntity cfgEntity = mock(PortalNotificationConfigEntity.class);
    when(cfgEntity.getReferenceType()).thenReturn(NotificationReferenceType.API.name());
    when(cfgEntity.getReferenceId()).thenReturn("123");
    when(cfgEntity.getUser()).thenReturn("user");
    when(cfgEntity.getHooks()).thenReturn(Arrays.asList("A", "B", "C"));
    when(portalNotificationConfigRepository.findById("user", NotificationReferenceType.API, "123")).thenReturn(of(cfg));
    when(portalNotificationConfigRepository.update(any(PortalNotificationConfig.class))).thenReturn(cfg);
    final PortalNotificationConfigEntity entity = portalNotificationConfigService.save(cfgEntity);
    assertNotNull(entity);
    assertEquals("referenceId", cfgEntity.getReferenceId(), entity.getReferenceId());
    assertEquals("referenceType", cfgEntity.getReferenceType(), entity.getReferenceType());
    assertEquals("user", cfgEntity.getUser(), entity.getUser());
    assertEquals("hooks", cfgEntity.getHooks(), entity.getHooks());
    verify(portalNotificationConfigRepository, times(1)).findById("user", NotificationReferenceType.API, "123");
    verify(portalNotificationConfigRepository, times(1)).update(any());
    verify(portalNotificationConfigRepository, never()).delete(any());
    verify(portalNotificationConfigRepository, never()).create(any());
}
Also used : PortalNotificationConfig(io.gravitee.repository.management.model.PortalNotificationConfig) PortalNotificationConfigEntity(io.gravitee.management.model.notification.PortalNotificationConfigEntity) Test(org.junit.Test)

Example 3 with PortalNotificationConfig

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

the class PortalNotificationConfigService_SaveTest method shouldCreate.

@Test
public void shouldCreate() throws TechnicalException {
    PortalNotificationConfig cfg = mock(PortalNotificationConfig.class);
    when(cfg.getReferenceType()).thenReturn(NotificationReferenceType.API);
    when(cfg.getReferenceId()).thenReturn("123");
    when(cfg.getUser()).thenReturn("user");
    when(cfg.getHooks()).thenReturn(Arrays.asList("A", "B", "C"));
    PortalNotificationConfigEntity cfgEntity = mock(PortalNotificationConfigEntity.class);
    when(cfgEntity.getReferenceType()).thenReturn(NotificationReferenceType.API.name());
    when(cfgEntity.getReferenceId()).thenReturn("123");
    when(cfgEntity.getUser()).thenReturn("user");
    when(cfgEntity.getHooks()).thenReturn(Arrays.asList("A", "B", "C"));
    when(portalNotificationConfigRepository.findById("user", NotificationReferenceType.API, "123")).thenReturn(empty());
    when(portalNotificationConfigRepository.create(any(PortalNotificationConfig.class))).thenReturn(cfg);
    final PortalNotificationConfigEntity entity = portalNotificationConfigService.save(cfgEntity);
    assertNotNull(entity);
    assertEquals("referenceId", cfgEntity.getReferenceId(), entity.getReferenceId());
    assertEquals("referenceType", cfgEntity.getReferenceType(), entity.getReferenceType());
    assertEquals("user", cfgEntity.getUser(), entity.getUser());
    assertEquals("hooks", cfgEntity.getHooks(), entity.getHooks());
    verify(portalNotificationConfigRepository, times(1)).findById("user", NotificationReferenceType.API, "123");
    verify(portalNotificationConfigRepository, times(1)).create(any());
    verify(portalNotificationConfigRepository, never()).delete(any());
    verify(portalNotificationConfigRepository, never()).update(any());
}
Also used : PortalNotificationConfig(io.gravitee.repository.management.model.PortalNotificationConfig) PortalNotificationConfigEntity(io.gravitee.management.model.notification.PortalNotificationConfigEntity) Test(org.junit.Test)

Example 4 with PortalNotificationConfig

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

the class PortalNotificationConfigServiceImpl method convert.

private PortalNotificationConfig convert(PortalNotificationConfigEntity entity) {
    PortalNotificationConfig portalNotificationConfig = new PortalNotificationConfig();
    portalNotificationConfig.setReferenceType(NotificationReferenceType.valueOf(entity.getReferenceType()));
    portalNotificationConfig.setReferenceId(entity.getReferenceId());
    portalNotificationConfig.setUser(entity.getUser());
    portalNotificationConfig.setHooks(entity.getHooks());
    return portalNotificationConfig;
}
Also used : PortalNotificationConfig(io.gravitee.repository.management.model.PortalNotificationConfig)

Example 5 with PortalNotificationConfig

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

the class PortalNotificationConfigServiceImpl method save.

@Override
public PortalNotificationConfigEntity save(PortalNotificationConfigEntity notificationEntity) {
    try {
        if (notificationEntity.getHooks() == null || notificationEntity.getHooks().isEmpty()) {
            portalNotificationConfigRepository.delete(convert(notificationEntity));
            return getDefaultEmpty(notificationEntity.getUser(), NotificationReferenceType.valueOf(notificationEntity.getReferenceType()), notificationEntity.getReferenceId());
        } else {
            Optional<PortalNotificationConfig> optionalConfig = portalNotificationConfigRepository.findById(notificationEntity.getUser(), NotificationReferenceType.valueOf(notificationEntity.getReferenceType()), notificationEntity.getReferenceId());
            PortalNotificationConfig notificationConfig = convert(notificationEntity);
            if (optionalConfig.isPresent()) {
                notificationConfig.setCreatedAt(optionalConfig.get().getCreatedAt());
                notificationConfig.setUpdatedAt(new Date());
                return convert(portalNotificationConfigRepository.update(notificationConfig));
            } else {
                notificationConfig.setCreatedAt(new Date());
                notificationConfig.setUpdatedAt(notificationConfig.getCreatedAt());
                return convert(portalNotificationConfigRepository.create(notificationConfig));
            }
        }
    } catch (TechnicalException te) {
        LOGGER.error("An error occurs while trying to save the notification settings {}", notificationEntity, te);
        throw new TechnicalManagementException("An error occurs while trying to save the notification settings " + notificationEntity, te);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) PortalNotificationConfig(io.gravitee.repository.management.model.PortalNotificationConfig) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

PortalNotificationConfig (io.gravitee.repository.management.model.PortalNotificationConfig)5 PortalNotificationConfigEntity (io.gravitee.management.model.notification.PortalNotificationConfigEntity)3 Test (org.junit.Test)3 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 Date (java.util.Date)1