@itwin/property-validation-client
Version:
Property Validation client for the iTwin platform
174 lines • 9.26 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { OperationUtils } from "../OperationUtils";
import { OperationsBase } from "../../base/OperationsBase";
import { EntityListIteratorImpl } from "../../base/iterators/EntityListIteratorImpl";
import { IModelsClient, NamedVersionOrderByProperty, OrderByOperator, toArray } from "@itwin/imodels-client-management";
import { PropertyValidationClient } from "../../PropertyValidationClient";
export class TestOperations extends OperationsBase {
constructor(options) {
super(options);
}
/**
* Gets Tests for a specific project. This method returns Tests in their summary representation. The returned
* iterator internally queries entities in pages. Wraps the
* {@link https://developer.bentley.com/apis/validation/operations/get-validation-propertyvalue-tests/ Get Tests}
* operation from Property Validation API.
* @param {ParamsToGetTestList} params parameters for this operation. See {@link ParamsToGetTestList}.
* @returns {EntityListIterator<TestItem>} iterator for Test list. See {@link EntityListIterator},
* {@link TestItem}.
*/
getList(params) {
const entityCollectionAccessor = (response) => {
const tests = response.tests;
return tests;
};
OperationUtils.ensureAccessTokenProvided(params.accessToken, this._options.accessTokenCallback);
return new EntityListIteratorImpl(async () => {
var _a, _b;
return this.getEntityCollectionPage({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
accessToken: (_a = params.accessToken) !== null && _a !== void 0 ? _a : await this._options.accessTokenCallback(),
url: this._options.urlFormatter.getTestListUrl({ urlParams: params.urlParams }),
entityCollectionAccessor,
userMetadata: (_b = params.userMetadata) !== null && _b !== void 0 ? _b : false,
});
});
}
/**
* Gets a single Test identified by id. This method returns a Test in its full representation.
* Wraps the {@link https://developer.bentley.com/apis/validation/operations/get-validation-propertyvalue-test/
* Get Test} operation from Property Validation API.
* @param {ParamsToGetTest} params parameters for this operation. See {@link ParamsToGetTest}.
* @returns {Promise<TestDetails>} a Test with specified id. See {@link TestDetails}.
*/
async getSingle(params) {
const { accessToken, testId, userMetadata } = params;
OperationUtils.ensureAccessTokenProvided(accessToken, this._options.accessTokenCallback);
const response = await this.sendGetRequest({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
accessToken: accessToken !== null && accessToken !== void 0 ? accessToken : await this._options.accessTokenCallback(),
url: this._options.urlFormatter.getSingleTestUrl({ testId }),
userMetadata: userMetadata !== null && userMetadata !== void 0 ? userMetadata : false,
});
return response.test;
}
/**
* Creates a Test. Wraps the {@link https://developer.bentley.com/apis/validation/operations/create-validation-propertyvalue-test/
* Create Test} operation from Property Validation API.
* @param {ParamsToCreateTest} params parameters for this operation. See {@link ParamsToCreateTest}.
* @returns {Promise<Test>} newly created Test. See {@link Test}.
*/
async create(params) {
var _a;
const body = {
projectId: params.projectId,
displayName: params.displayName,
description: params.description,
stopExecutionOnFailure: params.stopExecutionOnFailure,
rules: params.rules,
};
OperationUtils.ensureAccessTokenProvided(params.accessToken, this._options.accessTokenCallback);
const response = await this.sendPostRequest({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
accessToken: (_a = params.accessToken) !== null && _a !== void 0 ? _a : await this._options.accessTokenCallback(),
url: this._options.urlFormatter.createTestUrl(),
body,
});
return response.test;
}
/**
* Updates a Test. Wraps the {@link https://developer.bentley.com/apis/validation/operations/update-validation-propertyvalue-test/
* Update Test} operation from Property Validation API.
* @param {ParamsToUpdateTest} params parameters for this operation. See {@link ParamsToUpdateTest}.
* @returns {Promise<Test>} newly updated Test. See {@link Test}.
*/
async update(params) {
var _a;
const body = {
displayName: params.displayName,
description: params.description,
stopExecutionOnFailure: params.stopExecutionOnFailure,
rules: params.rules,
};
OperationUtils.ensureAccessTokenProvided(params.accessToken, this._options.accessTokenCallback);
const response = await this.sendPutRequest({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
accessToken: (_a = params.accessToken) !== null && _a !== void 0 ? _a : await this._options.accessTokenCallback(),
url: this._options.urlFormatter.updateTestUrl(params),
body,
});
return response.test;
}
/**
* Runs a test. Wraps the {@link https://developer.bentley.com/apis/validation/operations/run-validation-propertyvalue-test/
* Run test} operation from Property Validation API.
* @param {ParamsToRunTest} params parameters for this operation. See {@link ParamsToRunTest}.
* @returns {Promise<Run>} newly started Run. See {@link Run}.
*/
async runTest(params) {
var _a;
OperationUtils.ensureAccessTokenProvided(params.accessToken, this._options.accessTokenCallback);
// If namedVersionId is not specified, then try to get latest version as default
if (params.namedVersionId === undefined) {
const iModelsClient = new IModelsClient();
let authorization;
if (params.accessToken) {
authorization = PropertyValidationClient.toAuthorizationCallback(params.accessToken);
}
else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const accessToken = await this._options.accessTokenCallback();
authorization = PropertyValidationClient.toAuthorizationCallback(accessToken);
}
const getNamedVersionListParams = {
authorization,
iModelId: params.iModelId,
urlParams: {
$orderBy: {
property: NamedVersionOrderByProperty.ChangesetIndex,
operator: OrderByOperator.Descending,
},
},
};
const namedVersionsIterator = iModelsClient.namedVersions.getMinimalList(getNamedVersionListParams);
const namedVersions = await toArray(namedVersionsIterator);
if (namedVersions.length === 0) {
return undefined;
}
params.namedVersionId = namedVersions[0].id;
}
const body = {
testId: params.testId,
iModelId: params.iModelId,
namedVersionId: params.namedVersionId,
testSettings: params.testSettings,
};
const response = await this.sendPostRequest({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
accessToken: (_a = params.accessToken) !== null && _a !== void 0 ? _a : await this._options.accessTokenCallback(),
url: this._options.urlFormatter.runTestUrl(),
body,
});
return response.run;
}
/**
* Deletes a single Test identified by id.
* Wraps the {@link https://developer.bentley.com/apis/validation/operations/delete-validation-propertyvalue-test/
* Get Rule} operation from Property Validation API.
* @param {ParamsToDeleteTest} params parameters for this operation. See {@link ParamsToDeleteTest}.
* @returns {Promise<void>}.
*/
async delete(params) {
const { accessToken, testId } = params;
OperationUtils.ensureAccessTokenProvided(accessToken, this._options.accessTokenCallback);
await this.sendDeleteRequest({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
accessToken: accessToken !== null && accessToken !== void 0 ? accessToken : await this._options.accessTokenCallback(),
url: this._options.urlFormatter.deleteTestUrl({ testId }),
});
}
}
//# sourceMappingURL=TestOperations.js.map