Skip to content

Commit

Permalink
fix: grab asset composite property correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tracy-french authored and diehbria committed Dec 7, 2023
1 parent 85a7b35 commit 1c57017
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/source-iotsitewise/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
"lint": "eslint . --max-warnings=0",
"fix": "eslint --fix .",
"test": "npm run test:jest && npm run test:types",
"test:jest": "TZ=UTC jest",
"test:jest": "TZ=UTC jest --silent",
"test:types": "tsc --noEmit",
"test.watch": "TZ=UTC jest --watchAll",
"test.watch": "TZ=UTC jest --watchAll --silent",
"copy:license": "cp ../../LICENSE LICENSE",
"copy:notice": "cp ../../NOTICE NOTICE",
"prepack": "npm run copy:license && npm run copy:notice",
Expand Down Expand Up @@ -82,4 +82,4 @@
"bugs": {
"url": "https://github.com/awslabs/iot-app-kit/issues"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
DescribeAssetCommandOutput,
ListAssetModelPropertiesCommandOutput,
ListAssetPropertiesCommandOutput,
type DescribeAssetCommandOutput,
type ListAssetModelPropertiesCommandOutput,
type ListAssetPropertiesCommandOutput,
type IoTSiteWiseClient,
} from '@aws-sdk/client-iotsitewise';

import { DescribeModeledDataStreamRequest } from './describeModeledDataStreamRequest';

describe(DescribeModeledDataStreamRequest, () => {
Expand All @@ -14,8 +15,23 @@ describe(DescribeModeledDataStreamRequest, () => {
expect(request).not.toBeNull();
});

it('throws an error when DescribeAsset fails', async () => {
const client = {
send: jest.fn().mockRejectedValue(new Error()),
} as unknown as IoTSiteWiseClient;
const request = new DescribeModeledDataStreamRequest(client);

await expect(
request.send({
assetPropertyId: '',
assetId: '',
assetModelId: '',
})
).rejects.toThrowError();
});

it('returns a modeled data stream from an asset', async () => {
const fakeDescribeAssetCommandOutput: DescribeAssetCommandOutput = {
const fakeDescribeAssetCommandOutput = {
assetId: '456',
assetModelId: '789',
assetArn: '',
Expand All @@ -35,20 +51,27 @@ describe(DescribeModeledDataStreamRequest, () => {
state: 'ACTIVE',
},
$metadata: {},
};
} satisfies DescribeAssetCommandOutput;

const client = {
send: jest.fn().mockResolvedValueOnce(fakeDescribeAssetCommandOutput),
} as unknown as IoTSiteWiseClient;
const request = new DescribeModeledDataStreamRequest(client);

const response = await request.send({
assetPropertyId: '123',
assetId: '456',
assetModelId: '789',
const modeledDataStream = await request.send({
assetPropertyId: fakeDescribeAssetCommandOutput.assetProperties[0].id,
assetId: fakeDescribeAssetCommandOutput.assetId,
assetModelId: fakeDescribeAssetCommandOutput.assetModelId,
});

expect(response).not.toBeUndefined();
expect(modeledDataStream).toEqual({
assetId: fakeDescribeAssetCommandOutput.assetId,
assetName: fakeDescribeAssetCommandOutput.assetName,
dataType: fakeDescribeAssetCommandOutput.assetProperties[0].dataType,
id: fakeDescribeAssetCommandOutput.assetProperties[0].id,
name: fakeDescribeAssetCommandOutput.assetProperties[0].name,
propertyId: fakeDescribeAssetCommandOutput.assetProperties[0].id,
});
});

it('returns a cached modeled data stream from an asset', async () => {
Expand Down Expand Up @@ -188,6 +211,15 @@ describe(DescribeModeledDataStreamRequest, () => {
assetModelId: '789',
});

expect(response).toEqual({
assetId: '456',
assetName: 'asset',
name: 'composite model property',
dataType: 'DOUBLE',
id: 'xyz',
propertyId: 'xyz',
});

expect(response).not.toBeUndefined();
});

Expand Down Expand Up @@ -258,6 +290,77 @@ describe(DescribeModeledDataStreamRequest, () => {
expect(response).not.toBeUndefined();
});

it('throw error if ListAssetProperties fails', async () => {
const fakeDescribeAssetCommandOutput: DescribeAssetCommandOutput = {
assetId: '456',
assetModelId: '789',
assetArn: '',
assetCompositeModels: [],
assetProperties: [],
assetName: 'asset',
assetCreationDate: new Date(0),
assetLastUpdateDate: new Date(0),
assetHierarchies: [],
assetStatus: {
state: 'ACTIVE',
},
$metadata: {},
};

const client = {
send: jest.fn().mockResolvedValueOnce(fakeDescribeAssetCommandOutput).mockRejectedValue(new Error()),
} as unknown as IoTSiteWiseClient;
const request = new DescribeModeledDataStreamRequest(client);

await expect(
request.send({
assetPropertyId: 'asdf',
assetId: '456',
assetModelId: '789',
})
).rejects.toThrowError();
});

it('throws error if ListAssetModelProperties fails', async () => {
const fakeDescribeAssetCommandOutput: DescribeAssetCommandOutput = {
assetId: '456',
assetModelId: '789',
assetArn: '',
assetCompositeModels: [],
assetProperties: [],
assetName: 'asset',
assetCreationDate: new Date(0),
assetLastUpdateDate: new Date(0),
assetHierarchies: [],
assetStatus: {
state: 'ACTIVE',
},
$metadata: {},
};

const fakeListAssetPropertiesCommandOutput: ListAssetPropertiesCommandOutput = {
assetPropertySummaries: [],
$metadata: {},
};

const client = {
send: jest
.fn()
.mockResolvedValueOnce(fakeDescribeAssetCommandOutput)
.mockResolvedValueOnce(fakeListAssetPropertiesCommandOutput)
.mockRejectedValue(new Error()),
} as unknown as IoTSiteWiseClient;
const request = new DescribeModeledDataStreamRequest(client);

await expect(
request.send({
assetPropertyId: 'asdf',
assetId: '456',
assetModelId: '789',
})
).rejects.toThrowError();
});

it('returns cached properties from list calls', async () => {
const fakeDescribeAssetCommandOutput: DescribeAssetCommandOutput = {
assetId: '456',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ export class DescribeModeledDataStreamRequest {
return assetProperty;
}

const assetCompositeModelProperty = asset.assetCompositeModels?.find(({ properties = [] }) => {
const assetCompositeModel = asset.assetCompositeModels?.find(({ properties = [] }) => {
return properties?.find(({ id }) => {
return id === assetPropertyId;
});
});

const assetCompositeModelProperty = assetCompositeModel?.properties?.find(({ id }) => id === assetPropertyId);

if (assetCompositeModelProperty) {
return assetCompositeModelProperty;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type IoTSiteWiseClient } from '@aws-sdk/client-iotsitewise';

import { FindModeledDataStreamRequest } from './findModeledDataStreamRequest';

describe(FindModeledDataStreamRequest, () => {
it('builds', () => {
const client = { send: jest.fn() } as unknown as IoTSiteWiseClient;
const request = new FindModeledDataStreamRequest(client);

expect(request).not.toBeNull();
});
});

0 comments on commit 1c57017

Please sign in to comment.