Search in sources :

Example 16 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifierTest method testShouldNotLoadMessageWhenErrorOccurs.

@Test
public void testShouldNotLoadMessageWhenErrorOccurs() throws RegistryException {
    Resource resource = Mockito.mock(Resource.class);
    NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifier() {

        @Override
        protected Registry getConfigSystemRegistry(int tenantId) throws RegistryException {
            return registry;
        }
    };
    Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true).thenThrow(RegistryException.class);
    Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
    Mockito.when(resource.getContent()).thenReturn("".getBytes());
    try {
        NotificationDTO notification = emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.assertFalse(!notification.getMessage().equals(""));
        emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.fail("Should fail with an exception");
    } catch (NotificationException e) {
        // exception is expection
        return;
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) Test(org.junit.Test)

Example 17 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO in project carbon-apimgt by wso2.

the class NewAPIVersionEmailNotifierTest method testShouldReturnNotifiersWhenPropertiesValid.

@Test
public void testShouldReturnNotifiersWhenPropertiesValid() throws APIManagementException {
    final ClaimsRetriever claimsRetriever = Mockito.mock(ClaimsRetriever.class);
    final SortedMap<String, String> claims = new TreeMap<String, String>();
    claims.put(NotifierConstants.EMAIL_CLAIM, "admin@wso2.com");
    NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifier() {

        @Override
        protected ClaimsRetriever getClaimsRetriever(String claimsRetrieverImplClass) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
            return claimsRetriever;
        }
    };
    Mockito.doNothing().when(claimsRetriever).init();
    Mockito.when(claimsRetriever.getClaims(Mockito.anyString())).thenReturn(claims);
    try {
        Assert.assertTrue(emailNotifier.getNotifierSet(notificationDTO).size() > 0);
    } catch (NotificationException e) {
        Assert.fail("Should not throw any exceptions");
    }
}
Also used : NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) Test(org.junit.Test)

Example 18 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO 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 19 with NotificationDTO

use of org.wso2.carbon.apimgt.impl.notification.NotificationDTO 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

NotificationException (org.wso2.carbon.apimgt.impl.notification.exception.NotificationException)13 Test (org.junit.Test)11 Properties (java.util.Properties)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 JSONObject (org.json.simple.JSONObject)5 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)5 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)4 ClaimsRetriever (org.wso2.carbon.apimgt.impl.token.ClaimsRetriever)4 Resource (org.wso2.carbon.registry.core.Resource)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 JSONArray (org.json.simple.JSONArray)3 Map (java.util.Map)2 JSONParser (org.json.simple.parser.JSONParser)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIMConfigurations (org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations)2 NotificationConfigurations (org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations)2 NotificationException (org.wso2.carbon.apimgt.core.exception.NotificationException)2 NotificationExecutor (org.wso2.carbon.apimgt.core.executors.NotificationExecutor)2