Skip to content

Commit 96bfd29

Browse files
committed
added spring project demo using postgresql and graphql
Signed-off-by: Alberto Cao <[email protected]> Signed-off-by: Alberto Cao <[email protected]>
1 parent 26c2323 commit 96bfd29

File tree

19 files changed

+1116
-0
lines changed

19 files changed

+1116
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This repo contains the sample for [Keploy's](https://keploy.io) Java Application
2121
1. [employee-manager](https://github.com/keploy/samples-java/tree/main/employee-manager)
2222
2. [spring-boot-mongo](https://github.com/keploy/samples-java/tree/main/spring-boot-mongo)
2323
3. [user-manager](https://github.com/keploy/samples-java/tree/main/user-manager)
24+
4. [spring-boot-postgres-graphql](https://github.com/keploy/samples-java/tree/main/spring-boot-postgres-graphql)
2425

2526
## Community Support ❤️
2627

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.mvn/
2+
.idea/
3+
target/
4+
!.mvn/wrapper/maven-wrapper.jar
5+
!**/src/main/**/target/
6+
!**/src/test/**/target/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
23+
### NetBeans ###
24+
/nbproject/private/
25+
/nbbuild/
26+
/dist/
27+
/nbdist/
28+
/.nb-gradle/
29+
build/
30+
!**/src/main/**/build/
31+
!**/src/test/**/build/
32+
33+
### VS Code ###
34+
.vscode/
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Library DEMO
2+
3+
### Requirements
4+
5+
- JDK 17
6+
- Maven 3.9.5
7+
- Docker or PostgreSQL
8+
9+
### About the DEMO
10+
11+
This is a Spring Boot application implementing a GraphQL service to handle requests related to books and authors.
12+
13+
The controller provides four queries:
14+
15+
- `getBookByName`: Returns a book based on the specified name.
16+
- `getAllBooks`: Retrieves all books present in the repository.
17+
- `getAuthorById`: Fetches an author based on the provided ID.
18+
- `getAllAuthors`: Retrieves all authors available in the repository.
19+
20+
# Getting Started
21+
22+
Follow the steps to start the application:
23+
24+
## Clone
25+
- First, clone the repository:
26+
27+
```bash
28+
git clone https://github.com/keploy/samples-java
29+
```
30+
Navigate to the project folder:
31+
32+
```bash
33+
cd samples-java/spring-boot-postgres-graphql
34+
```
35+
36+
## Quick Keploy Installation
37+
38+
- Depending on your OS and preference (Docker/Native), you can set up Keploy using the one-click installation method:
39+
```bash
40+
curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh
41+
```
42+
43+
## Setup DB
44+
45+
You have two possible cases:
46+
47+
### Use your postgres installation
48+
49+
If you already have PostgreSQL installed on your machine and want to use this installation, follow these steps:
50+
1. If not already started, launch the PostgreSQL service:
51+
```bash
52+
sudo service postgresql start
53+
```
54+
55+
2. Set the password for the postgres user (or alternatively, modify the application.properties file within the project):
56+
```bash
57+
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'root';"
58+
```
59+
3. Create the demo database:
60+
61+
```bash
62+
sudo -u postgres createdb library_demo
63+
```
64+
### Use postgres docker image
65+
66+
If you have Docker installed and prefer a PostgreSQL instance in a container, follow these steps:
67+
1. Build the Docker image:
68+
69+
```bash
70+
docker build -t postgres_library:demo ./postgres_demo_docker
71+
```
72+
73+
2. Run a container from the generated image:
74+
75+
```bash
76+
docker run -d -p 5432:5432 --name postgres postgres_library:demo
77+
```
78+
79+
## Use of Keploy
80+
81+
1. Your application is ready to be executed!!
82+
To start Keploy in record mode, in the root directory, run:
83+
84+
```bash
85+
keploy record -c "mvn spring-boot:run"
86+
```
87+
88+
### Query from GraphQL GUI
89+
90+
Now, go to `localhost:8081/graphiql` and access the GraphQL interface to make requests to the application.
91+
- Make a `getAllBooks` query:
92+
93+
```graphql
94+
query {
95+
getAllBooks {
96+
name
97+
author {
98+
id
99+
firstName
100+
lastName
101+
}
102+
}
103+
}
104+
```
105+
106+
- Make a `getBookByName` query:
107+
108+
```graphql
109+
query {
110+
getBookByName(name: "The Secret of the Moon") {
111+
id
112+
name
113+
pageCount
114+
author {
115+
firstName
116+
lastName
117+
}
118+
}
119+
}
120+
```
121+
122+
- Make a `getAllAuthors` query:
123+
124+
```graphql
125+
query {
126+
getAllAuthors {
127+
id
128+
firstName
129+
lastName
130+
}
131+
}
132+
```
133+
134+
- Make a `getAuthorById` query:
135+
136+
```graphql
137+
query {
138+
getAuthorById(id: 2) {
139+
firstName
140+
}
141+
}
142+
```
143+
144+
You can experiment with the data you want to retrieve from the query by removing or rearranging fields.
145+
146+
The generated tests and mocks are stored in the `Keploy` directory in the current working directory.
147+
148+
149+
### Execute tests
150+
151+
To test the app, start Keploy in test mode. In the root directory, run:
152+
153+
```bash
154+
keploy test -c "./mvn spring-boot:run" --delay 15
155+
```
156+
157+
This will run the tests and generate the report in the `Keploy/reports` directory in the current working directory.
158+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
record:
2+
path: ""
3+
# mandatory
4+
command: ""
5+
proxyport: 0
6+
containerName: ""
7+
networkName: ""
8+
delay: 5
9+
passThroughPorts: []
10+
test:
11+
path: ""
12+
# mandatory
13+
command: ""
14+
proxyport: 0
15+
containerName: ""
16+
networkName: ""
17+
testSets: []
18+
globalNoise: |-
19+
{
20+
"global": {
21+
"body": {},
22+
"header": {}
23+
},
24+
"test-sets": {
25+
"test-set-name": {
26+
"body": {},
27+
"header": {}
28+
}
29+
}
30+
}
31+
delay: 5
32+
apiTimeout: 5
33+
passThroughPorts: []
34+
#
35+
# Example on using globalNoise
36+
# globalNoise: |-
37+
# {
38+
# "global": {
39+
# "body": {
40+
# # to ignore some values for a field,
41+
# # pass regex patterns to the corresponding array value
42+
# "url": ["https?://\S+", "http://\S+"],
43+
# },
44+
# "header": {
45+
# # to ignore the entire field, pass an empty array
46+
# "Date: [],
47+
# }
48+
# },
49+
# # to ignore fields or the corresponding values for a specific test-set,
50+
# # pass the test-set-name as a key to the "test-sets" object and
51+
# # populate the corresponding "body" and "header" objects
52+
# "test-sets": {
53+
# "test-set-1": {
54+
# "body": {
55+
# # ignore all the values for the "url" field
56+
# "url": []
57+
# },
58+
# "header": {
59+
# # we can also pass the exact value to ignore for a field
60+
# "User-Agent": ["PostmanRuntime/7.34.0"]
61+
# }
62+
# }
63+
# }
64+
# }

0 commit comments

Comments
 (0)