Compare commits
8 Commits
CAA160-ex2
...
CAA160-fin
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bb08b2e59 | ||
|
|
a0ca06fe45 | ||
|
|
1c18ea5105 | ||
|
|
1c79c2e4f2 | ||
|
|
c124137ab7 | ||
|
|
4838de7f71 | ||
|
|
d631b5106e | ||
|
|
b603a4bf48 |
24
bookstore/.gitignore
vendored
Normal file
24
bookstore/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
gen/
|
||||
edmx/
|
||||
schema.sql
|
||||
*.db
|
||||
|
||||
bin/
|
||||
target/
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
|
||||
node/
|
||||
node_modules/
|
||||
|
||||
.mta/
|
||||
*.mtar
|
||||
|
||||
*.log*
|
||||
gc_history*
|
||||
hs_err*
|
||||
*.tgz
|
||||
|
||||
default-env.json
|
||||
connection.properties
|
||||
3
bookstore/.npmrc
Normal file
3
bookstore/.npmrc
Normal file
@@ -0,0 +1,3 @@
|
||||
package-lock=false
|
||||
registry = https://registry.npmjs.org
|
||||
@sap:registry = https://npm.sap.com
|
||||
5
bookstore/db/data/sap.capire.bookstore-Authors.csv
Normal file
5
bookstore/db/data/sap.capire.bookstore-Authors.csv
Normal file
@@ -0,0 +1,5 @@
|
||||
ID;firstname;lastname
|
||||
6061f01b-7fa6-43c3-a6d7-12adb9d25198;Emily;Brontë
|
||||
2db38772-235e-432c-92a0-ce09ca340728;Charlotte;Brontë
|
||||
286ae4b1-b845-4af8-9827-4eb04fe5f380;Edgar Allen;Poe
|
||||
cbfec09e-0ff4-4cfc-adf1-0a37500da750;Richard;Carpenter
|
||||
|
6
bookstore/db/data/sap.capire.bookstore-Books.csv
Normal file
6
bookstore/db/data/sap.capire.bookstore-Books.csv
Normal file
@@ -0,0 +1,6 @@
|
||||
ID;title;descr;author_ID;stock;price;currency_code;category_ID;createdAt
|
||||
abed2f7a-c50e-4bc5-89fd-9a00a54b4b16;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";6061f01b-7fa6-43c3-a6d7-12adb9d25198;12;11.11;GBP;9;2019-09-20 10:10:10.000
|
||||
6869961b-c533-42b7-9d60-30fae4f46ab6;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";2db38772-235e-432c-92a0-ce09ca340728;11;12.34;GBP;10;2019-09-20 10:10:10.000
|
||||
fd0c5fda-8811-4e20-bcff-3a776abc290a;The Raven;"“The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";286ae4b1-b845-4af8-9827-4eb04fe5f380;333;13.13;USD;1;2019-09-20 10:10:10.000
|
||||
b7bca6dd-0497-465e-9a5a-56f244174c8c;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";286ae4b1-b845-4af8-9827-4eb04fe5f380;555;14.14;USD;5;2019-09-20 10:10:10.000
|
||||
5e69a718-d86b-4461-8953-0edadcd87960;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;cbfec09e-0ff4-4cfc-adf1-0a37500da750;22;15.15;EUR;3;2019-09-20 10:10:10.000
|
||||
|
7
bookstore/db/data/sap.capire.bookstore-Books_texts.csv
Normal file
7
bookstore/db/data/sap.capire.bookstore-Books_texts.csv
Normal file
@@ -0,0 +1,7 @@
|
||||
ID;locale;title;descr
|
||||
abed2f7a-c50e-4bc5-89fd-9a00a54b4b16;de;Sturmhöhe;Sturmhöhe (Originaltitel: Wuthering Heights) ist der einzige Roman der englischen Schriftstellerin Emily Brontë (1818–1848). Der 1847 unter dem Pseudonym Ellis Bell veröffentlichte Roman wurde vom viktorianischen Publikum weitgehend abgelehnt, heute gilt er als ein Klassiker der britischen Romanliteratur des 19. Jahrhunderts.
|
||||
abed2f7a-c50e-4bc5-89fd-9a00a54b4b16;es;Cumbres Borrascosas;Cumbres Borrascosas (Título original: Wuthering Heights) es la única novela de Emily Brontë (1818–1848). Fue publicada por primera vez en 1847 bajo el seudónimo de Ellis Bell. Aunque ahora se considera un clásico de la literatura inglesa, el recibimiento inicial de Cumbres Borrascosas fue tibio en el mejor de los casos.
|
||||
6869961b-c533-42b7-9d60-30fae4f46ab6;de;Jane Eyre;Jane Eyre. Eine Autobiographie (Originaltitel: Jane Eyre. An Autobiography), erstmals erschienen im Jahr 1847 unter dem Pseudonym Currer Bell, ist der erste veröffentlichte Roman der britischen Autorin Charlotte Brontë und ein Klassiker der viktorianischen Romanliteratur des 19. Jahrhunderts. Der Roman erzählt in Form einer Ich-Erzählung die Lebensgeschichte von Jane Eyre (ausgesprochen /ˌdʒeɪn ˈɛə/), die nach einer schweren Kindheit eine Stelle als Gouvernante annimmt und sich in ihren Arbeitgeber verliebt, jedoch immer wieder um ihre Freiheit und Selbstbestimmung kämpfen muss. Als klein, dünn, blass, stets schlicht dunkel gekleidet und mit strengem Mittelscheitel beschrieben, gilt die Heldin des Romans Jane Eyre nicht zuletzt aufgrund der Kino- und Fernsehversionen der melodramatischen Romanvorlage als die bekannteste englische Gouvernante der Literaturgeschichte
|
||||
6869961b-c533-42b7-9d60-30fae4f46ab6;es;Jane Eyre;Jane Eyre. Una autobiografía (Título original: Jane Eyre. An Autobiography), se publicó por primera vez en 1847 bajo el pseudónimo de Currer Bell. Es la primera novela publicada por la escritora británica Charlotte Brontë y es considerada un clásico de la literatura inglesa. La novela es narrada por Jane Eyre, contada en primera persona, y cuenta su histora de vida. Luego de una infancia dificil, a los 18 años comienza a trabajar como institutriz en Thornfield Hall y se enamora de su empleador. Durante su vida, Jane lucha por su libertad e independencia. Pequeña, delgada, pálida y vestida siempre de negro, la heroína Jane Eyre se transformó, gracias al cine y televisión, en uno de los personajes mas famosos de la literatura.
|
||||
b7bca6dd-0497-465e-9a5a-56f244174c8c;de;Eleonora;“Eleonora” ist eine Erzählung von Edgar Allan Poe. Sie wurde 1842 erstveröffentlicht.
|
||||
b7bca6dd-0497-465e-9a5a-56f244174c8c;es;Eleonora;“Eleonora” es un cuento de Edgar Allan Poe. Se publicó por primera vez en 1842. Es considerado por algunos como un cuento autobiográfico y tiene un final relativamente feliz.
|
||||
|
11
bookstore/db/data/sap.capire.products-Categories.csv
Normal file
11
bookstore/db/data/sap.capire.products-Categories.csv
Normal file
@@ -0,0 +1,11 @@
|
||||
ID;parent_ID;name
|
||||
1;;Poetry
|
||||
2;;Biography
|
||||
3;;Fantasy
|
||||
4;;Science Fiction
|
||||
5;;Romance
|
||||
6;;Mystery
|
||||
7;;Thriller
|
||||
8;;Dystopia
|
||||
9;;Tragedy
|
||||
10;;Novel
|
||||
|
30
bookstore/db/schema.cds
Normal file
30
bookstore/db/schema.cds
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace sap.capire.bookstore;
|
||||
|
||||
using { Currency, cuid, managed } from '@sap/cds/common';
|
||||
using { sap.capire.products.Products } from '@sap/capire-products';
|
||||
|
||||
entity Books as projection on Products; extend Products with {
|
||||
// Note: we map Books to Products to allow reusing AdminService as is
|
||||
author : Association to Authors;
|
||||
}
|
||||
|
||||
entity Authors : cuid {
|
||||
firstname : String(111);
|
||||
lastname : String(111);
|
||||
books : Association to many Books on books.author = $self;
|
||||
}
|
||||
|
||||
@Capabilities.Updatable: false
|
||||
entity Orders : cuid, managed {
|
||||
items : Composition of many OrderItems on items.parent = $self;
|
||||
total : Decimal(9,2) @readonly;
|
||||
currency : Currency;
|
||||
}
|
||||
|
||||
@Capabilities.Updatable: false
|
||||
entity OrderItems : cuid {
|
||||
parent : Association to Orders not null;
|
||||
book_ID : UUID;
|
||||
amount : Integer;
|
||||
netAmount : Decimal(9,2) @readonly;
|
||||
}
|
||||
7
bookstore/manifest.yml
Normal file
7
bookstore/manifest.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
applications:
|
||||
- name: bookstore
|
||||
path: srv/target/bookstore-1.0-SNAPSHOT.jar
|
||||
random-route: true
|
||||
services:
|
||||
- bookstore-hana
|
||||
44
bookstore/package.json
Normal file
44
bookstore/package.json
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "bookstore",
|
||||
"version": "1.0.0",
|
||||
"description": "Generated by cds-services-archetype",
|
||||
"license": "ISC",
|
||||
"repository": "<Add your repository here>",
|
||||
"scripts": {
|
||||
"build": "cds build/all --clean",
|
||||
"schema": "cds compile srv --to sql > srv/src/main/resources/schema.sql",
|
||||
"deploy": "cds deploy"
|
||||
},
|
||||
"dependencies": {
|
||||
"@sap/cds": "3.21.2",
|
||||
"@sap/capire-products": "file:sap-capire-products-1.0.0.tgz",
|
||||
"hdb": "^0.17.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@sap/hdi-deploy": "3.7.0",
|
||||
"sqlite3": "^4.1.1"
|
||||
},
|
||||
"cds": {
|
||||
"build": {
|
||||
"target": "."
|
||||
},
|
||||
"requires": {
|
||||
"db": {
|
||||
"kind": "sqlite",
|
||||
"model": [
|
||||
"db",
|
||||
"srv"
|
||||
],
|
||||
"credentials": {
|
||||
"database": "sqlite.db"
|
||||
},
|
||||
"[production]": {
|
||||
"kind": "hana"
|
||||
}
|
||||
}
|
||||
},
|
||||
"odata": {
|
||||
"version": "v4"
|
||||
}
|
||||
}
|
||||
}
|
||||
136
bookstore/pom.xml
Normal file
136
bookstore/pom.xml
Normal file
@@ -0,0 +1,136 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.sap.teched.cap</groupId>
|
||||
<artifactId>bookstore-parent</artifactId>
|
||||
<version>${revision}</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>bookstore parent</name>
|
||||
|
||||
<properties>
|
||||
<!-- OUR VERSION -->
|
||||
<revision>1.0-SNAPSHOT</revision>
|
||||
|
||||
<!-- DEPENDENCIES VERSION -->
|
||||
<jdk.version>1.8</jdk.version>
|
||||
<cds.services.version>1.2.0</cds.services.version>
|
||||
<cds4j.version>1.6.0</cds4j.version>
|
||||
<spring.boot.version>2.2.3.RELEASE</spring.boot.version>
|
||||
|
||||
<node.version>v10.4.1</node.version>
|
||||
<node.url>https://nodejs.org/dist/</node.url>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>srv</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!-- CDS SERVICES -->
|
||||
<dependency>
|
||||
<groupId>com.sap.cds</groupId>
|
||||
<artifactId>cds-services-bom</artifactId>
|
||||
<version>${cds.services.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- SPRING BOOT -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!-- STEPS TO GENERATE CDS ARTIFACTS -->
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
<version>1.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install-node-and-npm</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>install-node-and-npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<downloadRoot>${node.url}</downloadRoot>
|
||||
<nodeVersion>${node.version}</nodeVersion>
|
||||
<npmInheritsProxyConfigFromMaven>true</npmInheritsProxyConfigFromMaven>
|
||||
</configuration>
|
||||
</execution>
|
||||
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<arguments>install</arguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
|
||||
<execution>
|
||||
<id>cds build</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<arguments>run build</arguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
|
||||
<execution>
|
||||
<id>cds schema</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<arguments>run schema</arguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
|
||||
<plugins>
|
||||
<!-- JAVA VERSION -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>${jdk.version}</source>
|
||||
<target>${jdk.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- MAKE SPRING BOOT PLUGIN RUNNABLE FROM ROOT -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
<fork>false</fork>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
139
bookstore/srv/pom.xml
Normal file
139
bookstore/srv/pom.xml
Normal file
@@ -0,0 +1,139 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>bookstore-parent</artifactId>
|
||||
<groupId>com.sap.teched.cap</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>bookstore</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>bookstore</name>
|
||||
|
||||
<!-- ACTUAL DEPENDENCIES -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.sap.cds</groupId>
|
||||
<artifactId>cds-starter-spring-boot-odata</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sap.cds</groupId>
|
||||
<artifactId>cds-feature-hana</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sap.cds</groupId>
|
||||
<artifactId>cds-feature-cloudfoundry</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- SPRING BOOT PLUGIN -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<configuration>
|
||||
<skip>false</skip>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- STEPS TO GENERATE CDS ARTIFACTS IMPORTED FROM PARENT -->
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<!-- CONFIGURE ADDITIONAL SOURCE DIRECTORY FOR GENERATED CLASSES -->
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>src/gen/java</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- GENERATE POJO INTERFACES -->
|
||||
<plugin>
|
||||
<groupId>com.sap.cds</groupId>
|
||||
<artifactId>cds4j-maven-plugin</artifactId>
|
||||
<version>${cds4j.version}</version>
|
||||
<configuration>
|
||||
<outputDirectory>${project.basedir}/src/gen</outputDirectory>
|
||||
<basePackage>cds.gen</basePackage>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>cds4j-generate-model</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<csnFile>${project.basedir}/src/main/resources/edmx/csn.json</csnFile>
|
||||
<excludes>
|
||||
<exclude>localized.*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- CLEAN GENERATED ARTIFACTS -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<filesets>
|
||||
<fileset>
|
||||
<directory>src/gen</directory>
|
||||
<followSymlinks>false</followSymlinks>
|
||||
</fileset>
|
||||
<fileset>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>edmx/**</include>
|
||||
<include>schema.sql</include>
|
||||
</includes>
|
||||
<followSymlinks>false</followSymlinks>
|
||||
</fileset>
|
||||
</filesets>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
19
bookstore/srv/services.cds
Normal file
19
bookstore/srv/services.cds
Normal file
@@ -0,0 +1,19 @@
|
||||
using { sap.capire.bookstore as db } from '../db/schema';
|
||||
|
||||
// Define Books Service
|
||||
service BooksService {
|
||||
@readonly entity Books as projection on db.Books { *, category as genre } excluding { category, createdBy, createdAt, modifiedBy, modifiedAt };
|
||||
@readonly entity Authors as projection on db.Authors;
|
||||
}
|
||||
|
||||
// Define Orders Service
|
||||
service OrdersService {
|
||||
entity Orders as projection on db.Orders;
|
||||
// OrderItems are auto exposed
|
||||
}
|
||||
|
||||
// Reuse Admin Service
|
||||
using { AdminService } from '@sap/capire-products';
|
||||
extend service AdminService with {
|
||||
entity Authors as projection on db.Authors;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sap.teched.cap.bookstore;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.sap.teched.cap.bookstore.handlers;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.sap.cds.ql.Select;
|
||||
import com.sap.cds.ql.Update;
|
||||
import com.sap.cds.ql.cqn.CqnSelect;
|
||||
import com.sap.cds.ql.cqn.CqnUpdate;
|
||||
import com.sap.cds.services.ErrorStatuses;
|
||||
import com.sap.cds.services.ServiceException;
|
||||
import com.sap.cds.services.cds.CdsService;
|
||||
import com.sap.cds.services.handler.EventHandler;
|
||||
import com.sap.cds.services.handler.annotations.After;
|
||||
import com.sap.cds.services.handler.annotations.Before;
|
||||
import com.sap.cds.services.handler.annotations.ServiceName;
|
||||
import com.sap.cds.services.persistence.PersistenceService;
|
||||
|
||||
import cds.gen.ordersservice.OrderItems;
|
||||
import cds.gen.ordersservice.Orders;
|
||||
import cds.gen.sap.capire.bookstore.Books;
|
||||
import cds.gen.sap.capire.bookstore.Books_;
|
||||
import cds.gen.sap.capire.bookstore.OrderItems_;
|
||||
|
||||
@Component
|
||||
@ServiceName("OrdersService")
|
||||
public class OrdersService implements EventHandler {
|
||||
|
||||
@Autowired
|
||||
PersistenceService db;
|
||||
|
||||
@Before(event = CdsService.EVENT_CREATE, entity = "OrdersService.OrderItems")
|
||||
public void validateBookAndDecreaseStock(List<OrderItems> items) {
|
||||
for (OrderItems item : items) {
|
||||
String bookId = item.getBookId();
|
||||
Integer amount = item.getAmount();
|
||||
|
||||
// check if the book that should be ordered is existing
|
||||
CqnSelect sel = Select.from(Books_.class).columns(b -> b.stock()).where(b -> b.ID().eq(bookId));
|
||||
Books book = db.run(sel).first(Books.class).orElseThrow(() -> new ServiceException(ErrorStatuses.NOT_FOUND, "Book does not exist"));
|
||||
|
||||
// check if order could be fulfilled
|
||||
int stock = book.getStock();
|
||||
if (stock < amount) {
|
||||
throw new ServiceException(ErrorStatuses.BAD_REQUEST, "Not enough books on stock");
|
||||
}
|
||||
|
||||
// update the book with the new stock, means minus the order amount
|
||||
book.setStock(stock - amount);
|
||||
CqnUpdate update = Update.entity(Books_.class).data(book).where(b -> b.ID().eq(bookId));
|
||||
db.run(update);
|
||||
}
|
||||
}
|
||||
|
||||
@Before(event = CdsService.EVENT_CREATE, entity = "OrdersService.Orders")
|
||||
public void validateBookAndDecreaseStockViaOrders(List<Orders> orders) {
|
||||
for(Orders order : orders) {
|
||||
if(order.getItems() != null) {
|
||||
validateBookAndDecreaseStock(order.getItems());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@After(event = { CdsService.EVENT_READ, CdsService.EVENT_CREATE }, entity = "OrdersService.OrderItems")
|
||||
public void calculateNetAmount(List<OrderItems> items) {
|
||||
for (OrderItems item : items) {
|
||||
String bookId = item.getBookId();
|
||||
|
||||
// get the book that was ordered
|
||||
CqnSelect sel = Select.from(Books_.class).where(b -> b.ID().eq(bookId));
|
||||
Books book = db.run(sel).single(Books.class);
|
||||
|
||||
// calculate and set net amount
|
||||
item.setNetAmount(book.getPrice().multiply(new BigDecimal(item.getAmount())));
|
||||
}
|
||||
}
|
||||
|
||||
@After(event = { CdsService.EVENT_READ, CdsService.EVENT_CREATE }, entity = "OrdersService.Orders")
|
||||
public void calculateTotal(List<Orders> orders) {
|
||||
for (Orders order : orders) {
|
||||
// calculate net amount for expanded items
|
||||
if(order.getItems() != null) {
|
||||
calculateNetAmount(order.getItems());
|
||||
}
|
||||
|
||||
// get all items of the order
|
||||
CqnSelect selItems = Select.from(OrderItems_.class).where(i -> i.parent().ID().eq(order.getId()));
|
||||
List<OrderItems> allItems = db.run(selItems).listOf(OrderItems.class);
|
||||
|
||||
// calculate net amount of all items
|
||||
calculateNetAmount(allItems);
|
||||
|
||||
// calculate and set the orders total
|
||||
BigDecimal total = new BigDecimal(0);
|
||||
for(OrderItems item : allItems) {
|
||||
total = total.add(item.getNetAmount());
|
||||
}
|
||||
order.setTotal(total);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
7
bookstore/srv/src/main/resources/application.yaml
Normal file
7
bookstore/srv/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
spring:
|
||||
profiles: default
|
||||
datasource:
|
||||
url: "jdbc:sqlite:sqlite.db"
|
||||
driver-class-name: org.sqlite.JDBC
|
||||
initialization-mode: never
|
||||
@@ -0,0 +1,165 @@
|
||||
package com.sap.teched.cap.bookstore.handlers;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
|
||||
import cds.gen.sap.capire.bookstore.OrderItems;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class orderServiceTest {
|
||||
|
||||
private static final String ODATA_V4 = "/odata/v4";
|
||||
private static final String BOOKS_URI = ODATA_V4 + "/BooksService/Books";
|
||||
private static final String ORDERS_URI = ODATA_V4 + "/OrdersService/Orders";
|
||||
private static final String ORDER_ITEMS_URI = ODATA_V4 + "/OrdersService/OrderItems";
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void testGetBooks() throws Exception {
|
||||
|
||||
mockMvc.perform(get(BOOKS_URI).content("")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.value[0].title", is("Wuthering Heights")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateOrderItem() throws Exception {
|
||||
|
||||
final String data = "{\"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\",\"book_ID\": \"abed2f7a-c50e-4bc5-89fd-9a00a54b4b16\", \"amount\": 1 }";
|
||||
|
||||
mockMvc.perform(post(ORDER_ITEMS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(get(ORDER_ITEMS_URI).content("")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.value[0].parent_ID", is("12345678-aaaa-bbbb-dddd-ddddeeeeffef")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tesValidateBookAndDecreaseStock() throws Exception {
|
||||
|
||||
final String data = "{\"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffee\",\"book_ID\": \"5e69a718-d86b-4461-8953-0edadcd87960\", \"amount\": 20 }";
|
||||
|
||||
mockMvc.perform(post(ORDER_ITEMS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(post(ORDER_ITEMS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isBadRequest());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tesCalculateNetAmount() throws Exception {
|
||||
|
||||
final String data = "{\"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffbb\",\"book_ID\": \"b7bca6dd-0497-465e-9a5a-56f244174c8c\", \"amount\": 10 }";
|
||||
|
||||
MvcResult result = mockMvc.perform(post(ORDER_ITEMS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andReturn();
|
||||
|
||||
JSONObject jsonObj = new JSONObject(result.getResponse().getContentAsString());
|
||||
|
||||
String id = jsonObj.get("ID").toString();
|
||||
|
||||
mockMvc.perform(get(ORDER_ITEMS_URI+ "(" + id + ")").content("")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.netAmount", is(141.4)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCreateOrder() throws Exception {
|
||||
|
||||
final String orderData = "{\r\n" +
|
||||
" \"items\": [{\r\n" +
|
||||
" \"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\",\r\n" +
|
||||
" \"book_ID\": \"abed2f7a-c50e-4bc5-89fd-9a00a54b4b16\",\r\n" +
|
||||
" \"amount\": 1\r\n" +
|
||||
" }, {\r\n" +
|
||||
" \"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\",\r\n" +
|
||||
" \"book_ID\": \"b7bca6dd-0497-465e-9a5a-56f244174c8c\",\r\n" +
|
||||
" \"amount\": 1\r\n" +
|
||||
" }]\r\n" +
|
||||
"}";
|
||||
|
||||
|
||||
mockMvc.perform(post(ORDERS_URI).contentType(APPLICATION_JSON_UTF8).content(
|
||||
orderData)) .andExpect(status().isCreated());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculateTotal() throws Exception {
|
||||
|
||||
final String orderData = "{\r\n" +
|
||||
" \"items\": [{\r\n" +
|
||||
" \"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\",\r\n" +
|
||||
" \"book_ID\": \"abed2f7a-c50e-4bc5-89fd-9a00a54b4b16\",\r\n" +
|
||||
" \"amount\": 1\r\n" +
|
||||
" }, {\r\n" +
|
||||
" \"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\",\r\n" +
|
||||
" \"book_ID\": \"b7bca6dd-0497-465e-9a5a-56f244174c8c\",\r\n" +
|
||||
" \"amount\": 2\r\n" +
|
||||
" }]\r\n" +
|
||||
"}";
|
||||
|
||||
MvcResult result = mockMvc.perform(post(ORDERS_URI).contentType(APPLICATION_JSON_UTF8).content(orderData))
|
||||
.andReturn();
|
||||
|
||||
JSONObject jsonObj = new JSONObject(result.getResponse().getContentAsString());
|
||||
|
||||
String id = jsonObj.get("ID").toString();
|
||||
|
||||
mockMvc.perform(get(ORDERS_URI+ "(" + id + ")").content("")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.total", is(39.39)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateBookAndDecreaseStockViaOrders() throws Exception {
|
||||
|
||||
final String orderData = "{\r\n" +
|
||||
" \"items\": [{\r\n" +
|
||||
" \"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\",\r\n" +
|
||||
" \"book_ID\": \"fd0c5fda-8811-4e20-bcff-3a776abc290a\",\r\n" +
|
||||
" \"amount\": 300\r\n" +
|
||||
" }, {\r\n" +
|
||||
" \"parent_ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\",\r\n" +
|
||||
" \"book_ID\": \"b7bca6dd-0497-465e-9a5a-56f244174c8c\",\r\n" +
|
||||
" \"amount\": 1\r\n" +
|
||||
" }]\r\n" +
|
||||
"}";
|
||||
mockMvc.perform(post(ORDERS_URI).contentType(APPLICATION_JSON_UTF8).content(orderData))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(post(ORDERS_URI).contentType(APPLICATION_JSON_UTF8).content(orderData))
|
||||
.andExpect(status().isBadRequest());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.sap.teched.cap.bookstore.testUtil;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sap.cds.ql.Insert;
|
||||
import com.sap.cds.reflect.CdsElement;
|
||||
import com.sap.cds.reflect.CdsEntity;
|
||||
import com.sap.cds.reflect.CdsModel;
|
||||
import com.sap.cds.reflect.CdsSimpleType;
|
||||
import com.sap.cds.services.persistence.PersistenceService;
|
||||
|
||||
public class CsvUploader {
|
||||
|
||||
private final CdsModel model;
|
||||
private final PersistenceService ds;
|
||||
|
||||
public CsvUploader(CdsModel model, PersistenceService ds) {
|
||||
this.model = model;
|
||||
this.ds = ds;
|
||||
}
|
||||
|
||||
void insertContent(File file) throws FileNotFoundException, IOException {
|
||||
String fileName = file.getName();
|
||||
String entityName = fileName.replace("-", ".").substring(0, fileName.length() - 4);
|
||||
CdsEntity entity = model.getEntity(entityName);
|
||||
List<Map<String, Object>> data = new ArrayList<>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String line = br.readLine();
|
||||
CdsElement[] headers = getHeaders(entity, line);
|
||||
while ((line = br.readLine()) != null) {
|
||||
Map<String, Object> row = new HashMap<>();
|
||||
String[] values = line.split(";");
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
CdsElement element = headers[i];
|
||||
String typeName = element.getType().as(CdsSimpleType.class).getQualifiedName();
|
||||
final Object value;
|
||||
String txt = values[i];
|
||||
if ("".equals(txt)) {
|
||||
continue;
|
||||
}
|
||||
switch (typeName) {
|
||||
case "cds.Integer":
|
||||
value = Integer.valueOf(txt);
|
||||
break;
|
||||
case "cds.Date":
|
||||
value = LocalDate.parse(txt);
|
||||
break;
|
||||
default:
|
||||
value = txt;
|
||||
}
|
||||
row.put(element.getName(), value);
|
||||
}
|
||||
|
||||
data.add(row);
|
||||
}
|
||||
}
|
||||
|
||||
Insert insert = Insert.into(entity).entries(data);
|
||||
ds.run(insert);
|
||||
}
|
||||
|
||||
private CdsElement[] getHeaders(CdsEntity entity, String line) {
|
||||
String[] elementNames = line.split(";");
|
||||
CdsElement[] result = new CdsElement[elementNames.length];
|
||||
for (int i = 0; i < elementNames.length; i++) {
|
||||
result[i] = entity.getElement(elementNames[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.sap.teched.cap.bookstore.testUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.sap.cds.services.persistence.PersistenceService;
|
||||
import com.sap.cds.services.runtime.CdsRuntime;
|
||||
|
||||
@Component
|
||||
public class DataLoader implements ApplicationRunner {
|
||||
|
||||
@Autowired
|
||||
private CdsRuntime runtime;
|
||||
|
||||
public void run(ApplicationArguments args) {
|
||||
|
||||
PersistenceService ds = (PersistenceService) runtime.getServiceCatalog()
|
||||
.getService(PersistenceService.DEFAULT_NAME);
|
||||
|
||||
runtime.runInRequestContext(null, r -> {
|
||||
runtime.runInChangeSetContext(c -> {
|
||||
File folder = new File("../db/data");
|
||||
CsvUploader uploader = new CsvUploader(runtime.getCdsModel(), ds);
|
||||
for (File file : folder.listFiles()) {
|
||||
try {
|
||||
if(file.getName().equals("sap.capire.bookstore-Books_texts.csv"))
|
||||
continue;
|
||||
uploader.insertContent(file);
|
||||
} catch (RuntimeException | IOException e) {
|
||||
throw new RuntimeException("exception reading file " + file.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
7
bookstore/srv/src/test/resources/application.yaml
Normal file
7
bookstore/srv/src/test/resources/application.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
spring:
|
||||
profiles: default
|
||||
datasource:
|
||||
url: "jdbc:sqlite::memory:"
|
||||
driver-class-name: org.sqlite.JDBC
|
||||
initialization-mode: always
|
||||
@@ -25,6 +25,11 @@
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.sap.teched.cap.productsservice;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class productServiceTest {
|
||||
|
||||
private static final String ODATA_V4 = "/odata/v4";
|
||||
private static final String PRODUCTS_URI = ODATA_V4 + "/AdminService/Products";
|
||||
private static final String CATEGORIES_URI = ODATA_V4 + "/AdminService/Categories";
|
||||
private static final String CURRENCIES_URI = ODATA_V4 + "/AdminService/Currencies";
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void testProducts() throws Exception {
|
||||
mockMvc.perform(get(PRODUCTS_URI).content("")).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostProducts() throws Exception {
|
||||
final String data = "{\"ID\": \"12345678-aaaa-bbbb-cccc-ddddeeeeffff\", \"title\": \"Awesome product\", \"descr\": \"It is really really awesome!\"}";
|
||||
|
||||
mockMvc.perform(post(PRODUCTS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProduct() throws Exception {
|
||||
final String id = "12345611-aaaa-bbbb-cccc-ddddeeeeffef";
|
||||
final String data = "{\"ID\": \"12345611-aaaa-bbbb-cccc-ddddeeeeffef\", \"title\": \"Awesome book\", \"descr\": \"It is an awesome book!\"}";
|
||||
|
||||
mockMvc.perform(post(PRODUCTS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(get(PRODUCTS_URI + "(" + id + ")").content("")).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.title", is("Awesome book")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditProduct() throws Exception {
|
||||
final String id = "12345678-aaaa-bbbb-dddd-ddddeeeeffef";
|
||||
final String data = "{\"ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\", \"title\": \"Awesome book\", \"descr\": \"It is an awesome book!\"}";
|
||||
final String changedData = "{\"ID\": \"12345678-aaaa-bbbb-dddd-ddddeeeeffef\", \"title\": \"Awesome book\", \"descr\": \"It is a good book! I love it\"}";
|
||||
|
||||
mockMvc.perform(post(PRODUCTS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(put(PRODUCTS_URI + "(" + id + ")").contentType(APPLICATION_JSON_UTF8).content(changedData))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.modifiedAt", is(not(nullValue()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteProduct() throws Exception {
|
||||
final String id = "12345678-aaaa-bbbb-cccc-ddddeeeeffdd";
|
||||
final String data = "{\"ID\": \"12345678-aaaa-bbbb-cccc-ddddeeeeffdd\", \"title\": \"Bad book\", \"descr\": \"It is a very bad book!\"}";
|
||||
|
||||
mockMvc.perform(post(PRODUCTS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(delete(PRODUCTS_URI + "(" + id + ")").content("")).andExpect(status().isNoContent());
|
||||
|
||||
mockMvc.perform(get(PRODUCTS_URI + "(" + id + ")").content("")).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProductWhenProductIDNotPresent() throws Exception {
|
||||
final String data = "{\"ID\": \"12345678-aaaa-bbbb-cccc-ddddeeeeffaa\", \"title\": \"Another book\", \"descr\": \"It is bad book!\"}";
|
||||
|
||||
mockMvc.perform(post(PRODUCTS_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(get(PRODUCTS_URI + "(123)").content("")).andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostCategories() throws Exception {
|
||||
final String data = "{\"ID\": 2, \"parent_ID\": 1, \"name\": \"Fiction\"}";
|
||||
|
||||
mockMvc.perform(post(CATEGORIES_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditCategories() throws Exception {
|
||||
final String data = "{\"ID\": 3, \"parent_ID\": 1, \"name\": \"Fiction\"}";
|
||||
final String changedData = "{\"ID\": 3, \"name\": \"Science Fiction\"}";
|
||||
|
||||
mockMvc.perform(post(CATEGORIES_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
mockMvc.perform(put(CATEGORIES_URI + "(3)").contentType(APPLICATION_JSON_UTF8).content(changedData))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostCurrencies() throws Exception {
|
||||
final String data = "{\"code\": \"USD\", \"symbol\": \"$\", \"name\": \"US Dollar\", \"descr\": \"United States Dollar\"}";
|
||||
|
||||
mockMvc.perform(post(CURRENCIES_URI).contentType(APPLICATION_JSON_UTF8).content(data))
|
||||
.andExpect(status().isCreated());
|
||||
}
|
||||
|
||||
}
|
||||
7
products-service/srv/src/test/resources/application.yaml
Normal file
7
products-service/srv/src/test/resources/application.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
spring:
|
||||
profiles: default
|
||||
datasource:
|
||||
url: "jdbc:sqlite::memory:"
|
||||
driver-class-name: org.sqlite.JDBC
|
||||
initialization-mode: always
|
||||
Reference in New Issue
Block a user