Search in sources :

Example 1 with PageSource

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

the class PageService_AutoFetchTest method shouldNotFetch_Descriptor_AutoFetch.

@Test
public void shouldNotFetch_Descriptor_AutoFetch() throws Exception {
    PageSource pageSource = new PageSource();
    pageSource.setType("type");
    pageSource.setConfiguration("{\"autoFetch\": true, \"fetchCron\" : \"* * * * * *\"}");
    when(mockRootPage.getSource()).thenReturn(pageSource);
    when(mockRootPage.getReferenceId()).thenReturn("apiid");
    when(mockRootPage.getType()).thenReturn(PageType.ROOT.name());
    when(mockRootPage.getUpdatedAt()).thenReturn(new Date(Instant.now().minus(2, ChronoUnit.SECONDS).toEpochMilli()));
    when(mockRootPage.getVisibility()).thenReturn("PUBLIC");
    when(mockPage.getSource()).thenReturn(pageSource);
    when(mockPage.getId()).thenReturn("someid");
    when(mockPage.getReferenceId()).thenReturn("apiid");
    when(mockPage.getReferenceType()).thenReturn(PageReferenceType.API);
    when(mockPage.getType()).thenReturn(PageType.MARKDOWN.name());
    when(mockPage.getUpdatedAt()).thenReturn(new Date(Instant.now().minus(2, ChronoUnit.SECONDS).toEpochMilli()));
    when(mockPage.getVisibility()).thenReturn("PUBLIC");
    when(pageRepository.findById(any())).thenReturn(Optional.of(mockPage));
    when(pageRepository.update(any())).thenReturn(mockPage);
    when(pageRepository.search(any())).thenReturn(Arrays.asList(mockRootPage)).thenReturn(Arrays.asList(mockPage));
    FetcherPlugin fetcherPlugin = mock(FetcherPlugin.class);
    when(fetcherPlugin.clazz()).thenReturn("io.gravitee.rest.api.service.PageService_ImportAutFetchDescriptorMockFetcher");
    when(fetcherPlugin.configuration()).thenReturn(PageService_MockAutoFetchDescriptorFetcherConfiguration.class);
    when(fetcherPluginManager.get(any())).thenReturn(fetcherPlugin);
    Class<PageService_ImportAutFetchDescriptorMockFetcher> mockFetcherClass = PageService_ImportAutFetchDescriptorMockFetcher.class;
    when(fetcherPlugin.fetcher()).thenReturn(mockFetcherClass);
    PageService_MockAutoFetchDescriptorFetcherConfiguration fetcherConfiguration = new PageService_MockAutoFetchDescriptorFetcherConfiguration();
    when(fetcherConfigurationFactory.create(eq(PageService_MockAutoFetchDescriptorFetcherConfiguration.class), anyString())).thenReturn(fetcherConfiguration);
    AutowireCapableBeanFactory mockAutowireCapableBeanFactory = mock(AutowireCapableBeanFactory.class);
    when(applicationContext.getAutowireCapableBeanFactory()).thenReturn(mockAutowireCapableBeanFactory);
    when(pageRepository.update(any())).thenReturn(mockPage);
    when(graviteeDescriptorService.descriptorName()).thenReturn(".gravitee.json");
    when(graviteeDescriptorService.read(anyString())).thenCallRealMethod();
    pageService.execAutoFetch(GraviteeContext.getCurrentEnvironment());
    verify(pageRepository, times(6)).update(any());
}
Also used : FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) Date(java.util.Date) PageSource(io.gravitee.repository.management.model.PageSource) Test(org.junit.Test)

Example 2 with PageSource

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

the class PageService_AutoFetchTest method shouldNotFetch_SourcePage_AutoFetch_NotRequiredYet.

@Test
public void shouldNotFetch_SourcePage_AutoFetch_NotRequiredYet() throws Exception {
    PageSource pageSource = new PageSource();
    pageSource.setType("type");
    pageSource.setConfiguration("{\"autoFetch\": true, \"fetchCron\" : \"* 10 * * * *\"}");
    when(mockPage.getSource()).thenReturn(pageSource);
    when(mockPage.getUpdatedAt()).thenReturn(new Date(Instant.now().toEpochMilli()));
    when(pageRepository.search(any())).thenReturn(Arrays.asList(mockPage));
    FetcherPlugin fetcherPlugin = mock(FetcherPlugin.class);
    when(fetcherPlugin.clazz()).thenReturn("io.gravitee.rest.api.service.PageService_ImportSimplePageMockFetcher");
    when(fetcherPlugin.configuration()).thenReturn(PageService_MockSinglePageFetcherConfiguration.class);
    when(fetcherPluginManager.get(any())).thenReturn(fetcherPlugin);
    Class<PageService_ImportSimplePageMockFetcher> mockFetcherClass = PageService_ImportSimplePageMockFetcher.class;
    when(fetcherPlugin.fetcher()).thenReturn(mockFetcherClass);
    PageService_MockSinglePageFetcherConfiguration fetcherConfiguration = new PageService_MockSinglePageFetcherConfiguration();
    when(fetcherConfigurationFactory.create(eq(PageService_MockSinglePageFetcherConfiguration.class), anyString())).thenReturn(fetcherConfiguration);
    AutowireCapableBeanFactory mockAutowireCapableBeanFactory = mock(AutowireCapableBeanFactory.class);
    when(applicationContext.getAutowireCapableBeanFactory()).thenReturn(mockAutowireCapableBeanFactory);
    PageService_MockSinglePageFetcherConfiguration.forceCronValue("* 10 * * * *");
    long pages = pageService.execAutoFetch(GraviteeContext.getCurrentEnvironment());
    assertEquals(0, pages);
    verify(pageRepository, times(0)).update(any());
    verify(pageRepository, times(0)).create(any());
}
Also used : FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) Date(java.util.Date) PageSource(io.gravitee.repository.management.model.PageSource) Test(org.junit.Test)

Example 3 with PageSource

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

the class PageService_AutoFetchTest method shouldNotFetch_SourcePage_NoAutoFetch.

@Test
public void shouldNotFetch_SourcePage_NoAutoFetch() throws Exception {
    PageSource pageSource = new PageSource();
    pageSource.setType("type");
    pageSource.setConfiguration("{}");
    when(mockPage.getSource()).thenReturn(pageSource);
    when(pageRepository.search(any())).thenReturn(Arrays.asList(mockPage));
    FetcherPlugin fetcherPlugin = mock(FetcherPlugin.class);
    when(fetcherPlugin.clazz()).thenReturn("io.gravitee.rest.api.service.PageService_ImportDescriptorMockFetcher");
    when(fetcherPlugin.configuration()).thenReturn(PageService_MockDescriptorFetcherConfiguration.class);
    when(fetcherPluginManager.get(any())).thenReturn(fetcherPlugin);
    Class<PageService_ImportDescriptorMockFetcher> mockFetcherClass = PageService_ImportDescriptorMockFetcher.class;
    when(fetcherPlugin.fetcher()).thenReturn(mockFetcherClass);
    PageService_MockDescriptorFetcherConfiguration fetcherConfiguration = new PageService_MockDescriptorFetcherConfiguration();
    when(fetcherConfigurationFactory.create(eq(PageService_MockDescriptorFetcherConfiguration.class), anyString())).thenReturn(fetcherConfiguration);
    AutowireCapableBeanFactory mockAutowireCapableBeanFactory = mock(AutowireCapableBeanFactory.class);
    when(applicationContext.getAutowireCapableBeanFactory()).thenReturn(mockAutowireCapableBeanFactory);
    long pages = pageService.execAutoFetch(GraviteeContext.getCurrentEnvironment());
    assertEquals(0, pages);
    verify(pageRepository, times(0)).update(any());
    verify(pageRepository, times(0)).create(any());
}
Also used : FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) PageSource(io.gravitee.repository.management.model.PageSource) Test(org.junit.Test)

Example 4 with PageSource

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

the class PageService_ImportDescriptorTest method shouldImportDescriptor.

@Test
public void shouldImportDescriptor() throws Exception {
    // We mock the validateSafeContent method because the fetcher keeps sending the same json descriptor which is
    // not a swagger valid document (and modify the fetcher mock to produce valid desc is overkill)
    when(pageService.validateSafeContent(any(), any())).thenReturn(new ArrayList<>());
    PageSourceEntity pageSource = new PageSourceEntity();
    pageSource.setType("type");
    pageSource.setConfiguration(mapper.readTree("{}"));
    ImportPageEntity pageEntity = new ImportPageEntity();
    pageEntity.setSource(pageSource);
    FetcherPlugin fetcherPlugin = mock(FetcherPlugin.class);
    when(fetcherPlugin.clazz()).thenReturn("io.gravitee.rest.api.service.PageService_ImportDescriptorMockFetcher");
    when(fetcherPlugin.configuration()).thenReturn(PageService_MockDescriptorFetcherConfiguration.class);
    when(fetcherPluginManager.get(any())).thenReturn(fetcherPlugin);
    Class<PageService_ImportDescriptorMockFetcher> mockFetcherClass = PageService_ImportDescriptorMockFetcher.class;
    when(fetcherPlugin.fetcher()).thenReturn(mockFetcherClass);
    PageService_MockDescriptorFetcherConfiguration fetcherConfiguration = new PageService_MockDescriptorFetcherConfiguration();
    when(fetcherConfigurationFactory.create(eq(PageService_MockDescriptorFetcherConfiguration.class), anyString())).thenReturn(fetcherConfiguration);
    AutowireCapableBeanFactory mockAutowireCapableBeanFactory = mock(AutowireCapableBeanFactory.class);
    when(applicationContext.getAutowireCapableBeanFactory()).thenReturn(mockAutowireCapableBeanFactory);
    Page newPage = mock(Page.class);
    PageSource ps = new PageSource();
    ps.setType(pageSource.getType());
    ps.setConfiguration(pageSource.getConfiguration());
    when(newPage.getId()).thenReturn(UuidString.generateRandom());
    when(newPage.getSource()).thenReturn(ps);
    when(newPage.getType()).thenReturn("MARKDOWN");
    when(newPage.getReferenceType()).thenReturn(PageReferenceType.ENVIRONMENT);
    when(newPage.getReferenceId()).thenReturn("envId");
    when(newPage.getVisibility()).thenReturn("PUBLIC");
    when(pageRepository.create(any())).thenReturn(newPage);
    when(graviteeDescriptorService.descriptorName()).thenReturn(".gravitee.json");
    when(graviteeDescriptorService.read(anyString())).thenCallRealMethod();
    List<PageEntity> pageEntities = pageService.importFiles(pageEntity, GraviteeContext.getCurrentEnvironment());
    assertNotNull(pageEntities);
    assertEquals(8, pageEntities.size());
    verify(pageRevisionService, times(6)).create(any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ImportPageEntity(io.gravitee.rest.api.model.ImportPageEntity) PageSourceEntity(io.gravitee.rest.api.model.PageSourceEntity) FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) Page(io.gravitee.repository.management.model.Page) ImportPageEntity(io.gravitee.rest.api.model.ImportPageEntity) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) PageSource(io.gravitee.repository.management.model.PageSource) Test(org.junit.Test)

Example 5 with PageSource

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

the class PageService_ImportDirectoryTest method shouldImportDirectory.

@Test
public void shouldImportDirectory() throws Exception {
    // We mock the validateSafeContent method because the fetcher keeps sending the same json descriptor which is
    // not a swagger valid document (and modify the fetcher mock to produce valid desc is overkill)
    when(pageService.validateSafeContent(any(), any())).thenReturn(new ArrayList<>());
    PageSourceEntity pageSource = new PageSourceEntity();
    pageSource.setType("type");
    pageSource.setConfiguration(mapper.readTree("{}"));
    ImportPageEntity pageEntity = new ImportPageEntity();
    pageEntity.setSource(pageSource);
    pageEntity.setPublished(true);
    FetcherPlugin fetcherPlugin = mock(FetcherPlugin.class);
    when(fetcherPlugin.clazz()).thenReturn("io.gravitee.rest.api.service.PageService_ImportDirectoryMockFetcher");
    when(fetcherPlugin.configuration()).thenReturn(PageService_MockFilesFetcherConfiguration.class);
    when(fetcherPluginManager.get(any())).thenReturn(fetcherPlugin);
    Class<PageService_ImportDirectoryMockFetcher> mockFetcherClass = PageService_ImportDirectoryMockFetcher.class;
    when(fetcherPlugin.fetcher()).thenReturn(mockFetcherClass);
    PageService_MockFilesFetcherConfiguration fetcherConfiguration = new PageService_MockFilesFetcherConfiguration();
    when(fetcherConfigurationFactory.create(eq(PageService_MockFilesFetcherConfiguration.class), anyString())).thenReturn(fetcherConfiguration);
    AutowireCapableBeanFactory mockAutowireCapableBeanFactory = mock(AutowireCapableBeanFactory.class);
    when(applicationContext.getAutowireCapableBeanFactory()).thenReturn(mockAutowireCapableBeanFactory);
    Page newPage = mock(Page.class);
    PageSource ps = new PageSource();
    ps.setType(pageSource.getType());
    ps.setConfiguration(pageSource.getConfiguration());
    when(newPage.getId()).thenReturn(UuidString.generateRandom());
    when(newPage.isPublished()).thenReturn(Boolean.TRUE);
    when(newPage.getSource()).thenReturn(ps);
    when(newPage.getType()).thenReturn("MARKDOWN");
    when(newPage.getReferenceType()).thenReturn(PageReferenceType.ENVIRONMENT);
    when(newPage.getReferenceId()).thenReturn("envId");
    when(newPage.getVisibility()).thenReturn("PUBLIC");
    when(pageRepository.create(any())).thenReturn(newPage);
    when(graviteeDescriptorService.descriptorName()).thenReturn(".gravitee.json");
    List<PageEntity> pageEntities = pageService.importFiles(pageEntity, GraviteeContext.getCurrentEnvironment());
    assertNotNull(pageEntities);
    assertEquals(8, pageEntities.size());
    verify(searchEngineService, times(8)).index(any(), eq(false));
    // //////////////////////
    // check Folder creation
    // //////////////////////
    verify(pageRepository, times(3)).create(argThat(pageToCreate -> "FOLDER".equals(pageToCreate.getType())));
    // /src
    verify(pageRepository).create(argThat(pageToCreate -> "src".equals(pageToCreate.getName()) && "FOLDER".equals(pageToCreate.getType()) && null == pageToCreate.getParentId()));
    // /src/doc
    verify(pageRepository).create(argThat(pageToCreate -> "doc".equals(pageToCreate.getName()) && "FOLDER".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
    // /src/folder.with.dot/
    verify(pageRepository).create(argThat(pageToCreate -> "folder.with.dot".equals(pageToCreate.getName()) && "FOLDER".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
    // //////////////////////
    // verify files creation
    // //////////////////////
    verify(pageRepository, times(5)).create(argThat(pageToCreate -> pageToCreate.getType() != null && !"FOLDER".equals(pageToCreate.getType())));
    // /src/doc/m1.md
    verify(pageRepository).create(argThat(pageToCreate -> "m1".equals(pageToCreate.getName()) && "MARKDOWN".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
    // /swagger.json
    verify(pageRepository).create(argThat(pageToCreate -> "swagger".equals(pageToCreate.getName()) && "SWAGGER".equals(pageToCreate.getType()) && null == pageToCreate.getParentId()));
    // /src/doc/sub.m11.md
    verify(pageRepository).create(argThat(pageToCreate -> "sub.m11".equals(pageToCreate.getName()) && "MARKDOWN".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
    // /src/doc/m2.yaml
    verify(pageRepository).create(argThat(pageToCreate -> "m2".equals(pageToCreate.getName()) && "SWAGGER".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
    // /src/folder.with.dot/m2.MD
    verify(pageRepository).create(argThat(pageToCreate -> "m2".equals(pageToCreate.getName()) && "MARKDOWN".equals(pageToCreate.getType()) && null != pageToCreate.getParentId()));
    verify(pageRevisionService, times(5)).create(any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ImportPageEntity(io.gravitee.rest.api.model.ImportPageEntity) PageEntity(io.gravitee.rest.api.model.PageEntity) PageServiceImpl(io.gravitee.rest.api.service.impl.PageServiceImpl) ArgumentMatchers(org.mockito.ArgumentMatchers) PageSource(io.gravitee.repository.management.model.PageSource) ImportConfiguration(io.gravitee.rest.api.service.spring.ImportConfiguration) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) ImportPageEntity(io.gravitee.rest.api.model.ImportPageEntity) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) ArrayList(java.util.ArrayList) PageReferenceType(io.gravitee.repository.management.model.PageReferenceType) FetcherConfigurationFactory(io.gravitee.rest.api.fetcher.FetcherConfigurationFactory) PageRepository(io.gravitee.repository.management.api.PageRepository) InjectMocks(org.mockito.InjectMocks) UuidString(io.gravitee.rest.api.service.common.UuidString) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PluginManager(io.gravitee.plugin.core.api.PluginManager) Test(org.junit.Test) ApplicationContext(org.springframework.context.ApplicationContext) FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) Mockito(org.mockito.Mockito) List(java.util.List) Page(io.gravitee.repository.management.model.Page) Visibility(io.gravitee.rest.api.model.Visibility) PageSourceEntity(io.gravitee.rest.api.model.PageSourceEntity) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) SearchEngineService(io.gravitee.rest.api.service.search.SearchEngineService) Page(io.gravitee.repository.management.model.Page) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) PageSource(io.gravitee.repository.management.model.PageSource) PageSourceEntity(io.gravitee.rest.api.model.PageSourceEntity) FetcherPlugin(io.gravitee.plugin.fetcher.FetcherPlugin) ImportPageEntity(io.gravitee.rest.api.model.ImportPageEntity) Test(org.junit.Test)

Aggregations

FetcherPlugin (io.gravitee.plugin.fetcher.FetcherPlugin)6 PageSource (io.gravitee.repository.management.model.PageSource)6 Test (org.junit.Test)6 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)6 Date (java.util.Date)3 Page (io.gravitee.repository.management.model.Page)2 ImportPageEntity (io.gravitee.rest.api.model.ImportPageEntity)2 PageEntity (io.gravitee.rest.api.model.PageEntity)2 PageSourceEntity (io.gravitee.rest.api.model.PageSourceEntity)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PluginManager (io.gravitee.plugin.core.api.PluginManager)1 PageRepository (io.gravitee.repository.management.api.PageRepository)1 PageReferenceType (io.gravitee.repository.management.model.PageReferenceType)1 FetcherConfigurationFactory (io.gravitee.rest.api.fetcher.FetcherConfigurationFactory)1 Visibility (io.gravitee.rest.api.model.Visibility)1 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 PageServiceImpl (io.gravitee.rest.api.service.impl.PageServiceImpl)1 SearchEngineService (io.gravitee.rest.api.service.search.SearchEngineService)1 ImportConfiguration (io.gravitee.rest.api.service.spring.ImportConfiguration)1