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);
}
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());
}
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());
}
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());
}
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();
}
Aggregations