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