Search in sources :

Example 91 with ApiEntity

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

the class ScheduledSubscriptionPreExpirationNotificationServiceTest method shouldSendEmail.

@Test
public void shouldSendEmail() {
    int day = 30;
    String subscriberEmail = "subscriber@gravitee.io";
    ApiEntity api = mock(ApiEntity.class);
    // here api.getReferenceId() = environmentId
    when(api.getReferenceId()).thenReturn(UUID.randomUUID().toString());
    PlanEntity plan = mock(PlanEntity.class);
    ApplicationEntity application = mock(ApplicationEntity.class);
    ApiKeyEntity apiKey = mock(ApiKeyEntity.class);
    service.sendEmail(subscriberEmail, day, api, plan, application, apiKey);
    EmailNotification emailNotification = new EmailNotificationBuilder().to(subscriberEmail).template(EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_SUBSCRIPTION_PRE_EXPIRATION).param("expirationDelay", day).param("api", api).param("plan", plan).param("application", application).param("apiKey", apiKey).build();
    verify(emailService, times(1)).sendAsyncEmailNotification(eq(emailNotification), any(GraviteeContext.ReferenceContext.class));
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) Test(org.junit.Test)

Example 92 with ApiEntity

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

the class ApiManager method undeploy.

public void undeploy(String apiId) {
    ApiEntity currentApi = apis.remove(apiId);
    if (currentApi != null) {
        logger.info("Undeployment of {}", currentApi);
        eventManager.publishEvent(ApiEvent.UNDEPLOY, currentApi);
        logger.info("{} has been undeployed", apiId);
    }
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity)

Example 93 with ApiEntity

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

the class ScheduledSubscriptionsServiceTest method shouldCloseOutdatedSubscriptions.

@Test
public void shouldCloseOutdatedSubscriptions() {
    ApiEntity apiEntity = mock(ApiEntity.class);
    when(apiEntity.getId()).thenReturn("API_ID");
    SubscriptionEntity endDateInThePast = mock(SubscriptionEntity.class);
    when(endDateInThePast.getId()).thenReturn("end_date_in_the_past");
    when(apiService.findAllLight()).thenReturn(Collections.singleton(apiEntity));
    when(subscriptionService.search(argThat(subscriptionQuery -> subscriptionQuery.getApis().equals(Collections.singleton("API_ID")) && subscriptionQuery.getStatuses().equals(Collections.singleton(SubscriptionStatus.ACCEPTED)) && subscriptionQuery.getEndingAtBefore() > 0))).thenReturn(new HashSet<>(Collections.singletonList(endDateInThePast)));
    service.run();
    verify(apiService, times(1)).findAllLight();
    verify(subscriptionService, times(1)).close("end_date_in_the_past");
}
Also used : InjectMocks(org.mockito.InjectMocks) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) Date(java.util.Date) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Test(org.junit.Test) ApiService(io.gravitee.rest.api.service.ApiService) HashSet(java.util.HashSet) Mockito(org.mockito.Mockito) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Test(org.junit.Test)

Example 94 with ApiEntity

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

the class DynamicPropertyUpdater method update.

private void update(Collection<DynamicProperty> dynamicProperties) {
    // Get latest changes
    ApiEntity latestApi = apiService.findById(api.getId());
    List<Property> properties = (latestApi.getProperties() != null) ? latestApi.getProperties().getProperties() : Collections.emptyList();
    List<Property> userDefinedProperties = properties.stream().filter(property -> !property.isDynamic()).collect(Collectors.toList());
    Map<String, Property> propertyMap = properties.stream().collect(Collectors.toMap(Property::getKey, property -> property));
    List<Property> updatedProperties = new ArrayList<>();
    boolean needToBeSaved = false;
    for (DynamicProperty dynamicProperty : dynamicProperties) {
        Property property = propertyMap.get(dynamicProperty.getKey());
        if (property == null || property.isDynamic()) {
            updatedProperties.add(dynamicProperty);
        }
        // save properties only if there's something new
        if (property == null || (property.isDynamic() && !property.getValue().equals(dynamicProperty.getValue()))) {
            needToBeSaved = true;
        }
    }
    if (needToBeSaved) {
        // Add previous user-defined properties
        updatedProperties.addAll(userDefinedProperties);
        // Sort properties alphabetically to avoid redeploy if just the order has changed.
        List<Property> sortedUpdatedProperties = updatedProperties.stream().sorted(Comparator.comparing(Property::getKey)).collect(Collectors.toList());
        // Create properties container
        Properties apiProperties = new Properties();
        try {
            apiProperties.setProperties(sortedUpdatedProperties);
        } catch (RuntimeException e) {
            logger.error(e.getMessage(), e);
        }
        latestApi.setProperties(apiProperties);
        boolean isSync = apiService.isSynchronized(api.getId());
        // Update API
        apiService.update(latestApi.getId(), ApiService.convert(latestApi));
        // Do not deploy if there are manual changes to push
        if (isSync) {
            // Publish API only in case of changes
            if (!updatedProperties.containsAll(properties) || !properties.containsAll(updatedProperties)) {
                ApiDeploymentEntity deployEntity = new ApiDeploymentEntity();
                deployEntity.setDeploymentLabel("Dynamic properties sync");
                apiService.deploy(latestApi.getId(), "dynamic-property-updater", EventType.PUBLISH_API, deployEntity);
            }
        }
    }
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) Logger(org.slf4j.Logger) Properties(io.gravitee.definition.model.Properties) Property(io.gravitee.definition.model.Property) Provider(io.gravitee.rest.api.services.dynamicproperties.provider.Provider) LoggerFactory(org.slf4j.LoggerFactory) DynamicProperty(io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty) RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) UserDetails(io.gravitee.rest.api.idp.api.authentication.UserDetails) Collectors(java.util.stream.Collectors) ApiService(io.gravitee.rest.api.service.ApiService) GrantedAuthority(org.springframework.security.core.GrantedAuthority) EventType(io.gravitee.rest.api.model.EventType) SecurityContext(org.springframework.security.core.context.SecurityContext) Handler(io.vertx.core.Handler) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) ApiDeploymentEntity(io.gravitee.rest.api.model.api.ApiDeploymentEntity) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) SystemRole(io.gravitee.rest.api.model.permissions.SystemRole) DynamicProperty(io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiDeploymentEntity(io.gravitee.rest.api.model.api.ApiDeploymentEntity) Properties(io.gravitee.definition.model.Properties) Property(io.gravitee.definition.model.Property) DynamicProperty(io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty)

Example 95 with ApiEntity

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

the class ApiPagesResourceNotAdminTest method shouldNotGetPrivateApiPublishedPage.

@Test
public void shouldNotGetPrivateApiPublishedPage() {
    reset(apiService, pageService, membershipService);
    final ApiEntity apiMock = mock(ApiEntity.class);
    when(apiMock.getVisibility()).thenReturn(Visibility.PRIVATE);
    when(apiMock.getName()).thenReturn(API_NAME);
    doReturn(apiMock).when(apiService).findById(API_NAME);
    final PageEntity pageMock = new PageEntity();
    pageMock.setPublished(true);
    pageMock.setName(PAGE_NAME);
    doReturn(pageMock).when(pageService).findById(PAGE_NAME, null);
    doReturn(true).when(roleService).hasPermission(any(), eq(ApiPermission.DOCUMENTATION), eq(new RolePermissionAction[] { RolePermissionAction.READ }));
    when(permissionService.hasPermission(any(), any(), any())).thenReturn(true);
    final Response response = envTarget().request().get();
    assertEquals(UNAUTHORIZED_401, response.getStatus());
    verify(apiService, atLeastOnce()).findById(API_NAME);
    verify(pageService, times(1)).findById(PAGE_NAME, null);
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) Test(org.junit.Test)

Aggregations

ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)273 Test (org.junit.Test)180 Response (javax.ws.rs.core.Response)65 Before (org.junit.Before)46 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)35 Error (io.gravitee.rest.api.portal.rest.model.Error)32 java.util (java.util)30 Collectors (java.util.stream.Collectors)27 Mockito.doReturn (org.mockito.Mockito.doReturn)27 Mockito (org.mockito.Mockito)25 ArgumentMatchers (org.mockito.ArgumentMatchers)24 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)23 Assert (org.junit.Assert)22 Collections.singletonList (java.util.Collections.singletonList)21 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)20 IOException (java.io.IOException)20 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)19 Entity (javax.ws.rs.client.Entity)19 HashSet (java.util.HashSet)18 Api (io.gravitee.repository.management.model.Api)17