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