blob: 218e1605dc759aa3a9475c27e8c17a8662a19fad [file] [log] [blame]
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00001#include "helpers.hpp"
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +02002#include "mocks/sensor_mock.hpp"
3#include "sensor_cache.hpp"
4
5#include <initializer_list>
6
7#include <gmock/gmock.h>
8
9using namespace testing;
10
11class TestSensorCache : public Test
12{
13 public:
14 SensorCache sut;
15};
16
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +020017struct IdParam
18{
19 IdParam() = default;
20 IdParam(std::string_view service, std::string_view path) :
21 service(service), path(path)
22 {}
23
24 std::string service;
25 std::string path;
26};
27
28class TestSensorCacheP :
29 public TestSensorCache,
30 public WithParamInterface<std::vector<IdParam>>
31{
32 public:
33 void SetUp() override
34 {
35 auto vec = GetParam();
36 ASSERT_THAT(vec, SizeIs(param.size()));
37 std::copy(vec.begin(), vec.end(), param.begin());
38 }
39
40 template <size_t index>
41 const IdParam& id() const
42 {
43 static_assert(index < std::tuple_size_v<decltype(param)>);
44 return param[index];
45 }
46
47 private:
48 std::array<IdParam, 2> param;
49};
50
51INSTANTIATE_TEST_SUITE_P(
52 UniqueIds, TestSensorCacheP,
53 Values(std::vector<IdParam>({IdParam("service1", "path1"),
54 IdParam("service1", "path2")}),
55 std::vector<IdParam>({IdParam("service1", "path1"),
56 IdParam("service2", "path1")}),
57 std::vector<IdParam>({IdParam("service1", "path1"),
58 IdParam("service2", "path2")})));
59
60TEST_P(TestSensorCacheP, shouldReturnDifferentSensorWhenIdsAreDifferent)
61{
Patrick Williams3a1c2972023-05-10 07:51:04 -050062 auto sensor1 = sut.makeSensor<NiceMock<SensorMock>>(id<0>().service,
63 id<0>().path);
64 auto sensor2 = sut.makeSensor<NiceMock<SensorMock>>(id<1>().service,
65 id<1>().path);
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +020066
67 ASSERT_THAT(sensor1.get(), Not(Eq(sensor2.get())));
68 ASSERT_THAT(sensor1->mockId, Not(Eq(sensor2->mockId)));
69}
70
71TEST_F(TestSensorCache, shouldReturnSameSensorWhenSensorWithSameIdStillExists)
72{
Patrick Williams3a1c2972023-05-10 07:51:04 -050073 auto sensor1 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
74 "sensor-path");
75 auto sensor2 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
76 "sensor-path");
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +020077
78 ASSERT_THAT(sensor1.get(), Eq(sensor2.get()));
79 ASSERT_THAT(sensor1->mockId, Eq(sensor2->mockId));
80}
81
82TEST_F(TestSensorCache, shouldReturnDifferentSensorWhenPreviousSensorExpired)
83{
Patrick Williams3a1c2972023-05-10 07:51:04 -050084 auto mockId1 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
85 "sensor-path")
86 ->mockId;
87 auto mockId2 = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
88 "sensor-path")
89 ->mockId;
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +020090
91 ASSERT_THAT(mockId2, Not(Eq(mockId1)));
92}
93
94TEST_F(TestSensorCache, shouldCreateSensorWithCorrespondingId)
95{
Patrick Williams3a1c2972023-05-10 07:51:04 -050096 auto id = sut.makeSensor<NiceMock<SensorMock>>("sensor-service",
97 "sensor-path")
98 ->id();
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +020099
100 auto expected = SensorMock::makeId("sensor-service", "sensor-path");
101
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100102 ASSERT_THAT(id, Eq(expected));
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +0200103}