|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +@testable import AWSLambdaEvents |
| 16 | +import XCTest |
| 17 | + |
| 18 | +class CloudwatchTests: XCTestCase { |
| 19 | + static func eventPayload(type: String, details: String) -> String { |
| 20 | + """ |
| 21 | + { |
| 22 | + "id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c", |
| 23 | + "detail-type": "\(type)", |
| 24 | + "source": "aws.events", |
| 25 | + "account": "123456789012", |
| 26 | + "time": "1970-01-01T00:00:00Z", |
| 27 | + "region": "us-east-1", |
| 28 | + "resources": [ |
| 29 | + "arn:aws:events:us-east-1:123456789012:rule/ExampleRule" |
| 30 | + ], |
| 31 | + "detail": \(details) |
| 32 | + } |
| 33 | + """ |
| 34 | + } |
| 35 | + |
| 36 | + func testScheduledEventFromJSON() { |
| 37 | + let payload = CloudwatchTests.eventPayload(type: Cloudwatch.Scheduled.name, details: "{}") |
| 38 | + let data = payload.data(using: .utf8)! |
| 39 | + var maybeEvent: Cloudwatch.ScheduledEvent? |
| 40 | + XCTAssertNoThrow(maybeEvent = try JSONDecoder().decode(Cloudwatch.ScheduledEvent.self, from: data)) |
| 41 | + |
| 42 | + guard let event = maybeEvent else { |
| 43 | + return XCTFail("Expected to have an event") |
| 44 | + } |
| 45 | + |
| 46 | + XCTAssertEqual(event.id, "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c") |
| 47 | + XCTAssertEqual(event.source, "aws.events") |
| 48 | + XCTAssertEqual(event.accountId, "123456789012") |
| 49 | + XCTAssertEqual(event.time, Date(timeIntervalSince1970: 0)) |
| 50 | + XCTAssertEqual(event.region, .us_east_1) |
| 51 | + XCTAssertEqual(event.resources, ["arn:aws:events:us-east-1:123456789012:rule/ExampleRule"]) |
| 52 | + } |
| 53 | + |
| 54 | + func testEC2InstanceStateChangeNotificationEventFromJSON() { |
| 55 | + let payload = CloudwatchTests.eventPayload(type: Cloudwatch.EC2.InstanceStateChangeNotification.name, |
| 56 | + details: "{ \"instance-id\": \"0\", \"state\": \"stopping\" }") |
| 57 | + let data = payload.data(using: .utf8)! |
| 58 | + var maybeEvent: Cloudwatch.EC2.InstanceStateChangeNotificationEvent? |
| 59 | + XCTAssertNoThrow(maybeEvent = try JSONDecoder().decode(Cloudwatch.EC2.InstanceStateChangeNotificationEvent.self, from: data)) |
| 60 | + |
| 61 | + guard let event = maybeEvent else { |
| 62 | + return XCTFail("Expected to have an event") |
| 63 | + } |
| 64 | + |
| 65 | + XCTAssertEqual(event.id, "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c") |
| 66 | + XCTAssertEqual(event.source, "aws.events") |
| 67 | + XCTAssertEqual(event.accountId, "123456789012") |
| 68 | + XCTAssertEqual(event.time, Date(timeIntervalSince1970: 0)) |
| 69 | + XCTAssertEqual(event.region, .us_east_1) |
| 70 | + XCTAssertEqual(event.resources, ["arn:aws:events:us-east-1:123456789012:rule/ExampleRule"]) |
| 71 | + XCTAssertEqual(event.detail.instanceId, "0") |
| 72 | + XCTAssertEqual(event.detail.state, .stopping) |
| 73 | + } |
| 74 | + |
| 75 | + func testEC2SpotInstanceInterruptionNoticeEventFromJSON() { |
| 76 | + let payload = CloudwatchTests.eventPayload(type: Cloudwatch.EC2.SpotInstanceInterruptionNotice.name, |
| 77 | + details: "{ \"instance-id\": \"0\", \"instance-action\": \"terminate\" }") |
| 78 | + let data = payload.data(using: .utf8)! |
| 79 | + var maybeEvent: Cloudwatch.EC2.SpotInstanceInterruptionNoticeEvent? |
| 80 | + XCTAssertNoThrow(maybeEvent = try JSONDecoder().decode(Cloudwatch.EC2.SpotInstanceInterruptionNoticeEvent.self, from: data)) |
| 81 | + |
| 82 | + guard let event = maybeEvent else { |
| 83 | + return XCTFail("Expected to have an event") |
| 84 | + } |
| 85 | + |
| 86 | + XCTAssertEqual(event.id, "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c") |
| 87 | + XCTAssertEqual(event.source, "aws.events") |
| 88 | + XCTAssertEqual(event.accountId, "123456789012") |
| 89 | + XCTAssertEqual(event.time, Date(timeIntervalSince1970: 0)) |
| 90 | + XCTAssertEqual(event.region, .us_east_1) |
| 91 | + XCTAssertEqual(event.resources, ["arn:aws:events:us-east-1:123456789012:rule/ExampleRule"]) |
| 92 | + XCTAssertEqual(event.detail.instanceId, "0") |
| 93 | + XCTAssertEqual(event.detail.action, .terminate) |
| 94 | + } |
| 95 | + |
| 96 | + func testCustomEventFromJSON() { |
| 97 | + struct Custom: CloudwatchDetail { |
| 98 | + public static let name = "Custom" |
| 99 | + |
| 100 | + let name: String |
| 101 | + } |
| 102 | + |
| 103 | + let payload = CloudwatchTests.eventPayload(type: Custom.name, details: "{ \"name\": \"foo\" }") |
| 104 | + let data = payload.data(using: .utf8)! |
| 105 | + var maybeEvent: Cloudwatch.Event<Custom>? |
| 106 | + XCTAssertNoThrow(maybeEvent = try JSONDecoder().decode(Cloudwatch.Event<Custom>.self, from: data)) |
| 107 | + |
| 108 | + guard let event = maybeEvent else { |
| 109 | + return XCTFail("Expected to have an event") |
| 110 | + } |
| 111 | + |
| 112 | + XCTAssertEqual(event.id, "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c") |
| 113 | + XCTAssertEqual(event.source, "aws.events") |
| 114 | + XCTAssertEqual(event.accountId, "123456789012") |
| 115 | + XCTAssertEqual(event.time, Date(timeIntervalSince1970: 0)) |
| 116 | + XCTAssertEqual(event.region, .us_east_1) |
| 117 | + XCTAssertEqual(event.resources, ["arn:aws:events:us-east-1:123456789012:rule/ExampleRule"]) |
| 118 | + XCTAssertEqual(event.detail.name, "foo") |
| 119 | + } |
| 120 | + |
| 121 | + func testUnregistredType() { |
| 122 | + let payload = CloudwatchTests.eventPayload(type: UUID().uuidString, details: "{}") |
| 123 | + let data = payload.data(using: .utf8)! |
| 124 | + XCTAssertThrowsError(try JSONDecoder().decode(Cloudwatch.ScheduledEvent.self, from: data)) { error in |
| 125 | + XCTAssert(error is Cloudwatch.PayloadTypeMismatch, "expected PayloadTypeMismatch but received \(error)") |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + func testTypeMismatch() { |
| 130 | + let payload = CloudwatchTests.eventPayload(type: Cloudwatch.EC2.InstanceStateChangeNotification.name, |
| 131 | + details: "{ \"instance-id\": \"0\", \"state\": \"stopping\" }") |
| 132 | + let data = payload.data(using: .utf8)! |
| 133 | + XCTAssertThrowsError(try JSONDecoder().decode(Cloudwatch.ScheduledEvent.self, from: data)) { error in |
| 134 | + XCTAssert(error is Cloudwatch.PayloadTypeMismatch, "expected PayloadTypeMismatch but received \(error)") |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments