Search in sources :

Example 1 with ApplicationListItem

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

the class FilteringServiceImpl method getCurrentUserSubscribedApis.

private FilteredEntities<ApiEntity> getCurrentUserSubscribedApis(Collection<ApiEntity> apis, boolean excluded) {
    // get Current user applications
    List<String> currentUserApplicationsId = applicationService.findByUser(getAuthenticatedUser().getUsername()).stream().map(ApplicationListItem::getId).collect(Collectors.toList());
    // find all subscribed apis for these applications
    SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
    subscriptionQuery.setApplications(currentUserApplicationsId);
    List<String> subscribedApis = subscriptionService.search(subscriptionQuery).stream().map(SubscriptionEntity::getApi).distinct().collect(Collectors.toList());
    // filter apis list with subscribed apis list
    return new FilteredEntities<>(apis.stream().filter(api -> (!excluded && subscribedApis.contains(api.getId())) || (excluded && !subscribedApis.contains(api.getId()))).sorted((a1, a2) -> String.CASE_INSENSITIVE_ORDER.compare(a1.getName(), a2.getName())).collect(Collectors.toList()), null);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) TopApiService(io.gravitee.rest.api.service.TopApiService) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) RatingService(io.gravitee.rest.api.service.RatingService) Autowired(org.springframework.beans.factory.annotation.Autowired) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) Component(org.springframework.stereotype.Component) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApplicationService(io.gravitee.rest.api.service.ApplicationService) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) FilteringService(io.gravitee.rest.api.service.filtering.FilteringService) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Example 2 with ApplicationListItem

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

the class ApplicationMapperTest method init.

@Before
public void init() {
    now = Instant.now();
    Date nowDate = Date.from(now);
    applicationEntity = new ApplicationEntity();
    applicationListItem = new ApplicationListItem();
    // init
    reset(groupService);
    reset(userService);
    reset(userMapper);
    GroupEntity grpEntity = new GroupEntity();
    grpEntity.setId(APPLICATION_GROUP_ID);
    grpEntity.setName(APPLICATION_GROUP_NAME);
    when(groupService.findById(APPLICATION_GROUP_ID)).thenReturn(grpEntity);
    UserEntity userEntity = Mockito.mock(UserEntity.class);
    when(userEntity.getDisplayName()).thenReturn(APPLICATION_USER_DISPLAYNAME);
    when(userEntity.getEmail()).thenReturn(APPLICATION_USER_EMAIL);
    when(userEntity.getId()).thenReturn(APPLICATION_USER_ID);
    when(userService.findById(APPLICATION_USER_ID)).thenReturn(userEntity);
    when(userMapper.convert(userEntity)).thenCallRealMethod();
    when(userMapper.computeUserLinks(anyString(), any())).thenCallRealMethod();
    PrimaryOwnerEntity primaryOwner = new PrimaryOwnerEntity(userEntity);
    when(uriInfo.getBaseUriBuilder()).thenReturn(UriBuilder.fromPath(""));
    applicationEntity.setCreatedAt(nowDate);
    applicationEntity.setDescription(APPLICATION_DESCRIPTION);
    applicationEntity.setGroups(new HashSet<String>(Arrays.asList(APPLICATION_GROUP_ID)));
    applicationEntity.setId(APPLICATION_ID);
    applicationEntity.setName(APPLICATION_NAME);
    applicationEntity.setPrimaryOwner(primaryOwner);
    applicationEntity.setStatus(APPLICATION_STATUS);
    applicationEntity.setType(APPLICATION_TYPE);
    applicationEntity.setUpdatedAt(nowDate);
    applicationListItem.setCreatedAt(nowDate);
    applicationListItem.setDescription(APPLICATION_DESCRIPTION);
    applicationListItem.setGroups(new HashSet<String>(Arrays.asList(APPLICATION_GROUP_ID)));
    applicationListItem.setId(APPLICATION_ID);
    applicationListItem.setName(APPLICATION_NAME);
    applicationListItem.setPrimaryOwner(primaryOwner);
    applicationListItem.setStatus(APPLICATION_STATUS);
    applicationListItem.setType(APPLICATION_TYPE);
    applicationListItem.setUpdatedAt(nowDate);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) GroupEntity(io.gravitee.rest.api.model.GroupEntity) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Before(org.junit.Before)

Example 3 with ApplicationListItem

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

the class ApplicationAlertServiceTest method shouldHandleNotificationTemplateUpdatedEventResponseTimeAlert.

@Test
public void shouldHandleNotificationTemplateUpdatedEventResponseTimeAlert() throws Exception {
    NotificationTemplateEntity notificationTemplate = new NotificationTemplateEntity();
    notificationTemplate.setHook(AlertHook.CONSUMER_RESPONSE_TIME.name());
    notificationTemplate.setTitle("notification-template-title");
    notificationTemplate.setContent("notification-template-content");
    NotificationTemplateEvent notificationTemplateEvent = new NotificationTemplateEvent("org-id", notificationTemplate);
    final SimpleEvent<ApplicationAlertEventType, Object> event = new SimpleEvent<>(ApplicationAlertEventType.NOTIFICATION_TEMPLATE_UPDATE, notificationTemplateEvent);
    ApplicationListItem app1 = new ApplicationListItem();
    app1.setId("app1");
    ApplicationListItem app2 = new ApplicationListItem();
    app2.setId("app2");
    when(applicationService.findByOrganization("org-id")).thenReturn(new HashSet<>(Arrays.asList(app1, app2)));
    final AlertTriggerEntity alertTrigger = mock(AlertTriggerEntity.class);
    when(alertTrigger.getType()).thenReturn("METRICS_AGGREGATION");
    when(alertService.findByReferenceAndReferenceIds(AlertReferenceType.APPLICATION, Arrays.asList("app1", "app2"))).thenReturn(Collections.singletonList(alertTrigger));
    Notification notification = new Notification();
    notification.setType("default-email");
    notification.setConfiguration("");
    when(alertTrigger.getNotifications()).thenReturn(Collections.singletonList(notification));
    JsonNode emailNode = JsonNodeFactory.instance.objectNode().put("to", "to").put("from", "from").put("subject", "subject").put("body", "body");
    when(mapper.readTree(notification.getConfiguration())).thenReturn(emailNode);
    cut.handleEvent(event);
    ArgumentCaptor<UpdateAlertTriggerEntity> updatingCaptor = ArgumentCaptor.forClass(UpdateAlertTriggerEntity.class);
    verify(alertService, times(1)).update(updatingCaptor.capture());
    final UpdateAlertTriggerEntity updating = updatingCaptor.getValue();
    assertThat(updating.getNotifications().get(0).getConfiguration()).contains("notification-template-content");
    assertThat(updating.getNotifications().get(0).getConfiguration()).contains("notification-template-title");
}
Also used : UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) SimpleEvent(io.gravitee.common.event.impl.SimpleEvent) NotificationTemplateEntity(io.gravitee.rest.api.model.notification.NotificationTemplateEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) UpdateAlertTriggerEntity(io.gravitee.rest.api.model.alert.UpdateAlertTriggerEntity) NewAlertTriggerEntity(io.gravitee.rest.api.model.alert.NewAlertTriggerEntity) AlertTriggerEntity(io.gravitee.rest.api.model.alert.AlertTriggerEntity) Notification(io.gravitee.notifier.api.Notification) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) NotificationTemplateEvent(io.gravitee.rest.api.model.notification.NotificationTemplateEvent) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) Test(org.junit.Test)

Example 4 with ApplicationListItem

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

the class ApplicationService_FindAllTest method shouldTryFindAll.

@Test
public void shouldTryFindAll() throws Exception {
    Application application = new Application();
    application.setId("appId");
    application.setType(ApplicationType.SIMPLE);
    application.setStatus(ApplicationStatus.ACTIVE);
    when(applicationRepository.findAllByEnvironment(eq("DEFAULT"), eq(ApplicationStatus.ACTIVE))).thenReturn(new HashSet<>(Collections.singletonList(application)));
    when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(new RoleEntity());
    when(membershipService.getMembershipsByReferencesAndRole(any(), any(), any())).thenReturn(new HashSet<>(Collections.singletonList(new MembershipEntity())));
    when(userService.findByIds(any())).thenReturn(Collections.emptySet());
    Set<ApplicationListItem> set = applicationService.findAll();
    assertThat(set).hasSize(1);
    verify(applicationRepository, times(1)).findAllByEnvironment("DEFAULT", ApplicationStatus.ACTIVE);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Application(io.gravitee.repository.management.model.Application) Test(org.junit.Test)

Example 5 with ApplicationListItem

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

the class ApplicationMapper method convert.

public Application convert(ApplicationListItem applicationListItem, UriInfo uriInfo) {
    final Application application = new Application();
    application.setApplicationType(applicationListItem.getType());
    application.setCreatedAt(applicationListItem.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    application.setDescription(applicationListItem.getDescription());
    Set<String> groupEntities = applicationListItem.getGroups();
    if (groupEntities != null && !groupEntities.isEmpty()) {
        List<Group> groups = groupEntities.stream().map(groupService::findById).map(groupEntity -> new Group().id(groupEntity.getId()).name(groupEntity.getName())).collect(Collectors.toList());
        application.setGroups(groups);
    }
    application.setId(applicationListItem.getId());
    application.setName(applicationListItem.getName());
    UserEntity primaryOwnerUserEntity = userService.findById(applicationListItem.getPrimaryOwner().getId());
    User owner = userMapper.convert(primaryOwnerUserEntity);
    owner.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), primaryOwnerUserEntity.getId()), primaryOwnerUserEntity.getUpdatedAt()));
    application.setOwner(owner);
    application.setUpdatedAt(applicationListItem.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    ApplicationSettings settings = applicationListItem.getSettings();
    application.setHasClientId(settings != null && ((settings.getoAuthClient() != null && settings.getoAuthClient().getClientId() != null && !settings.getoAuthClient().getClientId().isEmpty()) || (settings.getApp() != null && settings.getApp().getClientId() != null && !settings.getApp().getClientId().isEmpty())));
    return application;
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) OAuthClientSettings(io.gravitee.rest.api.model.application.OAuthClientSettings) Group(io.gravitee.rest.api.portal.rest.model.Group) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) GroupService(io.gravitee.rest.api.service.GroupService) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) User(io.gravitee.rest.api.portal.rest.model.User) Collectors(java.util.stream.Collectors) Application(io.gravitee.rest.api.portal.rest.model.Application) List(java.util.List) Component(org.springframework.stereotype.Component) OffsetDateTime(java.time.OffsetDateTime) UserService(io.gravitee.rest.api.service.UserService) ZoneOffset(java.time.ZoneOffset) UriInfo(javax.ws.rs.core.UriInfo) PortalApiLinkHelper.usersURL(io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper.usersURL) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationLinks(io.gravitee.rest.api.portal.rest.model.ApplicationLinks) Group(io.gravitee.rest.api.portal.rest.model.Group) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) User(io.gravitee.rest.api.portal.rest.model.User) Application(io.gravitee.rest.api.portal.rest.model.Application) UserEntity(io.gravitee.rest.api.model.UserEntity)

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