Search in sources :

Example 1 with NotificationExecutor

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);
}
Also used : APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) NotificationDTO(org.wso2.carbon.apimgt.core.template.dto.NotificationDTO) Set(java.util.Set) HashSet(java.util.HashSet) NotificationConfigurations(org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations) NotificationExecutor(org.wso2.carbon.apimgt.core.executors.NotificationExecutor) APIMConfigurations(org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) Properties(java.util.Properties) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with NotificationExecutor

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);
}
Also used : Registry(org.wso2.carbon.registry.core.Registry) NotificationExecutorWrapper(org.wso2.carbon.apimgt.impl.notification.util.NotificationExecutorWrapper) Before(org.junit.Before)

Example 3 with NotificationExecutor

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);
            }
        }
    }
}
Also used : NotificationDTO(org.wso2.carbon.apimgt.core.template.dto.NotificationDTO) NotificationConfigurations(org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations) NotificationExecutor(org.wso2.carbon.apimgt.core.executors.NotificationExecutor) NotificationException(org.wso2.carbon.apimgt.core.exception.NotificationException) Properties(java.util.Properties)

Example 4 with NotificationExecutor

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
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) NotificationDTO(org.wso2.carbon.apimgt.impl.notification.NotificationDTO) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) JSONObject(org.json.simple.JSONObject) RealmService(org.wso2.carbon.user.core.service.RealmService) NotificationExecutor(org.wso2.carbon.apimgt.impl.notification.NotificationExecutor) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with NotificationExecutor

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);
    }
}
Also used : NotificationDTO(org.wso2.carbon.apimgt.impl.notification.NotificationDTO) JSONObject(org.json.simple.JSONObject) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) NotificationExecutor(org.wso2.carbon.apimgt.impl.notification.NotificationExecutor) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties) Properties(java.util.Properties) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)

Aggregations

Properties (java.util.Properties)3 JSONObject (org.json.simple.JSONObject)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 NotificationConfigurations (org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations)2 NotificationExecutor (org.wso2.carbon.apimgt.core.executors.NotificationExecutor)2 NotificationDTO (org.wso2.carbon.apimgt.core.template.dto.NotificationDTO)2 NotificationDTO (org.wso2.carbon.apimgt.impl.notification.NotificationDTO)2 NotificationExecutor (org.wso2.carbon.apimgt.impl.notification.NotificationExecutor)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 QName (javax.xml.namespace.QName)1 Before (org.junit.Before)1 API (org.wso2.carbon.apimgt.api.model.API)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)1 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)1 APIMConfigurations (org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations)1