diff --git a/.vscode/launch.json b/.vscode/launch.json index bd2108f4..8b7a8f95 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "type": "node", "runtimeExecutable": "npx", "runtimeArgs": ["-n"], - "args": ["--", "cds", "run", "--with-mocks", "--in-memory?"], // the leading "--" arg ensures it works with as well as without debugging + "args": ["--", "cds", "run", "--with-mocks", "--in-memory"], // the leading "--" arg ensures it works with as well as without debugging "cwd": "${workspaceFolder}/packages/${input:service}", "console": "integratedTerminal", "serverReadyAction": { @@ -36,7 +36,7 @@ "reviews-service", "user-service" ], - "default": "bookstore" + "default": "bookshop" } ] } diff --git a/package.json b/package.json index 08b90658..14b26336 100644 --- a/package.json +++ b/package.json @@ -19,10 +19,11 @@ "@sap/cds": "latest", "express": "*" }, - "--add-these-to-devDependencies-for-tests": { + "devDependencies": { "@types/jest": "*", "sqlite3": "*", - "jest": "*" + "jest": "*", + "supertest": "^4.0.2" }, "license": "SAP SAMPLE CODE LICENSE" } diff --git a/packages/bookshop/tests/bookshop.test.js b/packages/bookshop/tests/bookshop.test.js new file mode 100644 index 00000000..b9851ee1 --- /dev/null +++ b/packages/bookshop/tests/bookshop.test.js @@ -0,0 +1,87 @@ + +const cds = require('@sap/cds/lib/cds') +const { setup, close } = require('./utils') +const request = require('supertest') + +describe('Samples: Bookshop', () => { + beforeAll(done => setup('packages/bookshop', done)) + afterAll(close) + + test('Service $metadata document', async () => { + const response = await request(cds.serve.app) + .get('/browse/$metadata') + .expect('Content-Type', /^application\/xml/) + .expect(200) + + const expectedVersion = '' + const expectedBooksEntitySet = '' + expect(response.text.includes(expectedVersion)).toBeTruthy() + expect(response.text.includes(expectedBooksEntitySet)).toBeTruthy() + }) + + + test('Get with select, expand and localized', async () => { + const response = await request(cds.serve.app) + .get('/browse/Books?$select=title,author&$expand=currency&sap-language=de') + .expect('Content-Type', /^application\/json/) + .expect(200) + + expect(response.body.value).toEqual([ + { + ID: 201, + title: "Sturmhöhe", + author: "Emily Brontë", + currency: { + name: "Pfund", + descr: "Britische Pfund", + code: "GBP", + symbol: "£" + } + }, + { + ID: 207, + title: "Jane Eyre", + author: "Charlotte Brontë", + currency: { + name: "Pfund", + descr: "Britische Pfund", + code: "GBP", + symbol: "£" + } + }, + { + ID: 251, + title: "The Raven", + author: "Edgar Allen Poe", + currency: { + name: "US-Dollar", + descr: "United States Dollar", + code: "USD", + symbol: "$" + } + }, + { + ID: 252, + title: "Eleonora", + author: "Edgar Allen Poe", + currency: { + name: "US-Dollar", + descr: "United States Dollar", + code: "USD", + symbol: "$" + } + }, + { + ID: 271, + title: "Catweazle", + author: "Richard Carpenter", + currency: { + name: "Euro", + descr: "European Euro", + code: "EUR", + symbol: "€" + } + } + ]) + }) +}) diff --git a/packages/bookshop/tests/openSAP.http b/packages/bookshop/tests/openSAP.http new file mode 100644 index 00000000..64e3bff5 --- /dev/null +++ b/packages/bookshop/tests/openSAP.http @@ -0,0 +1,47 @@ +########################################## +# Test bookstore +########################################## + +@PORT = 4004 +@HOST = localhost +####### +# List books +####### + +GET http://{{HOST}}:{{PORT}}/browse/Books HTTP/1.1 + +### List Books with their current stocks via Browse service +### title containts "discount" info due to custom handler in cat-service.js +GET http://{{HOST}}:{{PORT}}/browse/Books?$select=ID,title,stock + +### List Books with their current stocks via Admin service +### title DOES NOT containt "discount" info as there is no custom handler for admin service +# skip admin service +# GET http://{{HOST}}:{{PORT}}/admin/Books?$select=ID,title,stock + +### List all Orders +GET http://{{HOST}}:{{PORT}}/admin/Orders?&$expand=Items + +####### +# Create order +####### +# Sending this three times should result in a 409: 5 exceeds stock for book #201 +POST http://{{HOST}}:{{PORT}}/browse/Orders HTTP/1.1 +Content-Type: application/json;IEEE754Compatible=true + +{ "OrderNo":"2019-09...", "Items":[ + { "book_ID":201, "amount":3 }, + { "book_ID":207, "amount":3 } +]} + + +####### +# Delete order not allowed due to annotation @insertonly in cat-service.cds +####### +DELETE http://{{HOST}}:{{PORT}}//browse/Orders/7e2f2640-6866-4dcf-8f4d-3027aa831cad HTTP/1.1 + + +####### +# List order not allowed due to @insertonly +####### +GET http://{{HOST}}:{{PORT}}//browse/Orders HTTP/1.1 diff --git a/packages/bookshop/tests/utils.js b/packages/bookshop/tests/utils.js new file mode 100644 index 00000000..7323ae85 --- /dev/null +++ b/packages/bookshop/tests/utils.js @@ -0,0 +1,23 @@ +const cds = require('@sap/cds/lib/cds') +const run = require('@sap/cds/bin/run') + +let testServer +const cwd = process.cwd() + +const setup = (model, done) => { + cds.once('listening', ({ server }) => { + testServer = server + done() + }) + run([model], { 'in-memory?': true }) +} + +const close = done => { + testServer.close(done) + process.chdir(cwd) +} + +module.exports = { + setup, + close +} \ No newline at end of file