~aleteoryx/muditaos

ref: a405cad694b867fcd2498984830bd97d4b9bde2f muditaos/module-utils/phonenumber/tests/unittest_numbermatcher.cpp -rw-r--r-- 5.8 KiB
a405cad6Aleteoryx trim readme 8 days ago
                                                                                
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
150
151
152
153
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <catch2/catch.hpp>

#include "country.hpp"
#include "PhoneNumber.hpp"
#include "NumberHolderMatcher.hpp"

#include <initializer_list>
#include <string>
#include <vector>

using namespace utils;

class DummyHolder
{
    PhoneNumber number;
    static std::uint32_t dummyHolderCount; // init by 0
    std::uint32_t contactID;

  public:
    DummyHolder(const PhoneNumber &number, const std::uint32_t contactID = 0u) : number(number), contactID(contactID)
    {
        if (contactID == 0)
            this->contactID = ++dummyHolderCount;
    }
    const PhoneNumber &getNumber() const
    {
        return number;
    }

    const decltype(contactID) &getContactID() const
    {
        return contactID;
    }
};
std::uint32_t DummyHolder::dummyHolderCount = 0u;

static constexpr auto DefaultPageSize = 1; // so the paging mechanism is checked.

const static std::string NumberAPlNational = "600123456";
const static std::string NumberAPlE164     = "+48600123456";
const static PhoneNumber NumberAPlValid(NumberAPlE164, country::Id::UNKNOWN);
const static PhoneNumber NumberAPlInvalid(NumberAPlNational, country::Id::UNKNOWN);
const static PhoneNumber NumberAPlValidNational(NumberAPlNational, country::Id::POLAND);

const static std::string NumberBPlNational = "500123456";
const static std::string NumberBPlE164     = "+48500123456";
const static PhoneNumber NumberBPlValid(NumberBPlE164, country::Id::UNKNOWN);
const static PhoneNumber NumberBPlInvalid(NumberBPlNational, country::Id::UNKNOWN);
const static PhoneNumber NumberBPlValidNational(NumberBPlNational, country::Id::POLAND);

const static std::string NumberCPlNational = "500555555";
const static std::string NumberCPlE164     = "+48500555555";
const static PhoneNumber NumberCPlValid(NumberCPlE164, country::Id::UNKNOWN);
const static PhoneNumber NumberCPlInvalid(NumberCPlNational, country::Id::UNKNOWN);
const static PhoneNumber NumberCPlValidNational(NumberCPlNational, country::Id::POLAND);

const static PhoneNumber invalidNumber("456", country::Id::UNKNOWN);

std::initializer_list<PhoneNumber> all_test_numbers = {NumberAPlValid,
                                                       NumberAPlInvalid,
                                                       NumberAPlValidNational,
                                                       NumberBPlValid,
                                                       NumberBPlInvalid,
                                                       NumberBPlValidNational,
                                                       invalidNumber};

auto make_test_vector(std::initializer_list<PhoneNumber> lnumber)
{
    std::vector<DummyHolder> v;
    for (auto &number : lnumber) {
        v.push_back(DummyHolder(number));
    }

    return v;
}

auto retrieve_numbers(const std::vector<DummyHolder> &holders, unsigned int offset, unsigned int limit)
{
    const auto size = holders.size();
    if (offset > size - 1) {
        return std::vector<DummyHolder>{};
    }
    auto lastIndex = std::clamp<unsigned int>(offset + limit, offset, size);
    return std::vector<DummyHolder>(holders.begin() + offset, holders.begin() + lastIndex);
}

TEST_CASE("Number matcher - basics")
{
    std::vector<DummyHolder> numberHolders = {DummyHolder(PhoneNumber("500123456")),
                                              DummyHolder(PhoneNumber("600123456"))};
    auto provider = [&numberHolders]([[maybe_unused]] const utils::PhoneNumber &number, auto offset, auto limit) {
        return retrieve_numbers(numberHolders, offset, limit);
    };
    NumberHolderMatcher<std::vector, DummyHolder> matcher{std::move(provider), DefaultPageSize};

    auto match = matcher.bestMatch(PhoneNumber("600123456"));
    REQUIRE(match.has_value());
    REQUIRE(match->getNumber().get() == "600123456");

    // match again to see if search is started at the beginning again
    auto newMatch = matcher.bestMatch(PhoneNumber("600123456"));
    REQUIRE(newMatch.has_value());
    REQUIRE(newMatch->getNumber().get() == "600123456");
}

TEST_CASE("Number matcher - match incoming (full list)")
{
    auto dummyHolders = make_test_vector(all_test_numbers);
    auto provider     = [&dummyHolders]([[maybe_unused]] const utils::PhoneNumber &number, auto offset, auto limit) {
        return retrieve_numbers(dummyHolders, offset, limit);
    };
    NumberHolderMatcher<std::vector, DummyHolder> matcher{std::move(provider), DefaultPageSize};

    SECTION("Incoming e164")
    {
        auto match = matcher.bestMatch(PhoneNumber("+48600123456"));
        REQUIRE(match.has_value());
        REQUIRE(match->getNumber().get() == NumberAPlE164);
    }

    SECTION("Incoming national")
    {
        auto match = matcher.bestMatch(PhoneNumber("600123456", country::Id::UNKNOWN));
        REQUIRE(match.has_value());
        REQUIRE(match->getNumber().get() == NumberAPlNational);
    }

    SECTION("Incoming valid national")
    {
        auto match = matcher.bestMatch(PhoneNumber("600123456", country::Id::POLAND));
        REQUIRE(match.has_value());
        REQUIRE(match->getNumber().get() == NumberAPlE164);
    }
}

TEST_CASE("Number matcher - match incoming (loose)")
{
    auto dummyHolders = make_test_vector({NumberAPlInvalid, NumberBPlInvalid, NumberCPlInvalid});
    auto provider     = [&dummyHolders]([[maybe_unused]] const utils::PhoneNumber &number, auto offset, auto limit) {
        return retrieve_numbers(dummyHolders, offset, limit);
    };
    NumberHolderMatcher<std::vector, DummyHolder> matcher{std::move(provider), DefaultPageSize};

    auto match = matcher.bestMatch(PhoneNumber("+48500123456"));
    REQUIRE(!match.has_value());

    match = matcher.bestMatch(PhoneNumber("+48500123456"), PhoneNumber::Match::POSSIBLE);
    REQUIRE(match.has_value());
    REQUIRE(match->getNumber().get() == NumberBPlNational);
}