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