Search in sources :

Example 21 with ApiEntity

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

the class ApiMapperTest method testMinimalConvert.

@Test
public void testMinimalConvert() {
    // init
    apiEntity = new ApiEntity();
    apiEntity.setId(API_ID);
    apiEntity.setDescription(API_DESCRIPTION);
    Proxy proxy = new Proxy();
    proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/foo")));
    apiEntity.setProxy(proxy);
    doReturn(false).when(ratingService).isEnabled();
    apiEntity.setCategories(new HashSet<>(Arrays.asList(API_CATEGORY, API_CATEGORY_HIDDEN)));
    doReturn(false).when(parameterService).findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
    apiEntity.setLifecycleState(ApiLifecycleState.CREATED);
    // Test
    Api responseApi = apiMapper.convert(apiEntity);
    assertNotNull(responseApi);
    assertNull(responseApi.getPages());
    assertNull(responseApi.getPlans());
    assertEquals(API_DESCRIPTION, responseApi.getDescription());
    assertEquals(API_ID, responseApi.getId());
    assertNull(responseApi.getName());
    assertNull(responseApi.getVersion());
    assertTrue(responseApi.getDraft());
    List<String> entrypoints = responseApi.getEntrypoints();
    assertNull(entrypoints);
    List<String> labels = responseApi.getLabels();
    assertNotNull(labels);
    assertEquals(0, labels.size());
    assertNull(responseApi.getOwner());
    List<String> categories = responseApi.getCategories();
    assertNotNull(categories);
    assertEquals(0, categories.size());
    RatingSummary ratingSummary = responseApi.getRatingSummary();
    assertNull(ratingSummary);
}
Also used : RatingSummary(io.gravitee.rest.api.portal.rest.model.RatingSummary) Proxy(io.gravitee.definition.model.Proxy) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) VirtualHost(io.gravitee.definition.model.VirtualHost) Api(io.gravitee.rest.api.portal.rest.model.Api) Test(org.junit.Test)

Example 22 with ApiEntity

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

the class ApiMapperTest method testConvert.

@Test
public void testConvert() {
    Instant now = Instant.now();
    Date nowDate = Date.from(now);
    // init
    apiEntity = new ApiEntity();
    apiEntity.setId(API_ID);
    apiEntity.setDescription(API_DESCRIPTION);
    apiEntity.setName(API_NAME);
    apiEntity.setLabels(new ArrayList<>(Arrays.asList(API_LABEL)));
    doThrow(CategoryNotFoundException.class).when(categoryService).findNotHiddenById(API_CATEGORY_HIDDEN);
    apiEntity.setCategories(new HashSet<>(Arrays.asList(API_CATEGORY, API_CATEGORY_HIDDEN)));
    apiEntity.setEntrypoints(Arrays.asList(new ApiEntrypointEntity(API_ENTRYPOINT_1), new ApiEntrypointEntity(API + "/foo")));
    Map<String, Object> metadata = new HashMap<>();
    metadata.put("meta", API);
    apiEntity.setMetadata(metadata);
    apiEntity.setVersion(API_VERSION);
    UserEntity ownerEntity = new UserEntity();
    ownerEntity.setId(API_OWNER_ID);
    ownerEntity.setEmail(API_OWNER_EMAIL);
    ownerEntity.setFirstname(API_OWNER_FIRSTNAME);
    ownerEntity.setLastname(API_OWNER_LASTNAME);
    apiEntity.setPrimaryOwner(new PrimaryOwnerEntity(ownerEntity));
    RatingSummaryEntity ratingSummaryEntity = new RatingSummaryEntity();
    ratingSummaryEntity.setAverageRate(Double.valueOf(4.2));
    ratingSummaryEntity.setNumberOfRatings(10);
    doReturn(true).when(ratingService).isEnabled();
    doReturn(ratingSummaryEntity).when(ratingService).findSummaryByApi(API_ID);
    doReturn(true).when(parameterService).findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
    Proxy proxy = new Proxy();
    proxy.setVirtualHosts(Collections.singletonList(new VirtualHost("/foo")));
    apiEntity.setProxy(proxy);
    apiEntity.setLifecycleState(ApiLifecycleState.PUBLISHED);
    apiEntity.setUpdatedAt(nowDate);
    // Test
    Api responseApi = apiMapper.convert(apiEntity);
    assertNotNull(responseApi);
    assertNull(responseApi.getPages());
    assertNull(responseApi.getPlans());
    assertEquals(API_DESCRIPTION, responseApi.getDescription());
    assertEquals(API_ID, responseApi.getId());
    assertEquals(API_NAME, responseApi.getName());
    assertEquals(API_VERSION, responseApi.getVersion());
    assertFalse(responseApi.getDraft());
    List<String> entrypoints = responseApi.getEntrypoints();
    assertNotNull(entrypoints);
    assertEquals(2, entrypoints.size());
    assertEquals(API_ENTRYPOINT_1, entrypoints.get(0));
    assertEquals(API + "/foo", entrypoints.get(1));
    List<String> labels = responseApi.getLabels();
    assertNotNull(labels);
    assertTrue(labels.contains(API_LABEL));
    User owner = responseApi.getOwner();
    assertNotNull(owner);
    assertEquals(API_OWNER_ID, owner.getId());
    assertEquals(API_OWNER_EMAIL, owner.getEmail());
    assertEquals(API_OWNER_FIRSTNAME + ' ' + API_OWNER_LASTNAME, owner.getDisplayName());
    assertEquals(now.toEpochMilli(), responseApi.getUpdatedAt().toInstant().toEpochMilli());
    List<String> categories = responseApi.getCategories();
    assertNotNull(categories);
    assertTrue(categories.contains(API_CATEGORY));
    RatingSummary ratingSummary = responseApi.getRatingSummary();
    assertNotNull(ratingSummary);
    assertEquals(Double.valueOf(4.2), ratingSummary.getAverage());
    assertEquals(BigDecimal.valueOf(10), ratingSummary.getCount());
}
Also used : RatingSummary(io.gravitee.rest.api.portal.rest.model.RatingSummary) ApiEntrypointEntity(io.gravitee.rest.api.model.api.ApiEntrypointEntity) User(io.gravitee.rest.api.portal.rest.model.User) Instant(java.time.Instant) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Proxy(io.gravitee.definition.model.Proxy) VirtualHost(io.gravitee.definition.model.VirtualHost) Api(io.gravitee.rest.api.portal.rest.model.Api) Test(org.junit.Test)

Example 23 with ApiEntity

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

the class ApiPagesResourceTest method init.

@Before
public void init() throws IOException {
    resetAllMocks();
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    doReturn(mockApi).when(apiService).findById(API);
    PageEntity markdownTemplate = new PageEntity();
    markdownTemplate.setType(PageType.MARKDOWN_TEMPLATE.name());
    doReturn(Arrays.asList(new PageEntity(), markdownTemplate)).when(pageService).search(any(), isNull(), eq(GraviteeContext.getCurrentEnvironment()));
    when(accessControlService.canAccessPageFromPortal(eq(API), any(PageEntity.class))).thenAnswer(invocationOnMock -> {
        PageEntity page = invocationOnMock.getArgument(1);
        return !PageType.MARKDOWN_TEMPLATE.name().equals(page.getType());
    });
    doReturn(new Page()).when(pageMapper).convert(any());
    doReturn(new PageLinks()).when(pageMapper).computePageLinks(any(), any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Before(org.junit.Before)

Example 24 with ApiEntity

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

the class ApiRatingAnswerResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
    RatingEntity ratingEntity = new RatingEntity();
    ratingEntity.setId(RATING);
    ratingEntity.setComment(RATING);
    ratingEntity.setApi(API);
    ratingEntity.setRate(Integer.valueOf(1).byteValue());
    RatingAnswerEntity answer = new RatingAnswerEntity();
    answer.setId(ANSWER);
    ratingEntity.setAnswers(Arrays.asList(answer));
    doReturn(ratingEntity).when(ratingService).findById(eq(RATING));
    doReturn(ratingEntity).when(ratingService).createAnswer(any());
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Assert.assertNotNull(org.junit.Assert.assertNotNull) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Set(java.util.Set) Test(org.junit.Test) Error(io.gravitee.rest.api.portal.rest.model.Error) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Entity(javax.ws.rs.client.Entity) Collections.singletonList(java.util.Collections.singletonList) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) HashSet(java.util.HashSet) List(java.util.List) RatingAnswerInput(io.gravitee.rest.api.portal.rest.model.RatingAnswerInput) Response(javax.ws.rs.core.Response) Rating(io.gravitee.rest.api.portal.rest.model.Rating) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) RatingAnswerEntity(io.gravitee.rest.api.model.RatingAnswerEntity) Before(org.junit.Before) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) RatingAnswerEntity(io.gravitee.rest.api.model.RatingAnswerEntity) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 25 with ApiEntity

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

the class ApiRatingResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
    RatingEntity ratingEntity = new RatingEntity();
    ratingEntity.setId(RATING);
    ratingEntity.setComment(RATING);
    ratingEntity.setApi(API);
    ratingEntity.setRate(Integer.valueOf(1).byteValue());
    doReturn(ratingEntity).when(ratingService).findById(eq(RATING));
    doReturn(ratingEntity).when(ratingService).update(any());
}
Also used : RatingInput(io.gravitee.rest.api.portal.rest.model.RatingInput) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Assert.assertNotNull(org.junit.Assert.assertNotNull) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Set(java.util.Set) Test(org.junit.Test) Error(io.gravitee.rest.api.portal.rest.model.Error) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) NO_CONTENT_204(io.gravitee.common.http.HttpStatusCode.NO_CONTENT_204) Entity(javax.ws.rs.client.Entity) Collections.singletonList(java.util.Collections.singletonList) HashSet(java.util.HashSet) List(java.util.List) Response(javax.ws.rs.core.Response) Rating(io.gravitee.rest.api.portal.rest.model.Rating) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)273 Test (org.junit.Test)180 Response (javax.ws.rs.core.Response)65 Before (org.junit.Before)46 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)35 Error (io.gravitee.rest.api.portal.rest.model.Error)32 java.util (java.util)30 Collectors (java.util.stream.Collectors)27 Mockito.doReturn (org.mockito.Mockito.doReturn)27 Mockito (org.mockito.Mockito)25 ArgumentMatchers (org.mockito.ArgumentMatchers)24 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)23 Assert (org.junit.Assert)22 Collections.singletonList (java.util.Collections.singletonList)21 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)20 IOException (java.io.IOException)20 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)19 Entity (javax.ws.rs.client.Entity)19 HashSet (java.util.HashSet)18 Api (io.gravitee.repository.management.model.Api)17