|
| 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 | + |
0 commit comments