package ru.yandex.autotests.direct.api.campaigns.update;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import ru.yandex.aqua.annotations.project.Aqua;
import ru.yandex.autotests.direct.api.campaigns.CampaignsFeatures;
import ru.yandex.autotests.direct.api.campaigns.CampaignsLogins;
import ru.yandex.autotests.direct.db.models.jooq.ppc.tables.records.CampaignsRecord;
import ru.yandex.autotests.direct.db.steps.DirectJooqDbSteps;
import ru.yandex.autotests.direct.utils.tags.TagDictionary;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.model.api5.campaigns.CampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.UpdateRequestMap;
import ru.yandex.autotests.directapi.model.api5.general.ExpectedResult;
import ru.yandex.autotests.directapi.rules.ApiSteps;
import ru.yandex.autotests.directapi.rules.Trashman;
import ru.yandex.autotests.irt.testutils.json.JsonUtils;
import ru.yandex.qatools.Tag;
import ru.yandex.qatools.allure.annotations.Description;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Issue;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

import static org.hamcrest.Matchers.equalTo;
import static ru.yandex.autotests.irt.testutils.allure.TestSteps.assertThat;

/**
 * Created by ginger on 22.03.16.
 * https://st.yandex-team.ru/TESTIRT-6809
 */
@Aqua.Test
@Issue("https://st.yandex-team.ru/DIRECT-52067")
@Features(CampaignsFeatures.UPDATE)
@Description("Сохранение в базу SSP площадок в поле ExcludedSites")
@Tag(TagDictionary.TRUNK)
public class UpdateExcludedSitesDBTest {
    private static String client = CampaignsLogins.UPDATE_CLIENT;
    private static final String SSP_PLATFORM_DEFAULT = "Rubicon";
    private static final String SSP_PLATFORM_DOMAIN_FORMAT = "sspplatform.ru";
    private static final String DOMAIN = "qwerty.ru";
    private static Long id;
    private static CampaignsRecord campaigns;

    @ClassRule
    public static ApiSteps api = new ApiSteps().version(5).as(client);

    @ClassRule
    public static SemaphoreRule semaphore = Semaphore.getSemaphore();

    @Rule
    public Trashman trasher = new Trashman(api);

    @BeforeClass
    public static void createCampaign() {
        DirectJooqDbSteps jooqDbSteps = api.userSteps.getDirectJooqDbSteps();
        jooqDbSteps.sspPlatformsSteps().saveSspPlatformsIfNotExist(SSP_PLATFORM_DEFAULT);
        jooqDbSteps.sspPlatformsSteps().saveSspPlatformsIfNotExist(SSP_PLATFORM_DOMAIN_FORMAT);
        int shard = api.userSteps.clientFakeSteps().getUserShard(client);
        id = api.userSteps.campaignSteps().addDefaultTextCampaign();
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .withId(id)
                                .withExcludedSites(DOMAIN, SSP_PLATFORM_DEFAULT, SSP_PLATFORM_DOMAIN_FORMAT)
                        ),
                ExpectedResult.success(id));

        campaigns = api.userSteps.getDirectJooqDbSteps().useShard(shard).campaignsSteps().getCampaignById(id);
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("929")
    public void updateWithExcludedSitesAndCheckSaveOfDontShowColumn() {
        assertThat("верно сохранился домен", campaigns.getDontshow(),
                equalTo(String.join(",", DOMAIN, SSP_PLATFORM_DOMAIN_FORMAT)));
    }

    @Test
    @ru.yandex.qatools.allure.annotations.TestCaseId("930")
    public void updateWithExcludedSitesAndCheckSaveOfDisabledSsp() {
        assertThat("верно сохранилась SSP площадка", campaigns.getDisabledSsp(),
                equalTo(JsonUtils
                        .toStringDisabledHtmlEscaping(new String[]{SSP_PLATFORM_DEFAULT, SSP_PLATFORM_DOMAIN_FORMAT},
                                true)));
    }
}
