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