package com.atlassian.bitbucket.internal.plugin.extender;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.InvalidSyntaxException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import java.util.Hashtable;

import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class PluginOsgiBeanFactoryPostProcessorTest {

    private static final String ATLASSIAN_PLUGIN_KEY = "Atlassian-Plugin-Key";
    private static final String BLACKLISTED_PLUGIN_KEY = "com.atlassian.upm.atlassian-universal-plugin-manager-plugin";

    @Mock
    private ConfigurableListableBeanFactory beanFactory;
    @Mock
    private Bundle bundle;
    private Hashtable<String, String> bundleHeaders;
    @Mock
    private BundleContext bundleContext;
    private PluginOsgiBeanFactoryPostProcessor processor;

    @Before
    public void setUp() {
        when(bundleContext.getBundle()).thenReturn(bundle);

        bundleHeaders = new Hashtable<>();
        when(bundle.getHeaders()).thenReturn(bundleHeaders);

        processor = new PluginOsgiBeanFactoryPostProcessor();
    }

    @Test
    public void testIgnoredForStandardBundle() throws InvalidSyntaxException, BundleException {
        processor.postProcessBeanFactory(bundleContext, beanFactory);

        // Interception should only happen for Atlassian Plugins
        verifyZeroInteractions(beanFactory);
    }

    @Test
    public void testIgnoredForBlackListedPlugin() throws InvalidSyntaxException, BundleException {
        bundleHeaders.put(ATLASSIAN_PLUGIN_KEY, BLACKLISTED_PLUGIN_KEY);

        processor.postProcessBeanFactory(bundleContext, beanFactory);

        // Interception should not happen for blacklisted plugins
        verifyZeroInteractions(beanFactory);
    }

    @Test
    public void testInjectsBeanPostProcessorForBitbucketServerPlugins() throws InvalidSyntaxException, BundleException {
        bundleHeaders.put(ATLASSIAN_PLUGIN_KEY, "com.atlassian.bitbucket.server.bitbucket-some-plugin");

        processor.postProcessBeanFactory(bundleContext, beanFactory);

        // Interception should happen for Atlassian Plugins
        verify(beanFactory).addBeanPostProcessor(isA(BeanEventListenerScanner.class));
    }

    @Test
    public void testInjectsBeanPostProcessorForThirdPartyPlugins() throws InvalidSyntaxException, BundleException {
        bundleHeaders.put(ATLASSIAN_PLUGIN_KEY, "com.acme.some-plugin");

        processor.postProcessBeanFactory(bundleContext, beanFactory);

        // Interception should happen for Atlassian Plugins
        verify(beanFactory).addBeanPostProcessor(isA(BeanEventListenerScanner.class));
    }
}