Skip to content

Commit 6b10ba1

Browse files
Merge pull request #57 from Iterable/feature/mob-215-payload
[MOB-215] - Parse inAppType, contentType from payload
2 parents 9d603c7 + a882c56 commit 6b10ba1

File tree

14 files changed

+440
-125
lines changed

14 files changed

+440
-125
lines changed

Tests/common/Common.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import Foundation
88

99
@testable import IterableSDK
1010

11-
extension String {
12-
func toJsonDict() -> [AnyHashable : Any] {
13-
return try! JSONSerialization.jsonObject(with: self.data(using: .utf8)!, options: []) as! [AnyHashable : Any]
14-
}
15-
}
16-
1711
struct TestConsts {
1812
static let userDefaultsSuiteName = "testUserDefaults"
1913
}

Tests/common/CommonExtensions.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ import Foundation
88

99
@testable import IterableSDK
1010

11+
extension String {
12+
func toJsonDict() -> [AnyHashable : Any] {
13+
return try! JSONSerialization.jsonObject(with: self.data(using: .utf8)!, options: []) as! [AnyHashable : Any]
14+
}
15+
}
16+
1117
extension Dictionary where Key == AnyHashable {
12-
func toData() -> Data {
18+
func toJsonData() -> Data {
1319
return try! JSONSerialization.data(withJSONObject: self, options: [])
1420
}
21+
22+
func toJsonString() -> String {
23+
return String(data: toJsonData(), encoding: .utf8)!
24+
}
1525
}
1626

1727
// Used only by ojbc tests. Remove after converting to Swift.

Tests/common/CommonMocks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ class MockNetworkSession: NetworkSessionProtocol {
162162

163163
convenience init(statusCode: Int = 200) {
164164
self.init(statusCode: statusCode,
165-
data: [:].toData(),
165+
data: [:].toJsonData(),
166166
error: nil)
167167
}
168168

169169
convenience init(statusCode: Int, json: [AnyHashable : Any], error: Error? = nil) {
170170
self.init(statusCode: statusCode,
171-
data: json.toData(),
171+
data: json.toJsonData(),
172172
error: error)
173173
}
174174

Tests/swift-sdk-swift-tests/InAppHelperTests.swift

Lines changed: 131 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,17 @@ class InAppHelperTests: XCTestCase {
224224
wait(for: [expectation1], timeout: testExpectationTimeout)
225225
}
226226

227-
func testExtraInfoParsing() {
227+
func testCustomPayloadParsing() {
228228
IterableAPI.initializeForTesting()
229229

230-
let extraInfo: [AnyHashable : Any] = ["string1" : "value1", "bool1" : true, "date1" : Date()]
230+
let customPayload: [AnyHashable : Any] = ["string1" : "value1", "bool1" : true, "date1" : Date()]
231231

232-
let payload = createInAppPayload(withExtraInfo: extraInfo)
232+
let payload = createInAppPayload(withCustomPayload: customPayload)
233233

234234
let messages = InAppHelper.inAppMessages(fromPayload: payload, internalApi: IterableAPI.internalImplementation!)
235235

236236
XCTAssertEqual(messages.count, 1)
237-
let obtained = messages[0].extraInfo
237+
let obtained = messages[0].customPayload
238238
XCTAssertEqual(obtained?["string1"] as? String, "value1")
239239
XCTAssertEqual(obtained?["bool1"] as? Bool, true)
240240
}
@@ -304,45 +304,151 @@ class InAppHelperTests: XCTestCase {
304304
TestUtils.validateMatch(keyPath: KeyPath("myPayload.var1"), value: "val1", inDictionary: dict, message: "Expected to find val1")
305305
}
306306

307-
// This tests payload as we expect to get when backend is fixed
308-
func testInAppPayloadParsing() {
309-
let customPayload1 = """
307+
// Remove this test when backend is fixed
308+
// This test assumes that certain parts of payload
309+
// are in 'customPayload' element instead of the right places.
310+
func testInAppPayloadParsingWithPreprocessing() {
311+
let customPayloadStr1 = """
310312
{
311-
"contentType" : "html",
312-
"inAppType" : "default",
313-
"channelName" : "channel1"
313+
"messageId": "overridden",
314+
"var1" : "value1",
315+
"obj1" : {
316+
"something" : true,
317+
"nothing" : "is nothing"
318+
},
314319
}
315320
"""
316-
let customPayload2 = """
321+
var customPayload1 = customPayloadStr1.toJsonDict()
322+
customPayload1["inAppType"] = "default"
323+
customPayload1["contentType"] = "html"
324+
325+
let customPayloadStr2 = """
317326
{
318-
"contentType" : "html",
319-
"inAppType" : "inBox",
320-
"channelName" : "channel2",
321327
"promoteToContent" : {
322328
"title" : "title",
323329
"subTitle" : "subtitle",
324330
"imageUrl" : "http://somewhere.com/something.jpg"
325331
}
326332
}
327333
"""
334+
var customPayload2 = customPayloadStr2.toJsonDict()
335+
customPayload2["inAppType"] = "inbox"
336+
customPayload2["contentType"] = "html"
337+
338+
let payload = """
339+
{
340+
"inAppMessages" : [
341+
{
342+
"content" : {
343+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
344+
},
345+
"messageId" : "messageId1",
346+
"campaignId" : "campaignIdxxx",
347+
"trigger" : {
348+
"type" : "myNewKind",
349+
"myPayload" : {"var1" : "val1"}
350+
},
351+
"customPayload" : \(customPayload1.toJsonString())
352+
},
353+
{
354+
"content" : {
355+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
356+
},
357+
"messageId" : "messageId2",
358+
"campaignId" : "campaignIdxxx",
359+
"trigger" : {
360+
"type" : "myNewKind",
361+
"myPayload" : {"var1" : "val1"}
362+
},
363+
"customPayload" : \(customPayload2.toJsonString())
364+
},
365+
{
366+
"content" : {
367+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
368+
},
369+
"messageId" : "messageId3",
370+
"campaignId" : "campaignIdxxx",
371+
"trigger" : {
372+
"type" : "myNewKind",
373+
"myPayload" : {"var1" : "val1"}
374+
},
375+
"customPayload" : {}
376+
},
377+
{
378+
"content" : {
379+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
380+
},
381+
"messageId" : "messageId4",
382+
"campaignId" : "campaignIdxxx",
383+
"trigger" : {
384+
"type" : "myNewKind",
385+
"myPayload" : {"var1" : "val1"}
386+
}
387+
}
388+
]
389+
}
390+
""".toJsonDict()
391+
let messages = InAppHelper.inAppMessages(fromPayload: payload, internalApi: IterableAPI.internalImplementation!)
392+
393+
XCTAssertEqual(messages.count, 4)
394+
let message1 = messages[0]
395+
XCTAssertEqual(message1.messageId, "messageId1")
396+
XCTAssertEqual(message1.inAppType, .default)
397+
XCTAssertTrue(TestUtils.areEqual(dict1: message1.customPayload!, dict2: customPayloadStr1.toJsonDict()))
328398

399+
let message2 = messages[1]
400+
XCTAssertEqual(message2.inAppType, .inbox)
401+
XCTAssertTrue(TestUtils.areEqual(dict1: message2.customPayload!, dict2: customPayloadStr2.toJsonDict()))
402+
403+
let message3 = messages[2]
404+
XCTAssertEqual(message3.inAppType, .default)
405+
406+
let message4 = messages[3]
407+
XCTAssertEqual(message4.inAppType, .default)
408+
}
409+
410+
411+
func testInAppPayloadParsing() {
412+
let customPayloadStr1 = """
413+
{
414+
"var1" : "value1",
415+
"obj1" : {
416+
"something" : true,
417+
"nothing" : "is nothing"
418+
}
419+
}
420+
"""
421+
let customPayloadStr2 = """
422+
{
423+
"promoteToContent" : {
424+
"title" : "title",
425+
"subTitle" : "subtitle",
426+
"imageUrl" : "http://somewhere.com/something.jpg"
427+
}
428+
}
429+
"""
430+
329431
let payload = """
330432
{
331433
"inAppMessages" : [
332434
{
333435
"content" : {
436+
"contentType" : "html",
334437
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
335438
},
336439
"messageId" : "messageIdxxx",
337440
"campaignId" : "campaignIdxxx",
441+
"inAppType" : "default",
338442
"trigger" : {
339443
"type" : "myNewKind",
340444
"myPayload" : {"var1" : "val1"}
341445
},
342-
"customPayload" : \(customPayload1)
446+
"customPayload" : \(customPayloadStr1)
343447
},
344448
{
449+
"inAppType" : "inbox",
345450
"content" : {
451+
"contentType" : "html",
346452
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
347453
},
348454
"messageId" : "messageIdxxx",
@@ -351,7 +457,7 @@ class InAppHelperTests: XCTestCase {
351457
"type" : "myNewKind",
352458
"myPayload" : {"var1" : "val1"}
353459
},
354-
"customPayload" : \(customPayload2)
460+
"customPayload" : \(customPayloadStr2)
355461
},
356462
{
357463
"content" : {
@@ -383,21 +489,21 @@ class InAppHelperTests: XCTestCase {
383489

384490
XCTAssertEqual(messages.count, 4)
385491
let message1 = messages[0]
386-
XCTAssertEqual(message1.channelName, "channel1")
387-
XCTAssertTrue(TestUtils.areEqual(dict1: message1.extraInfo!, dict2: customPayload1.toJsonDict()))
492+
XCTAssertEqual(message1.inAppType, .default)
493+
XCTAssertTrue(TestUtils.areEqual(dict1: message1.customPayload!, dict2: customPayloadStr1.toJsonDict()))
388494

389495
let message2 = messages[1]
390-
XCTAssertEqual(message2.channelName, "channel2")
391-
XCTAssertTrue(TestUtils.areEqual(dict1: message2.extraInfo!, dict2: customPayload2.toJsonDict()))
496+
XCTAssertEqual(message2.inAppType, .inbox)
497+
XCTAssertTrue(TestUtils.areEqual(dict1: message2.customPayload!, dict2: customPayloadStr2.toJsonDict()))
392498

393499
let message3 = messages[2]
394-
XCTAssertEqual(message3.channelName, "")
395-
500+
XCTAssertEqual(message3.inAppType, .default)
501+
396502
let message4 = messages[3]
397-
XCTAssertEqual(message4.channelName, "")
503+
XCTAssertEqual(message4.inAppType, .default)
398504
}
399505

400-
private func createInAppPayload(withExtraInfo extraInfo: [AnyHashable : Any]) -> [AnyHashable : Any] {
506+
private func createInAppPayload(withCustomPayload customPayload: [AnyHashable : Any]) -> [AnyHashable : Any] {
401507
return [
402508
"inAppMessages" : [[
403509
"content" : [
@@ -406,7 +512,7 @@ class InAppHelperTests: XCTestCase {
406512
],
407513
"messageId" : "messageIdxxx",
408514
"campaignId" : "campaignIdxxx",
409-
"customPayload" : extraInfo
515+
"customPayload" : customPayload
410516
]]
411517
]
412518
}

Tests/swift-sdk-swift-tests/InAppTests.swift

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -703,13 +703,45 @@ class InAppTests: XCTestCase {
703703

704704

705705
func testFilePersistence() {
706-
let payload = ["inAppMessages" : [
707-
TestInAppPayloadGenerator.createOneInAppDictWithUrl(index: 1, trigger: IterableInAppTrigger(dict: ["type" : "event", "details" : "some event details"]), expiresAt: Date()),
708-
TestInAppPayloadGenerator.createOneInAppDictWithUrl(index: 2, triggerType: .immediate, expiresAt: Date()),
709-
TestInAppPayloadGenerator.createOneInAppDictWithUrl(index: 3, triggerType: .never),
710-
TestInAppPayloadGenerator.createOneInAppDictWithUrl(index: 4, trigger: IterableInAppTrigger(dict: ["type" : "newEventType", "nested" : ["var1" : "val1"]]), expiresAt: Date()),
711-
]]
712-
706+
let payload = """
707+
{"inAppMessages":
708+
[
709+
{
710+
"inAppType": "default",
711+
"content": {"contentType": "html", "inAppDisplaySettings": {"bottom": {"displayOption": "AutoExpand"}, "backgroundAlpha": 0.5, "left": {"percentage": 60}, "right": {"percentage": 60}, "top": {"displayOption": "AutoExpand"}}, "html": "<a href=\'https://www.site1.com\'>Click Here</a>", "payload": {"title": "Product 1 Available", "date": "2018-11-14T14:00:00:00.32Z"}},
712+
"trigger": {"type": "event", "details": "some event details"},
713+
"messageId": "message1",
714+
"expiresAt": 1550605745142,
715+
"campaignId": "campaign1",
716+
"customPayload": {"title": "Product 1 Available", "date": "2018-11-14T14:00:00:00.32Z"}
717+
},
718+
{
719+
"inAppType": "inbox",
720+
"content": {"contentType": "alert", "inAppDisplaySettings": {"bottom": {"displayOption": "AutoExpand"}, "backgroundAlpha": 0.5, "left": {"percentage": 60}, "right": {"percentage": 60}, "top": {"displayOption": "AutoExpand"}}, "html": "<a href=\'https://www.site2.com\'>Click Here</a>"},
721+
"trigger": {"type": "immediate"},
722+
"messageId": "message2",
723+
"expiresAt": 1550605745145,
724+
"campaignId": "campaign2",
725+
"customPayload": {"title": "Product 1 Available", "date": "2018-11-14T14:00:00:00.32Z"}
726+
},
727+
{
728+
"content": {"inAppDisplaySettings": {"bottom": {"displayOption": "AutoExpand"}, "backgroundAlpha": 0.5, "left": {"percentage": 60}, "right": {"percentage": 60}, "top": {"displayOption": "AutoExpand"}}, "html": "<a href=\'https://www.site3.com\'>Click Here</a>"},
729+
"trigger": {"type": "never"},
730+
"messageId": "message3",
731+
"campaignId": "campaign3",
732+
"customPayload": {"title": "Product 1 Available", "date": "2018-11-14T14:00:00:00.32Z"}
733+
},
734+
{
735+
"content": {"inAppDisplaySettings": {"bottom": {"displayOption": "AutoExpand"}, "backgroundAlpha": 0.5, "left": {"percentage": 60}, "right": {"percentage": 60}, "top": {"displayOption": "AutoExpand"}}, "html": "<a href=\'https://www.site4.com\'>Click Here</a>"},
736+
"trigger": {"type": "newEventType", "nested": {"var1": "val1"}},
737+
"messageId": "message4",
738+
"expiresAt": 1550605745145,
739+
"campaignId": "campaign4",
740+
"customPayload": {"title": "Product 1 Available", "date": "2018-11-14T14:00:00:00.32Z"}
741+
}
742+
]
743+
}
744+
""".toJsonDict()
713745
let messages = InAppHelper.inAppMessages(fromPayload: payload, internalApi: IterableAPI.internalImplementation!)
714746
let persister = InAppFilePersister()
715747
persister.persist(messages)
@@ -994,8 +1026,7 @@ extension IterableInAppMessage {
9941026
public override var description: String {
9951027
return IterableUtil.describe("messageId", messageId,
9961028
"campaignId", campaignId,
997-
"channelName", channelName,
998-
"contentType", contentType,
1029+
"inAppType", inAppType,
9991030
"trigger", trigger,
10001031
"expiresAt", expiresAt ?? "nil",
10011032
"content", content,

Tests/swift-sdk-swift-tests/TestInAppPayloadGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct TestInAppPayloadGenerator {
8484
],
8585
"messageId" : getMessageId(index: index),
8686
"campaignId" : getCampaignId(index: index),
87-
"customPayload" : ["channelName" : "inBox", "title" : "Product 1 Available", "date" : "2018-11-14T14:00:00:00.32Z"]
87+
"customPayload" : ["title" : "Product 1 Available", "date" : "2018-11-14T14:00:00:00.32Z"]
8888
]
8989
}
9090

host-app/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class ViewController: UIViewController {
135135
<a href="http://website/resource#something">Click Me</a>
136136
"""
137137
let content = IterableHtmlInAppContent(edgeInsets: .zero, backgroundAlpha: 0.0, html: html)
138-
let message = IterableInAppMessage(messageId: messageId, campaignId: "zeeCampaignId", contentType: .html, content: content)
138+
let message = IterableInAppMessage(messageId: messageId, campaignId: "zeeCampaignId", content: content)
139139

140140
let config = IterableConfig()
141141
let mockUrlDelegate = MockUrlDelegate(returnValue: true)
@@ -166,7 +166,7 @@ class ViewController: UIViewController {
166166
<a href="http://website/resource#something">Click Me</a>
167167
"""
168168
let content = IterableHtmlInAppContent(edgeInsets: UIEdgeInsets(top: -1, left: 10, bottom: -1, right: 10), backgroundAlpha: 0.5, html: html)
169-
let message = IterableInAppMessage(messageId: messageId, campaignId: "zeeCampaignId", contentType: .html, content: content)
169+
let message = IterableInAppMessage(messageId: messageId, campaignId: "zeeCampaignId", content: content)
170170

171171
let config = IterableConfig()
172172
let mockUrlDelegate = MockUrlDelegate(returnValue: false) // we don't handle, so the url will be opened

0 commit comments

Comments
 (0)