package ru.yandex.autotests.directintapi.bstransport.main.performance.context.filter;

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.cmd.rules.DirectCmdRule;
import ru.yandex.autotests.direct.cmd.rules.PerformanceBannersRule;
import ru.yandex.autotests.direct.db.models.jooq.ppc.tables.records.BidsPerformanceRecord;
import ru.yandex.autotests.directapi.darkside.Logins;
import ru.yandex.autotests.directapi.darkside.connection.Semaphore;
import ru.yandex.autotests.directapi.darkside.model.RunBsTransportScriptResponse;
import ru.yandex.autotests.directapi.darkside.model.bslogs.clientdata.Campaign;
import ru.yandex.autotests.directapi.darkside.model.bslogs.clientdata.Context;
import ru.yandex.autotests.directapi.darkside.model.bslogs.clientdata.Filter;
import ru.yandex.autotests.direct.utils.tags.TagDictionary;
import ru.yandex.autotests.directapi.rules.ApiSteps;
import ru.yandex.autotests.directapi.rules.Trashman;
import ru.yandex.autotests.directintapi.bstransport.FeatureNames;
import ru.yandex.autotests.directintapi.bstransport.TransportHelpSteps;
import ru.yandex.qatools.Tag;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Issue;
import ru.yandex.qatools.allure.annotations.Title;
import ru.yandex.qatools.hazelcast.SemaphoreRule;

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

/**
 * Created by buhter on 19.11.2015
 * https://st.yandex-team.ru/TESTIRT-7742
 * https://st.yandex-team.ru/TESTIRT-10194
 */

@Aqua.Test
@Tag(TagDictionary.TRUNK)
@Tag(TagDictionary.RELEASE)
@Features(FeatureNames.SMART_BANNERS)
@Issue("https://st.yandex-team.ru/DIRECT-48299")
@Title("Отправка в БК параметров для подстановки в URL")
public class BsTransportOfPerformanceFilterURLParamsTest {

    private static final String LOGIN = Logins.LOGIN_TRANSPORT;
    @ClassRule
    public static ApiSteps api = new ApiSteps().as(LOGIN);
    @ClassRule
    public static SemaphoreRule semaphore = Semaphore.getSemaphore();
    private static PerformanceBannersRule bannersRule = new PerformanceBannersRule().withUlogin(LOGIN);
    @ClassRule
    public static DirectCmdRule cmdRule = DirectCmdRule.defaultRule().as(Logins.LOGIN_SUPER).withRules(bannersRule);
    private static TransportHelpSteps helpSteps = new TransportHelpSteps(api);
    private static Filter filter;
    private static BidsPerformanceRecord bidsPerformance;
    @Rule
    public Trashman trashman = new Trashman(api);

    @BeforeClass
    public static void before() {
        int shard = api.userSteps.clientFakeSteps().getUserShard(LOGIN);
        helpSteps.makePerformanceCampaignReadyForSendingToBS(bannersRule, shard);

        bidsPerformance = api.userSteps.getDirectJooqDbSteps().useShard(shard)
                .bidsPerformanceSteps().getBidsPerformanceByPid(bannersRule.getGroupId()).get(0);

        RunBsTransportScriptResponse resp = api.userSteps.getDarkSideSteps().getTransportSteps().sendNewCampaign(shard,
                bannersRule.getCampaignId());
        Campaign campaign = api.userSteps.getDarkSideSteps().getTransportSteps().getClientDataRequestCampaign(resp, 0,
                bannersRule.getCampaignId());
        Context context = campaign != null ? campaign.getContext(bannersRule.getGroupId()) : null;
        assumeThat("в БК отправился CONTEXT"
                , context, notNullValue());
        filter = context.getFilter(bidsPerformance.getPerfFilterId().intValue());
        assumeThat("в БК на уровне CONTEXT отправился FILTER для pid=" + bannersRule.getGroupId()
                , filter, notNullValue());
        assumeThat("в БК на уровне FILTER отправились параметры", filter.getParams(), notNullValue());
    }


    @Test
    public void performanceContextFilterParams() {
        assertThat("отправилось правильное количество параметров", filter.getParams().size(), equalTo(2));
    }

    @Test
    public void performanceContextFilterParamAdTargetName() {
        assertThat("отправилось правильное значения для параметра " + Filter.PARAM126
                , filter.getParams().get(Filter.PARAM126), equalTo(bidsPerformance.getName()));
    }

    @Test
    public void performanceContextFilterParamAdTargetId() {
        assertThat("отправилось правильное значения для параметра " + Filter.PARAM127
                , filter.getParams().get(Filter.PARAM127), equalTo(bidsPerformance.getPerfFilterId().toString()));
    }
}
