Search in sources :

Example 11 with NotificationException

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

the class NotificationExecutorTest method testShouldNotThrowExceptionWhenConfigurationNotFound.

@Test
public void testShouldNotThrowExceptionWhenConfigurationNotFound() throws Exception {
    NotificationDTO notificationDTO = new NotificationDTO(new Properties(), NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
    notificationDTO.setTenantID(TENANT_ID);
    notificationDTO.setTenantDomain(TENANT_DOMAIN);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(NotifierConstants.Notifications_KEY, new JSONArray());
    PowerMockito.when(APIUtil.class, "getTenantConfig", TENANT_DOMAIN).thenReturn(jsonObject);
    try {
        notificationExecutor.sendAsyncNotifications(notificationDTO);
    } catch (NotificationException e) {
        Assert.fail("Should not throw an exception");
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with NotificationException

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

the class NotificationExecutorTest method testShouldNotThrowExceptionWhenNotifierNotDefined.

@Test
public void testShouldNotThrowExceptionWhenNotifierNotDefined() throws Exception {
    NotificationDTO notificationDTO = new NotificationDTO(new Properties(), NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
    notificationDTO.setTenantDomain(TENANT_DOMAIN);
    notificationDTO.setTenantID(TENANT_ID);
    Notifier notifier = Mockito.mock(Notifier.class);
    Mockito.doNothing().when(notifier).run();
    PowerMockito.when(APIUtil.class, "getTenantConfig", TENANT_DOMAIN).thenReturn(new JSONParser().parse(invalidNotifierConf));
    try {
        notificationExecutor.sendAsyncNotifications(notificationDTO);
    } catch (NotificationException e) {
        Assert.fail("Should not throw an exception");
    }
}
Also used : NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) JSONParser(org.json.simple.parser.JSONParser) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with NotificationException

use of org.wso2.carbon.apimgt.impl.notification.exception.NotificationException 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 14 with NotificationException

use of org.wso2.carbon.apimgt.impl.notification.exception.NotificationException 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 15 with NotificationException

use of org.wso2.carbon.apimgt.impl.notification.exception.NotificationException 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 Properties (java.util.Properties)8 Test (org.junit.Test)8 JSONObject (org.json.simple.JSONObject)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)4 JSONArray (org.json.simple.JSONArray)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)3 ClaimsRetriever (org.wso2.carbon.apimgt.impl.token.ClaimsRetriever)3 Resource (org.wso2.carbon.registry.core.Resource)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 JSONParser (org.json.simple.parser.JSONParser)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 NotificationException (org.wso2.carbon.apimgt.core.exception.NotificationException)2 APIMConfigurations (org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations)1 NotificationConfigurations (org.wso2.carbon.apimgt.core.configuration.models.NotificationConfigurations)1 NotifierConfigurations (org.wso2.carbon.apimgt.core.configuration.models.NotifierConfigurations)1