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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

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.utils.tags.TagDictionary;
import ru.yandex.autotests.directapi.apiclient.config.Semaphore;
import ru.yandex.autotests.directapi.apiclient.errors.Api5ErrorDetails;
import ru.yandex.autotests.directapi.model.api5.campaigns.CampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.CpmBannerCampaignAddItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.CpmBannerCampaignBaseMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.CpmBannerCampaignStrategyMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.CpmBannerCampaignUpdateItemMap;
import ru.yandex.autotests.directapi.model.api5.campaigns.UpdateRequestMap;
import ru.yandex.autotests.directapi.model.api5.general.ExpectedResult;
import ru.yandex.autotests.directapi.model.api5.general.JavaOrPerlExpectedResult;
import ru.yandex.autotests.directapi.model.api5.general.Notification;
import ru.yandex.autotests.directapi.rules.ApiSteps;
import ru.yandex.autotests.directapi.rules.Trashman;
import ru.yandex.autotests.irt.testutils.RandomUtils;
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.apache.commons.lang3.StringUtils.capitalize;

/**
 * Created by hmepas on 20.09.18.
 */
@Aqua.Test
@Features(CampaignsFeatures.UPDATE)
@Description("Проверка валидации поля CounterIds. Негативные сценарии")
@Issue("https://st.yandex-team.ru/DIRECT-83686")
@RunWith(Parameterized.class)
@Tag(TagDictionary.TRUNK)
public class CounterIdsValidationNegativeTest {

    private static final String CLIENT = CampaignsLogins.CLIENT;

    private long campaignId;

    @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);

    @Parameterized.Parameter(0)
    public String description;

    @Parameterized.Parameter(1)
    public Integer[] counterIds;

    @Parameterized.Parameter(2)
    public JavaOrPerlExpectedResult expectedResult;

    @Parameterized.Parameters(name = "{0}")
    public static Collection testData() {
        List<Integer> overLengthCounterIds = new ArrayList<Integer>();
        for (int i = 0; i <= CpmBannerCampaignAddItemMap.MAX_AMOUNT_OF_COUNTER_IDS; i++) {
            overLengthCounterIds.add(RandomUtils.getRandomInteger(1, 1000000));
        }
        Object[][] data = new Object[][]{
                {"negative id", new Integer[]{-1}, JavaOrPerlExpectedResult
                        .java(ExpectedResult.errorsIgnoringDetails(new Notification(5005)))
                        .perl(ExpectedResult.errors(new Notification(5005,
                                Api5ErrorDetails.ADDITIONAL_METRICA_COUNTERS_INCORRECTLY_SPECIFIED, "-1")))},
                {"zero id", new Integer[]{0}, JavaOrPerlExpectedResult
                        .java(ExpectedResult.errorsIgnoringDetails(new Notification(5005)))
                        .perl(ExpectedResult.errors(new Notification(5005,
                                Api5ErrorDetails.ADDITIONAL_METRICA_COUNTERS_INCORRECTLY_SPECIFIED, "0")))},
                {"повторяющееся значение", new Integer[]{1, 1}, JavaOrPerlExpectedResult
                        .java(ExpectedResult.errorsIgnoringDetails(
                                new Notification(9802), new Notification(9802)))
                        .perl(ExpectedResult.errors(new Notification(9802,
                                Api5ErrorDetails.ELEMENT_PRESENT_IN_LIST_MORE_THAN_ONE_TIME,
                        1, capitalize(CpmBannerCampaignBaseMap.COUNTER_IDS))))},
                {"Количество элементов, превышающее максимальную длину", overLengthCounterIds.toArray(new Integer[]{}),
                        JavaOrPerlExpectedResult
                                .java(ExpectedResult.errorsIgnoringDetails(new Notification(7001)))
                                .perl(ExpectedResult.errors(new Notification(7001,
                                        Api5ErrorDetails.NO_MORE_MERTRICA_COUNTERS_CAN_BE_SPECIFIED,
                                        CpmBannerCampaignUpdateItemMap.MAX_AMOUNT_OF_COUNTER_IDS)))},
        };
        return Arrays.asList(data);
    }

    @BeforeClass
    public static void initTest() {
        api.userSteps.clientFakeSteps().fakeClearClientSpentUnits(CLIENT);
    }

    @Before
    public void createCampaign() {
        campaignId = api.userSteps.campaignSteps().addDefaultCpmBannerCampaign();
    }

    @Test
    public void addWithCounterIds() {
        api.userSteps.campaignSteps().shouldGetResultOnUpdate(new UpdateRequestMap()
                        .withCampaigns(new CampaignUpdateItemMap()
                                .defaultCampaignUpdateItem()
                                .withId(campaignId)
                                .withCpmBannerCampaign(new CpmBannerCampaignUpdateItemMap()
                                        .withBiddingStrategy(new CpmBannerCampaignStrategyMap()
                                                .defaultCpmBannerCampaignStrategy())
                                        .withCounterIds(counterIds))
                        ),
                expectedResult);
    }
}
