Search in sources :

Example 11 with PrimaryOwnerEntity

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

the class ApiSubscribersResourceTest method shouldGetApiSubscribersNotAsPrimaryOwner.

@Test
public void shouldGetApiSubscribersNotAsPrimaryOwner() {
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    UserEntity user = new UserEntity();
    user.setId("ANOTHER_NAME");
    PrimaryOwnerEntity primaryOwner = new PrimaryOwnerEntity(user);
    mockApi.setPrimaryOwner(primaryOwner);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
    TopHitsAnalytics mockAnalytics = new TopHitsAnalytics();
    Map<String, Long> mockedValues = new HashMap<>();
    mockedValues.put("A", 10L);
    mockedValues.put("B", 20L);
    mockedValues.put("C", 30L);
    mockAnalytics.setValues(mockedValues);
    doReturn(mockAnalytics).when(analyticsService).execute(any(GroupByQuery.class));
    SubscriptionEntity subA1 = new SubscriptionEntity();
    subA1.setApplication("A");
    subA1.setApi(API);
    SubscriptionEntity subC1 = new SubscriptionEntity();
    subC1.setApplication("C");
    subC1.setApi(API);
    doReturn(Arrays.asList(subA1, subC1)).when(subscriptionService).search(any());
    ApplicationEntity appA = new ApplicationEntity();
    appA.setId("A");
    doReturn(appA).when(applicationService).findById("A");
    doReturn(new Application().id("A")).when(applicationMapper).convert(eq(appA), any());
    ApplicationEntity appC = new ApplicationEntity();
    appC.setId("C");
    doReturn(appC).when(applicationService).findById("C");
    doReturn(new Application().id("C")).when(applicationMapper).convert(eq(appC), any());
    ApplicationListItem appLIA = new ApplicationListItem();
    appLIA.setId("A");
    ApplicationListItem appLIC = new ApplicationListItem();
    appLIC.setId("C");
    doReturn(new HashSet<>(Arrays.asList(appLIA, appLIC))).when(applicationService).findByUser(USER_NAME);
    final Response response = target(API).path("subscribers").request().get();
    assertEquals(OK_200, response.getStatus());
    ArgumentCaptor<SubscriptionQuery> queryCaptor = ArgumentCaptor.forClass(SubscriptionQuery.class);
    Mockito.verify(subscriptionService).search(queryCaptor.capture());
    SubscriptionQuery value = queryCaptor.getValue();
    assertNotNull(value.getApplications());
    assertEquals(2, value.getApplications().size());
    assertTrue(value.getApplications().contains("A"));
    assertTrue(value.getApplications().contains("C"));
    final ApplicationsResponse applicationsResponse = response.readEntity(ApplicationsResponse.class);
    assertNotNull(applicationsResponse);
    assertEquals(2, applicationsResponse.getData().size());
    assertEquals("C", applicationsResponse.getData().get(0).getId());
    assertEquals("A", applicationsResponse.getData().get(1).getId());
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) Collections.singletonList(java.util.Collections.singletonList) Application(io.gravitee.rest.api.portal.rest.model.Application) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Collections.emptySet(java.util.Collections.emptySet) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) IOException(java.io.IOException) Test(org.junit.Test) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Mockito(org.mockito.Mockito) Response(javax.ws.rs.core.Response) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) Assert(org.junit.Assert) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) Application(io.gravitee.rest.api.portal.rest.model.Application) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) Test(org.junit.Test)

Example 12 with PrimaryOwnerEntity

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

the class EmailNotifierServiceTest method shouldHaveATemplateForApplicationHooksWithFreemarker.

@Test
public void shouldHaveATemplateForApplicationHooksWithFreemarker() throws TemplateException {
    GenericNotificationConfig cfg = new GenericNotificationConfig();
    cfg.setConfig("test@mail.com, ${api.primaryOwner.email} ");
    ApiEntity api = new ApiEntity();
    api.setName("api-name");
    UserEntity userEntity = new UserEntity();
    userEntity.setEmail("primary@owner.com");
    api.setPrimaryOwner(new PrimaryOwnerEntity(userEntity));
    PlanEntity plan = new PlanEntity();
    plan.setId("plan-id");
    plan.setName("plan-name");
    Map<String, Object> params = new HashMap<>();
    params.put((NotificationParamsBuilder.PARAM_API), api);
    params.put((NotificationParamsBuilder.PARAM_PLAN), plan);
    when(notificationTemplateService.resolveInlineTemplateWithParam(anyString(), anyString(), any())).thenReturn("primary@owner.com");
    for (ApplicationHook hook : ApplicationHook.values()) {
        reset(mockEmailService);
        service.trigger(hook, cfg, params);
        verify(mockEmailService, times(1)).sendAsyncEmailNotification(argThat(notification -> notification.getTo() != null && notification.getTo().length == 2 && notification.getTo()[0].equals("test@mail.com") && notification.getTo()[1].equals("primary@owner.com")), any());
        verify(mockEmailService, never()).sendEmailNotification(any());
    }
}
Also used : InjectMocks(org.mockito.InjectMocks) CoreMatchers(org.hamcrest.CoreMatchers) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) GenericNotificationConfig(io.gravitee.repository.management.model.GenericNotificationConfig) Mock(org.mockito.Mock) TemplateException(freemarker.template.TemplateException) PlanEntity(io.gravitee.rest.api.model.PlanEntity) io.gravitee.rest.api.service.notification(io.gravitee.rest.api.service.notification) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Test(org.junit.Test) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) EmailNotifierServiceImpl(io.gravitee.rest.api.service.notifiers.impl.EmailNotifierServiceImpl) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Map(java.util.Map) Assert(org.junit.Assert) Collections(java.util.Collections) UserEntity(io.gravitee.rest.api.model.UserEntity) HashMap(java.util.HashMap) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) GenericNotificationConfig(io.gravitee.repository.management.model.GenericNotificationConfig) PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 13 with PrimaryOwnerEntity

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

the class MetadataServiceTest method checkMetadataFormat_userWithoutEmail.

@Test
public void checkMetadataFormat_userWithoutEmail() throws TemplateException {
    when(this.notificationTemplateService.resolveInlineTemplateWithParam(anyString(), any(Reader.class), any())).thenReturn("");
    UserEntity userEntity = new UserEntity();
    PrimaryOwnerEntity primaryOwnerEntity = new PrimaryOwnerEntity(userEntity);
    ApiEntity apiEntity = new ApiEntity();
    apiEntity.setPrimaryOwner(primaryOwnerEntity);
    metadataService.checkMetadataFormat(MetadataFormat.MAIL, "${(api.primaryOwner.email)!''}", API, apiEntity);
}
Also used : PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Reader(java.io.Reader) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 14 with PrimaryOwnerEntity

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

the class ApiMapper method convert.

public Api convert(ApiEntity api) {
    final Api apiItem = new Api();
    apiItem.setDescription(api.getDescription());
    List<ApiEntrypointEntity> apiEntrypoints = api.getEntrypoints();
    if (apiEntrypoints != null) {
        List<String> entrypoints = apiEntrypoints.stream().map(ApiEntrypointEntity::getTarget).collect(Collectors.toList());
        apiItem.setEntrypoints(entrypoints);
    }
    apiItem.setDraft(api.getLifecycleState() == ApiLifecycleState.UNPUBLISHED || api.getLifecycleState() == ApiLifecycleState.CREATED);
    apiItem.setRunning(api.getState() == Lifecycle.State.STARTED);
    apiItem.setPublic(api.getVisibility() == Visibility.PUBLIC);
    apiItem.setId(api.getId());
    List<String> apiLabels = api.getLabels();
    if (apiLabels != null) {
        apiItem.setLabels(new ArrayList<>(apiLabels));
    } else {
        apiItem.setLabels(new ArrayList<>());
    }
    apiItem.setName(api.getName());
    PrimaryOwnerEntity primaryOwner = api.getPrimaryOwner();
    if (primaryOwner != null) {
        User owner = new User();
        owner.setId(primaryOwner.getId());
        owner.setDisplayName(primaryOwner.getDisplayName());
        owner.setEmail(primaryOwner.getEmail());
        apiItem.setOwner(owner);
    }
    apiItem.setPages(null);
    apiItem.setPlans(null);
    if (ratingService.isEnabled()) {
        final RatingSummaryEntity ratingSummaryEntity = ratingService.findSummaryByApi(api.getId());
        RatingSummary ratingSummary = new RatingSummary().average(ratingSummaryEntity.getAverageRate()).count(BigDecimal.valueOf(ratingSummaryEntity.getNumberOfRatings()));
        apiItem.setRatingSummary(ratingSummary);
    }
    if (api.getUpdatedAt() != null) {
        apiItem.setUpdatedAt(api.getUpdatedAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    apiItem.setVersion(api.getVersion());
    boolean isCategoryModeEnabled = this.parameterService.findAsBoolean(Key.PORTAL_APIS_CATEGORY_ENABLED, ParameterReferenceType.ENVIRONMENT);
    if (isCategoryModeEnabled && api.getCategories() != null) {
        apiItem.setCategories(api.getCategories().stream().filter(categoryId -> {
            try {
                categoryService.findNotHiddenById(categoryId);
                return true;
            } catch (CategoryNotFoundException v) {
                return false;
            }
        }).collect(Collectors.toList()));
    } else {
        apiItem.setCategories(new ArrayList<>());
    }
    return apiItem;
}
Also used : RatingSummary(io.gravitee.rest.api.portal.rest.model.RatingSummary) ApiEntrypointEntity(io.gravitee.rest.api.model.api.ApiEntrypointEntity) User(io.gravitee.rest.api.portal.rest.model.User) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) Api(io.gravitee.rest.api.portal.rest.model.Api)

Example 15 with PrimaryOwnerEntity

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

the class ApiService_FindPrimaryOwnerTest method testGroupModeWithExistingPOGroup.

// GROUP + import with PO GROUP
@Test
public void testGroupModeWithExistingPOGroup() {
    setPrimaryOwnerMode("GROUP");
    defineGroup(PO_GROUP_ID);
    JsonNode definition = poGroupDefinition();
    final PrimaryOwnerEntity primaryOwner = apiService.findPrimaryOwner(definition, CURRENT_USER);
    assertEquals(PO_GROUP_ID, primaryOwner.getId());
    assertEquals("GROUP", primaryOwner.getType());
}
Also used : PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)26 Test (org.junit.Test)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)16 UserEntity (io.gravitee.rest.api.model.UserEntity)9 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)8 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)3 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 Mockito (org.mockito.Mockito)3 NOT_FOUND_404 (io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404)2 OK_200 (io.gravitee.common.http.HttpStatusCode.OK_200)2 Proxy (io.gravitee.definition.model.Proxy)2 VirtualHost (io.gravitee.definition.model.VirtualHost)2 RatingSummaryEntity (io.gravitee.rest.api.model.RatingSummaryEntity)2 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)2 TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)2 GroupByQuery (io.gravitee.rest.api.model.analytics.query.GroupByQuery)2 ApiEntrypointEntity (io.gravitee.rest.api.model.api.ApiEntrypointEntity)2 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)2