use of org.wso2.carbon.apimgt.impl.notification.NotificationExecutor in project carbon-apimgt by wso2.
the class NotificationTestCase method testNotificationExecutor.
@Test
public void testNotificationExecutor() throws Exception {
Properties properties = Mockito.mock(Properties.class);
NotificationDTO notificationDTO = Mockito.mock(NotificationDTO.class);
Mockito.when(notificationDTO.getTitle()).thenReturn("Title");
Mockito.when(notificationDTO.getType()).thenReturn("ApiNewVersion");
Mockito.when(notificationDTO.getMessage()).thenReturn("Message");
Mockito.when(notificationDTO.getProperties()).thenReturn(properties);
APIMConfigurations apimConfigurations = Mockito.mock(APIMConfigurations.class);
NotificationConfigurations notificationConfigurations = Mockito.mock(NotificationConfigurations.class);
PowerMockito.mockStatic(APIMConfigurations.class);
PowerMockito.when(apimConfigurations.getNotificationConfigurations()).thenReturn(notificationConfigurations);
APIManagerFactory apiManagerFactory = Mockito.mock(APIManagerFactory.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
Set subscriber = new HashSet();
subscriber.add("User");
Mockito.when((Set<String>) notificationDTO.getProperty(NotifierConstants.SUBSCRIBERS_PER_API)).thenReturn(subscriber);
PowerMockito.mockStatic(APIManagerFactory.class);
PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(apiManagerFactory);
PowerMockito.when(apiManagerFactory.getIdentityProvider()).thenReturn(identityProvider);
PowerMockito.when(identityProvider.getIdOfUser("User")).thenReturn("1111");
PowerMockito.when(identityProvider.getEmailOfUser("1111")).thenReturn("admin@gmail.com");
new NotificationExecutor().sendAsyncNotifications(notificationDTO);
}
use of org.wso2.carbon.apimgt.impl.notification.NotificationExecutor in project carbon-apimgt by wso2.
the class NotificationExecutorTest method setup.
@Before
public void setup() {
registry = Mockito.mock(Registry.class);
notificationExecutor = new NotificationExecutorWrapper(registry);
PowerMockito.mockStatic(APIUtil.class);
}
use of org.wso2.carbon.apimgt.impl.notification.NotificationExecutor in project carbon-apimgt by wso2.
the class APIPublisherImpl method sendNotification.
private void sendNotification(String apiId, String apiName, String newVersion) throws APIManagementException {
Set<String> subscriberList;
NotificationConfigurations notificationConfigurations = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getNotificationConfigurations();
// check notification Enabled
if (notificationConfigurations.getNotificationEnable()) {
subscriberList = getSubscribersByAPIId(apiId);
// Notifications are sent only if there are subscribers
if (subscriberList.size() > 0) {
try {
Properties prop = new Properties();
prop.put(NotifierConstants.NEW_API_VERSION, newVersion);
prop.put(NotifierConstants.API_NAME, apiName);
prop.put(NotifierConstants.SUBSCRIBERS_PER_API, subscriberList);
NotificationDTO notificationDTO = new NotificationDTO(prop, NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
new NotificationExecutor().sendAsyncNotifications(notificationDTO);
} catch (NotificationException e) {
String msg = "Error occurred while sending Async Notifications";
log.error(msg, e);
}
}
}
}
use of org.wso2.carbon.apimgt.impl.notification.NotificationExecutor in project carbon-apimgt by wso2.
the class APIProviderImplTest method testEmailSentWhenPropergateAPIStatusChangeToGateways.
@Test
public void testEmailSentWhenPropergateAPIStatusChangeToGateways() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
api.setOrganization("carbon.super");
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
TestUtils.mockRegistryAndUserRealm(-1);
Resource resource = PowerMockito.mock(Resource.class);
JSONObject tenantConfig = PowerMockito.mock(JSONObject.class);
NotificationExecutor notificationExecutor = PowerMockito.mock(NotificationExecutor.class);
NotificationDTO notificationDTO = PowerMockito.mock(NotificationDTO.class);
UserRegistry configRegistry = PowerMockito.mock(UserRegistry.class);
RegistryService registryService = PowerMockito.mock(RegistryService.class);
Mockito.when(apimgtDAO.addAPI(api, -1, "testOrg")).thenReturn(1);
Mockito.doNothing().when(apimgtDAO).addURITemplates(1, api, -1);
Mockito.doNothing().when(keyManager).attachResourceScopes(api, api.getUriTemplates());
Mockito.when(artifactManager.newGovernanceArtifact(Matchers.any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
Map<String, String> failedToPubGWEnv = new HashMap<String, String>();
APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService()).thenReturn(configurationService);
PowerMockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
PowerMockito.when(apiManagerConfiguration.getApiRecommendationEnvironment()).thenReturn(null);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
apiProvider.addAPI(api);
PowerMockito.when(APIUtil.replaceEmailDomain(apiId.getProviderName())).thenReturn("admin");
PowerMockito.when(APIUtil.replaceEmailDomainBack(api.getId().getProviderName())).thenReturn("admin");
PowerMockito.when(apimgtDAO.getPublishedDefaultVersion(api.getId())).thenReturn("1.0.0");
// Change to PUBLISHED state
// Existing APIs of the provider
API api1 = new API(new APIIdentifier("admin", "API1", "0.0.5"));
api1.setStatus(APIConstants.PUBLISHED);
API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
prepareForGetAPIsByProvider(artifactManager, apiProvider, "admin", api1, api2);
PowerMockito.when(registryService.getConfigSystemRegistry(-1)).thenReturn(configRegistry);
Mockito.when(resource.getContent()).thenReturn(getTenantConfigContent());
PowerMockito.when(tenantConfig.get(NotifierConstants.NOTIFICATIONS_ENABLED)).thenReturn("true");
PowerMockito.when(tenantConfig.get(APIConstants.EXTENSION_HANDLER_POSITION)).thenReturn("bottom");
PowerMockito.whenNew(NotificationDTO.class).withAnyArguments().thenReturn(notificationDTO);
PowerMockito.whenNew(NotificationExecutor.class).withAnyArguments().thenReturn(notificationExecutor);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.when(registryService.getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, -1234)).thenReturn(registry);
PowerMockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
apiProvider.propergateAPIStatusChangeToGateways(APIConstants.PUBLISHED, api);
// Mockito.verify(notificationExecutor).sendAsyncNotifications(notificationDTO); Not valid. notification logic moved outside
}
use of org.wso2.carbon.apimgt.impl.notification.NotificationExecutor in project carbon-apimgt by wso2.
the class APIProviderImpl method sendEmailNotification.
/**
* This method used to send notifications to the previous subscribers of older versions of a given API
*
* @param api
* @throws APIManagementException
*/
private void sendEmailNotification(API api) throws APIManagementException {
try {
JSONObject tenantConfig = APIUtil.getTenantConfig(tenantDomain);
String isNotificationEnabled = "false";
if (tenantConfig.containsKey(NotifierConstants.NOTIFICATIONS_ENABLED)) {
isNotificationEnabled = (String) tenantConfig.get(NotifierConstants.NOTIFICATIONS_ENABLED);
}
if (JavaUtils.isTrueExplicitly(isNotificationEnabled)) {
List<APIIdentifier> apiIdentifiers = getOldPublishedAPIList(api);
for (APIIdentifier oldAPI : apiIdentifiers) {
Properties prop = new Properties();
prop.put(NotifierConstants.API_KEY, oldAPI);
prop.put(NotifierConstants.NEW_API_KEY, api.getId());
Set<Subscriber> subscribersOfAPI = apiMgtDAO.getSubscribersOfAPI(oldAPI);
prop.put(NotifierConstants.SUBSCRIBERS_PER_API, subscribersOfAPI);
NotificationDTO notificationDTO = new NotificationDTO(prop, NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
notificationDTO.setTenantID(tenantId);
notificationDTO.setTenantDomain(tenantDomain);
new NotificationExecutor().sendAsyncNotifications(notificationDTO);
}
}
} catch (NotificationException e) {
log.error(e.getMessage(), e);
}
}
Aggregations