package com.atlassian.bitbucket.internal.webhook.json;

import org.junit.Test;

import static org.junit.Assert.*;

public class JsonWebHookFilterParametersHelperTest {
    @Test
    public void testNullJson() throws Exception {
        assertNotNull(JsonWebHookParametersHelper.toFilterParameters(null));
        assertEquals(0, JsonWebHookParametersHelper.toFilterParameters(null).size());
    }

    @Test
    public void testNoJson() throws Exception {
        assertNotNull(JsonWebHookParametersHelper.toFilterParameters(""));
        assertEquals(0, JsonWebHookParametersHelper.toFilterParameters("").size());
    }

    @Test
    public void testGarbageJson() throws Exception {
        assertNotNull(JsonWebHookParametersHelper.toFilterParameters("sdf67v sdf67"));
        assertEquals(0, JsonWebHookParametersHelper.toFilterParameters("sdf67v sdf67").size());
    }

    @Test
    public void testEmptyJsonObject() throws Exception {
        assertNotNull(JsonWebHookParametersHelper.toFilterParameters("{}"));
        assertEquals(0, JsonWebHookParametersHelper.toFilterParameters("{}").size());
    }

    @Test
    public void testProjectIdParameter() throws Exception {
        JsonWebHookFilterParameters projectParam = JsonWebHookParametersHelper.toFilterParameters("{\"projectId\": 1}");
        assertNotNull(projectParam);
        assertTrue(projectParam.getProjectId().isPresent());
        assertEquals(1, projectParam.getProjectId().get().intValue());
    }

    @Test
    public void testRepositoryIdParameter() throws Exception {
        JsonWebHookFilterParameters repositoryParam = JsonWebHookParametersHelper.toFilterParameters("{\"repositoryId\": 1}");
        assertNotNull(repositoryParam);
        assertTrue(repositoryParam.getRepositoryId().isPresent());
        assertEquals(1, repositoryParam.getRepositoryId().get().intValue());
        assertFalse(repositoryParam.getProjectId().isPresent());
    }
}