package com.atlassian.bitbucket.test.rest.server.stub.webhook;

import com.atlassian.bitbucket.test.rest.server.stub.mirror.StubServerResponder;
import com.atlassian.bitbucket.test.rest.server.stub.StubServer;
import org.hamcrest.Matcher;

import static com.confluex.mock.http.matchers.HttpMatchers.*;

/**
 * A subclass of {@link StubServer} that receives web hook event callbacks.
 */
public class StubWebHookSink extends StubServer {
    @Override
    public void configureDefaultResponders() {
        onWebhookEventCallback(statusCode(200));
    }

    public String getWebhookEventsPath() {
        return "/";
    }

    public <S extends StubWebHookSink> void onWebhookEventCallback(StubServerResponder<S> responder) {
        respondTo(post(getWebhookEventsPath()), responder);
    }

    public boolean waitForWebhookEventCallback(Matcher<String> bodyMatcher, long timeout) {
        return server.waitFor(post().and(path(getWebhookEventsPath())).and(body(bodyMatcher)), timeout);
    }
}
