@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)
119 lines • 6 kB
JavaScript
;
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());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai = require("chai");
require("url-search-params-polyfill");
const sdk_1 = require("../src/api/sdk");
const utils_1 = require("../src/api/utils");
const test_utils_1 = require("./test-utils");
chai.should();
const timeOffset = new Date().getTime();
describe("[SDK] AssetManagementClient.Files", () => {
const auth = (0, utils_1.loadAuth)();
const sdk = new sdk_1.MindSphereSdk(Object.assign(Object.assign({}, auth), { basicAuth: (0, utils_1.decrypt)(auth, (0, test_utils_1.getPasskeyForUnitTest)()) }));
const am = sdk.GetAssetManagementClient();
before(() => __awaiter(void 0, void 0, void 0, function* () {
yield deleteFiles(am);
}));
after(() => __awaiter(void 0, void 0, void 0, function* () {
yield deleteFiles(am);
}));
it("SDK should not be undefined", () => __awaiter(void 0, void 0, void 0, function* () {
sdk.should.not.be.undefined;
}));
it("should be able to POST AND GET file @sanity", () => __awaiter(void 0, void 0, void 0, function* () {
if (process.env.CI) {
return; // ! lets do this only locally as mindsphere sometimes behaves strange on fast deletion of files
}
// ! the behavior here is asynchronous...
const result = yield (0, utils_1.retry)(5, () => am.PostFile(Buffer.from("xyz"), `${timeOffset}xyz.2.text`, {
mimeType: "text/plain",
description: "Blubb2",
}), 1000);
const files = yield am.GetFiles({ filter: JSON.stringify({ name: `${timeOffset}xyz.2.text` }) });
files._embedded.files.length.should.equal(1);
yield am.DeleteFile(`${result.id}`, { ifMatch: `${result.etag}` });
}));
it("should be able to Download file", () => __awaiter(void 0, void 0, void 0, function* () {
if (process.env.CI) {
return; // ! lets do this only locally as mindsphere sometimes behaves strange on fast deletion of files
}
const result = yield am.PostFile(Buffer.from("xyz"), `${timeOffset}xyz.2.text`, {
mimeType: "text/plain",
description: "Blubb2",
});
// ! the behavior here is asynchronous...
const file = (yield (0, utils_1.retry)(5, () => am.DownloadFile(`${result.id}`), 1000));
const text = yield file.text();
text.should.be.equal("xyz");
yield am.DeleteFile(`${result.id}`, { ifMatch: `${result.etag}` });
}));
it("should be able to DELETE file", () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield am.PostFile(Buffer.from("abc"), `${timeOffset}abc.2.text`, {
mimeType: "text/plain",
description: "Blubb2",
});
yield am.DeleteFile(`${result.id}`, { ifMatch: `${result.etag}` });
}));
it("should be able to PUT file", () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield am.PostFile(Buffer.from("abc"), `${timeOffset}test2.2.text`, {
mimeType: "text/plain",
description: "Blubb2",
});
const updatedFile = yield am.PutFile(`${result.id}`, Buffer.from("abcabc"), `${timeOffset}test2.2.text`, {
scope: sdk_1.AssetManagementModels.FileMetadataResource.ScopeEnum.Private,
description: result.description,
ifMatch: `${result.etag}`,
});
yield am.DeleteFile(`${result.id}`, { ifMatch: `${updatedFile.etag}` });
}));
});
function deleteFiles(am) {
return __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
yield (0, test_utils_1.sleep)(2000);
const files = (yield am.GetFiles({
filter: JSON.stringify({
and: {
name: {
endsWith: ".2.text",
},
},
}),
sort: "DESC",
page: 0,
size: 0,
}));
try {
for (var _d = true, _e = __asyncValues(files._embedded.files), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const x = _c;
yield am.DeleteFile(x.id, { ifMatch: x.etag });
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
});
}
//# sourceMappingURL=asset-management-files.spec.js.map