Skip to content

Commit

Permalink
Merge pull request #292 from adobe/update-e2e-tests
Browse files Browse the repository at this point in the history
Update e2e tests
  • Loading branch information
sbenedicadb authored Aug 13, 2024
2 parents 5c85d66 + 184864f commit 873993f
Showing 1 changed file with 264 additions and 32 deletions.
296 changes: 264 additions & 32 deletions AEPMessaging/Tests/E2EFunctionalTests/E2EFunctionalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {

func testRefreshInAppMessagesHappy() throws {
// setup
var testCompleted = false
let messagingRequestContentExpectation = XCTestExpectation(description: "messaging request content listener called")
registerMessagingRequestContentListener() { event in
XCTAssertNotNil(event)
let data = event.data
XCTAssertNotNil(data)
XCTAssertEqual(true, data?[MessagingConstants.Event.Data.Key.REFRESH_MESSAGES] as? Bool)
messagingRequestContentExpectation.fulfill()
if (!testCompleted) {
XCTAssertNotNil(event)
let data = event.data
XCTAssertNotNil(data)
XCTAssertEqual(true, data?[MessagingConstants.Event.Data.Key.REFRESH_MESSAGES] as? Bool)
testCompleted = true
messagingRequestContentExpectation.fulfill()
}
}

// test
Expand All @@ -111,36 +115,171 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {
// verify
wait(for: [messagingRequestContentExpectation], timeout: asyncTimeout)
}

func testIAMMessagesReturnedFromXASHaveCorrectJsonFormat() throws {
// setup
var testCompleted = false
let edgePersonalizationDecisionsExpectation = XCTestExpectation(description: "edge personalization decisions listener called")
registerEdgePersonalizationDecisionsListener() { event in
if (!testCompleted) {
XCTAssertNotNil(event)

// validate the payload exists
guard let payload = event.data?["payload"] as? [[String: Any]] else {
// no payload means this event is a request, not a response
return
}

// validate the payload is not empty
guard !payload.isEmpty else {
XCTFail("SDK TEST ERROR - expected a payload object, but payload is empty")
return
}

// loop through the payload and verify the format for each object
for payloadObject in payload {
self.validateIAMPayloadObject(payloadObject)
self.validateIAMPayloadContainsMatchingScope(payloadObject)
}
testCompleted = true
edgePersonalizationDecisionsExpectation.fulfill()
}
}

// test
Messaging.refreshInAppMessages()

func testMessagesReturnedFromXASHaveCorrectJsonFormat() throws {
// verify
wait(for: [edgePersonalizationDecisionsExpectation], timeout: asyncTimeout)
}

func testUpdatePropositionsForSurfacesCBEHappy() throws {
// setup
var testCompleted = false
let messagingRequestContentExpectation = XCTestExpectation(description: "messaging request content listener called")
registerMessagingRequestContentListener() { event in
if(!testCompleted) {
XCTAssertNotNil(event)
let data = event.data
XCTAssertNotNil(data)
XCTAssertEqual(true, data?[MessagingConstants.Event.Data.Key.UPDATE_PROPOSITIONS] as? Bool)
testCompleted = true
messagingRequestContentExpectation.fulfill()
}
}

// test
let surfaces: [Surface] = [
Surface(path: "cbeJson")
]
Messaging.updatePropositionsForSurfaces(surfaces)

// verify
wait(for: [messagingRequestContentExpectation], timeout: asyncTimeout)
}

func testCBEMessagesReturnedFromXASHaveCorrectJsonFormat() throws {
// setup
var testCompleted = false
let edgePersonalizationDecisionsExpectation = XCTestExpectation(description: "edge personalization decisions listener called")
registerEdgePersonalizationDecisionsListener() { event in
XCTAssertNotNil(event)

// validate the payload exists
guard let payload = event.data?["payload"] as? [[String: Any]] else {
// no payload means this event is a request, not a response
return
if (!testCompleted) {
XCTAssertNotNil(event)

// validate the payload exists
guard let payload = event.data?["payload"] as? [[String: Any]] else {
// no payload means this event is a request, not a response
return
}

// validate the payload is not empty
guard !payload.isEmpty else {
XCTFail("SDK TEST ERROR - expected a payload object, but payload is empty")
return
}


// loop through the payload and verify the format for each object
for payloadObject in payload {
self.validateCBEPayloadObject(payloadObject)
self.validateCBEPayloadContainsMatchingScope(payloadObject)
}
testCompleted = true
edgePersonalizationDecisionsExpectation.fulfill()
}

// validate the payload is not empty
guard !payload.isEmpty else {
XCTFail("SDK TEST ERROR - expected a payload object, but payload is empty")
return
}

// test
let surfaces: [Surface] = [
Surface(path: "cbeJson")
]
Messaging.updatePropositionsForSurfaces(surfaces)

// verify
wait(for: [edgePersonalizationDecisionsExpectation], timeout: asyncTimeout)
}

func testUpdatePropositionsForSurfacesContentCardHappy() throws {
// setup
var testCompleted = false
let messagingRequestContentExpectation = XCTestExpectation(description: "messaging request content listener called")
registerMessagingRequestContentListener() { event in
if(!testCompleted) {
XCTAssertNotNil(event)
let data = event.data
XCTAssertNotNil(data)
XCTAssertEqual(true, data?[MessagingConstants.Event.Data.Key.UPDATE_PROPOSITIONS] as? Bool)
testCompleted = true
messagingRequestContentExpectation.fulfill()
}

// loop through the payload and verify the format for each object
for payloadObject in payload {
self.validatePayloadObject(payloadObject)
self.validatePayloadContainsMatchingScope(payloadObject)
}

// test
let surfaces: [Surface] = [
Surface(path: "cards/ms")
]
Messaging.updatePropositionsForSurfaces(surfaces)

// verify
wait(for: [messagingRequestContentExpectation], timeout: asyncTimeout)
}

func testContentCardMessagesReturnedFromXASHaveCorrectJsonFormat() throws {
// setup
var testCompleted = false
let edgePersonalizationDecisionsExpectation = XCTestExpectation(description: "edge personalization decisions listener called")
registerEdgePersonalizationDecisionsListener() { event in
if (!testCompleted) {
XCTAssertNotNil(event)

// validate the payload exists
guard let payload = event.data?["payload"] as? [[String: Any]] else {
// no payload means this event is a request, not a response
return
}

// validate the payload is not empty
guard !payload.isEmpty else {
XCTFail("SDK TEST ERROR - expected a payload object, but payload is empty")
return
}


// loop through the payload and verify the format for each object
for payloadObject in payload {
self.validateContentCardPayloadObject(payloadObject)
self.validateContentCardPayloadContainsMatchingScope(payloadObject)
}
testCompleted = true
edgePersonalizationDecisionsExpectation.fulfill()
}

edgePersonalizationDecisionsExpectation.fulfill()
}

// test
Messaging.refreshInAppMessages()
let surfaces: [Surface] = [
Surface(path: "cards/ms")
]
Messaging.updatePropositionsForSurfaces(surfaces)

// verify
wait(for: [edgePersonalizationDecisionsExpectation], timeout: asyncTimeout)
Expand All @@ -152,13 +291,13 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {
// // setup
// let edgePersonalizationDecisionsExpectation = XCTestExpectation(description: "edge personalization decisions listener called")
// registerEdgePersonalizationDecisionsListener() { event in
//
//
// // validate the content is a valid rule containing a valid message
// guard let propositions = event.payload else {
// // no payload means this event is a request, not a response
// return
// }
//
//
// let messagingRulesEngine = MessagingRulesEngine(name: "testRulesEngine", extensionRuntime: self.mockRuntime, cache: self.mockCache)
// var rulesArray: [LaunchRule] = []
//
Expand All @@ -170,10 +309,10 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {
// rulesArray.append(contentsOf: rule)
// }
// }
//
//
// // load the parsed rules into the rules engine
// messagingRulesEngine.loadRules(rulesArray, clearExisting: true)
//
//
// // rules load async - brief sleep to allow it to finish
// self.runAfter(seconds: 3) {
// XCTAssertTrue(messagingRulesEngine.rulesEngine.rulesEngine.rules.count > 0, "Message definition successfully loaded into the rules engine.")
Expand Down Expand Up @@ -207,7 +346,7 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {
// }
// }
// MobileCore.messagingDelegate = self
//
//
// // allow rules engine to be hydrated
// runAfter(seconds: 5) {
// MobileCore.track(action: "showModal", data: nil)
Expand All @@ -222,7 +361,77 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(seconds), execute: closure)
}

func validatePayloadObject(_ payload: [String: Any]) {
func validateIAMPayloadObject(_ payload: [String: Any]) {
let expectedPayloadJSON = #"""
{
"id": "string",
"scope": "string",
"scopeDetails": {
"activity": {
"id": "string",
"matchedSurfaces": []
},
"characteristics": {
"eventToken": "string"
},
"correlationID": "string",
"decisionProvider": "string"
},
"items": [
{
"id": "string",
"schema": "string",
"data": {
"rules" : [
{
"condition": {},
"consequences": []
}
],
"version": 12.34
}
}
]
}
"""#

// validate required fields are in first payload item and their types are correct
assertTypeMatch(expected: expectedPayloadJSON.toAnyCodable()!, actual: AnyCodable(payload), pathOptions: [])
}

func validateCBEPayloadObject(_ payload: [String: Any]) {
let expectedPayloadJSON = #"""
{
"id": "string",
"scope": "string",
"scopeDetails": {
"activity": {
"id": "string",
"matchedSurfaces": []
},
"characteristics": {
"eventToken": "string"
},
"correlationID": "string",
"decisionProvider": "string"
},
"items": [
{
"id": "string",
"schema": "string",
"data": {
"content" : {}
}
}
]
}
"""#

// validate required fields are in first payload item and their types are correct
assertTypeMatch(expected: expectedPayloadJSON.toAnyCodable()!, actual: AnyCodable(payload), pathOptions: [])
}

func validateContentCardPayloadObject(_ payload: [String: Any]) {
let expectedPayloadJSON = #"""
{
"id": "string",
Expand Down Expand Up @@ -260,7 +469,7 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {
assertTypeMatch(expected: expectedPayloadJSON.toAnyCodable()!, actual: AnyCodable(payload), pathOptions: [])
}

func validatePayloadContainsMatchingScope(_ payload: [String: Any]) {
func validateIAMPayloadContainsMatchingScope(_ payload: [String: Any]) {
let expectedPayloadJSON = #"""
{
"scope": "mobileapp://com.adobe.ajoinbounde2etestsonly"
Expand All @@ -271,6 +480,28 @@ class E2EFunctionalTests: XCTestCase, AnyCodableAsserts {
assertExactMatch(expected: expectedPayloadJSON.toAnyCodable()!, actual: AnyCodable(payload))
}

func validateCBEPayloadContainsMatchingScope(_ payload: [String: Any]) {
let expectedPayloadJSON = #"""
{
"scope": "mobileapp://com.adobe.ajoinbounde2etestsonly/cbeJson"
}
"""#

// validate only the scope and that it has the correct value
assertExactMatch(expected: expectedPayloadJSON.toAnyCodable()!, actual: AnyCodable(payload))
}

func validateContentCardPayloadContainsMatchingScope(_ payload: [String: Any]) {
let expectedPayloadJSON = #"""
{
"scope": "mobileapp://com.adobe.ajoinbounde2etestsonly/cards/ms"
}
"""#

// validate only the scope and that it has the correct value
assertExactMatch(expected: expectedPayloadJSON.toAnyCodable()!, actual: AnyCodable(payload))
}

func missingField(_ key: String) -> String {
return "SDK TEST ERROR - Required field '\(key)' is missing from the map."
}
Expand Down Expand Up @@ -308,3 +539,4 @@ extension Event {
return propositionEventType.contains(where: { $0.key == type})
}
}

0 comments on commit 873993f

Please sign in to comment.