package it.com.atlassian.stash.rest.analytics;

import com.atlassian.bitbucket.test.AnalyticsTestHelper;
import com.atlassian.bitbucket.test.annotations.CompatibleWithProductTopologies;
import it.com.atlassian.stash.BaseRestTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

import static com.atlassian.bitbucket.test.ProductTopology.BITBUCKET_STANDALONE;
import static org.junit.Assert.assertTrue;

//This test requires node affinity which isn't possible with the REST test suite
@CompatibleWithProductTopologies(BITBUCKET_STANDALONE)
public class AnalyticsPluginRestTest extends BaseRestTest {

    @Before
    public void setup() {
        AnalyticsTestHelper.enableEventLogging();
    }

    @After
    public void tearDown() {
        AnalyticsTestHelper.disableEventLogging();
    }

    @Test
    public void testAnalytics() {
        // It's difficult to test the entire analytics flow to EC2 and it's not our responsibility.  This is a
        // smoke test to ensure the analytics plugin receives Stash events and has the whitelist.
        // We're trusting the analytics plugin will send the event.
        testContext.project("ANALYTICS", "Analytics Test Project");

        Map<String, Object> event = AnalyticsTestHelper.findWhitelistedEvent("stash.project.created");
        assertTrue("Project ID should be an attribute on the event",
                event.containsKey("properties") && ((Map) event.get("properties")).containsKey("project.id"));
    }
}
