Search in sources :

Example 1 with NewViewEntity

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

the class ViewService_CreateTest method shouldCreate2Views.

@Test
public void shouldCreate2Views() throws TechnicalException {
    NewViewEntity v1 = new NewViewEntity();
    v1.setName("v1");
    NewViewEntity v2 = new NewViewEntity();
    v2.setName("v2");
    when(mockViewRepository.create(any())).thenReturn(new View());
    List<ViewEntity> views = viewService.create(Arrays.asList(v1, v2));
    assertNotNull("result is null", views);
    assertFalse("result is empty", views.isEmpty());
    assertEquals("result size is 2", 2, views.size());
    verify(mockAuditService, times(2)).createPortalAuditLog(any(), eq(VIEW_CREATED), any(), isNull(), any());
    verify(mockViewRepository, times(1)).create(argThat(new ArgumentMatcher<View>() {

        @Override
        public boolean matches(Object arg) {
            return arg instanceof View && ((View) arg).getName().equals("v1");
        }
    }));
    verify(mockViewRepository, times(1)).create(argThat(new ArgumentMatcher<View>() {

        @Override
        public boolean matches(Object arg) {
            return arg instanceof View && ((View) arg).getName().equals("v2");
        }
    }));
}
Also used : ViewEntity(io.gravitee.management.model.ViewEntity) NewViewEntity(io.gravitee.management.model.NewViewEntity) NewViewEntity(io.gravitee.management.model.NewViewEntity) ArgumentMatcher(org.mockito.ArgumentMatcher) View(io.gravitee.repository.management.model.View) Test(org.junit.Test)

Example 2 with NewViewEntity

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

the class ViewServiceImpl method create.

@Override
public List<ViewEntity> create(final List<NewViewEntity> viewEntities) {
    // First we prevent the duplicate view name
    final List<String> viewNames = viewEntities.stream().map(NewViewEntity::getName).collect(Collectors.toList());
    final Optional<ViewEntity> optionalView = findAll().stream().filter(view -> viewNames.contains(view.getName())).findAny();
    if (optionalView.isPresent()) {
        throw new DuplicateViewNameException(optionalView.get().getName());
    }
    final List<ViewEntity> savedViews = new ArrayList<>(viewEntities.size());
    viewEntities.forEach(viewEntity -> {
        try {
            View view = convert(viewEntity);
            savedViews.add(convert(viewRepository.create(view)));
            auditService.createPortalAuditLog(Collections.singletonMap(VIEW, view.getId()), VIEW_CREATED, new Date(), null, view);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create view {}", viewEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create view " + viewEntity.getName(), ex);
        }
    });
    return savedViews;
}
Also used : ViewEntity(io.gravitee.management.model.ViewEntity) java.util(java.util) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) Logger(org.slf4j.Logger) ApiService(io.gravitee.management.service.ApiService) ViewService(io.gravitee.management.service.ViewService) VIEW_DELETED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_DELETED) DuplicateViewNameException(io.gravitee.management.service.exceptions.DuplicateViewNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) VIEW(io.gravitee.repository.management.model.Audit.AuditProperties.VIEW) VIEW_UPDATED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_UPDATED) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) View(io.gravitee.repository.management.model.View) Autowired(org.springframework.beans.factory.annotation.Autowired) NewViewEntity(io.gravitee.management.model.NewViewEntity) AuditService(io.gravitee.management.service.AuditService) Collectors(java.util.stream.Collectors) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) VIEW_CREATED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_CREATED) ViewRepository(io.gravitee.repository.management.api.ViewRepository) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ViewEntity(io.gravitee.management.model.ViewEntity) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) NewViewEntity(io.gravitee.management.model.NewViewEntity) DuplicateViewNameException(io.gravitee.management.service.exceptions.DuplicateViewNameException) View(io.gravitee.repository.management.model.View) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 3 with NewViewEntity

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

the class ViewService_CreateTest method shouldNotCreateExistingView.

@Test(expected = DuplicateViewNameException.class)
public void shouldNotCreateExistingView() throws TechnicalException {
    View v1 = new View();
    NewViewEntity nv1 = new NewViewEntity();
    v1.setName("v1");
    nv1.setName("v1");
    when(mockViewRepository.findAll()).thenReturn(Collections.singleton(v1));
    try {
        viewService.create(singletonList(nv1));
    } catch (DuplicateViewNameException e) {
        verify(mockViewRepository, never()).create(any());
        throw e;
    }
    Assert.fail("should throw DuplicateViewNameException");
}
Also used : NewViewEntity(io.gravitee.management.model.NewViewEntity) DuplicateViewNameException(io.gravitee.management.service.exceptions.DuplicateViewNameException) View(io.gravitee.repository.management.model.View) Test(org.junit.Test)

Aggregations

NewViewEntity (io.gravitee.management.model.NewViewEntity)3 View (io.gravitee.repository.management.model.View)3 ViewEntity (io.gravitee.management.model.ViewEntity)2 DuplicateViewNameException (io.gravitee.management.service.exceptions.DuplicateViewNameException)2 Test (org.junit.Test)2 IdGenerator (io.gravitee.common.utils.IdGenerator)1 UpdateViewEntity (io.gravitee.management.model.UpdateViewEntity)1 ApiService (io.gravitee.management.service.ApiService)1 AuditService (io.gravitee.management.service.AuditService)1 ViewService (io.gravitee.management.service.ViewService)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 ViewRepository (io.gravitee.repository.management.api.ViewRepository)1 VIEW (io.gravitee.repository.management.model.Audit.AuditProperties.VIEW)1 VIEW_CREATED (io.gravitee.repository.management.model.View.AuditEvent.VIEW_CREATED)1 VIEW_DELETED (io.gravitee.repository.management.model.View.AuditEvent.VIEW_DELETED)1 VIEW_UPDATED (io.gravitee.repository.management.model.View.AuditEvent.VIEW_UPDATED)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1