Search in sources :

Example 36 with PageEntity

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

the class PageService_CreateOrUpdatePagesTest method shouldCreateOrUpdatePages.

@Test
public void shouldCreateOrUpdatePages() throws TechnicalException {
    PageEntity page1 = new PageEntity();
    page1.setId(UuidString.generateRandom());
    page1.setName("Page 1");
    page1.setType("SWAGGER");
    page1.setReferenceType("API");
    page1.setReferenceId(API_ID);
    PageEntity page2 = new PageEntity();
    page2.setId(UuidString.generateRandom());
    page2.setName("Page 2");
    page2.setType("MARKDOWN");
    page2.setReferenceType("API");
    page2.setReferenceId(API_ID);
    PageEntity page3 = new PageEntity();
    page3.setId(UuidString.generateRandom());
    page3.setName("Sub Page 3");
    page3.setType("ASCIIDOC");
    page3.setParentId(page1.getId());
    page3.setReferenceType("API");
    page3.setReferenceId(API_ID);
    when(pageRepository.create(any(Page.class))).thenAnswer(returnsFirstArg());
    when(pageRepository.update(any(Page.class))).thenAnswer(returnsFirstArg());
    String page1NewId = UuidString.generateForEnvironment(ENVIRONMENT_ID, API_ID, page1.getId());
    Page page = new Page();
    page.setId(page1NewId);
    page.setName(page1.getName());
    page.setType(page1.getType());
    page.setReferenceType(PageReferenceType.valueOf(page1.getReferenceType()));
    page.setReferenceId(page1.getReferenceId());
    page.setVisibility("PUBLIC");
    // Simulate the fact that page 1 is already created
    when(pageRepository.findById(page1NewId)).thenReturn(Optional.of(page));
    when(pageRepository.findById(argThat(id -> !id.equals(page1NewId)))).thenThrow(new PageNotFoundException(""));
    when(planService.findByApi(anyString())).thenReturn(Collections.emptySet());
    pageService.createOrUpdatePages(List.of(page1, page2, page3), ENVIRONMENT_ID, API_ID);
    ArgumentCaptor<Page> createdPagesCaptor = ArgumentCaptor.forClass(Page.class);
    verify(pageRepository, times(2)).create(createdPagesCaptor.capture());
    List<Page> createdPages = createdPagesCaptor.getAllValues();
    assertThat(createdPages.size()).isEqualTo(2);
    assertThat(createdPages).extracting(Page::getName).containsExactly("Sub Page 3", "Page 2");
    // New parent id of page 3 must be new id of page 1
    assertThat(createdPages.get(0)).extracting(Page::getParentId).isEqualTo(page1NewId);
    assertThat(createdPages).extracting(Page::getId).doesNotContain(page2.getId(), page3.getId());
    ArgumentCaptor<Page> updatedPageCaptor = ArgumentCaptor.forClass(Page.class);
    verify(pageRepository, times(1)).update(updatedPageCaptor.capture());
    List<Page> updatedPages = updatedPageCaptor.getAllValues();
    assertThat(updatedPages.size()).isEqualTo(1);
    assertThat(updatedPages).extracting(Page::getName, Page::getId).containsExactly(tuple("Page 1", page1NewId));
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) PageEntity(io.gravitee.rest.api.model.PageEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) PageServiceImpl(io.gravitee.rest.api.service.impl.PageServiceImpl) org.mockito(org.mockito) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) AssertionsForClassTypes.tuple(org.assertj.core.api.AssertionsForClassTypes.tuple) AdditionalAnswers.returnsFirstArg(org.mockito.AdditionalAnswers.returnsFirstArg) PageReferenceType(io.gravitee.repository.management.model.PageReferenceType) Mockito(org.mockito.Mockito) List(java.util.List) Page(io.gravitee.repository.management.model.Page) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) PageRepository(io.gravitee.repository.management.api.PageRepository) Optional(java.util.Optional) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) Page(io.gravitee.repository.management.model.Page) UuidString(io.gravitee.rest.api.service.common.UuidString) Test(org.junit.Test)

Example 37 with PageEntity

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

the class PlanService_PublishTest method shouldPublish_WithPublishGCPage.

@Test
public void shouldPublish_WithPublishGCPage() throws TechnicalException {
    final String GC_PAGE_ID = "GC_PAGE_ID";
    when(plan.getStatus()).thenReturn(Plan.Status.STAGING);
    when(plan.getType()).thenReturn(Plan.PlanType.API);
    when(plan.getValidation()).thenReturn(Plan.PlanValidationType.AUTO);
    when(plan.getApi()).thenReturn(API_ID);
    when(plan.getGeneralConditions()).thenReturn(GC_PAGE_ID);
    when(planRepository.findById(PLAN_ID)).thenReturn(Optional.of(plan));
    when(planRepository.update(plan)).thenAnswer(returnsFirstArg());
    when(apiService.findById(API_ID)).thenReturn(apiEntity);
    PageEntity page = mock(PageEntity.class);
    when(page.getId()).thenReturn(GC_PAGE_ID);
    when(page.isPublished()).thenReturn(true);
    when(pageService.findById(page.getId())).thenReturn(page);
    planService.publish(PLAN_ID);
    verify(plan, times(1)).setStatus(Plan.Status.PUBLISHED);
    verify(planRepository, times(1)).update(plan);
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) Test(org.junit.Test)

Example 38 with PageEntity

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

the class PlanService_PublishTest method shouldNotPublish_WithNotPublishGCPage.

@Test(expected = PlanGeneralConditionStatusException.class)
public void shouldNotPublish_WithNotPublishGCPage() throws TechnicalException {
    final String GC_PAGE_ID = "GC_PAGE_ID";
    when(plan.getStatus()).thenReturn(Plan.Status.STAGING);
    when(plan.getType()).thenReturn(Plan.PlanType.API);
    when(plan.getValidation()).thenReturn(Plan.PlanValidationType.AUTO);
    when(plan.getApi()).thenReturn(API_ID);
    when(plan.getGeneralConditions()).thenReturn(GC_PAGE_ID);
    when(planRepository.findById(PLAN_ID)).thenReturn(Optional.of(plan));
    PageEntity page = mock(PageEntity.class);
    when(page.getId()).thenReturn(GC_PAGE_ID);
    when(page.isPublished()).thenReturn(false);
    when(pageService.findById(page.getId())).thenReturn(page);
    planService.publish(PLAN_ID);
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) Test(org.junit.Test)

Example 39 with PageEntity

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

the class PortalPagesResourceAnonymousTest method shouldGetPage_LoginOptional.

@Test
public void shouldGetPage_LoginOptional() {
    PageEntity page = new PageEntity();
    page.setPublished(true);
    page.setVisibility(Visibility.PUBLIC);
    doReturn(page).when(pageService).findById(any(), any());
    doReturn(false).when(configService).portalLoginForced();
// final Response response = envTarget(PAGE_NAME).request().get();
// 
// assertNotNull("Response should be present", response);
// assertEquals("Response should be 200", OK.getStatusCode(), response.getStatus());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) Test(org.junit.Test)

Example 40 with PageEntity

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

the class PortalPagesResourceAnonymousTest method shouldNotGetPage_LoginRequired.

@Test
public void shouldNotGetPage_LoginRequired() {
    PageEntity page = new PageEntity();
    page.setPublished(true);
    page.setVisibility(Visibility.PUBLIC);
    doReturn(page).when(pageService).findById(any(), any());
    doReturn(true).when(configService).portalLoginForced();
// final Response response = envTarget(PAGE_NAME).request().get();
// 
// assertNotNull("Response should be present", response);
// assertEquals("Response should be 401", UNAUTHORIZED.getStatusCode(), response.getStatus());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) Test(org.junit.Test)

Aggregations

PageEntity (io.gravitee.rest.api.model.PageEntity)67 Test (org.junit.Test)48 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)15 OAIDescriptor (io.gravitee.rest.api.service.swagger.OAIDescriptor)15 Page (io.gravitee.rest.api.portal.rest.model.Page)13 Response (javax.ws.rs.core.Response)13 UpdatePageEntity (io.gravitee.rest.api.model.UpdatePageEntity)9 PageConfigurationOAITransformer (io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)6 EntrypointsOAITransformer (io.gravitee.rest.api.service.impl.swagger.transformer.entrypoints.EntrypointsOAITransformer)6 SwaggerDescriptor (io.gravitee.rest.api.service.swagger.SwaggerDescriptor)5 Before (org.junit.Before)5 Page (io.gravitee.repository.management.model.Page)4 NewPageEntity (io.gravitee.rest.api.model.NewPageEntity)4 PageNotFoundException (io.gravitee.rest.api.service.exceptions.PageNotFoundException)4 ArgumentMatchers (org.mockito.ArgumentMatchers)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 PageRepository (io.gravitee.repository.management.api.PageRepository)3 PageReferenceType (io.gravitee.repository.management.model.PageReferenceType)3