package com.atlassian.stash.internal.plugin.info;

import com.atlassian.analytics.api.services.AnalyticsConfigService;
import com.atlassian.bitbucket.plugin.OptionalPluginInformationProvider;
import org.eclipse.gemini.blueprint.mock.MockBundleContext;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class DefaultPluginInformationProviderTest {

    @Test
    public void testIsAnalyticsEnabled() {
        OptionalPluginInformationProvider optionalPluginInformationProvider = new DefaultPluginInformationProvider();
        assertFalse(optionalPluginInformationProvider.canCollectAnalytics());
    }

    @Test
    public void testAnalyticsDisabledWithMock() {
        BundleContext bundleContext = new MockBundleContext() {
            @Override
            public Object getService(ServiceReference reference) {
                return new AnalyticsConfigService() {

                    @Override
                    public boolean isAnalyticsEnabled() {
                        return false;
                    }

                    @Override
                    public boolean canCollectAnalytics() {
                        return false;
                    }
                };
            }
        };
        DefaultPluginInformationProvider optionalPluginInformationProvider = new DefaultPluginInformationProvider();
        optionalPluginInformationProvider.setBundleContext(bundleContext);
        assertFalse(optionalPluginInformationProvider.canCollectAnalytics());
    }

    @Test
    public void testAnalyticsEnabledWithMock() {
        BundleContext bundleContext = new MockBundleContext() {
            @Override
            public Object getService(ServiceReference reference) {
                return new AnalyticsConfigService() {

                    @Override
                    public boolean isAnalyticsEnabled() {
                        return true;
                    }

                    @Override
                    public boolean canCollectAnalytics() {
                        return true;
                    }
                };
            }
        };
        DefaultPluginInformationProvider optionalPluginInformationProvider = new DefaultPluginInformationProvider();
        optionalPluginInformationProvider.setBundleContext(bundleContext);
        assertTrue(optionalPluginInformationProvider.canCollectAnalytics());
    }
}
