Search in sources :

Example 16 with ApiEntity

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

the class ApiRatingsResourceTest method shouldBadRequestWhileCreatingApiRatings.

@Test
public void shouldBadRequestWhileCreatingApiRatings() {
    // init
    ApiEntity userApi = new ApiEntity();
    userApi.setId("1");
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(userApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any());
    // test
    final Response response = target(API).path("ratings").request().post(Entity.json(null));
    assertEquals(BAD_REQUEST_400, response.getStatus());
    ErrorResponse errorResponse = response.readEntity(ErrorResponse.class);
    List<Error> errors = errorResponse.getErrors();
    assertNotNull(errors);
    assertEquals(1, errors.size());
    Error error = errors.get(0);
    assertNotNull(error);
    assertEquals("errors.unexpected", error.getCode());
    assertEquals("400", error.getStatus());
    assertEquals("Input must not be null.", error.getMessage());
}
Also used : Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with ApiEntity

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

the class ApiRatingsResourceTest method init.

@Before
public void init() throws IOException {
    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 rating1 = new RatingEntity();
    RatingEntity rating2 = new RatingEntity();
    List<RatingEntity> ratingEntityPage = Arrays.asList(rating1, rating2);
    doReturn(ratingEntityPage).when(ratingService).findByApi(eq(API));
    Rating rating = new Rating();
    rating.setId(RATING);
    rating.setComment(RATING);
    rating.setValue(1);
    doReturn(rating).when(ratingMapper).convert(any(), any());
    RatingEntity createdRating = new RatingEntity();
    createdRating.setId(RATING);
    createdRating.setComment(RATING);
    createdRating.setRate((byte) 1);
    doReturn(createdRating).when(ratingService).create(any());
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Collections.emptySet(java.util.Collections.emptySet) Assert.assertNotNull(org.junit.Assert.assertNotNull) RatingEntity(io.gravitee.rest.api.model.RatingEntity) io.gravitee.rest.api.portal.rest.model(io.gravitee.rest.api.portal.rest.model) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Error(io.gravitee.rest.api.portal.rest.model.Error) ApiRatingUnavailableException(io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException) Entity(javax.ws.rs.client.Entity) Collections.singletonList(java.util.Collections.singletonList) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) HashSet(java.util.HashSet) Mockito.doThrow(org.mockito.Mockito.doThrow) List(java.util.List) Response(javax.ws.rs.core.Response) 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)

Example 18 with ApiEntity

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

the class ApiResourceTest method shouldHaveNotFoundWhileGettingApiPicture.

@Test
public void shouldHaveNotFoundWhileGettingApiPicture() {
    // init
    ApiEntity userApi = new ApiEntity();
    userApi.setId("1");
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(userApi));
    doReturn(mockApis).when(apiService).findByUser(any(), any(), eq(true));
    // test
    final Response response = target(API).path("picture").request().get();
    assertEquals(NOT_FOUND_404, response.getStatus());
    ErrorResponse errorResponse = response.readEntity(ErrorResponse.class);
    List<Error> errors = errorResponse.getErrors();
    assertNotNull(errors);
    assertEquals(1, errors.size());
    Error error = errors.get(0);
    assertNotNull(error);
    assertEquals("errors.api.notFound", error.getCode());
    assertEquals("404", error.getStatus());
    assertEquals("Api [" + API + "] can not be found.", error.getMessage());
}
Also used : Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) Test(org.junit.Test)

Example 19 with ApiEntity

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

the class ApiResourceTest method shouldHaveNotFoundWhileGettingApi.

@Test
public void shouldHaveNotFoundWhileGettingApi() {
    // init
    ApiEntity userApi = new ApiEntity();
    userApi.setId("1");
    doReturn(false).when(accessControlService).canAccessApiFromPortal(API);
    // test
    final Response response = target(API).request().get();
    assertEquals(NOT_FOUND_404, response.getStatus());
    ErrorResponse errorResponse = response.readEntity(ErrorResponse.class);
    List<Error> errors = errorResponse.getErrors();
    assertNotNull(errors);
    assertEquals(1, errors.size());
    Error error = errors.get(0);
    assertNotNull(error);
    assertEquals("errors.api.notFound", error.getCode());
    assertEquals("404", error.getStatus());
    assertEquals("Api [" + API + "] can not be found.", error.getMessage());
}
Also used : Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) Test(org.junit.Test)

Example 20 with ApiEntity

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

the class ApisResourceNotAuthenticatedTest method init.

@Before
public void init() {
    resetAllMocks();
    ApiEntity publishedApi = new ApiEntity();
    publishedApi.setLifecycleState(ApiLifecycleState.PUBLISHED);
    publishedApi.setName("A");
    publishedApi.setId("A");
    ApiEntity unpublishedApi = new ApiEntity();
    unpublishedApi.setLifecycleState(ApiLifecycleState.UNPUBLISHED);
    unpublishedApi.setName("B");
    unpublishedApi.setId("B");
    ApiEntity anotherPublishedApi = new ApiEntity();
    anotherPublishedApi.setLifecycleState(ApiLifecycleState.PUBLISHED);
    anotherPublishedApi.setName("C");
    anotherPublishedApi.setId("C");
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(publishedApi, anotherPublishedApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(isNull(), any(ApiQuery.class));
    doReturn(new FilteredEntities<ApiEntity>(new ArrayList<>(mockApis), null)).when(filteringService).filterApis(any(), any(), any());
    doReturn(false).when(ratingService).isEnabled();
    doReturn(new Api().name("A").id("A")).when(apiMapper).convert(publishedApi);
    doReturn(new Api().name("B").id("B")).when(apiMapper).convert(unpublishedApi);
    doReturn(new Api().name("C").id("C")).when(apiMapper).convert(anotherPublishedApi);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ArrayList(java.util.ArrayList) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Api(io.gravitee.rest.api.portal.rest.model.Api) 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