/*
 * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
 *
 * Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.mqtt;

import io.entgra.device.mgt.core.device.mgt.common.push.notification.NotificationStrategy;
import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationConfig;
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.mqtt.internal.MQTTDataHolder;
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.mqtt.internal.util.MQTTAdapterConstants;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException;
import org.wso2.carbon.event.output.adapter.core.internal.CarbonOutputEventAdapterService;
import org.wso2.carbon.registry.core.exceptions.RegistryException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Unit tests for MQTTBasedPushNotificationProvider class
 */
public class MQTTBasedPushNotificationProviderTest {
    private MQTTBasedPushNotificationProvider mqttBasedPushNotificationProvider;
    private CarbonOutputEventAdapterService carbonOutputEventAdapterService;
    private static final String ADAPTER_NAME = "SampleMqttAdapterName";
    private static final String BROKER_URL = "SampleBrokerUrl";

    @BeforeClass
    public void init() throws NoSuchFieldException, IllegalAccessException, IOException, RegistryException,
            OutputEventAdapterException {
        CarbonContextInitializationUtil.initializeCarbonContext();
        mqttBasedPushNotificationProvider = Mockito.mock(MQTTBasedPushNotificationProvider.class,
                Mockito.CALLS_REAL_METHODS);
        carbonOutputEventAdapterService = Mockito.mock(CarbonOutputEventAdapterService.class,
                Mockito.CALLS_REAL_METHODS);
        Mockito.doReturn(true).when(carbonOutputEventAdapterService).isPolled(ADAPTER_NAME);
    }

    @Test(description = "test getType method")
    public void testGetType() {
        String type = mqttBasedPushNotificationProvider.getType();
        Assert.assertEquals(type, "MQTT");
    }

    @Test(description = "test get notification strategy method")
    public void getNotificationStrategy() {
        Map<String, String> properties = new HashMap<>();
        properties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, BROKER_URL);
        properties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME, ADAPTER_NAME);
        PushNotificationConfig pushNotificationConfig = new PushNotificationConfig("MQTT", true, properties);
        MQTTDataHolder mqttDataHolder = MQTTDataHolder.getInstance();
        mqttDataHolder.setOutputEventAdapterService(carbonOutputEventAdapterService);
        NotificationStrategy notificationStrategy = mqttBasedPushNotificationProvider.
                getNotificationStrategy(pushNotificationConfig);
        Assert.assertNotNull(notificationStrategy, "null notificationStrategyReceived");
    }
}
