Skip to content

Commit

Permalink
Added support for Offer score (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
swarna04 authored Apr 27, 2022
1 parent 17d387e commit 7864ffe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/AEPOptimize/Offer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class Offer: NSObject, Codable {
/// Offer revision detail at the time of the request
@objc public let etag: String

/// Offer priority score
@objc public let score: Int

/// Offer schema string
@objc public let schema: String

Expand All @@ -46,6 +49,7 @@ public class Offer: NSObject, Codable {
enum CodingKeys: String, CodingKey {
case id
case etag
case score
case schema
case meta
case data
Expand All @@ -68,6 +72,8 @@ public class Offer: NSObject, Codable {
// Try and decode format, if present. Target response doesn't contain etag, so setting the default value to empty string.
etag = try container.decodeIfPresent(String.self, forKey: .etag) ?? ""

score = try container.decodeIfPresent(Int.self, forKey: .score) ?? 0

schema = try container.decode(String.self, forKey: .schema)

meta = AnyCodable.toAnyDictionary(dictionary: try container.decodeIfPresent([String: AnyCodable].self, forKey: .meta))
Expand Down Expand Up @@ -134,6 +140,7 @@ public class Offer: NSObject, Codable {

try container.encode(id, forKey: .id)
try container.encode(etag, forKey: .etag)
try container.encode(score, forKey: .score)
try container.encode(schema, forKey: .schema)
try container.encode(AnyCodable.from(dictionary: meta), forKey: .meta)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ class OptimizeFunctionalTests: XCTestCase {
{
"id": "xcore:personalized-offer:1111111111111111",
"etag": "10",
"score": 1,
"schema": "https://ns.adobe.com/experience/offer-management/content-component-json",
"data": {
"id": "xcore:personalized-offer:1111111111111111",
Expand Down Expand Up @@ -835,6 +836,7 @@ class OptimizeFunctionalTests: XCTestCase {
XCTAssertEqual(1, proposition?.offers.count)
XCTAssertEqual("xcore:personalized-offer:1111111111111111", proposition?.offers[0].id)
XCTAssertEqual("https://ns.adobe.com/experience/offer-management/content-component-json", proposition?.offers[0].schema)
XCTAssertEqual(1, proposition?.offers[0].score)
XCTAssertEqual(.json, proposition?.offers[0].type)
XCTAssertEqual("{\"key\":\"value\"}", proposition?.offers[0].content)
XCTAssertEqual(1, proposition?.offers[0].characteristics?.count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class OptimizeIntegrationTests: XCTestCase {
{\
"id": "xcore:personalized-offer:2222222222222222",\
"etag": "39",\
"score": 1,\
"schema": "https://ns.adobe.com/experience/offer-management/content-component-text",\
"data": {\
"id": "xcore:personalized-offer:2222222222222222",\
Expand Down Expand Up @@ -207,6 +208,7 @@ class OptimizeIntegrationTests: XCTestCase {
XCTAssertEqual("xcore:personalized-offer:2222222222222222", proposition?.offers[0].id)
XCTAssertEqual("39", proposition?.offers[0].etag)
XCTAssertEqual("https://ns.adobe.com/experience/offer-management/content-component-text", proposition?.offers[0].schema)
XCTAssertEqual(1, proposition?.offers[0].score)
XCTAssertEqual(.text, proposition?.offers[0].type)
XCTAssertEqual("This is a plain text content!", proposition?.offers[0].content)
XCTAssertEqual(["en-us"], proposition?.offers[0].language)
Expand Down
45 changes: 45 additions & 0 deletions Tests/AEPOptimizeTests/UnitTests/OfferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ class OfferTests: XCTestCase {
"content": "This is a plain text content!"\
}\
}
"""

private let OFFER_WITH_SCORE =
"""
{\
"id": "xcore:personalized-offer:2222222222222222",\
"etag": "7",\
"score": 2,\
"schema": "https://ns.adobe.com/experience/offer-management/content-component-text",\
"data": {\
"id": "xcore:personalized-offer:2222222222222222",\
"format": "text/plain",\
"content": "This is a plain text content!",\
"language": [\
"en-us"\
],\
"characteristics": {\
"mobile": "true"\
}\
}\
}
"""

private let OFFER_INVALID_NO_CONTENT =
Expand Down Expand Up @@ -194,6 +215,7 @@ class OfferTests: XCTestCase {

XCTAssertEqual("xcore:personalized-offer:1111111111111111", offer.id)
XCTAssertEqual("8", offer.etag)
XCTAssertEqual(0, offer.score)
XCTAssertEqual("https://ns.adobe.com/experience/offer-management/content-component-json", offer.schema)
XCTAssertEqual(OfferType.init(rawValue: 1), offer.type)
XCTAssertEqual("{\"testing\":\"ho-ho\"}", offer.content)
Expand All @@ -213,6 +235,7 @@ class OfferTests: XCTestCase {

XCTAssertEqual("xcore:personalized-offer:2222222222222222", offer.id)
XCTAssertEqual("7", offer.etag)
XCTAssertEqual(0, offer.score)
XCTAssertEqual("https://ns.adobe.com/experience/offer-management/content-component-text", offer.schema)
XCTAssertEqual(OfferType.init(rawValue: 2), offer.type)
XCTAssertEqual("This is a plain text content!", offer.content)
Expand All @@ -232,6 +255,7 @@ class OfferTests: XCTestCase {

XCTAssertEqual("xcore:personalized-offer:3333333333333333", offer.id)
XCTAssertEqual("8", offer.etag)
XCTAssertEqual(0, offer.score)
XCTAssertEqual("https://ns.adobe.com/experience/offer-management/content-component-html", offer.schema)
XCTAssertEqual(OfferType.init(rawValue: 3), offer.type)
XCTAssertEqual("<h1>Hello, Welcome!</h1>", offer.content)
Expand All @@ -251,6 +275,7 @@ class OfferTests: XCTestCase {

XCTAssertEqual("xcore:personalized-offer:4444444444444444", offer.id)
XCTAssertEqual("8", offer.etag)
XCTAssertEqual(0, offer.score)
XCTAssertEqual("https://ns.adobe.com/experience/offer-management/content-component-imagelink", offer.schema)
XCTAssertEqual(OfferType.init(rawValue: 4), offer.type)
XCTAssertEqual("https://example.com/avatar1.png?alt=media", offer.content)
Expand Down Expand Up @@ -321,6 +346,26 @@ class OfferTests: XCTestCase {
XCTAssertNil(offer.characteristics)
}

func testOffer_withScore() {
guard let offerData = OFFER_WITH_SCORE.data(using: .utf8),
let offer = try? JSONDecoder().decode(Offer.self, from: offerData)
else {
XCTFail("Offer should be valid.")
return
}

XCTAssertEqual("xcore:personalized-offer:2222222222222222", offer.id)
XCTAssertEqual("7", offer.etag)
XCTAssertEqual(2, offer.score)
XCTAssertEqual("https://ns.adobe.com/experience/offer-management/content-component-text", offer.schema)
XCTAssertEqual(OfferType.text, offer.type)
XCTAssertEqual("This is a plain text content!", offer.content)
XCTAssertEqual(1, offer.language?.count)
XCTAssertEqual("en-us", offer.language?[0])
XCTAssertEqual(1, offer.characteristics?.count)
XCTAssertEqual("true", offer.characteristics?["mobile"])
}

func testOffer_invalidNoContent() {
guard let offerData = OFFER_INVALID_NO_CONTENT.data(using: .utf8) else {
XCTFail("Offer json data should be valid.")
Expand Down

0 comments on commit 7864ffe

Please sign in to comment.