| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package broker |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
|
| 22 |
. "gopkg.in/check.v1" |
| 23 |
|
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/store" |
| 25 |
help "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 26 |
) |
| 27 |
|
| 28 |
type exchangesImplSuite struct{} |
| 29 |
|
| 30 |
var _ = Suite(&exchangesImplSuite{}) |
| 31 |
|
| 32 |
func (s *exchangesImplSuite) TestFilterByLevel(c *C) { |
| 33 |
notifs := help.Ns( |
| 34 |
json.RawMessage(`{"a": 3}`), |
| 35 |
json.RawMessage(`{"a": 4}`), |
| 36 |
json.RawMessage(`{"a": 5}`), |
| 37 |
) |
| 38 |
res := filterByLevel(5, 5, notifs) |
| 39 |
c.Check(len(res), Equals, 0) |
| 40 |
res = filterByLevel(4, 5, notifs) |
| 41 |
c.Check(len(res), Equals, 1) |
| 42 |
c.Check(res[0].Payload, DeepEquals, json.RawMessage(`{"a": 5}`)) |
| 43 |
res = filterByLevel(3, 5, notifs) |
| 44 |
c.Check(len(res), Equals, 2) |
| 45 |
c.Check(res[0].Payload, DeepEquals, json.RawMessage(`{"a": 4}`)) |
| 46 |
res = filterByLevel(2, 5, notifs) |
| 47 |
c.Check(len(res), Equals, 3) |
| 48 |
res = filterByLevel(1, 5, notifs) |
| 49 |
c.Check(len(res), Equals, 3) |
| 50 |
// too ahead, pick only last |
| 51 |
res = filterByLevel(10, 5, notifs) |
| 52 |
c.Check(len(res), Equals, 1) |
| 53 |
c.Check(res[0].Payload, DeepEquals, json.RawMessage(`{"a": 5}`)) |
| 54 |
} |
| 55 |
|
| 56 |
func (s *exchangesImplSuite) TestFilterByLevelEmpty(c *C) { |
| 57 |
res := filterByLevel(5, 0, nil) |
| 58 |
c.Check(len(res), Equals, 0) |
| 59 |
res = filterByLevel(5, 10, nil) |
| 60 |
c.Check(len(res), Equals, 0) |
| 61 |
} |
| 62 |
|
| 63 |
func (s *exchangesImplSuite) TestChannelFilter(c *C) { |
| 64 |
payloads := []json.RawMessage{ |
| 65 |
json.RawMessage(`{"a/x": 3}`), |
| 66 |
json.RawMessage(`{"b/x": 4}`), |
| 67 |
json.RawMessage(`{"a/y": 5}`), |
| 68 |
json.RawMessage(`{"a/x": 6}`), |
| 69 |
} |
| 70 |
decoded := make([]map[string]interface{}, 4) |
| 71 |
for i, p := range payloads { |
| 72 |
err := json.Unmarshal(p, &decoded[i]) |
| 73 |
c.Assert(err, IsNil) |
| 74 |
} |
| 75 |
notifs := help.Ns(payloads...) |
| 76 |
|
| 77 |
other := store.InternalChannelId("1") |
| 78 |
|
| 79 |
c.Check(channelFilter("", store.SystemInternalChannelId, nil, nil), IsNil) |
| 80 |
c.Check(channelFilter("", other, notifs[1:], decoded), DeepEquals, payloads[1:]) |
| 81 |
|
| 82 |
// use tag when channel is the sytem channel |
| 83 |
|
| 84 |
c.Check(channelFilter("c/z", store.SystemInternalChannelId, notifs, decoded), HasLen, 0) |
| 85 |
|
| 86 |
c.Check(channelFilter("a/x", store.SystemInternalChannelId, notifs, decoded), DeepEquals, []json.RawMessage{payloads[0], payloads[3]}) |
| 87 |
|
| 88 |
c.Check(channelFilter("a/x", store.SystemInternalChannelId, notifs[1:], decoded), DeepEquals, []json.RawMessage{payloads[3]}) |
| 89 |
|
| 90 |
} |