diff --git a/.gitignore b/.gitignore index 9d0800b..61de367 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ test/TestCases/Case02/output/** test/TestCases/Case03/output/** test/TestCases/Case04/output/** test/TestCases/Case05/output/** -test/TestCases/Case06/output/** \ No newline at end of file +test/TestCases/Case06/output/** +*.iml diff --git a/.jest.js b/.jest.js new file mode 100644 index 0000000..283dceb --- /dev/null +++ b/.jest.js @@ -0,0 +1,12 @@ +export default { + 'transform': {}, + 'verbose': true, + 'testSequencer': './test/jestTestSequencer.js', + 'globals': { + // neptune db that has pre-loaded air routes sample data host and port + // ex. db-neptune-foo-bar.cluster-abc.us-west-2.neptune.amazonaws.com + 'AIR_ROUTES_DB_HOST': process.env.AIR_ROUTES_DB_HOST, + // ex. 8182 + 'AIR_ROUTES_DB_PORT': process.env.AIR_ROUTES_DB_PORT + } +}; diff --git a/.jest.json b/.jest.json deleted file mode 100644 index 757e7da..0000000 --- a/.jest.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "transform": {}, - "verbose": true, - "testSequencer": "./test/jestTestSequencer.js" -} diff --git a/package.json b/package.json index 0522d5d..e22a98c 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ "scripts": { "postinstall": "cd templates/Lambda4AppSyncHTTP && npm install && cd ../Lambda4AppSyncSDK && npm install", "lint": "eslint neptune-for-graphql.mjs ./src", - "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --runInBand --detectOpenHandles --config .jest.json", - "test:resolver": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --runInBand --detectOpenHandles --config .jest.json --testPathPattern=test/TestCases/Case01" + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --runInBand --detectOpenHandles --config .jest.js", + "test:resolver": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --runInBand --detectOpenHandles --config .jest.js --testPathPattern=test/TestCases/Case01" }, "jest": { "collectCoverage": true, diff --git a/src/main.js b/src/main.js index 9bee38f..346d5a3 100644 --- a/src/main.js +++ b/src/main.js @@ -404,7 +404,7 @@ async function main() { // Outputs // **************************************************************************** - mkdirSync('./output', { recursive: true }); + mkdirSync(outputFolderPath, { recursive: true }); // Output GraphQL schema no directives if (inputGraphQLSchema != '') { diff --git a/test/TestCases/Case01/Case01.04.test.js b/test/TestCases/Case01/Case01.04.test.js index a8cdd05..ae10316 100644 --- a/test/TestCases/Case01/Case01.04.test.js +++ b/test/TestCases/Case01/Case01.04.test.js @@ -4,7 +4,7 @@ import { readJSONFile, testResolverQueriesResults } from '../../testLib'; const casetest = readJSONFile('./test/TestCases/Case01/case.json'); -await testResolverQueriesResults( './TestCases/Case01/output/output.resolver.graphql.cjs', +await testResolverQueriesResults( './TestCases/Case01/output/output.resolver.graphql.js', './test/TestCases/Case01/queries', casetest.host, casetest.port); \ No newline at end of file diff --git a/test/TestCases/Case01/case.json b/test/TestCases/Case01/case.json index 98adc2d..a592867 100644 --- a/test/TestCases/Case01/case.json +++ b/test/TestCases/Case01/case.json @@ -2,13 +2,13 @@ "name": "Unit Test (Air Routes)", "description":"Start from graphql schema with directives, using Air Routes unit test schema", "argv":["--quiet", - "--input-schema-file", "./test/TestCases/Case01/input/airports.source.schema.graphql", + "--input-schema-file", "./test/TestCases/airports.source.schema.graphql", "--input-schema-changes-file", "./test/TestCases/Case01/input/changesAirport.json", "--output-js-resolver-file", "./test/TestCases/Case01/output/output.resolver.graphql.cjs", "--output-folder-path", "./test/TestCases/Case01/output", "--output-no-lambda-zip"], - "host": "", - "port": "" + "host": "", + "port": "", "testOutputFilesSize": ["output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] } \ No newline at end of file diff --git a/test/TestCases/Case02/case.json b/test/TestCases/Case02/case.json index a7fb103..7c06259 100644 --- a/test/TestCases/Case02/case.json +++ b/test/TestCases/Case02/case.json @@ -6,8 +6,8 @@ "--output-js-resolver-file", "./test/TestCases/Case02/output/output.resolver.graphql.cjs", "--output-folder-path", "./test/TestCases/Case02/output", "--output-no-lambda-zip"], - "host": "", - "port": "" + "host": "", + "port": "", "testOutputFilesSize": ["output.lambda.zip", "output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] } \ No newline at end of file diff --git a/test/TestCases/Case03/case.json b/test/TestCases/Case03/case.json index e73b848..c4983a8 100644 --- a/test/TestCases/Case03/case.json +++ b/test/TestCases/Case03/case.json @@ -5,8 +5,8 @@ "--input-schema-file", "./test/TestCases/Case03/input/todo.schema.graphql", "--output-folder-path", "./test/TestCases/Case03/output", "--output-no-lambda-zip"], - "host": "", - "port": "", + "host": "", + "port": "", "testOutputFilesSize": ["output.lambda.zip", "output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] } \ No newline at end of file diff --git a/test/TestCases/Case04/case.json b/test/TestCases/Case04/case.json index 6212e48..672fa04 100644 --- a/test/TestCases/Case04/case.json +++ b/test/TestCases/Case04/case.json @@ -2,11 +2,11 @@ "name": "Get Database Schema", "description":"", "argv":["--quiet", - "--input-graphdb-schema-neptune-endpoint", "", + "--input-graphdb-schema-neptune-endpoint", ":", "--output-folder-path", "./test/TestCases/Case04/output", "--output-no-lambda-zip"], - "host": "", - "port": "", + "host": "", + "port": "", "testOutputFilesSize": ["output.lambda.zip", "output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] } \ No newline at end of file diff --git a/test/TestCases/Case05/case01.json b/test/TestCases/Case05/case01.json index d9fddf2..1c89591 100644 --- a/test/TestCases/Case05/case01.json +++ b/test/TestCases/Case05/case01.json @@ -1,11 +1,15 @@ { - "name": "Unit Test (Air Routes) Remove Pipeline", - "description":"Remove pipeline", - "argv":["--quiet", - "--remove-aws-pipeline-name", "AirportsJestTest", - "--output-folder-path", "./test/TestCases/Case05/output"], - "host": "", - "port": "", + "name": "Unit Test (Air Routes) Pipeline", + "description":"Create pipeline", + "argv":["--quiet", + "--input-schema-file", "./test/TestCases/airports.source.schema.graphql", + "--output-folder-path", "./test/TestCases/Case05/output", + "--create-update-aws-pipeline", + "--create-update-aws-pipeline-name", "AirportsJestTest", + "--create-update-aws-pipeline-neptune-endpoint", ":", + "--output-resolver-query-https"], + "host": "", + "port": "", "testOutputFilesSize": ["output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] } \ No newline at end of file diff --git a/test/TestCases/Case05/case02.json b/test/TestCases/Case05/case02.json index 4f0f2cf..006bc71 100644 --- a/test/TestCases/Case05/case02.json +++ b/test/TestCases/Case05/case02.json @@ -1,15 +1,11 @@ { - "name": "Unit Test (Air Routes) Pipeline", - "description":"Create pipeline", - "argv":["--quiet", - "--input-schema-file", "./test/TestCases/Case05/input/airports.source.schema.graphql", - "--output-folder-path", "./test/TestCases/Case05/output", - "--create-update-aws-pipeline", - "--create-update-aws-pipeline-name", "AirportsJestTest", - "--create-update-aws-pipeline-neptune-endpoint", "", - "--output-resolver-query-https"], - "host": "", - "port": "", - "testOutputFilesSize": ["output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], - "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] + "name": "Unit Test (Air Routes) Remove Pipeline", + "description":"Remove pipeline", + "argv":["--quiet", + "--remove-aws-pipeline-name", "AirportsJestTest", + "--output-folder-path", "./test/TestCases/Case05/output"], + "host": "", + "port": "", + "testOutputFilesSize": ["output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], + "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] } \ No newline at end of file diff --git a/test/TestCases/Case06/case.json b/test/TestCases/Case06/case.json index 6e12357..b6e9287 100644 --- a/test/TestCases/Case06/case.json +++ b/test/TestCases/Case06/case.json @@ -2,16 +2,16 @@ "name": "Unit Test (Air Routes) CDK Pipeline", "description":"Create CDK pipeline", "argv":["--quiet", - "--input-schema-file", "./test/TestCases/Case06/input/airports.source.schema.graphql", + "--input-schema-file", "./test/TestCases/airports.source.schema.graphql", "--output-folder-path", "./test/TestCases/Case06/output", "--output-aws-pipeline-cdk", "--output-aws-pipeline-cdk-name", "AirportCDKTestJest", "--output-aws-pipeline-cdk-neptume-database-name", "airport00", "--output-aws-pipeline-cdk-region", "us-east-1", - "--output-aws-pipeline-cdk-neptume-endpoint", "", + "--output-aws-pipeline-cdk-neptume-endpoint", ":", "--output-resolver-query-https"], - "host": "", - "port": "", + "host": "", + "port": "", "testOutputFilesSize": ["output.resolver.graphql.js", "output.schema.graphql", "output.source.schema.graphql"], "testOutputFilesContent": ["output.schema.graphql", "output.source.schema.graphql"] } \ No newline at end of file diff --git a/test/TestCases/Case01/input/airports.source.schema.graphql b/test/TestCases/airports.source.schema.graphql similarity index 100% rename from test/TestCases/Case01/input/airports.source.schema.graphql rename to test/TestCases/airports.source.schema.graphql diff --git a/test/testLib.js b/test/testLib.js index 0d26e10..bb6a47d 100644 --- a/test/testLib.js +++ b/test/testLib.js @@ -2,7 +2,8 @@ import axios from 'axios'; import fs from 'fs'; - +const HOST_PLACEHOLDER = ''; +const PORT_PLACEHOLDER = ''; async function queryNeptune(q, language, host, port, param) { try { @@ -18,9 +19,32 @@ async function queryNeptune(q, language, host, port, param) { } } +/** + * Searches the given text for a placeholder text and if found replaces it with an environment variable value of the same name. + * It is expected that the placeholder text is surrounded by angle brackets and the environment variable name is the placeholder text without the angle brackets. + * + * @param text the text to search and replace the placeholder + * @param placeholder the placeholder text to search for + * @returns text with replaced placeholder if found + * @throws error if the placeholder text is found but the environment variable is not set + */ +function replacePlaceholderWithEnvironmentVariable(text, placeholder) { + if (text.includes(placeholder)) { + // remove angle brackets + let envVarName = placeholder.replace(/[<>]/g, ''); + if (process.env[envVarName]) { + return text.replace(placeholder, process.env[envVarName]); + } + throw new Error('Expected environment variable ' + envVarName + ' is not set'); + } + return text; +} function readJSONFile(fileName) { - return JSON.parse(fs.readFileSync(fileName, 'utf8')); + let text = fs.readFileSync(fileName, 'utf8'); + text = replacePlaceholderWithEnvironmentVariable(text, HOST_PLACEHOLDER); + text = replacePlaceholderWithEnvironmentVariable(text, PORT_PLACEHOLDER); + return JSON.parse(text); }