Search in sources :

Example 1 with ViewEntity

use of io.gravitee.management.model.ViewEntity 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 ViewEntity

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

the class ViewService_FindTest method shouldFindView.

@Test
public void shouldFindView() throws TechnicalException {
    View view = mock(View.class);
    when(view.getId()).thenReturn("view-id");
    when(view.getName()).thenReturn("view-name");
    when(view.getDescription()).thenReturn("view-description");
    when(view.isDefaultView()).thenReturn(true);
    when(view.getOrder()).thenReturn(1);
    when(view.isHidden()).thenReturn(true);
    when(view.getUpdatedAt()).thenReturn(new Date(1234567890L));
    when(view.getCreatedAt()).thenReturn(new Date(9876543210L));
    when(mockViewRepository.findAll()).thenReturn(singleton(view));
    List<ViewEntity> list = viewService.findAll();
    assertFalse(list.isEmpty());
    assertEquals("one element", 1, list.size());
    assertEquals("Id", "view-id", list.get(0).getId());
    assertEquals("Name", "view-name", list.get(0).getName());
    assertEquals("Description", "view-description", list.get(0).getDescription());
    assertEquals("Total APIs", 0, list.get(0).getTotalApis());
    assertEquals("default View", true, list.get(0).isDefaultView());
    assertEquals("Order", 1, list.get(0).getOrder());
    assertEquals("Hidden", true, list.get(0).isHidden());
    assertEquals("UpdatedAt", new Date(1234567890L), list.get(0).getUpdatedAt());
    assertEquals("CreatedAt", new Date(9876543210L), list.get(0).getCreatedAt());
    verify(mockViewRepository, times(1)).findAll();
}
Also used : ViewEntity(io.gravitee.management.model.ViewEntity) View(io.gravitee.repository.management.model.View) Date(java.util.Date) Test(org.junit.Test)

Example 3 with ViewEntity

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

the class ViewService_UpdateTest method shouldNotUpdateUnknownView.

@Test
public void shouldNotUpdateUnknownView() throws TechnicalException {
    UpdateViewEntity mockView = mock(UpdateViewEntity.class);
    when(mockView.getId()).thenReturn("unknown");
    when(mockViewRepository.findById("unknown")).thenReturn(Optional.empty());
    List<ViewEntity> list = viewService.update(singletonList(mockView));
    assertTrue(list.isEmpty());
    verify(mockViewRepository, times(1)).findById(any());
    verify(mockViewRepository, never()).update(any());
    verify(mockAuditService, never()).createPortalAuditLog(any(), eq(VIEW_UPDATED), any(), any(), any());
}
Also used : ViewEntity(io.gravitee.management.model.ViewEntity) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) Test(org.junit.Test)

Example 4 with ViewEntity

use of io.gravitee.management.model.ViewEntity 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 5 with ViewEntity

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

the class ViewService_UpdateTest method shouldUpdateView.

@Test
public void shouldUpdateView() throws TechnicalException {
    UpdateViewEntity mockView = mock(UpdateViewEntity.class);
    when(mockView.getId()).thenReturn("known");
    when(mockViewRepository.findById("known")).thenReturn(Optional.of(new View()));
    View updatedView = mock(View.class);
    when(updatedView.getId()).thenReturn("view-id");
    when(updatedView.getName()).thenReturn("view-name");
    when(updatedView.getDescription()).thenReturn("view-description");
    when(updatedView.isDefaultView()).thenReturn(true);
    when(updatedView.getOrder()).thenReturn(1);
    when(updatedView.isHidden()).thenReturn(true);
    when(updatedView.getUpdatedAt()).thenReturn(new Date(1234567890L));
    when(updatedView.getCreatedAt()).thenReturn(new Date(9876543210L));
    when(mockViewRepository.update(any())).thenReturn(updatedView);
    List<ViewEntity> list = viewService.update(singletonList(mockView));
    assertFalse(list.isEmpty());
    assertEquals("one element", 1, list.size());
    assertEquals("Id", "view-id", list.get(0).getId());
    assertEquals("Name", "view-name", list.get(0).getName());
    assertEquals("Description", "view-description", list.get(0).getDescription());
    assertEquals("Total APIs", 0, list.get(0).getTotalApis());
    assertEquals("default View", true, list.get(0).isDefaultView());
    assertEquals("Order", 1, list.get(0).getOrder());
    assertEquals("Hidden", true, list.get(0).isHidden());
    assertEquals("UpdatedAt", new Date(1234567890L), list.get(0).getUpdatedAt());
    assertEquals("CreatedAt", new Date(9876543210L), list.get(0).getCreatedAt());
    verify(mockViewRepository, times(1)).findById(any());
    verify(mockViewRepository, times(1)).update(any());
    verify(mockAuditService, times(1)).createPortalAuditLog(any(), eq(VIEW_UPDATED), any(), any(), any());
}
Also used : ViewEntity(io.gravitee.management.model.ViewEntity) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) View(io.gravitee.repository.management.model.View) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ViewEntity (io.gravitee.management.model.ViewEntity)7 UpdateViewEntity (io.gravitee.management.model.UpdateViewEntity)5 View (io.gravitee.repository.management.model.View)5 NewViewEntity (io.gravitee.management.model.NewViewEntity)4 Test (org.junit.Test)4 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Date (java.util.Date)2 IdGenerator (io.gravitee.common.utils.IdGenerator)1 ApiService (io.gravitee.management.service.ApiService)1 AuditService (io.gravitee.management.service.AuditService)1 ViewService (io.gravitee.management.service.ViewService)1 DuplicateViewNameException (io.gravitee.management.service.exceptions.DuplicateViewNameException)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