Search in sources :

Example 6 with ApplicationEntity

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

the class ApplicationService_FindByIdTest method shouldFindById.

@Test
public void shouldFindById() throws TechnicalException {
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    Membership po = new Membership(USER_NAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferenceAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
    when(userService.findByUsername(USER_NAME, false)).thenReturn(new UserEntity());
    final ApplicationEntity applicationEntity = applicationService.findById(APPLICATION_ID);
    assertNotNull(applicationEntity);
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 7 with ApplicationEntity

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

the class ApplicationService_FindByUserTest method shouldFindByUserAndGroup.

@Test
public void shouldFindByUserAndGroup() throws Exception {
    when(appMembership.getReferenceId()).thenReturn(APPLICATION_ID);
    when(groupAppMembership.getReferenceId()).thenReturn(GROUP_APPLICATION_ID);
    when(groupAppMembership.getRoles()).thenReturn(Collections.singletonMap(RoleScope.APPLICATION.getId(), "USER"));
    when(application.getId()).thenReturn(APPLICATION_ID);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(groupApplication.getId()).thenReturn(GROUP_APPLICATION_ID);
    when(groupApplication.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.APPLICATION)).thenReturn(Collections.singleton(appMembership));
    when(applicationRepository.findByIds(Collections.singletonList(APPLICATION_ID))).thenReturn(Collections.singleton(application));
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.GROUP)).thenReturn(Collections.singleton(groupAppMembership));
    when(applicationRepository.findByGroups(Collections.singletonList(GROUP_APPLICATION_ID), ApplicationStatus.ACTIVE)).thenReturn(Collections.singleton(groupApplication));
    Membership poApp = new Membership(USERNAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    poApp.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    Membership poGroupApp = new Membership(USERNAME, GROUP_APPLICATION_ID, MembershipReferenceType.APPLICATION);
    poGroupApp.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    Set<Membership> memberships = new HashSet<>();
    memberships.add(poApp);
    memberships.add(poGroupApp);
    when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(memberships);
    when(userService.findByUsername(USERNAME, false)).thenReturn(new UserEntity());
    Set<ApplicationEntity> apps = applicationService.findByUser(USERNAME);
    Assert.assertNotNull(apps);
    Assert.assertFalse("should find apps", apps.isEmpty());
    Assert.assertEquals(2, apps.size());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ApplicationEntity

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

the class ApplicationService_UpdateTest method shouldUpdate.

@Test
public void shouldUpdate() throws TechnicalException {
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(application.getName()).thenReturn(APPLICATION_NAME);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(existingApplication.getName()).thenReturn(APPLICATION_NAME);
    when(existingApplication.getDescription()).thenReturn("My description");
    when(applicationRepository.update(any())).thenReturn(application);
    Membership po = new Membership(USER_NAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
    final ApplicationEntity applicationEntity = applicationService.update(APPLICATION_ID, existingApplication);
    verify(applicationRepository).update(argThat(new ArgumentMatcher<Application>() {

        public boolean matches(Object argument) {
            final Application application = (Application) argument;
            return APPLICATION_NAME.equals(application.getName()) && application.getUpdatedAt() != null;
        }
    }));
    assertNotNull(applicationEntity);
    assertEquals(APPLICATION_NAME, applicationEntity.getName());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) UpdateApplicationEntity(io.gravitee.management.model.UpdateApplicationEntity) ArgumentMatcher(org.mockito.ArgumentMatcher) Membership(io.gravitee.repository.management.model.Membership) Application(io.gravitee.repository.management.model.Application) Test(org.junit.Test)

Example 9 with ApplicationEntity

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

the class PermissionFilterTest method shouldBeAuthorizedWhenApplicationPermissions.

@Test
public void shouldBeAuthorizedWhenApplicationPermissions() {
    ApplicationEntity application = initApplicationMocks();
    when(roleService.hasPermission(any(), any(), any())).thenReturn(true);
    permissionFilter.filter(permissions, containerRequestContext);
    verify(apiService, never()).findById(any());
    verify(applicationService, times(1)).findById(application.getId());
    verify(roleService, times(1)).hasPermission(any(), any(), any());
    verify(membershipService, times(1)).getMemberPermissions(application, USERNAME);
    verify(membershipService, never()).getRole(any(), any(), any(), any());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Test(org.junit.Test)

Example 10 with ApplicationEntity

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

the class PlatformAnalyticsResource method platformAnalytics.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = MANAGEMENT_PLATFORM, acls = READ) })
public Response platformAnalytics(@BeanParam AnalyticsParam analyticsParam) {
    analyticsParam.validate();
    Analytics analytics = null;
    // add filter by Apis or Applications
    String extraFilter = null;
    if (!isAdmin()) {
        if ("api".equals(analyticsParam.getField())) {
            extraFilter = getExtraFilter(analyticsParam.getField(), apiService.findByUser(getAuthenticatedUser()).stream().filter(api -> permissionService.hasPermission(API_ANALYTICS, api.getId(), READ)).map(ApiEntity::getId).collect(Collectors.toList()));
        } else if ("application".equals(analyticsParam.getField())) {
            extraFilter = getExtraFilter(analyticsParam.getField(), applicationService.findByUser(getAuthenticatedUser()).stream().filter(app -> permissionService.hasPermission(APPLICATION_ANALYTICS, app.getId(), READ)).map(ApplicationEntity::getId).collect(Collectors.toList()));
        }
    }
    switch(analyticsParam.getTypeParam().getValue()) {
        case DATE_HISTO:
            analytics = executeDateHisto(analyticsParam, extraFilter);
            break;
        case GROUP_BY:
            analytics = executeGroupBy(analyticsParam, extraFilter);
            break;
        case COUNT:
            analytics = executeCount(analyticsParam, extraFilter);
            break;
    }
    return Response.ok(analytics).build();
}
Also used : Analytics(io.gravitee.management.model.analytics.Analytics) io.gravitee.management.model.analytics.query(io.gravitee.management.model.analytics.query) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiEntity(io.gravitee.management.model.ApiEntity) ApiService(io.gravitee.management.service.ApiService) RolePermission(io.gravitee.management.model.permissions.RolePermission) Aggregation(io.gravitee.management.rest.resource.param.Aggregation) AnalyticsService(io.gravitee.management.service.AnalyticsService) ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Function(java.util.function.Function) Inject(javax.inject.Inject) Map(java.util.Map) Api(io.swagger.annotations.Api) Permission(io.gravitee.management.rest.security.Permission) AnalyticsParam(io.gravitee.management.rest.resource.param.AnalyticsParam) RolePermissionAction(io.gravitee.management.model.permissions.RolePermissionAction) API_ANALYTICS(io.gravitee.management.model.permissions.RolePermission.API_ANALYTICS) MANAGEMENT_PLATFORM(io.gravitee.management.model.permissions.RolePermission.MANAGEMENT_PLATFORM) Range(io.gravitee.management.rest.resource.param.Range) Permissions(io.gravitee.management.rest.security.Permissions) PermissionService(io.gravitee.management.service.PermissionService) Collectors(java.util.stream.Collectors) ApplicationService(io.gravitee.management.service.ApplicationService) BeanParam(javax.ws.rs.BeanParam) READ(io.gravitee.management.model.permissions.RolePermissionAction.READ) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) Response(javax.ws.rs.core.Response) APPLICATION_ANALYTICS(io.gravitee.management.model.permissions.RolePermission.APPLICATION_ANALYTICS) ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Analytics(io.gravitee.management.model.analytics.Analytics) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Permissions(io.gravitee.management.rest.security.Permissions)

Aggregations

ApplicationEntity (io.gravitee.management.model.ApplicationEntity)16 Test (org.junit.Test)9 Membership (io.gravitee.repository.management.model.Membership)6 UserEntity (io.gravitee.management.model.UserEntity)5 Permission (io.gravitee.management.rest.security.Permission)4 RolePermission (io.gravitee.management.model.permissions.RolePermission)3 Permissions (io.gravitee.management.rest.security.Permissions)3 MediaType (io.gravitee.common.http.MediaType)2 ApiEntity (io.gravitee.management.model.ApiEntity)2 UpdateApplicationEntity (io.gravitee.management.model.UpdateApplicationEntity)2 RolePermissionAction (io.gravitee.management.model.permissions.RolePermissionAction)2 ApplicationService (io.gravitee.management.service.ApplicationService)2 Application (io.gravitee.repository.management.model.Application)2 Api (io.swagger.annotations.Api)2 Principal (java.security.Principal)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2