Search in sources :

Example 1 with SearchEngineService

use of io.gravitee.rest.api.service.search.SearchEngineService 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)

Example 2 with SearchEngineService

use of io.gravitee.rest.api.service.search.SearchEngineService in project gravitee-management-rest-api by gravitee-io.

the class ApiService_UpdateTest method shouldNotDuplicateLabels.

@Test
public void shouldNotDuplicateLabels() throws TechnicalException {
    prepareUpdate();
    when(existingApi.getLabels()).thenReturn(asList("label1", "label1"));
    when(api.getDefinition()).thenReturn("{\"id\": \"" + API_ID + "\",\"name\": \"" + API_NAME + "\",\"proxy\": {\"context_path\": \"/old\"} ,\"labels\": [\"public\"]}");
    final ApiEntity apiEntity = apiService.update(API_ID, existingApi);
    verify(apiRepository).update(argThat(api -> api.getLabels().size() == 1));
    assertNotNull(apiEntity);
    verify(searchEngineService, times(1)).index(any(), eq(false));
}
Also used : API(io.gravitee.rest.api.model.WorkflowReferenceType.API) ApiLifecycleState(io.gravitee.rest.api.model.api.ApiLifecycleState) Services(io.gravitee.definition.model.services.Services) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Sets.newSet(org.mockito.internal.util.collections.Sets.newSet) RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ApiPermissionFilter(io.gravitee.rest.api.service.jackson.filter.ApiPermissionFilter) Arrays.asList(java.util.Arrays.asList) ApiRepository(io.gravitee.repository.management.api.ApiRepository) After(org.junit.After) Spy(org.mockito.Spy) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) AfterClass(org.junit.AfterClass) ApiLifecycleState(io.gravitee.repository.management.model.ApiLifecycleState) Reader(java.io.Reader) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Key(io.gravitee.rest.api.model.parameters.Key) Api(io.gravitee.repository.management.model.Api) SecurityContext(org.springframework.security.core.context.SecurityContext) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) HealthCheckService(io.gravitee.definition.model.services.healthcheck.HealthCheckService) Authentication(org.springframework.security.core.Authentication) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) SystemRole(io.gravitee.rest.api.model.permissions.SystemRole) ApiServiceImpl(io.gravitee.rest.api.service.impl.ApiServiceImpl) SearchEngineService(io.gravitee.rest.api.service.search.SearchEngineService) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) java.util(java.util) Mock(org.mockito.Mock) ApiPermission(io.gravitee.rest.api.model.permissions.ApiPermission) RunWith(org.junit.runner.RunWith) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) NotificationTemplateService(io.gravitee.rest.api.service.notification.NotificationTemplateService) Sets(org.mockito.internal.util.collections.Sets) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) io.gravitee.rest.api.model(io.gravitee.rest.api.model) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) io.gravitee.definition.model(io.gravitee.definition.model) GraviteeMapper(io.gravitee.definition.jackson.datatype.GraviteeMapper) Mockito(org.mockito.Mockito) io.gravitee.rest.api.service.exceptions(io.gravitee.rest.api.service.exceptions) Workflow(io.gravitee.repository.management.model.Workflow) PropertyFilter(com.fasterxml.jackson.databind.ser.PropertyFilter) Assert(org.junit.Assert) Collections(java.util.Collections) REVIEW(io.gravitee.rest.api.model.WorkflowType.REVIEW) UpdateApiEntity(io.gravitee.rest.api.model.api.UpdateApiEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)2 SearchEngineService (io.gravitee.rest.api.service.search.SearchEngineService)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 InjectMocks (org.mockito.InjectMocks)2 Mock (org.mockito.Mock)2 Mockito (org.mockito.Mockito)2 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 PropertyFilter (com.fasterxml.jackson.databind.ser.PropertyFilter)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 GraviteeMapper (io.gravitee.definition.jackson.datatype.GraviteeMapper)1 io.gravitee.definition.model (io.gravitee.definition.model)1 HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)1 Services (io.gravitee.definition.model.services.Services)1 HealthCheckService (io.gravitee.definition.model.services.healthcheck.HealthCheckService)1 PluginManager (io.gravitee.plugin.core.api.PluginManager)1 FetcherPlugin (io.gravitee.plugin.fetcher.FetcherPlugin)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1