Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

test: add Subscriptions test infrastructure/helper #569

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions integration-tests/SubscriptionsHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*
*/
import * as AWS from 'aws-sdk';
import { DynamoDB } from 'aws-sdk';

export interface SubscriptionNotification {
httpMethod: string;
path: string;
body?: string | null;
headers?: string[];
}

// eslint-disable-next-line import/prefer-default-export
export class SubscriptionsHelper {
private readonly notificationsTableName: string;

private readonly dynamodbClient: DynamoDB;

constructor(notificationsTableName: string) {
this.notificationsTableName = notificationsTableName;
this.dynamodbClient = new AWS.DynamoDB();
}

/**
* Gets all notifications received for a given path.
* @param path - The path where the notifications were sent. It is recommended to use unique paths for each test run (e.g. by appending an uui to it)
*/
async getNotifications(path: string): Promise<SubscriptionNotification[]> {
const { Items } = await this.dynamodbClient
.query({
TableName: this.notificationsTableName,
KeyConditionExpression: '#path = :pathValue',
ExpressionAttributeNames: { '#path': 'path' },
ExpressionAttributeValues: { ':pathValue': { S: path } },
})
.promise();

if (Items === undefined) {
return [];
}

return Items.map((item) => AWS.DynamoDB.Converter.unmarshall(item) as SubscriptionNotification);
}
}
209 changes: 209 additions & 0 deletions integration-tests/infrastructure/subscriptions-endpoint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@

# Created by https://www.toptal.com/developers/gitignore/api/osx,node,linux,windows,sam
# Edit at https://www.toptal.com/developers/gitignore?templates=osx,node,linux,windows,sam

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test
.env*.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Storybook build outputs
.out
.storybook-out
storybook-static

# rollup.js default build output
dist/

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# Temporary folders
tmp/
temp/

### OSX ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### SAM ###
# Ignore build directories for the AWS Serverless Application Model (SAM)
# Info: https://aws.amazon.com/serverless/sam/
# Docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html

**/.aws-sam
samconfig.toml

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/osx,node,linux,windows,sam
16 changes: 16 additions & 0 deletions integration-tests/infrastructure/subscriptions-endpoint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# FHIR Subscriptions Test Endpoint

This small SAM app creates an API (APIGW + Lambda) that can be used as an endpoint in FHIR Subscriptions integration tests.

The Lambda function replies successfully to all requests and records the notification message in DynamoDB.
The integration tests can query DynamoDB to verify that notifications were received.

To build and deploy your application for the first time, run the following in your shell:

```bash
sam build
sam deploy --guided
```

Use the outputs of the CFN stack as values for the `SUBSCRIPTIONS_NOTIFICATIONS_TABLE` and `SUBSCRIPTIONS_ENDPOINT` environment variables when running the integration tests

50 changes: 50 additions & 0 deletions integration-tests/infrastructure/subscriptions-endpoint/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const AWS = require('aws-sdk');

/**
*
* Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
* @param {Object} event - API Gateway Lambda Proxy Input Format
*
* Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
* @param {Object} context
*
* Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
* @returns {Object} object - API Gateway Lambda Proxy Output Format
*
*/
const dynamoDb = new AWS.DynamoDB();

const { TABLE_NAME } = process.env;

if (TABLE_NAME === undefined) {
throw new Error(`Required env variable TABLE_NAME is not defined`);
}

exports.lambdaHandler = async (event) => {
const { path, httpMethod, headers, body } = event;

const item = {
path,
timestamp: Date.now(),
httpMethod,
headers,
body,
};

console.log('Received notification:', JSON.stringify(item, null, 2));

await dynamoDb
.putItem({
TableName: TABLE_NAME,
Item: AWS.DynamoDB.Converter.marshall(item),
})
.promise();

return {
statusCode: 200,
body: JSON.stringify({
message: 'ok',
}),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "fhir-subscriptions-test-endpoint",
"version": "1.0.0",
"description": "Simple endpoint used for FHIR Subscriptions integ tests",
"main": "app.js",
"license": "Apache-2.0",
"dependencies": {
},
"scripts": {
},
"devDependencies": {
}
}
Loading