1 #include "helpers.hpp"
2 #include "mocks/sensor_mock.hpp"
3 #include "sensor_cache.hpp"
4 
5 #include <initializer_list>
6 
7 #include <gmock/gmock.h>
8 
9 using namespace testing;
10 
11 class TestSensorCache : public Test
12 {
13   public:
14     SensorCache sut;
15 };
16 
17 struct IdParam
18 {
19     IdParam() = default;
IdParamIdParam20     IdParam(std::string_view service, std::string_view path) :
21         service(service), path(path)
22     {}
23 
PrintTo(const IdParam & param,std::ostream * os)24     friend void PrintTo(const IdParam& param, std::ostream* os)
25     {
26         *os << "(" << param.service << "): " << param.path;
27     }
28 
29     std::string service;
30     std::string path;
31 };
32 
33 class TestSensorCacheP :
34     public TestSensorCache,
35     public WithParamInterface<std::vector<IdParam>>
36 {
37   public:
SetUp()38     void SetUp() override
39     {
40         auto vec = GetParam();
41         ASSERT_THAT(vec, SizeIs(param.size()));
42         std::copy(vec.begin(), vec.end(), param.begin());
43     }
44 
45     template <size_t index>
id() const46     const IdParam& id() const
47     {
48         static_assert(index < std::tuple_size_v<decltype(param)>);
49         return param[index];
50     }
51 
52   private:
53     std::array<IdParam, 2> param;
54 };
55 
56 INSTANTIATE_TEST_SUITE_P(
57     UniqueIds, TestSensorCacheP,
58     Values(std::vector<IdParam>({IdParam("service1", "path1"),
59                                  IdParam("service1", "path2")}),
60            std::vector<IdParam>({IdParam("service1", "path1"),
61                                  IdParam("service2", "path1")}),
62            std::vector<IdParam>({IdParam("service1", "path1"),
63                                  IdParam("service2", "path2")})));
64 
TEST_P(TestSensorCacheP,shouldReturnDifferentSensorWhenIdsAreDifferent)65 TEST_P(TestSensorCacheP, shouldReturnDifferentSensorWhenIdsAreDifferent)
66 {
67     auto sensor1 = sut.makeSensor<NiceMock<SensorMock>>(id<0>().service,
68                                                         id<0>().path);
69     auto sensor2 = sut.makeSensor<NiceMock<SensorMock>>(id<1>().service,
70                                                         id<1>().path);
71 
72     ASSERT_THAT(sensor1.get(), Not(Eq(sensor2.get())));
73     ASSERT_THAT(sensor1->mockId, Not(Eq(sensor2->mockId)));
74 }
75 
TEST_F(TestSensorCache,shouldReturnSameSensorWhenSensorWithSameIdStillExists)76 TEST_F(TestSensorCache, shouldReturnSameSensorWhenSensorWithSameIdStillExists)
77 {
78     auto sensor1 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
79                                                         "sensor-path");
80     auto sensor2 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
81                                                         "sensor-path");
82 
83     ASSERT_THAT(sensor1.get(), Eq(sensor2.get()));
84     ASSERT_THAT(sensor1->mockId, Eq(sensor2->mockId));
85 }
86 
TEST_F(TestSensorCache,shouldReturnDifferentSensorWhenPreviousSensorExpired)87 TEST_F(TestSensorCache, shouldReturnDifferentSensorWhenPreviousSensorExpired)
88 {
89     auto mockId1 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
90                                                         "sensor-path")
91                        ->mockId;
92     auto mockId2 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
93                                                         "sensor-path")
94                        ->mockId;
95 
96     ASSERT_THAT(mockId2, Not(Eq(mockId1)));
97 }
98 
TEST_F(TestSensorCache,shouldCreateSensorWithCorrespondingId)99 TEST_F(TestSensorCache, shouldCreateSensorWithCorrespondingId)
100 {
101     auto id = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
102                                                    "sensor-path")
103                   ->id();
104 
105     auto expected = SensorMock::makeId("sensor-service", "sensor-path");
106 
107     ASSERT_THAT(id, Eq(expected));
108 }
109