Search in sources :

Example 11 with PlanEntity

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;
}
Also used : PlanEntity(io.gravitee.rest.api.model.PlanEntity) PlanNotFoundException(io.gravitee.rest.api.service.exceptions.PlanNotFoundException) Key(io.gravitee.rest.api.portal.rest.model.Key)

Example 12 with PlanEntity

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);
}
Also used : PlanMapper(io.gravitee.rest.api.portal.rest.mapper.PlanMapper) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Plan(io.gravitee.rest.api.portal.rest.model.Plan) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth) PlanStatus(io.gravitee.rest.api.model.PlanStatus) Inject(javax.inject.Inject) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) API_PLAN(io.gravitee.rest.api.model.permissions.RolePermission.API_PLAN) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) READ(io.gravitee.rest.api.model.permissions.RolePermissionAction.READ) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) GroupService(io.gravitee.rest.api.service.GroupService) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Collectors(java.util.stream.Collectors) BeanParam(javax.ws.rs.BeanParam) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) Response(javax.ws.rs.core.Response) Visibility(io.gravitee.rest.api.model.Visibility) PlanService(io.gravitee.rest.api.service.PlanService) Comparator(java.util.Comparator) Collections(java.util.Collections) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Plan(io.gravitee.rest.api.portal.rest.model.Plan) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 13 with PlanEntity

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());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Page(io.gravitee.rest.api.portal.rest.model.Page) Api(io.gravitee.rest.api.portal.rest.model.Api) Plan(io.gravitee.rest.api.portal.rest.model.Plan) Before(org.junit.Before)

Example 14 with PlanEntity

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);
}
Also used : PlanEntity(io.gravitee.rest.api.model.PlanEntity) NewSubscriptionEntity(io.gravitee.rest.api.model.NewSubscriptionEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Page(io.gravitee.common.data.domain.Page) Before(org.junit.Before)

Example 15 with PlanEntity

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

Aggregations

PlanEntity (io.gravitee.rest.api.model.PlanEntity)27 Test (org.junit.Test)17 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)9 Page (io.gravitee.repository.management.model.Page)5 GenericNotificationConfig (io.gravitee.repository.management.model.GenericNotificationConfig)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 List (java.util.List)4 TemplateException (freemarker.template.TemplateException)3 Subscription (io.gravitee.rest.api.management.rest.model.Subscription)3 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)3 UserEntity (io.gravitee.rest.api.model.UserEntity)3 io.gravitee.rest.api.service.notification (io.gravitee.rest.api.service.notification)3 EmailNotifierServiceImpl (io.gravitee.rest.api.service.notifiers.impl.EmailNotifierServiceImpl)3 Map (java.util.Map)3 CoreMatchers (org.hamcrest.CoreMatchers)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 RunWith (org.junit.runner.RunWith)3 InjectMocks (org.mockito.InjectMocks)3