1+ package models
2+
3+ import models .TopicTypes .{Breaking , FootballMatch }
4+ import org .specs2 .mutable .Specification
5+ import play .api .libs .json .Json
6+
7+ class RegistrationSpec extends Specification {
8+ " Registration" should {
9+ " parse a valid registration" in {
10+ val json = Json .parse(
11+ """ {"deviceToken":"abc","platform":"ios","topics":[{"type":"breaking","name":"uk"}]}"""
12+ )
13+ val result = json.validate[Registration ]
14+ result.isSuccess must beTrue
15+ result.get.topics must_== Set (Topic (Breaking , " uk" ))
16+ }
17+
18+ " filter out invalid topic types and keep valid ones" in {
19+ val json = Json .parse(
20+ """ {"deviceToken":"abc","platform":"ios","topics":[{"type":"tag","name":"some-tag"},{"type":"breaking","name":"uk"}]}"""
21+ )
22+ val result = json.validate[Registration ]
23+ result.isSuccess must beTrue
24+ result.get.topics must_== Set (Topic (Breaking , " uk" ))
25+ }
26+
27+ " parse successfully with an empty topic set if all topics are invalid (controller will reject)" in {
28+ val json = Json .parse(
29+ """ {"deviceToken":"abc","platform":"ios","topics":[{"type":"tag","name":"some-tag"}]}"""
30+ )
31+ val result = json.validate[Registration ]
32+ result.isSuccess must beTrue
33+ result.get.topics must beEmpty
34+ }
35+
36+ " still fail for other invalid fields" in {
37+ val json = Json .parse(
38+ """ {"deviceToken":"abc","platform":"unknown-platform","topics":[{"type":"breaking","name":"uk"}]}"""
39+ )
40+ json.validate[Registration ].isError must beTrue
41+ }
42+ }
43+ }
0 commit comments