use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class EnvironmentAnalyticsResourceTest method shouldGetCountAnalyticsWhenNotAdminAndApp.
@Test
public void shouldGetCountAnalyticsWhenNotAdminAndApp() {
ApplicationListItem app = new ApplicationListItem();
app.setId("appId");
when(applicationService.findByUser(any())).thenReturn(Collections.singleton(app));
when(permissionService.hasPermission(APPLICATION_ANALYTICS, app.getId(), READ)).thenReturn(true);
Response response = envTarget().queryParam("type", "count").queryParam("field", "application").queryParam("interval", 1000).queryParam("to", 1000).request().get();
assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(HttpStatusCode.OK_200);
StatsAnalytics analytics = response.readEntity(StatsAnalytics.class);
assertThat(analytics.getAvg()).isNull();
assertThat(analytics.getCount()).isEqualTo(1);
}
use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method findByNameAndStatus.
@Override
public Set<ApplicationListItem> findByNameAndStatus(String userName, String name, String status) {
LOGGER.debug("Find applications by name {} and status {}", name, status);
try {
if (name == null || name.trim().isEmpty()) {
return emptySet();
}
// find applications where the user is a member
Set<String> appIds = membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userName, MembershipReferenceType.APPLICATION).stream().map(MembershipEntity::getReferenceId).collect(toSet());
// find user groups
List<String> groupIds = membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, userName, MembershipReferenceType.GROUP).stream().filter(m -> m.getRoleId() != null && roleService.findById(m.getRoleId()).getScope().equals(RoleScope.APPLICATION)).map(MembershipEntity::getReferenceId).collect(toList());
appIds.addAll(this.findByGroups(groupIds).stream().map(ApplicationListItem::getId).collect(toSet()));
ApplicationCriteria criteria = new ApplicationCriteria.Builder().status(ApplicationStatus.valueOf(status)).name(name.trim()).environmentIds(singletonList(GraviteeContext.getCurrentEnvironment())).ids(appIds.toArray(new String[0])).build();
Page<Application> applications = applicationRepository.search(criteria, null);
return ApplicationStatus.ACTIVE.equals(status) ? convertToList(new HashSet<>(applications.getContent())) : convertToSimpleList(new HashSet<>(applications.getContent()));
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find applications for name {}", name, ex);
throw new TechnicalManagementException("An error occurs while trying to find applications for name " + name, ex);
}
}
use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class UserServiceTest method shouldNotDeleteIfApplicationPO.
@Test
public void shouldNotDeleteIfApplicationPO() throws TechnicalException {
ApplicationListItem applicationListItem = mock(ApplicationListItem.class);
PrimaryOwnerEntity primaryOwnerEntity = mock(PrimaryOwnerEntity.class);
when(applicationListItem.getPrimaryOwner()).thenReturn(primaryOwnerEntity);
when(primaryOwnerEntity.getId()).thenReturn(USER_NAME);
when(applicationService.findByUser(USER_NAME)).thenReturn(Collections.singleton(applicationListItem));
try {
userService.delete(USER_NAME);
fail("should throw StillPrimaryOwnerException");
} catch (StillPrimaryOwnerException e) {
// success
verify(membershipService, never()).removeMemberMemberships(MembershipMemberType.USER, USER_NAME);
verify(userRepository, never()).update(any());
verify(searchEngineService, never()).delete(any(), eq(false));
}
}
use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class UserServiceTest method shouldCreateDefaultApplicationWhenNotExistingInEnvironment.
@Test
public void shouldCreateDefaultApplicationWhenNotExistingInEnvironment() throws TechnicalException {
setField(userService, "defaultApplicationForFirstConnection", true);
when(user.getLastConnectionAt()).thenReturn(null);
when(userRepository.findById(USER_NAME)).thenReturn(of(user));
EnvironmentEntity environment1 = new EnvironmentEntity();
environment1.setId("envId1");
EnvironmentEntity environment2 = new EnvironmentEntity();
environment2.setId("envId2");
EnvironmentEntity environment3 = new EnvironmentEntity();
environment3.setId("envId3");
when(environmentService.findByUser(any())).thenReturn(Arrays.asList(environment1, environment2, environment3));
ApplicationListItem defaultApp = new ApplicationListItem();
defaultApp.setName("Default application");
defaultApp.setDescription("My default application");
defaultApp.setType(ApplicationType.SIMPLE.name());
userService.connect(USER_NAME);
verify(applicationService, times(1)).create(any(), eq(USER_NAME), eq("envId2"));
verify(applicationService, times(1)).create(any(), eq(USER_NAME), eq("envId3"));
}
use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class PermissionsResource method getCurrentUserPermissions.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getCurrentUserPermissions(@QueryParam("apiId") String apiId, @QueryParam("applicationId") String applicationId) {
final String userId = getAuthenticatedUser();
if (apiId != null) {
ApiQuery apiQuery = new ApiQuery();
apiQuery.setIds(Collections.singletonList(apiId));
Set<ApiEntity> publishedByUser = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
ApiEntity apiEntity = publishedByUser.stream().filter(a -> a.getId().equals(apiId)).findFirst().orElseThrow(() -> new ApiNotFoundException(apiId));
Map<String, char[]> permissions;
permissions = membershipService.getUserMemberPermissions(apiEntity, userId);
return Response.ok(permissions).build();
} else if (applicationId != null) {
ApplicationListItem applicationListItem = applicationService.findByUser(getAuthenticatedUser()).stream().filter(a -> a.getId().equals(applicationId)).findFirst().orElseThrow(() -> new ApplicationNotFoundException(applicationId));
ApplicationEntity application = applicationService.findById(applicationListItem.getId());
Map<String, char[]> permissions;
permissions = membershipService.getUserMemberPermissions(application, userId);
return Response.ok(permissions).build();
}
throw new BadRequestException("One of the two parameters appId or applicationId must not be null.");
}
Aggregations