1+ #include < gmock/gmock.h>
2+ #include < rest/controller/MeterReadingController.h>
3+
4+ #include < boost/beast/http.hpp>
5+ #include < nlohmann/json.hpp>
6+
7+ using nlohmann::json;
8+ using ::testing::Eq;
9+
10+ namespace http = boost::beast::http;
11+
12+ class MeterReadingControllerTest : public ::testing::Test {
13+ protected:
14+ std::unordered_map<std::string, std::vector<ElectricityReading>> meterAssociatedReadings;
15+ ElectricityReadingService electricityReadingService{meterAssociatedReadings};
16+ MeterReadingService meterReadingService{meterAssociatedReadings};
17+ MeterReadingController controller{electricityReadingService, meterReadingService};
18+
19+ http::request<http::string_body> BuildRequest (http::verb verb, boost::string_view target, const json &request_body) {
20+ http::request<http::string_body> req{verb, target, 11 };
21+ req.set (http::field::content_type, " application/json" );
22+ req.body () = request_body.dump ();
23+ req.prepare_payload ();
24+ return req;
25+ }
26+ };
27+
28+ TEST_F (MeterReadingControllerTest, StoreShouldResponseWithErrorGivenNoMeterIdIsSupplied) {
29+ auto req = BuildRequest (http::verb::post , " /readings/store" , R"( {})" _json);
30+ std::vector<std::string> queries;
31+
32+ auto response = controller.Store (req, queries);
33+
34+ EXPECT_THAT (response.result (), Eq (http::status::internal_server_error));
35+ }
36+
37+ TEST_F (MeterReadingControllerTest, StoreShouldResponseWithErrorGivenEmptyMeterReading) {
38+ json body = R"( {
39+ "smartMeterId": "smart-meter-0",
40+ "electricityReadings": []
41+ })" _json;
42+ auto req = BuildRequest (http::verb::post , " /readings/store" , body);
43+ std::vector<std::string> queries;
44+
45+ auto response = controller.Store (req, queries);
46+
47+ EXPECT_THAT (response.result (), Eq (http::status::internal_server_error));
48+ }
49+
50+ TEST_F (MeterReadingControllerTest, StoreShouldResponseWithErrorGivenNoMeterReadingIsSupplied) {
51+ json body = R"( {
52+ "smartMeterId": "smart-meter-0"
53+ })" _json;
54+ auto req = BuildRequest (http::verb::post , " /readings/store" , body);
55+ std::vector<std::string> queries;
56+
57+ auto response = controller.Store (req, queries);
58+
59+ EXPECT_THAT (response.result (), Eq (http::status::internal_server_error));
60+ }
61+
62+ TEST_F (MeterReadingControllerTest, StoreShouldStoreGivenMultipleBatchesOfMeterReadings) {
63+ json body1 = R"( {
64+ "smartMeterId": "smart-meter-0",
65+ "electricityReadings": [
66+ {
67+ "time": "2021-08-18T06:42:15.725202Z",
68+ "reading": 1
69+ }
70+ ]
71+ })" _json;
72+ json body2 = R"( {
73+ "smartMeterId": "smart-meter-0",
74+ "electricityReadings": [
75+ {
76+ "time": "2021-08-18T06:44:15.725202Z",
77+ "reading": 2
78+ }
79+ ]
80+ })" _json;
81+ auto req1 = BuildRequest (http::verb::post , " /readings/store" , body1);
82+ auto req2 = BuildRequest (http::verb::post , " /readings/store" , body2);
83+ std::vector<std::string> queries;
84+
85+ controller.Store (req1, queries);
86+ controller.Store (req2, queries);
87+
88+ std::vector<ElectricityReading> expectedElectricityReadings = {
89+ {detail::fromRfc3339 (" 2021-08-18T06:42:15.725202Z" ), 1 },
90+ {detail::fromRfc3339 (" 2021-08-18T06:44:15.725202Z" ), 2 }
91+ };
92+
93+ EXPECT_THAT (meterReadingService.getReadings (" smart-meter-0" ), Eq (expectedElectricityReadings));
94+ }
95+
96+ TEST_F (MeterReadingControllerTest, StoreShouldStoreAssociatedWithUserGivenMeterReadingsAssociatedWithTheUser) {
97+ json body1 = R"( {
98+ "smartMeterId": "smart-meter-0",
99+ "electricityReadings": [
100+ {
101+ "time": "2021-08-18T06:42:15.725202Z",
102+ "reading": 1
103+ }
104+ ]
105+ })" _json;
106+ json body2 = R"( {
107+ "smartMeterId": "smart-meter-1",
108+ "electricityReadings": [
109+ {
110+ "time": "2021-08-18T06:44:15.725202Z",
111+ "reading": 2
112+ }
113+ ]
114+ })" _json;
115+ auto req1 = BuildRequest (http::verb::post , " /readings/store" , body1);
116+ auto req2 = BuildRequest (http::verb::post , " /readings/store" , body2);
117+ std::vector<std::string> queries;
118+
119+ controller.Store (req1, queries);
120+ controller.Store (req2, queries);
121+
122+ std::vector<ElectricityReading> expectedElectricityReadings = {
123+ {detail::fromRfc3339 (" 2021-08-18T06:42:15.725202Z" ), 1 },
124+ };
125+
126+ EXPECT_THAT (meterReadingService.getReadings (" smart-meter-0" ), Eq (expectedElectricityReadings));
127+ }
128+
129+ TEST_F (MeterReadingControllerTest, ReadShouldReturnNotFoundGivenMeterIdThatIsNotRecognised) {
130+ http::request<http::string_body> req;
131+ std::vector<std::string> queries = {" smart-meter-0" };
132+
133+ auto response = controller.Read (req, queries);
134+
135+ EXPECT_THAT (response.result (), Eq (http::status::not_found));
136+ }
0 commit comments