From 0b910457e2ff3972331fd34d3d75b082546be35e Mon Sep 17 00:00:00 2001 From: jjspace <8007967+jjspace@users.noreply.github.com> Date: Thu, 23 Jan 2025 12:49:23 -0500 Subject: [PATCH] add tests --- packages/engine/Source/Scene/ITwinData.js | 17 +++++- packages/engine/Specs/Scene/ITwinDataSpec.js | 63 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/packages/engine/Source/Scene/ITwinData.js b/packages/engine/Source/Scene/ITwinData.js index c1af93f96ac..ba791201c7a 100644 --- a/packages/engine/Source/Scene/ITwinData.js +++ b/packages/engine/Source/Scene/ITwinData.js @@ -6,6 +6,7 @@ import RuntimeError from "../Core/RuntimeError.js"; import Check from "../Core/Check.js"; import KmlDataSource from "../DataSources/KmlDataSource.js"; import GeoJsonDataSource from "../DataSources/GeoJsonDataSource.js"; +import DeveloperError from "../Core/DeveloperError.js"; /** * Methods for loading iTwin platform data into CesiumJS @@ -205,6 +206,17 @@ ITwinData.createDataSourceForRealityDataId = async function ( return KmlDataSource.load(tilesetAccessUrl); }; +/** + * Load data from the Geospatial Features API as GeoJSON. + * + * NOTE: this function does not implement the full WFS schema yet so the limit + * should be larger than the entire dataset to retrieve everything + * + * @param {string} iTwinId The id of the iTwin to load data from + * @param {string} collectionId The id of the data collection to load + * @param {number} [limit=10000] number of items per page, must be between 1 and 10,000 inclusive + * @returns {Promise} + */ ITwinData.loadGeospatialFeatures = async function ( iTwinId, collectionId, @@ -218,11 +230,14 @@ ITwinData.loadGeospatialFeatures = async function ( Check.typeOf.number.lessThanOrEquals("limit", limit, 10000); Check.typeOf.number.greaterThanOrEquals("limit", limit, 1); } + if (!defined(ITwinPlatform.defaultAccessToken)) { + throw new DeveloperError("Must set ITwinPlatform.defaultAccessToken first"); + } //>>includeEnd('debug') const pageLimit = limit ?? 10000; - const tilesetUrl = `${ITwinPlatform.apiEndpoint}/geospatial-features/itwins/${iTwinId}/ogc/collections/${collectionId}/items`; + const tilesetUrl = `${ITwinPlatform.apiEndpoint}geospatial-features/itwins/${iTwinId}/ogc/collections/${collectionId}/items`; const resource = new Resource({ url: tilesetUrl, diff --git a/packages/engine/Specs/Scene/ITwinDataSpec.js b/packages/engine/Specs/Scene/ITwinDataSpec.js index 25cb98cd2f3..2d0c1723cfb 100644 --- a/packages/engine/Specs/Scene/ITwinDataSpec.js +++ b/packages/engine/Specs/Scene/ITwinDataSpec.js @@ -346,4 +346,67 @@ describe("ITwinData", () => { expect(geojsonSpy).not.toHaveBeenCalled(); }); }); + + describe("loadGeospatialFeatures", () => { + let geojsonSpy; + beforeEach(() => { + geojsonSpy = spyOn(GeoJsonDataSource, "load"); + }); + + it("rejects with no iTwinId", async () => { + await expectAsync( + // @ts-expect-error + ITwinData.loadGeospatialFeatures(undefined), + ).toBeRejectedWithDeveloperError( + "Expected iTwinId to be typeof string, actual typeof was undefined", + ); + }); + + it("rejects with no collectionId", async () => { + await expectAsync( + // @ts-expect-error + ITwinData.loadGeospatialFeatures("itwin-id-1", undefined), + ).toBeRejectedWithDeveloperError( + "Expected collectionId to be typeof string, actual typeof was undefined", + ); + }); + + it("rejects with limit < 1", async () => { + await expectAsync( + ITwinData.loadGeospatialFeatures("itwin-id-1", "collection-id-1", 0), + ).toBeRejectedWithDeveloperError( + "Expected limit to be greater than or equal to 1, actual value was 0", + ); + }); + + it("rejects with limit > 10000", async () => { + await expectAsync( + ITwinData.loadGeospatialFeatures( + "itwin-id-1", + "collection-id-1", + 20000, + ), + ).toBeRejectedWithDeveloperError( + "Expected limit to be less than or equal to 10000, actual value was 20000", + ); + }); + + it("rejects with no default access token set", async () => { + ITwinPlatform.defaultAccessToken = undefined; + await expectAsync( + ITwinData.loadGeospatialFeatures("itwin-id-1", "collection-id-1"), + ).toBeRejectedWithDeveloperError( + "Must set ITwinPlatform.defaultAccessToken first", + ); + }); + + it("creates a GeoJsonDataSource from the constructed blob url if the type is GeoJSON", async () => { + await ITwinData.loadGeospatialFeatures("itwin-id-1", "collection-id-1"); + + expect(geojsonSpy).toHaveBeenCalledTimes(1); + expect(geojsonSpy.calls.mostRecent().args[0].url).toEqual( + "https://api.bentley.com/geospatial-features/itwins/itwin-id-1/ogc/collections/collection-id-1/items?limit=10000&client=CesiumJS", + ); + }); + }); });