adapt bookshop for openSAP course
This commit is contained in:
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
87
packages/bookshop/tests/bookshop.test.js
Normal file
87
packages/bookshop/tests/bookshop.test.js
Normal file
@@ -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 = '<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">'
|
||||
const expectedBooksEntitySet = '<EntitySet Name="Books" EntityType="CatalogService.Books">'
|
||||
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: "€"
|
||||
}
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
||||
47
packages/bookshop/tests/openSAP.http
Normal file
47
packages/bookshop/tests/openSAP.http
Normal file
@@ -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
|
||||
23
packages/bookshop/tests/utils.js
Normal file
23
packages/bookshop/tests/utils.js
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user