Search in sources :

Example 1 with NotificationDTO

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

the class NotificationExecutor method sendAsyncNotifications.

/**
 * Executes the notifer classes in separtate threads.
 *
 * @param notificationDTO
 * @throws org.wso2.carbon.apimgt.core.exception.NotificationException
 */
public void sendAsyncNotifications(NotificationDTO notificationDTO) throws NotificationException {
    List<NotifierConfigurations> notifierConfigurations = new APIMConfigurations().getNotificationConfigurations().getNewVersionNotifierConfiguration().getNotifierConfigurations();
    if (notificationDTO.getType().equalsIgnoreCase(NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION)) {
        for (NotifierConfigurations listItem : notifierConfigurations) {
            String executorClass = listItem.getExecutorClass();
            Map property = listItem.getPropertyList();
            Properties prop = notificationDTO.getProperties();
            prop.putAll(property);
            notificationDTO.setProperties(prop);
            // starting Notifier threads
            if (executorClass != null && !executorClass.isEmpty()) {
                Notifier notifier;
                try {
                    notifier = (Notifier) APIUtils.getClassForName(executorClass).newInstance();
                } catch (InstantiationException e) {
                    throw new NotificationException("Instantiation Error while Initializing the notifier class", e);
                } catch (IllegalAccessException e) {
                    throw new NotificationException("IllegalAccess Error while Initializing the notifier class", e);
                } catch (ClassNotFoundException e) {
                    throw new NotificationException("ClassNotFound Error while Initializing the notifier class", e);
                }
                notifier.setNotificationDTO(notificationDTO);
                executor.execute(notifier);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Class " + executorClass + " Empty Or Null");
                }
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Notification Type Does Not match with " + NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
        }
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
    }
}
Also used : APIMConfigurations(org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations) NotificationException(org.wso2.carbon.apimgt.core.exception.NotificationException) NotifierConfigurations(org.wso2.carbon.apimgt.core.configuration.models.NotifierConfigurations) Properties(java.util.Properties) Map(java.util.Map) Notifier(org.wso2.carbon.apimgt.core.impl.Notifier)

Example 2 with NotificationDTO

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

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

the class NewAPIVersionEmailNotifierTest method testShouldNotThrowExceptionWhenRetrievingNotifiers.

@Test
public void testShouldNotThrowExceptionWhenRetrievingNotifiers() throws APIManagementException {
    NewAPIVersionEmailNotifier emailNotifier = new NewAPIVersionEmailNotifierWrapper(registry, claimsRetriever);
    ClaimsRetriever claimsRetriever = Mockito.mock(ClaimsRetriever.class);
    Mockito.doNothing().when(claimsRetriever).init();
    try {
        emailNotifier.getNotifierSet(notificationDTO);
    } catch (NotificationException e) {
        Assert.fail("Should not throw any exceptions");
    }
}
Also used : NewAPIVersionEmailNotifierWrapper(org.wso2.carbon.apimgt.impl.notification.util.NewAPIVersionEmailNotifierWrapper) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) Test(org.junit.Test)

Example 4 with NotificationDTO

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

the class NewAPIVersionEmailNotifierTest method setup.

@Before
public void setup() {
    notifier = new NewAPIVersionEmailNotifier();
    registry = Mockito.mock(Registry.class);
    claimsRetriever = Mockito.mock(ClaimsRetriever.class);
    Subscriber subscriber = new Subscriber(ADMIN);
    Set<Subscriber> subscribersOfAPI = new HashSet<Subscriber>();
    subscribersOfAPI.add(subscriber);
    Properties properties = new Properties();
    properties.put(NotifierConstants.API_KEY, new APIIdentifier(ADMIN, API_NAME, "1.0.0"));
    properties.put(NotifierConstants.NEW_API_KEY, new APIIdentifier(ADMIN, API_NAME, "2.0.0"));
    properties.put(NotifierConstants.TITLE_KEY, "New Version");
    properties.put(NotifierConstants.SUBSCRIBERS_PER_API, subscribersOfAPI);
    properties.put(NotifierConstants.CLAIMS_RETRIEVER_IMPL_CLASS, "org.wso2.carbon.apimgt.impl.token.DefaultClaimsRetriever");
    properties.put(NotifierConstants.TEMPLATE_KEY, "<html>$1</html>");
    notificationDTO = new NotificationDTO(properties, NotifierConstants.NOTIFICATION_TYPE_NEW_VERSION);
    notificationDTO.setTenantID(TENANT_ID);
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Registry(org.wso2.carbon.registry.core.Registry) ClaimsRetriever(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever) Before(org.junit.Before)

Example 5 with NotificationDTO

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

the class NewAPIVersionEmailNotifierTest method testShouldLoadMessageTemplateWhenDtoValid.

@Test
public void testShouldLoadMessageTemplateWhenDtoValid() 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).thenReturn(false);
    Mockito.when(registry.get(Mockito.anyString())).thenReturn(resource);
    Mockito.when(resource.getContent()).thenReturn("<html>$1-$2-$3</html>".getBytes());
    try {
        NotificationDTO notification = emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.assertFalse(!notification.getMessage().contains(API_NAME));
        // should use dto template if resource not found in registry
        notification = emailNotifier.loadMessageTemplate(notificationDTO);
        Assert.assertFalse(!notification.getMessage().contains(API_NAME));
    } catch (NotificationException e) {
        Assert.fail("Should not throw any exceptions");
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) Test(org.junit.Test)

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