Search in sources :

Example 6 with Notifier

use of org.wso2.carbon.apimgt.impl.notifier.Notifier in project carbon-apimgt by wso2.

the class NotificationExecutorTest method testShouldNotThrowExceptionWhenNotificationTypeNotRegistered.

@Test
public void testShouldNotThrowExceptionWhenNotificationTypeNotRegistered() throws Exception {
    NotificationDTO notificationDTO = new NotificationDTO(new Properties(), "new_subscription");
    notificationDTO.setTenantDomain(TENANT_DOMAIN);
    notificationDTO.setTenantID(TENANT_ID);
    Notifier notifier = Mockito.mock(Notifier.class);
    Mockito.doNothing().when(notifier).run();
    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 7 with Notifier

use of org.wso2.carbon.apimgt.impl.notifier.Notifier in project carbon-apimgt by wso2.

the class NotificationExecutorTest method testShouldThrowExceptionsWhenRegistryErrorOccurs.

@Test(expected = NotificationException.class)
public void testShouldThrowExceptionsWhenRegistryErrorOccurs() 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).thenThrow(APIManagementException.class);
    notificationExecutor.sendAsyncNotifications(notificationDTO);
    Assert.fail("Should throw an exception");
}
Also used : APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Properties(java.util.Properties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with Notifier

use of org.wso2.carbon.apimgt.impl.notifier.Notifier in project carbon-apimgt by wso2.

the class NotificationExecutor method sendAsyncNotifications.

/**
 * Executes the notifer classes in separtate threads.
 * @param notificationDTO
 * @throws NotificationException
 */
public void sendAsyncNotifications(NotificationDTO notificationDTO) throws NotificationException {
    JSONObject notificationConfig;
    String notificationType = notificationDTO.getType();
    try {
        notificationConfig = APIUtil.getTenantConfig(notificationDTO.getTenantDomain());
        JSONArray notificationArray = (JSONArray) notificationConfig.get(NotifierConstants.Notifications_KEY);
        for (Object notification : notificationArray) {
            JSONObject notificationJson = (JSONObject) notification;
            String notifierType = (String) notificationJson.get(NotifierConstants.TYPE_KEY);
            if (notificationType.equals(notifierType)) {
                JSONArray notifierArray = (JSONArray) notificationJson.get("Notifiers");
                for (Object notifier : notifierArray) {
                    JSONObject jsonNotifier = (JSONObject) notifier;
                    String notifierClass = (String) jsonNotifier.get("Class");
                    Map map = (Map) jsonNotifier;
                    Properties prop = notificationDTO.getProperties();
                    prop.putAll(map);
                    notificationDTO.setProperties(prop);
                    // starting Notifier threads
                    if (notifierClass != null && !notifierClass.isEmpty()) {
                        Notifier notfier = (Notifier) APIUtil.getClassInstance(notifierClass);
                        notfier.setNotificationDTO(notificationDTO);
                        notfier.setTenantDomain(notificationDTO.getTenantDomain());
                        Thread notificationThread = new Thread(notfier);
                        notificationThread.start();
                    }
                }
            }
        }
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException | APIManagementException e) {
        throw new NotificationException("Error while Initializing the notifier class", e);
    }
}
Also used : JSONArray(org.json.simple.JSONArray) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) Properties(java.util.Properties) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) Map(java.util.Map)

Example 9 with Notifier

use of org.wso2.carbon.apimgt.impl.notifier.Notifier in project carbon-apimgt by wso2.

the class ExternallyDeployedApiNotifier method process.

/**
 * Process API lifecycle notifier events related to APIs deployed in external gateway
 *
 * @param event related to deployments
 * @throws NotifierException if error occurs when casting event
 */
private void process(Event event) throws NotifierException {
    APIEvent apiEvent;
    apiEvent = (APIEvent) event;
    if (APIConstants.EventType.API_LIFECYCLE_CHANGE.name().equals(event.getType())) {
        // Handle API retiring life cycle change in external gateway
        undeployApiWhenRetiring(apiEvent);
    } else if (APIConstants.EventType.API_DELETE.name().equals(event.getType())) {
        // Handle API deletion in external gateway
        undeployWhenDeleting(apiEvent);
    }
}
Also used : APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent)

Example 10 with Notifier

use of org.wso2.carbon.apimgt.impl.notifier.Notifier in project carbon-apimgt by wso2.

the class SolaceKeyGenNotifier method process.

/**
 * Process Application notifier event related to key generation related to Solace Applications
 *
 * @param event related to Key generation handling
 * @throws NotifierException if error occurs when casting event
 */
private void process(Event event) throws NotifierException {
    ApplicationRegistrationEvent applicationRegistrationEvent;
    applicationRegistrationEvent = (ApplicationRegistrationEvent) event;
    if (APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name().equals(event.getType())) {
        syncSolaceApplicationClientId(applicationRegistrationEvent);
    }
}
Also used : ApplicationRegistrationEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationRegistrationEvent)

Aggregations

Properties (java.util.Properties)6 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 NotificationException (org.wso2.carbon.apimgt.impl.notification.exception.NotificationException)4 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 Before (org.junit.Before)2 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)2 Notifier (org.wso2.carbon.apimgt.impl.notifier.Notifier)2 CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)1 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)1 Matchers.anyString (org.mockito.Matchers.anyString)1 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1