aws-iot-device-sdk-v2
Version:
NodeJS API for the AWS IoT service
370 lines • 15.4 kB
JavaScript
;
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const aws_crt_1 = require("aws-crt");
const uuid_1 = require("uuid");
const mqtt_request_response_1 = require("../mqtt_request_response");
const events_1 = require("events");
const iotshadowclientv2_1 = require("./iotshadowclientv2");
jest.setTimeout(10000);
function hasTestEnvironment() {
if (process.env.AWS_TEST_MQTT5_IOT_CORE_HOST === undefined) {
return false;
}
if (process.env.AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH === undefined) {
return false;
}
if (process.env.AWS_TEST_MQTT5_IOT_KEY_PATH === undefined) {
return false;
}
return true;
}
const conditional_test = (condition) => condition ? it : it.skip;
function build_protocol_client_mqtt5() {
let builder = aws_crt_1.iot.AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
// @ts-ignore
process.env.AWS_TEST_MQTT5_IOT_CORE_HOST, process.env.AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH, process.env.AWS_TEST_MQTT5_IOT_KEY_PATH);
builder.withConnectProperties({
clientId: `test-${(0, uuid_1.v4)()}`,
keepAliveIntervalSeconds: 1200,
});
return new aws_crt_1.mqtt5.Mqtt5Client(builder.build());
}
function build_protocol_client_mqtt311() {
// @ts-ignore
let builder = aws_crt_1.iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder_from_path(process.env.AWS_TEST_MQTT5_IOT_CERTIFICATE_PATH, process.env.AWS_TEST_MQTT5_IOT_KEY_PATH);
// @ts-ignore
builder.with_endpoint(process.env.AWS_TEST_MQTT5_IOT_CORE_HOST);
builder.with_client_id(`test-${(0, uuid_1.v4)()}`);
let client = new aws_crt_1.mqtt.MqttClient();
return client.new_connection(builder.build());
}
var ProtocolVersion;
(function (ProtocolVersion) {
ProtocolVersion[ProtocolVersion["Mqtt311"] = 0] = "Mqtt311";
ProtocolVersion[ProtocolVersion["Mqtt5"] = 1] = "Mqtt5";
})(ProtocolVersion || (ProtocolVersion = {}));
class ShadowTestingContext {
startProtocolClient() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.protocolStarted) {
this.protocolStarted = true;
if (this.mqtt5Client) {
let connected = (0, events_1.once)(this.mqtt5Client, aws_crt_1.mqtt5.Mqtt5Client.CONNECTION_SUCCESS);
this.mqtt5Client.start();
yield connected;
}
if (this.mqtt311Client) {
yield this.mqtt311Client.connect();
}
}
});
}
stopProtocolClient() {
return __awaiter(this, void 0, void 0, function* () {
if (this.protocolStarted) {
this.protocolStarted = false;
if (this.mqtt5Client) {
let stopped = (0, events_1.once)(this.mqtt5Client, aws_crt_1.mqtt5.Mqtt5Client.STOPPED);
this.mqtt5Client.stop();
yield stopped;
this.mqtt5Client.close();
}
if (this.mqtt311Client) {
yield this.mqtt311Client.disconnect();
}
}
});
}
constructor(options) {
var _a, _b;
this.protocolStarted = false;
if (options.version == ProtocolVersion.Mqtt5) {
this.mqtt5Client = build_protocol_client_mqtt5();
let rrOptions = {
maxRequestResponseSubscriptions: 6,
maxStreamingSubscriptions: 2,
operationTimeoutInSeconds: (_a = options.timeoutSeconds) !== null && _a !== void 0 ? _a : 60,
};
this.client = iotshadowclientv2_1.IotShadowClientv2.newFromMqtt5(this.mqtt5Client, rrOptions);
}
else {
this.mqtt311Client = build_protocol_client_mqtt311();
let rrOptions = {
maxRequestResponseSubscriptions: 6,
maxStreamingSubscriptions: 2,
operationTimeoutInSeconds: (_b = options.timeoutSeconds) !== null && _b !== void 0 ? _b : 60,
};
this.client = iotshadowclientv2_1.IotShadowClientv2.newFromMqtt311(this.mqtt311Client, rrOptions);
}
}
open() {
return __awaiter(this, void 0, void 0, function* () {
yield this.startProtocolClient();
});
}
close() {
return __awaiter(this, void 0, void 0, function* () {
this.client.close();
yield this.stopProtocolClient();
});
}
}
function doCreateDestroyTest(version) {
return __awaiter(this, void 0, void 0, function* () {
let context = new ShadowTestingContext({
version: version
});
yield context.open();
yield context.close();
});
}
test('shadowv2 - create destroy mqtt5', () => __awaiter(void 0, void 0, void 0, function* () {
if (hasTestEnvironment()) {
yield doCreateDestroyTest(ProtocolVersion.Mqtt5);
}
}));
conditional_test(hasTestEnvironment())('shadowv2 - create destroy mqtt311', () => __awaiter(void 0, void 0, void 0, function* () {
yield doCreateDestroyTest(ProtocolVersion.Mqtt311);
}));
function getNonexistentShadow(client, thingName, shadowName) {
return __awaiter(this, void 0, void 0, function* () {
let request = {
thingName: thingName,
shadowName: shadowName,
};
try {
yield client.getNamedShadow(request);
expect(false);
}
catch (err) {
expect(err.message).toContain("Request failed");
expect(err.modeledError).toBeDefined();
expect(err.modeledError.code).toEqual(404);
expect(err.modeledError.message).toContain("No shadow exists with name");
}
});
}
function doGetNonexistentShadowTest(version) {
return __awaiter(this, void 0, void 0, function* () {
let context = new ShadowTestingContext({
version: version
});
yield context.open();
yield getNonexistentShadow(context.client, (0, uuid_1.v4)(), (0, uuid_1.v4)());
yield context.close();
});
}
conditional_test(hasTestEnvironment())('shadowv2 - get non-existent shadow mqtt5', () => __awaiter(void 0, void 0, void 0, function* () {
yield doGetNonexistentShadowTest(ProtocolVersion.Mqtt5);
}));
conditional_test(hasTestEnvironment())('shadowv2 - get non-existent shadow mqtt311', () => __awaiter(void 0, void 0, void 0, function* () {
yield doGetNonexistentShadowTest(ProtocolVersion.Mqtt311);
}));
function createShadow(client, thingName, shadowName, document) {
return __awaiter(this, void 0, void 0, function* () {
let request = {
thingName: thingName,
shadowName: shadowName,
state: {
desired: document,
reported: document,
}
};
try {
let response = yield client.updateNamedShadow(request);
expect(response.state).toBeDefined();
// @ts-ignore
expect(response.state.desired).toBeDefined();
// @ts-ignore
expect(response.state.desired).toEqual(document);
// @ts-ignore
expect(response.state.reported).toBeDefined();
// @ts-ignore
expect(response.state.reported).toEqual(document);
}
catch (err) {
throw err;
}
});
}
function getShadow(client, thingName, shadowName, expectedDocument) {
return __awaiter(this, void 0, void 0, function* () {
let request = {
thingName: thingName,
shadowName: shadowName,
};
let response = yield client.getNamedShadow(request);
expect(response.state).toBeDefined();
// @ts-ignore
expect(response.state.desired).toBeDefined();
// @ts-ignore
expect(response.state.desired).toEqual(expectedDocument);
// @ts-ignore
expect(response.state.reported).toBeDefined();
// @ts-ignore
expect(response.state.reported).toEqual(expectedDocument);
});
}
function deleteShadow(client, thingName, shadowName, expectedVersion) {
return __awaiter(this, void 0, void 0, function* () {
let request = {
thingName: thingName,
shadowName: shadowName,
};
let response = yield client.deleteNamedShadow(request);
expect(response.version).toEqual(expectedVersion);
});
}
function doCreateDeleteShadowTest(version) {
return __awaiter(this, void 0, void 0, function* () {
let context = new ShadowTestingContext({
version: version
});
yield context.open();
let thingName = (0, uuid_1.v4)();
let shadowName = (0, uuid_1.v4)();
let document = {
color: "green",
on: true,
};
// shouldn't exist yet
yield getNonexistentShadow(context.client, thingName, shadowName);
try {
yield createShadow(context.client, thingName, shadowName, document);
yield getShadow(context.client, thingName, shadowName, document);
}
finally {
yield deleteShadow(context.client, thingName, shadowName, 1);
}
yield getNonexistentShadow(context.client, thingName, shadowName);
yield context.close();
});
}
conditional_test(hasTestEnvironment())('shadowv2 - create-destroy shadow mqtt5', () => __awaiter(void 0, void 0, void 0, function* () {
yield doCreateDeleteShadowTest(ProtocolVersion.Mqtt5);
}));
conditional_test(hasTestEnvironment())('shadowv2 - create-destroy shadow mqtt311', () => __awaiter(void 0, void 0, void 0, function* () {
yield doCreateDeleteShadowTest(ProtocolVersion.Mqtt311);
}));
function updateShadowDesired(client, thingName, shadowName, document) {
return __awaiter(this, void 0, void 0, function* () {
let request = {
thingName: thingName,
shadowName: shadowName,
state: {
desired: document,
},
};
try {
let response = yield client.updateNamedShadow(request);
expect(response.state).toBeDefined();
// @ts-ignore
expect(response.state.desired).toBeDefined();
// @ts-ignore
expect(response.state.desired).toEqual(document);
}
catch (err) {
throw err;
}
});
}
function updateShadowReported(client, thingName, shadowName, document) {
return __awaiter(this, void 0, void 0, function* () {
let request = {
thingName: thingName,
shadowName: shadowName,
state: {
reported: document,
},
};
try {
let response = yield client.updateNamedShadow(request);
expect(response.state).toBeDefined();
// @ts-ignore
expect(response.state.reported).toBeDefined();
// @ts-ignore
expect(response.state.reported).toEqual(document);
}
catch (err) {
throw err;
}
});
}
function doUpdateShadowTest(version) {
return __awaiter(this, void 0, void 0, function* () {
let context = new ShadowTestingContext({
version: version
});
yield context.open();
let thingName = (0, uuid_1.v4)();
let shadowName = (0, uuid_1.v4)();
let document = {
color: "green",
on: true,
};
let deltaEventStream = context.client.createNamedShadowDeltaUpdatedStream({
thingName: thingName,
shadowName: shadowName,
});
let deltaStatusUpdate = (0, events_1.once)(deltaEventStream, mqtt_request_response_1.StreamingOperation.SUBSCRIPTION_STATUS);
deltaEventStream.open();
let deltaStatus = (yield deltaStatusUpdate)[0];
expect(deltaStatus.type).toEqual(aws_crt_1.mqtt_request_response.SubscriptionStatusEventType.SubscriptionEstablished);
let stateStream = context.client.createNamedShadowDeltaUpdatedStream({
thingName: thingName,
shadowName: shadowName,
});
let stateStatusUpdate = (0, events_1.once)(stateStream, mqtt_request_response_1.StreamingOperation.SUBSCRIPTION_STATUS);
stateStream.open();
let stateStatus = (yield stateStatusUpdate)[0];
expect(stateStatus.type).toEqual(aws_crt_1.mqtt_request_response.SubscriptionStatusEventType.SubscriptionEstablished);
// shouldn't exist yet
yield getNonexistentShadow(context.client, thingName, shadowName);
try {
yield createShadow(context.client, thingName, shadowName, document);
yield getShadow(context.client, thingName, shadowName, document);
let updateDocument = {
color: "blue",
on: false,
};
let deltaEvent = (0, events_1.once)(deltaEventStream, mqtt_request_response_1.StreamingOperation.INCOMING_PUBLISH);
let stateEvent = (0, events_1.once)(stateStream, mqtt_request_response_1.StreamingOperation.INCOMING_PUBLISH);
yield updateShadowDesired(context.client, thingName, shadowName, updateDocument);
let deltaResult = (yield deltaEvent)[0];
expect(deltaResult).toBeDefined();
expect(deltaResult.message.state).toEqual(updateDocument);
let stateResult = (yield stateEvent)[0];
expect(stateResult).toBeDefined();
expect(stateResult.message.state).toEqual(updateDocument);
yield updateShadowReported(context.client, thingName, shadowName, updateDocument);
yield getShadow(context.client, thingName, shadowName, updateDocument);
}
finally {
deltaEventStream.close();
stateStream.close();
yield deleteShadow(context.client, thingName, shadowName, 3);
}
yield getNonexistentShadow(context.client, thingName, shadowName);
yield context.close();
});
}
conditional_test(hasTestEnvironment())('shadowv2 - update shadow mqtt5', () => __awaiter(void 0, void 0, void 0, function* () {
yield doUpdateShadowTest(ProtocolVersion.Mqtt5);
}));
conditional_test(hasTestEnvironment())('shadowv2 - update shadow mqtt311', () => __awaiter(void 0, void 0, void 0, function* () {
yield doUpdateShadowTest(ProtocolVersion.Mqtt311);
}));
//# sourceMappingURL=iotshadowclientv2.spec.js.map