UNPKG

@mindconnect/mindconnect-nodejs

Version:

NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)

211 lines 9.11 kB
"use strict"; 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 }); exports.tearDownAgents = exports.createUnitTestAsset = exports.setupStructure = exports.unitTestSetup = void 0; const fs = require("fs"); const src_1 = require("../src"); const sdk_1 = require("../src/api/sdk"); const test_utils_1 = require("./test-utils"); function unitTestSetup(sdk, profile) { return __awaiter(this, void 0, void 0, function* () { const agentMgmt = sdk.GetAgentManagementClient(); const assetMgmt = sdk.GetAssetManagementClient(); const { targetAsset, folderid } = yield setupStructure(sdk); const aspects = yield assetMgmt.GetAspects(`${targetAsset.assetId}`); targetAsset.aspects = unroll(aspects); const agentAsset = yield assetMgmt.PostAsset({ name: `Agent${new Date().getTime()}`, parentId: folderid, typeId: "core.mclib", }); const agent = yield agentMgmt.PostAgent({ entityId: `${agentAsset.assetId}`, name: `${agentAsset.assetId}`, securityProfile: profile, }); const agentConfig = yield agentMgmt.GetBoardingConfiguration(agent.entityId, { retry: 5 }); return { targetAsset, agentAsset, agent, agentConfig, folderid }; }); } exports.unitTestSetup = unitTestSetup; function setupStructure(sdk) { return __awaiter(this, void 0, void 0, function* () { const agentMgmt = sdk.GetAgentManagementClient(); const assetMgmt = sdk.GetAssetManagementClient(); const tenant = sdk.GetTenant(); const rootassetid = `${(yield assetMgmt.GetRootAsset()).assetId}`; const folders = unroll(yield assetMgmt.GetAssets({ filter: JSON.stringify({ name: { eq: "UnitTestFolder", }, }), })); if (folders.length === 0) { const folder = yield assetMgmt.PostAsset({ name: "UnitTestFolder", typeId: "core.basicarea", parentId: rootassetid, }); folders.push(folder); } const folder = folders[0]; const folderid = `${folder.assetId}`; // Check if we have the aspect types setup const aspectTypes = unroll(yield assetMgmt.GetAspectTypes({ filter: JSON.stringify({ id: { in: { value: [`${tenant}.UnitTestEnvironment`, `${tenant}.UnitTestVibration`], }, }, }), })); if (aspectTypes.length === 0) { yield assetMgmt.PutAspectType(`${tenant}.UnitTestEnvironment`, { name: "UnitTestEnvironment", category: sdk_1.AssetManagementModels.AspectType.CategoryEnum.Dynamic, scope: sdk_1.AssetManagementModels.AspectType.ScopeEnum.Private, variables: [ { name: "Humidity", dataType: sdk_1.AssetManagementModels.VariableDefinition.DataTypeEnum.INT, qualityCode: true, unit: "%", }, { name: "Pressure", dataType: sdk_1.AssetManagementModels.VariableDefinition.DataTypeEnum.DOUBLE, qualityCode: true, unit: "kPa", }, { name: "Temperature", dataType: sdk_1.AssetManagementModels.VariableDefinition.DataTypeEnum.DOUBLE, qualityCode: true, unit: "°C", }, ], }); yield assetMgmt.PutAspectType(`${tenant}.UnitTestVibration`, { name: "UnitTestVibration", category: sdk_1.AssetManagementModels.AspectType.CategoryEnum.Dynamic, scope: sdk_1.AssetManagementModels.AspectType.ScopeEnum.Private, variables: [ { name: "Acceleration", dataType: sdk_1.AssetManagementModels.VariableDefinition.DataTypeEnum.DOUBLE, qualityCode: true, unit: "mm/s^2", }, { name: "Displacement", dataType: sdk_1.AssetManagementModels.VariableDefinition.DataTypeEnum.DOUBLE, qualityCode: true, unit: "mm", }, { name: "Frequency", dataType: sdk_1.AssetManagementModels.VariableDefinition.DataTypeEnum.DOUBLE, qualityCode: true, unit: "Hz", }, { name: "Velocity", dataType: sdk_1.AssetManagementModels.VariableDefinition.DataTypeEnum.DOUBLE, qualityCode: true, unit: "mm/s", }, ], }); } const assetType = unroll(yield assetMgmt.GetAssetTypes({ filter: JSON.stringify({ name: { eq: `UnitTestEngine` } }), })); if (assetType.length === 0) { yield assetMgmt.PutAssetType(`${tenant}.UnitTestEngine`, { name: "UnitTestEngine", parentTypeId: "core.BasicAsset", aspects: [ { name: "EnvironmentData", aspectTypeId: `${tenant}.UnitTestEnvironment` }, { name: "VibrationData", aspectTypeId: `${tenant}.UnitTestVibration` }, ], }); } const name = "UnitTestEngineInstance"; const targetAsset = yield createUnitTestAsset({ assetMgmt, folderid, tenant, name }); return { targetAsset, folderid }; }); } exports.setupStructure = setupStructure; function unroll(obj) { const embedded = obj._embedded; if (!embedded) return []; const keys = Object.keys(embedded); if (keys.length === 0) return []; if (keys.length === 1) return embedded[keys[0]]; throw new Error("cant unroll object"); } function createUnitTestAsset(_a) { return __awaiter(this, arguments, void 0, function* ({ assetMgmt, folderid, tenant, name, }) { const asset = unroll(yield assetMgmt.GetAssets({ filter: JSON.stringify({ and: { name: { eq: name, }, parentId: { eq: folderid, }, }, }), })); if (asset.length === 0) { yield assetMgmt.PostAsset({ name: name, typeId: `${tenant}.UnitTestEngine`, parentId: folderid, }); } const targetAsset = unroll(yield assetMgmt.GetAssets({ filter: JSON.stringify({ name: { eq: name, }, }), }))[0]; return targetAsset; }); } exports.createUnitTestAsset = createUnitTestAsset; function tearDownAgents(sdk, config) { return __awaiter(this, void 0, void 0, function* () { const agentMgmt = sdk.GetAgentManagementClient(); const assetMgmt = sdk.GetAssetManagementClient(); const status = yield agentMgmt.GetOnboardingStatus(`${config.agent.id}`); try { fs.unlinkSync(`.mc/${config.agent.id}.json`); } catch (_a) { } if (status.status === sdk_1.AgentManagementModels.OnboardingStatus.StatusEnum.ONBOARDED) { yield (0, src_1.retry)(5, () => agentMgmt.OffboardAgent(`${config.agent.id}`)); } yield (0, test_utils_1.sleep)(5000); const etag = (yield assetMgmt.GetAsset(`${config.agentAsset.assetId}`)).etag; yield (0, src_1.retry)(5, () => agentMgmt.DeleteAgent(`${config.agent.id}`, { ifMatch: `${config.agent.eTag}` })); yield (0, src_1.retry)(5, () => assetMgmt.DeleteAsset(`${config.agentAsset.assetId}`, { ifMatch: `${etag}` })); }); } exports.tearDownAgents = tearDownAgents; //# sourceMappingURL=test-agent-setup-utils.js.map