-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap SiteWise Asset related API calls in a Data Source #25
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5415b2c
Wrap SiteWise Asset related API calls in a Data Source
gareth-amazon 4a6d691
Add Unit Testing for RequestProcessorWorkerGroup
gareth-amazon c5094e4
Fix RequestProcessorWorker so it fund the finalizer on the last unsub…
gareth-amazon 6c4eede
Add test with proof that finalizers run on the worker process
gareth-amazon 1c45760
Results of
gareth-amazon a4c3962
Fixup
gareth-amazon 7f80cd2
Merge branch 'main' into sitewise-asset-data-source
gareth-amazon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 155 additions & 4 deletions
159
packages/core/src/asset-modules/sitewise/requestProcessor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,160 @@ | ||
import { RequestProcessor } from './requestProcessor'; | ||
import { IoTSiteWiseClient } from '@aws-sdk/client-iotsitewise'; | ||
import { SiteWiseAssetCache } from './cache'; | ||
import { SiteWiseAssetDataSource } from '../../data-module/types'; | ||
import { | ||
AssetSummary, | ||
AssetPropertyValue, | ||
DescribeAssetCommandInput, | ||
DescribeAssetCommandOutput, | ||
DescribeAssetModelResponse, | ||
DescribeAssetModelCommandInput, | ||
DescribeAssetModelCommandOutput, | ||
GetAssetPropertyValueCommandInput, | ||
GetAssetPropertyValueCommandOutput, | ||
ListAssetsCommandInput, | ||
ListAssetsCommandOutput, | ||
ListAssociatedAssetsCommandInput, | ||
ListAssociatedAssetsCommandOutput, | ||
} from '@aws-sdk/client-iotsitewise'; | ||
import { Observable } from 'rxjs'; | ||
import { sampleAssetModel, sampleAssetSummary, samplePropertyValue } from '../mocks.spec'; | ||
import { HIERARCHY_ROOT_ID, HierarchyAssetSummaryList, LoadingStateEnum } from './types'; | ||
|
||
it('initializes', () => { | ||
expect( | ||
() => new RequestProcessor(new IoTSiteWiseClient({ region: 'us-east' }), new SiteWiseAssetCache()) | ||
).not.toThrowError(); | ||
expect(() => { | ||
new RequestProcessor({} as SiteWiseAssetDataSource, new SiteWiseAssetCache()); | ||
}).not.toThrowError(); | ||
}); | ||
|
||
const createMockSiteWiseAssetDataSource = (): SiteWiseAssetDataSource => { | ||
return { | ||
describeAsset: (input: DescribeAssetCommandInput): Promise<DescribeAssetCommandOutput> => { | ||
throw 'No Calls Expected'; | ||
}, | ||
|
||
getPropertyValue: (input: GetAssetPropertyValueCommandInput): Promise<GetAssetPropertyValueCommandOutput> => { | ||
throw 'No Calls Expected'; | ||
}, | ||
|
||
describeAssetModel: (input: DescribeAssetModelCommandInput): Promise<DescribeAssetModelCommandOutput> => { | ||
throw 'No Calls Expected'; | ||
}, | ||
|
||
listAssets: (input: ListAssetsCommandInput): Promise<ListAssetsCommandOutput> => { | ||
throw 'No Calls Expected'; | ||
}, | ||
|
||
listAssociatedAssets: (input: ListAssociatedAssetsCommandInput): Promise<ListAssociatedAssetsCommandOutput> => { | ||
throw 'No Calls Expected'; | ||
}, | ||
}; | ||
}; | ||
|
||
describe('Request an AssetSummary', () => { | ||
const mockDataSource = createMockSiteWiseAssetDataSource(); | ||
let mockDescribeAsset = jest.fn(); | ||
mockDescribeAsset.mockReturnValue(Promise.resolve(sampleAssetSummary)); | ||
mockDataSource.describeAsset = mockDescribeAsset; | ||
|
||
const requestProcessor: RequestProcessor = new RequestProcessor(mockDataSource, new SiteWiseAssetCache()); | ||
const observable: Observable<AssetSummary> = new Observable<AssetSummary>((observer) => { | ||
requestProcessor.getAssetSummary({ assetId: sampleAssetSummary.id as string }, observer); | ||
}); | ||
|
||
it('waits for the AssetSummary', (done) => { | ||
observable.subscribe((result) => { | ||
expect(result).not.toBeUndefined(); | ||
expect(result).toEqual(sampleAssetSummary); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Request an Asset Model', () => { | ||
const mockDataSource = createMockSiteWiseAssetDataSource(); | ||
let mockDescribeAssetModel = jest.fn(); | ||
mockDescribeAssetModel.mockReturnValue(Promise.resolve(sampleAssetModel)); | ||
mockDataSource.describeAssetModel = mockDescribeAssetModel; | ||
|
||
const requestProcessor: RequestProcessor = new RequestProcessor(mockDataSource, new SiteWiseAssetCache()); | ||
const observable: Observable<DescribeAssetModelResponse> = new Observable<DescribeAssetModelResponse>((observer) => { | ||
requestProcessor.getAssetModel({ assetModelId: sampleAssetModel.assetModelId as string }, observer); | ||
}); | ||
|
||
it('waits for the Asset Model', (done) => { | ||
observable.subscribe((result) => expect(sampleAssetModel).toEqual(result)); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('Request an Asset Property Value', () => { | ||
const mockDataSource = createMockSiteWiseAssetDataSource(); | ||
let mockGetPropertyValue = jest.fn(); | ||
mockGetPropertyValue.mockReturnValue(Promise.resolve(samplePropertyValue)); | ||
mockDataSource.getPropertyValue = mockGetPropertyValue; | ||
|
||
const requestProcessor: RequestProcessor = new RequestProcessor(mockDataSource, new SiteWiseAssetCache()); | ||
const observable: Observable<AssetPropertyValue> = new Observable<AssetPropertyValue>((observer) => { | ||
requestProcessor.getAssetPropertyValue( | ||
{ | ||
assetId: sampleAssetSummary.id as string, | ||
propertyId: 'doesnt matter', | ||
}, | ||
observer | ||
); | ||
}); | ||
|
||
it('waits for the Asset Property Value', (done) => { | ||
observable.subscribe((result) => expect(samplePropertyValue).toEqual(result)); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('Request an Asset Hierarchy of a parent Asset', () => { | ||
const mockDataSource = createMockSiteWiseAssetDataSource(); | ||
const mockListAssociatedAssets = jest.fn(); | ||
const result: ListAssetsCommandOutput = { $metadata: {}, assetSummaries: [sampleAssetSummary] }; | ||
mockListAssociatedAssets.mockReturnValue(Promise.resolve<ListAssetsCommandOutput>(result)); | ||
mockDataSource.listAssociatedAssets = mockListAssociatedAssets; | ||
const requestProcessor: RequestProcessor = new RequestProcessor(mockDataSource, new SiteWiseAssetCache()); | ||
const observable: Observable<HierarchyAssetSummaryList> = new Observable<HierarchyAssetSummaryList>((observer) => { | ||
requestProcessor.getAssetHierarchy({ assetId: 'parentAssetId', assetHierarchyId: 'hierarchyId' }, observer); | ||
}); | ||
|
||
it('waits for the Asset Hierarchy to become loaded', (done) => { | ||
observable.subscribe((result) => { | ||
// the worker returns the completed list of assets: | ||
expect({ | ||
assetHierarchyId: 'hierarchyId', | ||
assets: [sampleAssetSummary], | ||
loadingState: LoadingStateEnum.LOADED, | ||
}).toEqual(result); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Request the root assets', () => { | ||
const mockDataSource = createMockSiteWiseAssetDataSource(); | ||
const mockListAssets = jest.fn(); | ||
const result: ListAssetsCommandOutput = { $metadata: {}, assetSummaries: [sampleAssetSummary] }; | ||
mockListAssets.mockReturnValue(Promise.resolve<ListAssetsCommandOutput>(result)); | ||
mockDataSource.listAssets = mockListAssets; | ||
const requestProcessor: RequestProcessor = new RequestProcessor(mockDataSource, new SiteWiseAssetCache()); | ||
|
||
const observable: Observable<HierarchyAssetSummaryList> = new Observable<HierarchyAssetSummaryList>((observer) => { | ||
requestProcessor.getAssetHierarchy({ assetHierarchyId: HIERARCHY_ROOT_ID }, observer); | ||
}); | ||
|
||
it('waits for the root assets to become loaded', (done) => { | ||
observable.subscribe((result) => { | ||
// the worker returns the completed list of assets: | ||
expect({ | ||
assetHierarchyId: HIERARCHY_ROOT_ID, | ||
assets: [sampleAssetSummary], | ||
loadingState: LoadingStateEnum.LOADED, | ||
}).toEqual(result); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: do we need this assertion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weirdly, yes. I spent the better part of a day debugging the tests only to find out that I was comparing undefined to undefined and getting passing tests when they should have failed.