1 // Copyright 2015 The Weave Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/notification/notification_parser.h"
6 
7 #include <gmock/gmock.h>
8 #include <gtest/gtest.h>
9 #include <weave/test/unittest_utils.h>
10 
11 using testing::SaveArg;
12 using testing::Invoke;
13 using testing::_;
14 
15 namespace weave {
16 
17 using test::CreateDictionaryValue;
18 
19 MATCHER_P(MatchDict, str, "") {
20   return arg.Equals(CreateDictionaryValue(str).get());
21 }
22 
23 class MockNotificationDelegate : public NotificationDelegate {
24  public:
25   MOCK_METHOD1(OnConnected, void(const std::string&));
26   MOCK_METHOD0(OnDisconnected, void());
27   MOCK_METHOD0(OnPermanentFailure, void());
28   MOCK_METHOD2(OnCommandCreated,
29                void(const base::DictionaryValue& command,
30                     const std::string& channel_name));
31   MOCK_METHOD1(OnDeviceDeleted, void(const std::string&));
32 };
33 
34 class NotificationParserTest : public ::testing::Test {
35  protected:
36   testing::StrictMock<MockNotificationDelegate> delegate_;
37 };
38 
TEST_F(NotificationParserTest,CommandCreated)39 TEST_F(NotificationParserTest, CommandCreated) {
40   auto json = CreateDictionaryValue(R"({
41     "kind": "weave#notification",
42     "type": "COMMAND_CREATED",
43     "deviceId": "device_id",
44     "command": {
45       "kind": "weave#command",
46       "deviceId": "device_id",
47       "state": "queued",
48       "name": "storage.list",
49       "parameters": {
50         "path": "/somepath1"
51       },
52       "expirationTimeMs": "1406036174811",
53       "id": "command_id",
54       "creationTimeMs": "1403444174811"
55     },
56     "commandId": "command_id"
57   })");
58 
59   const char expected_json[] = R"({
60       "kind": "weave#command",
61       "deviceId": "device_id",
62       "state": "queued",
63       "name": "storage.list",
64       "parameters": {
65         "path": "/somepath1"
66       },
67       "expirationTimeMs": "1406036174811",
68       "id": "command_id",
69       "creationTimeMs": "1403444174811"
70     })";
71 
72   EXPECT_CALL(delegate_, OnCommandCreated(MatchDict(expected_json), "foo"))
73       .Times(1);
74   EXPECT_TRUE(ParseNotificationJson(*json, &delegate_, "foo"));
75 }
76 
TEST_F(NotificationParserTest,DeviceDeleted)77 TEST_F(NotificationParserTest, DeviceDeleted) {
78   auto json = CreateDictionaryValue(R"({
79     "kind":"weave#notification",
80     "type":"DEVICE_DELETED",
81     "deviceId":"some_device_id"
82   })");
83 
84   std::string device_id;
85   EXPECT_CALL(delegate_, OnDeviceDeleted(_)).WillOnce(SaveArg<0>(&device_id));
86   EXPECT_TRUE(ParseNotificationJson(*json, &delegate_, "foo"));
87   EXPECT_EQ("some_device_id", device_id);
88 }
89 
TEST_F(NotificationParserTest,Failure_NoKind)90 TEST_F(NotificationParserTest, Failure_NoKind) {
91   auto json = CreateDictionaryValue(R"({
92     "type": "COMMAND_CREATED",
93     "deviceId": "device_id",
94     "command": {
95       "kind": "weave#command",
96       "deviceId": "device_id",
97       "state": "queued",
98       "name": "storage.list",
99       "parameters": {
100         "path": "/somepath1"
101       },
102       "expirationTimeMs": "1406036174811",
103       "id": "command_id",
104       "creationTimeMs": "1403444174811"
105     },
106     "commandId": "command_id"
107   })");
108 
109   EXPECT_FALSE(ParseNotificationJson(*json, &delegate_, "bar"));
110 }
111 
TEST_F(NotificationParserTest,Failure_NoType)112 TEST_F(NotificationParserTest, Failure_NoType) {
113   auto json = CreateDictionaryValue(R"({
114     "kind": "weave#notification",
115     "deviceId": "device_id",
116     "command": {
117       "kind": "weave#command",
118       "deviceId": "device_id",
119       "state": "queued",
120       "name": "storage.list",
121       "parameters": {
122         "path": "/somepath1"
123       },
124       "expirationTimeMs": "1406036174811",
125       "id": "command_id",
126       "creationTimeMs": "1403444174811"
127     },
128     "commandId": "command_id"
129   })");
130 
131   EXPECT_FALSE(ParseNotificationJson(*json, &delegate_, "baz"));
132 }
133 
TEST_F(NotificationParserTest,IgnoredNotificationType)134 TEST_F(NotificationParserTest, IgnoredNotificationType) {
135   auto json = CreateDictionaryValue(R"({
136     "kind": "weave#notification",
137     "type": "COMMAND_EXPIRED",
138     "deviceId": "device_id",
139     "command": {
140       "kind": "weave#command",
141       "deviceId": "device_id",
142       "state": "queued",
143       "name": "storage.list",
144       "parameters": {
145         "path": "/somepath1"
146       },
147       "expirationTimeMs": "1406036174811",
148       "id": "command_id",
149       "creationTimeMs": "1403444174811"
150     },
151     "commandId": "command_id"
152   })");
153 
154   EXPECT_TRUE(ParseNotificationJson(*json, &delegate_, "quux"));
155 }
156 
157 }  // namespace weave
158