use of io.gravitee.rest.api.model.PlanEntity in project gravitee-management-rest-api by gravitee-io.
the class KeyMapper method convert.
public Key convert(ApiKeyEntity apiKeyEntity) {
final Key keyItem = new Key();
final String plan = apiKeyEntity.getPlan();
try {
PlanEntity planEntity = planService.findById(plan);
keyItem.setApi(planEntity.getApi());
} catch (PlanNotFoundException e) {
LOGGER.warn("plan does not exist : {}", plan);
}
keyItem.setApplication(apiKeyEntity.getApplication());
keyItem.setCreatedAt(apiKeyEntity.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
keyItem.setId(apiKeyEntity.getKey());
keyItem.setPaused(apiKeyEntity.isPaused());
keyItem.setPlan(plan);
keyItem.setRevoked(apiKeyEntity.isRevoked());
if (apiKeyEntity.isRevoked()) {
keyItem.setRevokedAt(apiKeyEntity.getRevokedAt().toInstant().atOffset(ZoneOffset.UTC));
}
keyItem.setExpired(apiKeyEntity.isExpired());
if (apiKeyEntity.getExpireAt() != null) {
keyItem.setExpireAt(apiKeyEntity.getExpireAt().toInstant().atOffset(ZoneOffset.UTC));
}
return keyItem;
}
use of io.gravitee.rest.api.model.PlanEntity in project gravitee-management-rest-api by gravitee-io.
the class ApiPlansResource method getApiPlansByApiId.
@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getApiPlansByApiId(@PathParam("apiId") String apiId, @BeanParam PaginationParam paginationParam) {
String username = getAuthenticatedUserOrNull();
final ApiQuery apiQuery = new ApiQuery();
apiQuery.setIds(Collections.singletonList(apiId));
Collection<ApiEntity> userApis = apiService.findPublishedByUser(username, apiQuery);
if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
ApiEntity apiEntity = apiService.findById(apiId);
if (Visibility.PUBLIC.equals(apiEntity.getVisibility()) || hasPermission(API_PLAN, apiId, READ)) {
List<Plan> plans = planService.findByApi(apiId).stream().filter(plan -> PlanStatus.PUBLISHED.equals(plan.getStatus())).filter(plan -> groupService.isUserAuthorizedToAccessApiData(apiEntity, plan.getExcludedGroups(), username)).sorted(Comparator.comparingInt(PlanEntity::getOrder)).map(p -> planMapper.convert(p)).collect(Collectors.toList());
return createListResponse(plans, paginationParam);
} else {
return createListResponse(emptyList(), paginationParam);
}
}
throw new ApiNotFoundException(apiId);
}
use of io.gravitee.rest.api.model.PlanEntity in project gravitee-management-rest-api by gravitee-io.
the class ApiResourceNotAuthenticatedTest method init.
@Before
public void init() {
resetAllMocks();
mockApi = new ApiEntity();
mockApi.setId(API);
doReturn(mockApi).when(apiService).findById(API);
when(accessControlService.canAccessApiFromPortal(API)).thenReturn(true);
when(accessControlService.canAccessApiFromPortal(mockApi)).thenReturn(true);
doReturn(Arrays.asList(new PageEntity())).when(pageService).search(any(), eq(GraviteeContext.getCurrentEnvironment()));
PlanEntity plan1 = new PlanEntity();
plan1.setId("A");
plan1.setStatus(PlanStatus.PUBLISHED);
PlanEntity plan2 = new PlanEntity();
plan2.setId("B");
plan2.setStatus(PlanStatus.PUBLISHED);
PlanEntity plan3 = new PlanEntity();
plan3.setId("C");
plan3.setStatus(PlanStatus.CLOSED);
doReturn(new HashSet<PlanEntity>(Arrays.asList(plan1, plan2, plan3))).when(planService).findByApi(API);
doReturn(new Api()).when(apiMapper).convert(any());
doReturn(new Page()).when(pageMapper).convert(any());
doReturn(new Plan()).when(planMapper).convert(any());
}
use of io.gravitee.rest.api.model.PlanEntity in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionsResourceTest method init.
@Before
public void init() {
resetAllMocks();
SubscriptionEntity subscriptionEntity1 = new SubscriptionEntity();
subscriptionEntity1.setId(SUBSCRIPTION);
SubscriptionEntity subscriptionEntity2 = new SubscriptionEntity();
subscriptionEntity2.setId(ANOTHER_SUBSCRIPTION);
final Page<SubscriptionEntity> subscriptionPage = new Page<>(asList(subscriptionEntity1, subscriptionEntity2), 0, 1, 2);
doReturn(subscriptionPage.getContent()).when(subscriptionService).search(any());
doReturn(subscriptionPage).when(subscriptionService).search(any(), any());
doReturn(new Subscription().id(SUBSCRIPTION)).when(subscriptionMapper).convert(subscriptionEntity1);
doReturn(new Subscription().id(ANOTHER_SUBSCRIPTION)).when(subscriptionMapper).convert(subscriptionEntity2);
SubscriptionEntity createdSubscription = new SubscriptionEntity();
createdSubscription.setId(SUBSCRIPTION);
doReturn(createdSubscription).when(subscriptionService).create(any());
SubscriptionEntity subscriptionEntity = new SubscriptionEntity();
subscriptionEntity.setApi(API);
subscriptionEntity.setApplication(APPLICATION);
doReturn(subscriptionEntity).when(subscriptionService).findById(eq(SUBSCRIPTION));
doReturn(true).when(permissionService).hasPermission(any(), any(), any());
PlanEntity planEntity = new PlanEntity();
planEntity.setApi(API);
doReturn(planEntity).when(planService).findById(PLAN);
}
use of io.gravitee.rest.api.model.PlanEntity 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());
}
}
Aggregations