blob: fd5f0a7281f387a22707f0b84957c608f4dd1b1f [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Stefan Sauer2d16dfa2015-09-25 17:08:35 +02005#include "src/notification/notification_parser.h"
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -07006
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
Alex Vakulenko8a05beb2015-11-24 17:13:20 -08009#include <weave/test/unittest_utils.h>
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070010
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070011using testing::SaveArg;
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070012using testing::Invoke;
13using testing::_;
14
Vitaly Bukab6f015a2015-07-09 14:59:23 -070015namespace weave {
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070016
Vitaly Buka0f6b2ec2015-08-20 15:35:19 -070017using test::CreateDictionaryValue;
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070018
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070019MATCHER_P(MatchDict, str, "") {
20 return arg.Equals(CreateDictionaryValue(str).get());
21}
22
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070023class MockNotificationDelegate : public NotificationDelegate {
24 public:
25 MOCK_METHOD1(OnConnected, void(const std::string&));
26 MOCK_METHOD0(OnDisconnected, void());
27 MOCK_METHOD0(OnPermanentFailure, void());
Vitaly Buka34668e72015-12-15 14:46:47 -080028 MOCK_METHOD2(OnCommandCreated,
29 void(const base::DictionaryValue& command,
30 const std::string& channel_name));
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070031 MOCK_METHOD1(OnDeviceDeleted, void(const std::string&));
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070032};
33
34class NotificationParserTest : public ::testing::Test {
35 protected:
36 testing::StrictMock<MockNotificationDelegate> delegate_;
37};
38
39TEST_F(NotificationParserTest, CommandCreated) {
40 auto json = CreateDictionaryValue(R"({
Alex Vakulenkof21c83a2015-11-25 11:34:33 -080041 "kind": "weave#notification",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070042 "type": "COMMAND_CREATED",
43 "deviceId": "device_id",
44 "command": {
Alex Vakulenkof21c83a2015-11-25 11:34:33 -080045 "kind": "weave#command",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070046 "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
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070059 const char expected_json[] = R"({
Alex Vakulenkof21c83a2015-11-25 11:34:33 -080060 "kind": "weave#command",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070061 "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 })";
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070071
72 EXPECT_CALL(delegate_, OnCommandCreated(MatchDict(expected_json), "foo"))
73 .Times(1);
74 EXPECT_TRUE(ParseNotificationJson(*json, &delegate_, "foo"));
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070075}
76
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070077TEST_F(NotificationParserTest, DeviceDeleted) {
78 auto json = CreateDictionaryValue(R"({
Alex Vakulenkof21c83a2015-11-25 11:34:33 -080079 "kind":"weave#notification",
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070080 "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));
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -070086 EXPECT_TRUE(ParseNotificationJson(*json, &delegate_, "foo"));
Alex Vakulenko6b40d8f2015-06-24 11:44:22 -070087 EXPECT_EQ("some_device_id", device_id);
88}
89
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070090TEST_F(NotificationParserTest, Failure_NoKind) {
91 auto json = CreateDictionaryValue(R"({
92 "type": "COMMAND_CREATED",
93 "deviceId": "device_id",
94 "command": {
Alex Vakulenkof21c83a2015-11-25 11:34:33 -080095 "kind": "weave#command",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -070096 "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
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -0700109 EXPECT_FALSE(ParseNotificationJson(*json, &delegate_, "bar"));
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -0700110}
111
112TEST_F(NotificationParserTest, Failure_NoType) {
113 auto json = CreateDictionaryValue(R"({
Alex Vakulenkof21c83a2015-11-25 11:34:33 -0800114 "kind": "weave#notification",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -0700115 "deviceId": "device_id",
116 "command": {
Alex Vakulenkof21c83a2015-11-25 11:34:33 -0800117 "kind": "weave#command",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -0700118 "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
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -0700131 EXPECT_FALSE(ParseNotificationJson(*json, &delegate_, "baz"));
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -0700132}
133
134TEST_F(NotificationParserTest, IgnoredNotificationType) {
135 auto json = CreateDictionaryValue(R"({
Alex Vakulenkof21c83a2015-11-25 11:34:33 -0800136 "kind": "weave#notification",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -0700137 "type": "COMMAND_EXPIRED",
138 "deviceId": "device_id",
139 "command": {
Alex Vakulenkof21c83a2015-11-25 11:34:33 -0800140 "kind": "weave#command",
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -0700141 "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
Alex Vakulenkoe07c29d2015-10-22 10:31:12 -0700154 EXPECT_TRUE(ParseNotificationJson(*json, &delegate_, "quux"));
Alex Vakulenko6e3c30e2015-05-21 17:39:25 -0700155}
156
Vitaly Bukab6f015a2015-07-09 14:59:23 -0700157} // namespace weave