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

import com.atlassian.bitbucket.test.rest.server.stub.webhook.StubWebHookSink;
import com.google.common.collect.ImmutableList;
import org.hamcrest.Matcher;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.util.Collections;
import java.util.function.Supplier;

import static com.atlassian.bitbucket.test.DefaultFuncTestData.getAdminPassword;
import static com.atlassian.bitbucket.test.DefaultFuncTestData.getAdminUser;
import static it.com.atlassian.stash.rest.webhook.WebHookCallbackHelper.assertWebHookCallbackArrivedOn;

/**
 * Tests that callbacks occur for the web hooks registered through the web hook REST interface.
 */
public class WebHookCallbackTest extends BaseWebHookCallbackTest {
    private static StubWebHookSink stubWebHookSink;

    @BeforeClass
    public static void setUp() throws Exception {
        stubWebHookSink = new StubWebHookSink();
        stubWebHookSink.configureDefaultResponders();

        getClassTestContext().webHook(
                WebHookCallbackTest.class.getSimpleName() + "-webhook",
                stubWebHookSink.getUrl(stubWebHookSink.getWebhookEventsPath()),
                ImmutableList.of("*"), Collections.emptyMap(), getAdminUser(), getAdminPassword());
    }

    @AfterClass
    public static void tearDown() throws Exception {
        stubWebHookSink.stop();
    }

    @Override
    protected void assertWebHookCallbackArrived(String eventId, Supplier<Matcher<String>> matcherSupplier) {
        assertWebHookCallbackArrivedOn(stubWebHookSink, eventId, matcherSupplier);
    }

}
