|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +import { assert } from "chai"; |
| 4 | + |
| 5 | +import { |
| 6 | + convertEventGridEventToModelType, |
| 7 | + convertCloudEventToModelType |
| 8 | +} from "../../src/eventGridClient"; |
| 9 | + |
| 10 | +describe("convertEventGridEventToModelType", function() { |
| 11 | + it("sets a default ID if one is not provided", () => { |
| 12 | + const convertedEvent = convertEventGridEventToModelType({ |
| 13 | + dataVersion: "1.0", |
| 14 | + eventType: "Azure.Sdk.TestEvent", |
| 15 | + subject: "Test Event", |
| 16 | + data: { hello: "world " } |
| 17 | + }); |
| 18 | + |
| 19 | + assert.isDefined(convertedEvent.id); |
| 20 | + }); |
| 21 | + |
| 22 | + it("sets a default event time if one is not provided", () => { |
| 23 | + const convertedEvent = convertEventGridEventToModelType({ |
| 24 | + dataVersion: "1.0", |
| 25 | + eventType: "Azure.Sdk.TestEvent", |
| 26 | + subject: "Test Event", |
| 27 | + data: { hello: "world " } |
| 28 | + }); |
| 29 | + |
| 30 | + assert.isDefined(convertedEvent.eventTime); |
| 31 | + }); |
| 32 | + |
| 33 | + it("does not change set values", () => { |
| 34 | + const time = new Date(); |
| 35 | + const id = "272871ba-2496-4750-9a90-bedd1ea10191"; |
| 36 | + |
| 37 | + const convertedEvent = convertEventGridEventToModelType({ |
| 38 | + id: id, |
| 39 | + eventTime: time, |
| 40 | + dataVersion: "1.0", |
| 41 | + eventType: "Azure.Sdk.TestEvent", |
| 42 | + subject: "Test Event", |
| 43 | + data: { hello: "world " } |
| 44 | + }); |
| 45 | + |
| 46 | + assert.strictEqual(convertedEvent.id, id); |
| 47 | + assert.strictEqual(convertedEvent.eventTime, time); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("convertCloudEventToModelType", function() { |
| 52 | + it("sets a default ID if one is not provided", () => { |
| 53 | + const convertedEvent = convertCloudEventToModelType({ |
| 54 | + source: "/azure/sdk/tests", |
| 55 | + type: "Azure.Sdk.TestEvent" |
| 56 | + }); |
| 57 | + |
| 58 | + assert.isDefined(convertedEvent.id); |
| 59 | + }); |
| 60 | + |
| 61 | + it("sets a default event time if one is not provided", () => { |
| 62 | + const convertedEvent = convertCloudEventToModelType({ |
| 63 | + source: "/azure/sdk/tests", |
| 64 | + type: "Azure.Sdk.TestEvent" |
| 65 | + }); |
| 66 | + |
| 67 | + assert.isDefined(convertedEvent.time); |
| 68 | + }); |
| 69 | + |
| 70 | + it("does not change set values", () => { |
| 71 | + const time = new Date(); |
| 72 | + const id = "272871ba-2496-4750-9a90-bedd1ea10191"; |
| 73 | + |
| 74 | + const convertedEvent = convertCloudEventToModelType({ |
| 75 | + id: id, |
| 76 | + time: time, |
| 77 | + source: "/azure/sdk/tests", |
| 78 | + type: "Azure.Sdk.TestEvent" |
| 79 | + }); |
| 80 | + |
| 81 | + assert.strictEqual(convertedEvent.id, id); |
| 82 | + assert.strictEqual(convertedEvent.time, time); |
| 83 | + }); |
| 84 | + |
| 85 | + it("promotes extension attributes", () => { |
| 86 | + const traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"; |
| 87 | + const tracestate = |
| 88 | + "rojo=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01,congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4"; |
| 89 | + |
| 90 | + const convertedEvent = convertCloudEventToModelType({ |
| 91 | + source: "/azure/sdk/tests", |
| 92 | + type: "Azure.Sdk.TestEvent", |
| 93 | + extensionAttributes: { |
| 94 | + traceparent, |
| 95 | + tracestate |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // When converted to a model type to send over the wire, the extension attributes are promoted to be |
| 100 | + // properties on the envelope itself. |
| 101 | + assert.equal(convertedEvent["traceparent"], traceparent); |
| 102 | + assert.equal(convertedEvent["tracestate"], tracestate); |
| 103 | + }); |
| 104 | + |
| 105 | + it("base64 encodes binary data", () => { |
| 106 | + const binaryData = new Uint8Array(10); |
| 107 | + for (let i = 0; i < binaryData.length; i++) { |
| 108 | + binaryData[i] = i; |
| 109 | + } |
| 110 | + |
| 111 | + const convertedEvent = convertCloudEventToModelType({ |
| 112 | + source: "/azure/sdk/tests", |
| 113 | + type: "Azure.Sdk.TestEvent", |
| 114 | + data: binaryData, |
| 115 | + datacontenttype: "application/binary" |
| 116 | + }); |
| 117 | + |
| 118 | + assert.isUndefined(convertedEvent.data); |
| 119 | + assert.strictEqual(convertedEvent.dataBase64, binaryData); |
| 120 | + }); |
| 121 | + |
| 122 | + it("fails if data content type is missing for binary data", () => { |
| 123 | + const binaryData = new Uint8Array(10); |
| 124 | + for (let i = 0; i < binaryData.length; i++) { |
| 125 | + binaryData[i] = i; |
| 126 | + } |
| 127 | + |
| 128 | + assert.throws(() => { |
| 129 | + convertCloudEventToModelType({ |
| 130 | + source: "/azure/sdk/tests", |
| 131 | + type: "Azure.Sdk.TestEvent", |
| 132 | + data: binaryData |
| 133 | + }); |
| 134 | + }, /data content type/); |
| 135 | + }); |
| 136 | + |
| 137 | + it("fails if extenion attributes are invalid", () => { |
| 138 | + const binaryData = new Uint8Array(10); |
| 139 | + for (let i = 0; i < binaryData.length; i++) { |
| 140 | + binaryData[i] = i; |
| 141 | + } |
| 142 | + |
| 143 | + assert.throws(() => { |
| 144 | + convertCloudEventToModelType({ |
| 145 | + source: "/azure/sdk/tests", |
| 146 | + type: "Azure.Sdk.TestEvent", |
| 147 | + extensionAttributes: { |
| 148 | + source: "this-is-not-allowed" |
| 149 | + } |
| 150 | + }); |
| 151 | + }, /invalid extension attribute name: source/); |
| 152 | + |
| 153 | + assert.throws(() => { |
| 154 | + convertCloudEventToModelType({ |
| 155 | + source: "/azure/sdk/tests", |
| 156 | + type: "Azure.Sdk.TestEvent", |
| 157 | + extensionAttributes: { |
| 158 | + MiXedCasE: "this-is-not-allowed" |
| 159 | + } |
| 160 | + }); |
| 161 | + }, /invalid extension attribute name: MiXedCasE/); |
| 162 | + |
| 163 | + assert.throws(() => { |
| 164 | + convertCloudEventToModelType({ |
| 165 | + source: "/azure/sdk/tests", |
| 166 | + type: "Azure.Sdk.TestEvent", |
| 167 | + extensionAttributes: { |
| 168 | + data_base64: "this-is-not-allowed" |
| 169 | + } |
| 170 | + }); |
| 171 | + }, /invalid extension attribute name: data_base64/); |
| 172 | + }); |
| 173 | +}); |
0 commit comments