summaryrefslogtreecommitdiff
path: root/battleNetApi/tests/tests.cpp
blob: b79a37be1a753c5e10b9f0cbcc0bd0d8c0310591 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "apiUrl.hpp"
#include "hearthstoneApi.hpp"
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/benchmark/catch_chronometer.hpp>
#include <catch2/benchmark/detail/catch_analyse.hpp>
#include <catch2/benchmark/detail/catch_benchmark_function.hpp>
#include <catch2/benchmark/detail/catch_estimate_clock.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_config.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cstdint>
#include <fstream>
#include <nlohmann/json.hpp>
#include <string>
#include <tokenPool.hpp>

TEST_CASE("Retrieve API Token", "[generateApiToken]") {
    const std::string kCredentialsFile{"tests/resources/credentials.json"};
    Token aToken(kCredentialsFile);
    const std::string kTokenContent{aToken.text()};
    const std::string::size_type kTokenSize{kTokenContent.size()};
    REQUIRE(kTokenSize >= 30);
    REQUIRE(kTokenSize <= 40);
}

TEST_CASE("Retrieve number of cards", "[countCards]") {
    const std::string kCredentialsFile{};
    const std::string kContentType{"cards"};
    const std::string kRegion{"eu"};
    ApiUrl aApiUrl(kContentType, kRegion);
    aApiUrl.setLocale("fr_FR");
    aApiUrl.setGameMode("constructed");
    aApiUrl.setPageSize("1");
    TokenPool::feedTokenSource("tests/resources/credentials.json");
    const json kResponse = HearthstoneApi::makeApiRequest(
        aApiUrl, TokenPool::availableToken().value().text());
    REQUIRE(kResponse.size() > 0);
    REQUIRE(kResponse.contains("pageCount"));
    std::uint64_t kNumberOfCards{kResponse["pageCount"]};
    std::uint64_t kExpectedNumberOfCards{6178};
    REQUIRE(kNumberOfCards == kExpectedNumberOfCards);
}

TEST_CASE("Retrieve page once", "[retrievePageOnce]") {
    const std::string kCredentialsFile{"tests/resources/credentials.json"};
    Token aToken(kCredentialsFile);
    const std::string kContentType{"cards"};
    const std::string kRegion{"eu"};
    ApiUrl aApiUrl(kContentType, kRegion);
    aApiUrl.setLocale("fr_FR");
    aApiUrl.setGameMode("constructed");
    aApiUrl.setPageSize("16");
    TokenPool::feedTokenSource("tests/resources/credentials.json");
    const json kApiResponse = HearthstoneApi::makeApiRequest(
        aApiUrl, TokenPool::availableToken().value().text());
    REQUIRE(kApiResponse["cards"].size() == 16);
}

TEST_CASE("Retrieve pages 10 times without parallelization",
          "[retrievePagesNoParallel]") {
    const std::string kCredentialsFile{"tests/resources/credentials.json"};
    Token aToken(kCredentialsFile);
    const std::string kContentType{"cards"};
    const std::string kRegion{"eu"};
    ApiUrl aApiUrl(kContentType, kRegion);
    aApiUrl.setLocale("fr_FR");
    aApiUrl.setGameMode("constructed");
    aApiUrl.setPageSize("16");
    aApiUrl.setPage("1");
    TokenPool::feedTokenSource("tests/resources/credentials.json");
    std::string kTokenText{TokenPool::availableToken().value().text()};
    json kApiResponse = HearthstoneApi::makeApiRequest(aApiUrl, kTokenText);

    // Retrieve number of pages
    const int kNumberOfRetrievals{10};
    const int kNumberOfPages{kApiResponse["pageCount"]};
    REQUIRE(kNumberOfPages >= kNumberOfRetrievals);
    for (int i{1}; i < kNumberOfRetrievals; ++i) {
        REQUIRE(kApiResponse["cards"].size() == 16);
        aApiUrl.setPage(std::to_string(i + 1));
        kApiResponse = HearthstoneApi::makeApiRequest(aApiUrl, kTokenText);
    }
    REQUIRE(kApiResponse["cards"].size() >= 1);
    REQUIRE(kApiResponse["cards"].size() <= 16);
}

TEST_CASE("Retrieve pages 10 times with parallelization",
          "[retrievePagesParallel]") {
    const std::string kCredentialsFile{"tests/resources/credentials.json"};
    Token aToken(kCredentialsFile);
    const std::string kContentType{"cards"};
    const std::string kRegion{"eu"};
    ApiUrl aApiUrl(kContentType, kRegion);
    aApiUrl.setLocale("fr_FR");
    aApiUrl.setGameMode("constructed");
    aApiUrl.setPageSize("16");
    aApiUrl.setPage("1");
    std::vector<std::string> aRequests;
    TokenPool::feedTokenSource("tests/resources/credentials.json");
    std::string kTokenText{TokenPool::availableToken().value().text()};
    json kFirstPageJson = HearthstoneApi::makeApiRequest(aApiUrl, kTokenText);

    int kNumberOfPages{kFirstPageJson["pageCount"]};
    const int kNumberOfRetrievals{10};
    REQUIRE(kNumberOfPages >= kNumberOfRetrievals);
    for (int i{1}; i < kNumberOfRetrievals; ++i) {
        aApiUrl.setPage(std::to_string(i + 1));
        aRequests.push_back(aApiUrl.text());
    }
    std::vector<json> kApiResponses =
        HearthstoneApi::makeApiRequest(aRequests, kTokenText);
    kApiResponses.emplace(kApiResponses.begin(), kFirstPageJson);
    REQUIRE(kApiResponses.size() == kNumberOfRetrievals);
    for (const json &kApiResponse : kApiResponses) {
        REQUIRE(kApiResponse["cards"].size() == 16);
    }
}

TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") {
    std::ifstream aFirstPage{"tests/resources/page_merge_00.json"};
    json kFirstPage = json::parse(aFirstPage);
    std::ifstream aSecondPage{"tests/resources/page_merge_01.json"};
    const json kSecondPage = json::parse(aSecondPage);

    json::reference aMergedCards = kFirstPage["cards"];
    for (auto &card : kSecondPage["cards"]) {
        aMergedCards.push_back(std::move(card));
    }
    REQUIRE(aMergedCards.size() == 32);
    std::ofstream aOutputJson("build/output_pages_merge.json");
    aOutputJson << aMergedCards.dump(4);
}

TEST_CASE("Retrieve a single page with HearthstoneApi::downloadCards",
          "[retrievePageOnceHearthstoneApi]") {
    const std::string kContentType{"cards"};
    const std::string kRegion{"eu"};
    ApiUrl aApiUrl(kContentType, kRegion);
    aApiUrl.setPage(3);
    TokenPool::feedTokenSource("tests/resources/credentials.json");
    json kApiResponse = HearthstoneApi::makeApiRequest(
        aApiUrl, TokenPool::availableToken().value().text());
    const json::size_type kExpectedNumberOfCards{kApiResponse["cards"].size()};
    TokenPool::feedTokenSource("tests/resources/credentials.json");
    kApiResponse = HearthstoneApi::downloadCards(
        aApiUrl, TokenPool::availableToken().value());
    const json::size_type kCurrentNumberOfCards{kApiResponse["cards"].size()};
    REQUIRE(kCurrentNumberOfCards == kExpectedNumberOfCards);
}