Search in sources :

Example 31 with ApiEntity

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

the class ApiPageResourceNotAuthenticatedTest method init.

@Before
public void init() {
    resetAllMocks();
    mockApi = new ApiEntity();
    mockApi.setId(API);
    doReturn(mockApi).when(apiService).findById(API);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(isNull(), argThat(q -> singletonList(API).equals(q.getIds())));
    mockPage = new PageEntity();
    mockPage.setPublished(true);
    mockPage.setVisibility(Visibility.PUBLIC);
    doReturn(mockPage).when(pageService).findById(PAGE, null);
    mockAnotherPage = new PageEntity();
    mockAnotherPage.setPublished(true);
    mockAnotherPage.setVisibility(Visibility.PUBLIC);
    Map<String, String> metadataMap = new HashMap<>();
    metadataMap.put(ANOTHER_PAGE, ANOTHER_PAGE);
    mockAnotherPage.setMetadata(metadataMap);
    doReturn(mockAnotherPage).when(pageService).findById(ANOTHER_PAGE, null);
    doReturn(new Page()).when(pageMapper).convert(any(), any(), any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) SecurityContext(javax.ws.rs.core.SecurityContext) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Page(io.gravitee.rest.api.portal.rest.model.Page) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Collections.singletonList(java.util.Collections.singletonList) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Priority(javax.annotation.Priority) Principal(java.security.Principal) Response(javax.ws.rs.core.Response) Visibility(io.gravitee.rest.api.model.Visibility) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) PageEntity(io.gravitee.rest.api.model.PageEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Page(io.gravitee.rest.api.portal.rest.model.Page) Before(org.junit.Before)

Example 32 with ApiEntity

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

the class PermissionFilterTest method shouldBeAuthorizedWhenApiPermissions.

@Test
public void shouldBeAuthorizedWhenApiPermissions() {
    ApiEntity api = initApiMocks();
    when(roleService.hasPermission(any(), any(), any())).thenReturn(true);
    permissionFilter.filter(permissions, containerRequestContext);
    verify(apiService, times(1)).findById(api.getId());
    verify(applicationService, never()).findById(any());
    verify(roleService, times(1)).hasPermission(any(), any(), any());
    verify(membershipService, times(1)).getUserMemberPermissions(api, USERNAME);
    verify(membershipService, never()).getRoles(any(), any(), any(), any());
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Example 33 with ApiEntity

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

the class PermissionFilterTest method initApiMocks.

/**
 * API Tests
 */
private ApiEntity initApiMocks() {
    ApiEntity api = new ApiEntity();
    api.setId(API_ID);
    Principal user = () -> USERNAME;
    when(apiService.findById(api.getId())).thenReturn(api);
    when(securityContext.getUserPrincipal()).thenReturn(user);
    Permission perm = mock(Permission.class);
    when(perm.value()).thenReturn(RolePermission.API_ANALYTICS);
    when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
    when(permissions.value()).thenReturn(new Permission[] { perm });
    UriInfo uriInfo = mock(UriInfo.class);
    MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
    map.put("apiId", Collections.singletonList(api.getId()));
    when(uriInfo.getPathParameters()).thenReturn(map);
    when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
    return api;
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Permission(io.gravitee.rest.api.portal.rest.security.Permission) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Principal(java.security.Principal) UriInfo(javax.ws.rs.core.UriInfo)

Example 34 with ApiEntity

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

the class WebhookNotifierServiceImpl method toJson.

private String toJson(final Hook hook, final Map<String, Object> params) {
    JsonObject content = new JsonObject();
    // hook
    content.put("event", hook.name());
    content.put("scope", hook.getScope().name());
    // api
    if (params.containsKey(PARAM_API)) {
        Object api = params.get(PARAM_API);
        if (api != null) {
            JsonObject jsonApi = new JsonObject();
            jsonApi.put("id", api instanceof ApiModelEntity ? ((ApiModelEntity) api).getId() : ((ApiEntity) api).getId());
            jsonApi.put("name", api instanceof ApiModelEntity ? ((ApiModelEntity) api).getName() : ((ApiEntity) api).getName());
            jsonApi.put("version", api instanceof ApiModelEntity ? ((ApiModelEntity) api).getVersion() : ((ApiEntity) api).getVersion());
            content.put("api", jsonApi);
        }
    }
    // application
    if (params.containsKey(PARAM_APPLICATION)) {
        ApplicationEntity application = (ApplicationEntity) params.get(PARAM_APPLICATION);
        if (application != null) {
            JsonObject jsonApplication = new JsonObject();
            jsonApplication.put("id", application.getId());
            jsonApplication.put("name", application.getName());
            /*
                if (application.getType() != null) {
                    jsonApplication.put("type", application.getType());
                }
                */
            content.put("application", jsonApplication);
        }
    }
    // owner
    if (params.containsKey(PARAM_OWNER)) {
        PrimaryOwnerEntity owner = (PrimaryOwnerEntity) params.get(PARAM_OWNER);
        if (owner != null) {
            JsonObject jsonOwner = new JsonObject();
            jsonOwner.put("id", owner.getId());
            jsonOwner.put("username", owner.getDisplayName());
            content.put("owner", jsonOwner);
        }
    }
    // plan
    if (params.containsKey(PARAM_PLAN)) {
        PlanEntity plan = (PlanEntity) params.get(PARAM_PLAN);
        if (plan != null) {
            JsonObject jsonPlan = new JsonObject();
            jsonPlan.put("id", plan.getId());
            jsonPlan.put("name", plan.getName());
            jsonPlan.put("security", plan.getSecurity());
            content.put("plan", jsonPlan);
        }
    }
    // subscription
    if (params.containsKey(PARAM_SUBSCRIPTION)) {
        SubscriptionEntity subscription = (SubscriptionEntity) params.get(PARAM_SUBSCRIPTION);
        if (subscription != null) {
            JsonObject jsonSubscription = new JsonObject();
            jsonSubscription.put("id", subscription.getId());
            jsonSubscription.put("status", subscription.getStatus());
            content.put("subscription", jsonSubscription);
        }
    }
    return content.encode();
}
Also used : JsonObject(io.vertx.core.json.JsonObject) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) JsonObject(io.vertx.core.json.JsonObject)

Example 35 with ApiEntity

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

the class ApiSynchronizationProcessor method processCheckSynchronization.

public boolean processCheckSynchronization(ApiEntity deployedApi, ApiEntity apiToDeploy) {
    Class<ApiEntity> cl = ApiEntity.class;
    List<Object> requiredFieldsDeployedApi = new ArrayList<Object>();
    List<Object> requiredFieldsApiToDeploy = new ArrayList<Object>();
    for (Field f : cl.getDeclaredFields()) {
        if (f.getAnnotation(DeploymentRequired.class) != null) {
            boolean previousAccessibleState = f.isAccessible();
            f.setAccessible(true);
            try {
                requiredFieldsDeployedApi.add(f.get(deployedApi));
                requiredFieldsApiToDeploy.add(f.get(apiToDeploy));
            } catch (Exception e) {
                LOGGER.error("Error access API required deployment fields", e);
            } finally {
                f.setAccessible(previousAccessibleState);
            }
        }
    }
    try {
        String requiredFieldsDeployedApiDefinition = objectMapper.writeValueAsString(requiredFieldsDeployedApi);
        String requiredFieldsApiToDeployDefinition = objectMapper.writeValueAsString(requiredFieldsApiToDeploy);
        return requiredFieldsDeployedApiDefinition.equals(requiredFieldsApiToDeployDefinition);
    } catch (Exception e) {
        LOGGER.error("Unexpected error while generating API deployment required fields definition", e);
        return false;
    }
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) DeploymentRequired(io.gravitee.rest.api.model.DeploymentRequired)

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