From f9914dbb5225b919e3982c05b5c1121b53f6e6e1 Mon Sep 17 00:00:00 2001 From: Tobias Schweizer Date: Wed, 28 Oct 2020 17:06:01 +0100 Subject: [PATCH] DSP-817 Health Endpoint: integrate test data (#251) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * tests (health endpoint): integrate test data * tests (health endpoint): integrate test data * tests (health endpoint): integrate test data * tests (health endpoint): simplify header response handling * docs: Update README Co-authored-by: André Kilchenmann --- Makefile | 2 + README.md | 5 +- package.json | 1 + scripts/integrate-files-system.js | 21 +++++ src/api/system/health/health-endpoint.spec.ts | 93 +++++++++++++++++-- .../system/health/get-health-response.json | 6 -- .../health/maintenance-mode-response.json | 1 + .../api/system/health/response-headers.txt | 2 + .../api/system/health/running-response.json | 1 + .../api/system/health/stopped-response.json | 1 + 10 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 scripts/integrate-files-system.js delete mode 100644 test/data/api/system/health/get-health-response.json create mode 100644 test/data/api/system/health/maintenance-mode-response.json create mode 100644 test/data/api/system/health/response-headers.txt create mode 100644 test/data/api/system/health/running-response.json create mode 100644 test/data/api/system/health/stopped-response.json diff --git a/Makefile b/Makefile index 49c935caa..8b876ee39 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,7 @@ generate-test-data: ## prepare test data from Knora-API .PHONY: delete-test-data delete-test-data: ## delete static test data before integration + rm -rf test/data/api/system/health/* rm -rf test/data/api/admin/groups/* rm -rf test/data/api/admin/lists/* rm -rf test/data/api/admin/permissions/* @@ -61,6 +62,7 @@ delete-test-data: ## delete static test data before integration .PHONY: integrate-test-data integrate-test-data: ## integrates generated test data + npm run integrate-system-test-data $(CURRENT_DIR)/.tmp/typescript/test-data npm run integrate-admin-test-data $(CURRENT_DIR)/.tmp/typescript/test-data npm run integrate-v2-test-data $(CURRENT_DIR)/.tmp/typescript/test-data npm run expand-jsonld-test-data diff --git a/README.md b/README.md index f95fb5290..fe4b9bb03 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,9 @@ If you need a local version of this lib that contains the mocks, do the followin DSP-JS is compatible with a specified release of DSP-API. To update the target release of DSP-API, the following steps have to be carried out: -1. Update DSP-API version in `vars.mk`, e.g., change `v13.0.0-rc.16` to `v13.0.0-rc.17`. +1. Update DSP-API version in + - `vars.mk`, e.g., change `v13.0.0-rc.16` to `v13.0.0-rc.17`. + - `src/api/system/health/health-endpoint.spec.ts` 1. Delete local test data with `make delete-test-data` 1. Generate test data using the target DSP-API release, - Variant 1: See -> Internals -> Development -> Generating Client Test Data @@ -144,7 +146,6 @@ To update the target release of DSP-API, the following steps have to be carried - change to directory `test-framework` - add the locally build library using `npm run yalc-add` and run `npm install` - run `npm run webdriver-update` and then `npm run e2e` -1. Update DSP-API version in `.github/workflows/main.yml` in step `Get api client test data from knora-api`, e.g., change `tags/v13.0.0-rc.16` to `tags/v13.0.0-rc.17`. 1. See if the tests pass on GitHub CI ## Integration of Generated Test Data diff --git a/package.json b/package.json index 6ae12cb8a..999df1453 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ ], "scripts": { "integrate-admin-test-data": "node scripts/integrate-files-admin.js", + "integrate-system-test-data": "node scripts/integrate-files-system.js", "integrate-v2-test-data": "node scripts/integrate-files-v2.js", "expand-jsonld-test-data": "node scripts/expand-jsonld-test-data-files.js", "test": "karma start karma.conf.js", diff --git a/scripts/integrate-files-system.js b/scripts/integrate-files-system.js new file mode 100644 index 000000000..861045620 --- /dev/null +++ b/scripts/integrate-files-system.js @@ -0,0 +1,21 @@ +"use strict"; + +const fs = require('fs-extra'); + +let pathToGeneratedCode; + +// expect three args +if (process.argv.length !== 3) { + console.error("Usage: npm run integrate-system-code "); + process.exit(1); +} else { + pathToGeneratedCode = process.argv[2]; + if (!fs.existsSync(pathToGeneratedCode)) { + console.error("Specified path to generated client code is not valid"); + process.exit(1); + } +} + +fs.copySync(pathToGeneratedCode + "/system", "./test/data/api/system"); + +fs.moveSync('./test/data/api/system/health/response-headers.json', './test/data/api/system/health/response-headers.txt') diff --git a/src/api/system/health/health-endpoint.spec.ts b/src/api/system/health/health-endpoint.spec.ts index 6b8d20b86..6a802fd60 100644 --- a/src/api/system/health/health-endpoint.spec.ts +++ b/src/api/system/health/health-endpoint.spec.ts @@ -9,6 +9,13 @@ describe("HealthEndpoint", () => { const config = new KnoraApiConfig("http", "localhost", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); + const getServerFromResponseHeader = (resHeader: string) => { + // split by newline: first line server info, second line date + const headerParts = resHeader.split("\n"); + // remove "Server: " from string + return headerParts[0].replace("Server: ", ""); + } + beforeEach(() => { jasmine.Ajax.install(); }); @@ -19,7 +26,7 @@ describe("HealthEndpoint", () => { describe("Method getHealthStatus", () => { - it("should return health status", done => { + it("should return a running health status", done => { knoraApiConnection.system.healthEndpoint.getHealthStatus().subscribe( (response: ApiResponseData) => { @@ -29,7 +36,43 @@ describe("HealthEndpoint", () => { expect(response.body.severity).toEqual("non fatal"); expect(response.body.status).toEqual("healthy"); - expect(response.body.webapiVersion).toEqual("v13.0.0-rc.16"); + expect(response.body.webapiVersion).toEqual("v13.0.0-rc.21"); + expect(response.body.akkaVersion).toEqual("10.1.12"); + + done(); + }); + + const request = jasmine.Ajax.requests.mostRecent(); + + const health = require("../../../../test/data/api/system/health/running-response.json"); + + const responseHeader = require("../../../../test/data/api/system/health/response-headers.txt"); + + request.respondWith({ + status: 200, + responseText: JSON.stringify(health), + responseHeaders: { + server: getServerFromResponseHeader(responseHeader) + } + }); + + expect(request.url).toBe("http://localhost:3333/health"); + + expect(request.method).toEqual("GET"); + + }); + + it("should return a maintenance mode health status", done => { + + knoraApiConnection.system.healthEndpoint.getHealthStatus().subscribe( + (response: ApiResponseData) => { + + expect(response.body.name).toEqual("AppState"); + expect(response.body.message).toEqual("Application is in maintenance mode. Please retry later."); + expect(response.body.severity).toEqual("non fatal"); + expect(response.body.status).toEqual("unhealthy"); + + expect(response.body.webapiVersion).toEqual("v13.0.0-rc.21"); expect(response.body.akkaVersion).toEqual("10.1.12"); done(); @@ -37,13 +80,51 @@ describe("HealthEndpoint", () => { const request = jasmine.Ajax.requests.mostRecent(); - const health = require("../../../../test/data/api/system/health/get-health-response.json"); + const health = require("../../../../test/data/api/system/health/maintenance-mode-response.json"); + + const responseHeader = require("../../../../test/data/api/system/health/response-headers.txt"); + + request.respondWith({ + status: 200, + responseText: JSON.stringify(health), + responseHeaders: { + server: getServerFromResponseHeader(responseHeader) + } + }); + + expect(request.url).toBe("http://localhost:3333/health"); + + expect(request.method).toEqual("GET"); + + }); + + it("should return a stopped mode health status", done => { + + knoraApiConnection.system.healthEndpoint.getHealthStatus().subscribe( + (response: ApiResponseData) => { + + expect(response.body.name).toEqual("AppState"); + expect(response.body.message).toEqual("Stopped. Please retry later."); + expect(response.body.severity).toEqual("non fatal"); + expect(response.body.status).toEqual("unhealthy"); + + expect(response.body.webapiVersion).toEqual("v13.0.0-rc.21"); + expect(response.body.akkaVersion).toEqual("10.1.12"); + + done(); + }); + + const request = jasmine.Ajax.requests.mostRecent(); + + const health = require("../../../../test/data/api/system/health/stopped-response.json"); + + const responseHeader = require("../../../../test/data/api/system/health/response-headers.txt"); request.respondWith({ status: 200, responseText: JSON.stringify(health), responseHeaders: { - server: "webapi/v13.0.0-rc.16 akka-http/10.1.12" + server: getServerFromResponseHeader(responseHeader) } }); @@ -65,7 +146,7 @@ describe("HealthEndpoint", () => { const request = jasmine.Ajax.requests.mostRecent(); - const health = require("../../../../test/data/api/system/health/get-health-response.json"); + const health = require("../../../../test/data/api/system/health/running-response.json"); request.respondWith({ status: 200, @@ -90,7 +171,7 @@ describe("HealthEndpoint", () => { const request = jasmine.Ajax.requests.mostRecent(); - const health = require("../../../../test/data/api/system/health/get-health-response.json"); + const health = require("../../../../test/data/api/system/health/running-response.json"); request.respondWith({ status: 200, diff --git a/test/data/api/system/health/get-health-response.json b/test/data/api/system/health/get-health-response.json deleted file mode 100644 index b9702941f..000000000 --- a/test/data/api/system/health/get-health-response.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "message": "Application is healthy", - "name": "AppState", - "severity": "non fatal", - "status": "healthy" -} diff --git a/test/data/api/system/health/maintenance-mode-response.json b/test/data/api/system/health/maintenance-mode-response.json new file mode 100644 index 000000000..d4e68fd7b --- /dev/null +++ b/test/data/api/system/health/maintenance-mode-response.json @@ -0,0 +1 @@ +{"message":"Application is in maintenance mode. Please retry later.","name":"AppState","severity":"non fatal","status":"unhealthy"} diff --git a/test/data/api/system/health/response-headers.txt b/test/data/api/system/health/response-headers.txt new file mode 100644 index 000000000..60bc986ea --- /dev/null +++ b/test/data/api/system/health/response-headers.txt @@ -0,0 +1,2 @@ +Server: webapi/v13.0.0-rc.21 akka-http/10.1.12 +Date: Wed, 21 Oct 2020 23:41:38 GMT diff --git a/test/data/api/system/health/running-response.json b/test/data/api/system/health/running-response.json new file mode 100644 index 000000000..f45be9ac2 --- /dev/null +++ b/test/data/api/system/health/running-response.json @@ -0,0 +1 @@ +{"message":"Application is healthy","name":"AppState","severity":"non fatal","status":"healthy"} diff --git a/test/data/api/system/health/stopped-response.json b/test/data/api/system/health/stopped-response.json new file mode 100644 index 000000000..edb4226c4 --- /dev/null +++ b/test/data/api/system/health/stopped-response.json @@ -0,0 +1 @@ +{"message":"Stopped. Please retry later.","name":"AppState","severity":"non fatal","status":"unhealthy"}