Search in sources :

Example 6 with ApplicationListItem

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);
}
Also used : Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) StatsAnalytics(io.gravitee.rest.api.model.analytics.query.StatsAnalytics) Test(org.junit.Test)

Example 7 with ApplicationListItem

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);
    }
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ApplicationCriteria(io.gravitee.repository.management.api.search.ApplicationCriteria) UuidString(io.gravitee.rest.api.service.common.UuidString)

Example 8 with ApplicationListItem

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));
    }
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Test(org.junit.Test)

Example 9 with ApplicationListItem

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"));
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Test(org.junit.Test)

Example 10 with ApplicationListItem

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.");
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) MembershipService(io.gravitee.rest.api.service.MembershipService) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Set(java.util.Set) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) MediaType(io.gravitee.common.http.MediaType) QueryParam(javax.ws.rs.QueryParam) Response(javax.ws.rs.core.Response) Map(java.util.Map) ApplicationService(io.gravitee.rest.api.service.ApplicationService) BadRequestException(javax.ws.rs.BadRequestException) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Collections(java.util.Collections) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) BadRequestException(javax.ws.rs.BadRequestException) Map(java.util.Map) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)29 Test (org.junit.Test)16 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)11 Response (javax.ws.rs.core.Response)9 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)8 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)7 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 Collectors (java.util.stream.Collectors)6 ApplicationService (io.gravitee.rest.api.service.ApplicationService)5 java.util (java.util)5 MediaType (io.gravitee.common.http.MediaType)4 FilterableItem (io.gravitee.rest.api.model.filtering.FilterableItem)4 Inject (javax.inject.Inject)4 Before (org.junit.Before)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 SimpleEvent (io.gravitee.common.event.impl.SimpleEvent)3 UserEntity (io.gravitee.rest.api.model.UserEntity)3 AlertTriggerEntity (io.gravitee.rest.api.model.alert.AlertTriggerEntity)3 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)3 Application (io.gravitee.rest.api.portal.rest.model.Application)3