blob: a3831dcc718206b6f9a68d76bf333e7a6cc1cd4d [file] [log] [blame]
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +02001#include "mocks/sensor_mock.hpp"
2#include "sensor_cache.hpp"
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02003#include "utils/sensor_id_eq.hpp"
Krzysztof Grobelny7f06f612020-09-24 13:42:10 +02004
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{
62 auto sensor1 =
63 sut.makeSensor<NiceMock<SensorMock>>(id<0>().service, id<0>().path);
64 auto sensor2 =
65 sut.makeSensor<NiceMock<SensorMock>>(id<1>().service, id<1>().path);
66
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{
73 auto sensor1 =
74 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path");
75 auto sensor2 =
76 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path");
77
78 ASSERT_THAT(sensor1.get(), Eq(sensor2.get()));
79 ASSERT_THAT(sensor1->mockId, Eq(sensor2->mockId));
80}
81
82TEST_F(TestSensorCache, shouldReturnDifferentSensorWhenPreviousSensorExpired)
83{
84 auto mockId1 =
85 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path")
86 ->mockId;
87 auto mockId2 =
88 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path")
89 ->mockId;
90
91 ASSERT_THAT(mockId2, Not(Eq(mockId1)));
92}
93
94TEST_F(TestSensorCache, shouldCreateSensorWithCorrespondingId)
95{
96 auto id =
97 sut.makeSensor<NiceMock<SensorMock>>("sensor-service", "sensor-path")
98 ->id();
99
100 auto expected = SensorMock::makeId("sensor-service", "sensor-path");
101
102 ASSERT_THAT(id, sensorIdEq(expected));
103}