diff --git a/README.md b/README.md index 135a427..bdbbd05 100644 --- a/README.md +++ b/README.md @@ -1,354 +1,54 @@ -# Example Employee-Manager App -A sample Employee-Manager app to test Keploy integration capabilities using [SpringBoot](https://spring.io) and -[PostgreSQL](https://www.postgresql.org/). +# Employee-Manager App -**Note** :- Issue Creation is disabled on this Repository, please visit [here](https://github.com/keploy/keploy/issues/new/choose) to submit Issue. +A sample Employee-Manager app using [Spring Boot](https://spring.io) and [PostgreSQL](https://www.postgresql.org). ## Pre-requisites - [Java 8+](https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.installing) -## Quick Installation of "KEPLOY" server +## Build and Run -### **MacOS** +1. Clone the repository: -
-MacOS + ```bash + git clone https://github.com/keploy/samples-java + ``` -```shell -curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_darwin_all.tar.gz" | tar xz -C /tmp - -sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy -``` - -
- -### **Linux** - -
-Linux - -```shell -curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp - -sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy -``` - -
- -
-Linux ARM - -```shell -curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz -C /tmp - -sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy -``` +2. Start a PostgreSQL instance. You can use Docker: + ```bash + docker-compose up -d + ``` -
+3. Build the application: -### **Windows** + ```shell + mvn clean install -Dmaven.test.skip=true + ``` -
-Windows +4. Start the Employee-Manager App: -- Download - the [Keploy Windows AMD64](https://github.com/keploy/keploy/releases/latest/download/keploy_windows_amd64.tar.gz), and - extract the files from the zip folder. -- Run the `keploy.exe` file. + - Using your preferred IDE: -
+ - Run your application. + - You can also run the application with coverage to see the test coverage. -
-Windows ARM + - Using the command line: -- Download - the [Keploy Windows ARM64](https://github.com/keploy/keploy/releases/latest/download/keploy_windows_arm64.tar.gz), and - extract the files from the zip folder. -- Run the `keploy.exe` file. + 1. Run your tests using the following command: `mvn test`. -
- -## Build configuration - -[Find the latest release](https://search.maven.org/artifact/io.keploy/keploy-sdk) of the Keploy Java SDK at maven -central. - -Add *keploy-sdk* as a dependency to your *pom.xml*: - - - io.keploy - keploy-sdk - N.N.N (eg: 1.2.8) - - -or to *build.gradle*: - - implementation 'io.keploy:keploy-sdk:N.N.N' (eg: 1.2.8) +5. The application is now running. You can access it at `http://localhost:8080`. ## Usage -- Refer [this](https://github.com/gouravkrosx/java-sdk#usage). - -## Setup Employee-Manager App - -```bash -git clone https://github.com/keploy/samples-java -``` - -## Agent-jar -Download the latest version (1.2.8) of agent jar from [here](https://central.sonatype.com/artifact/io.keploy/agent/1.2.8) and add this jar to main directory. - - -### Start PostgreSQL instance -Download docker dekstop from [here](https://docs.docker.com/get-docker/). -```bash -docker-compose up -d -``` - -### Maven clean install - -```shell -mvn clean install -Dmaven.test.skip=true -``` - -### Set KEPLOY_MODE to record - -- To record testcases use `KEPLOY_MODE` and `DENOISE` as env variables. Set `KEPLOY` to `record` mode and mark `DENOISE` as `true`. Also add - -`-javaagent:path of agent jar` like this 👇. - - -![Edit_configurations](/src/main/resources/Edit-configurations.png "Edit configurations") - - -## Generate testcases - -To generate testcases we just need to **make some API calls.** You can use [Postman](https://www.postman.com/) -, [Hoppscotch](https://hoppscotch.io/), or simply `curl` - -### 1. Make an employee entry - -```bash -curl --location --request POST 'http://localhost:8080/api/employees' \ ---header 'Content-Type: application/json' \ ---data-raw '{ - "firstName": "Myke", - "lastName": "Tyson", - "email": "mt@gmail.com", - "timestamp":1 -}' -``` - -this will return the response or an entry. The timestamp would automatically be ignored during testing because it'll -always be different. - -``` -{ - "id": 1, - "firstName": "Myke", - "lastName": "Tyson", - "email": "mt@gmail.com", - "timestamp": 1661493301 -} -``` - -### 2. Fetch recorded info about employees - -```bash -curl --location --request GET 'http://localhost:8080/api/employees/1' -``` - -or by querying through the browser `http://localhost:8080/api/employees/1` - -Now both these API calls were captured as **editable** testcases and written to `test/e2e/keploy-tests` folder. The -keploy directory would also have `mocks` folder. -![testcases](/src/main/resources/test-cases.png "Recorded Test cases and mocks") +For more details on using the Employee-Manager App, please refer to the documentation specific to this project. -Now, let's see the magic! 🪄💫 +**Note**: Issue creation is disabled in this repository. If you have questions or issues to report, please visit [Keploy's main repository](https://github.com/keploy/keploy/issues/new/choose). -## Test mode +--- -There are 2 ways to test the application with Keploy. - -1. [Unit Test File](/README.md#testing-using-unit-test-file) -2. [Without Unit Test File](/README.md#testing-without-using-unit-test-file) - -### **Testing using Unit Test File** - - -- Set `KEPLOY_MODE = test` (default "off") - -_NOTE: You will be required to set the `javaagent` again in your test profile just like below._ - -![run_configuration](./src/main/resources/Run_Configuration.png "Run_Configuration") - -- Now that we have our testcase captured, run the unit test file (`SampleJavaApplication_Test.java`) already present in -the sample app repo. - -- If not present, you can make and add below code in `SampleJavaApplication_Test.java` in the test module of your sample application. - -```java - @Test - public void TestKeploy() throws InterruptedException { - - CountDownLatch countDownLatch = HaltThread.getInstance().getCountDownLatch(); - Mode.setTestMode(); - - new Thread(() -> { - .main(new String[]{""}); - countDownLatch.countDown(); - }).start(); - - countDownLatch.await(); - assertTrue(AssertKTests.result(), "Keploy Test Result"); - } +_[Original Keploy-specific content has been removed from this README.]_ ``` -- **Using IDE:** _(for local use-case we prefer running tests via IDE)_ - 1. Run your application. - 2. You can also run the application with coverage to see the test coverage. - -- **Using command line** - 1. Add maven-surefire-plugin to your *pom.xml*. In ` ` don't add jacoco agent if you don't want coverage report. - - ```xml - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.2 - - - - - -javaagent:.jar - -javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/0.8.7/org.jacoco.agent-0.8.7-runtime.jar=destfile=target/jacoco.exec - - - - target/jacoco.exec - - - - - ``` - - 2. If you want coverage report also add Jacoco plugin to your *pom.xml*. - - ```xml - - org.jacoco - jacoco-maven-plugin - 0.8.7 - - - prepare-agent - - prepare-agent - - - - report - prepare-package - - report - - - - post-unit-test - test - - report - - - - - target/jacoco.exec - - target/my-reports - - - - - ``` - 3. Run your tests using command : `mvn test`. - - -It will create .html files as test-reports which can be found in your target folder !! - -**_We got 53% without writing any testcases. 🎉_** - -Go to the Keploy Console TestRuns Page to get deeper insights on what testcases ran, what failed. - -![testruns](https://i.imgur.com/tg6OT0n.png "Summary") - -### **Testing without using Unit Test File** - -To test using `KEPLOY_MODE` env variable, set the same to `test` mode. - -``` -export KEPLOY_MODE=test -``` - -Now simply run the application either by ide or using command: - -```shell -java -javaagent:.jar -jar .jar -``` - -Keploy will run all the captures test-cases, compare and show the results on the console. - -```shell -10b3ddd5-42fa-48e7-b98a-b47257272e39 total tests: 2 -2022-08-26 14:13:08.993 INFO 11560 --- [ Thread-4] io.keploy.service.GrpcService : testing 1 of 2 testcase id: [ae4a6c91-712a-4566-bf0d-97d708f94b2d] -2022-08-26 14:13:08.994 INFO 11560 --- [ Thread-4] io.keploy.service.GrpcService : testing 2 of 2 testcase id: [4843e03e-76a8-4194-99cb-f62740978d15] -2022-08-26 14:13:09.061 INFO 11560 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' -2022-08-26 14:13:09.061 INFO 11560 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' -2022-08-26 14:13:09.062 INFO 11560 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms -Hibernate: insert into employees (email, first_name, last_name, timestamp) values (?, ?, ?, ?) -2022-08-26 14:13:09.247 INFO 11560 --- [pool-3-thread-1] io.keploy.service.GrpcService : result : testcase id: [ae4a6c91-712a-4566-bf0d-97d708f94b2d] passed: true -Hibernate: select employee0_.id as id1_0_0_, employee0_.email as email2_0_0_, employee0_.first_name as first_na3_0_0_, employee0_.last_name as last_nam4_0_0_, employee0_.timestamp as timestam5_0_0_ from employees employee0_ where employee0_.id=? -2022-08-26 14:13:09.291 INFO 11560 --- [pool-3-thread-1] io.keploy.service.GrpcService : result : testcase id: [4843e03e-76a8-4194-99cb-f62740978d15] passed: true -2022-08-26 14:13:09.388 INFO 11560 --- [ Thread-4] io.keploy.service.GrpcService : test run completed with run id [1e81233d-e3be-4a4a-afda-a800902ad965] -2022-08-26 14:13:09.388 INFO 11560 --- [ Thread-4] io.keploy.service.GrpcService : || passed overall: TRUE || -2022-08-26 14:13:19.408 INFO 11560 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' -2022-08-26 14:13:19.410 INFO 11560 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... -2022-08-26 14:13:19.414 INFO 11560 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. -``` - -![testruns](/src/main/resources/test-runs-success.png "Recent testruns") - -## Let's add a Bug in the App - -Now let's introduce a bug! Let's try changing something like adding some extra headers in -controllers `./EmployeeController.java` on line 35 like : - -```java -return ResponseEntity.ok().header("MyNewHeader","abc").body(employee); -``` - -Let's run the test-file to see if Keploy catches the regression introduced. - -```shell -mvn test -``` - -You'll notice the failed test-case in the output. - -```shell -2022-08-26 13:10:10.289 TRACE 70155 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1] -2022-08-26 13:10:10.307 INFO 70155 --- [pool-3-thread-1] io.keploy.service.GrpcService : result : testcase id: [d42a3724-2c78-4f42-8dc6-d25a9b611c7c] passed: false -2022-08-26 13:10:10.312 INFO 70155 --- [ Thread-1] io.keploy.service.GrpcService : test run completed with run id [fcb61332-1025-463f-854e-6f406bce870d] -2022-08-26 13:10:10.312 INFO 70155 --- [ Thread-1] io.keploy.service.GrpcService : || passed overall: FALSE || -``` - -To deep dive the problem go to [test runs](http://localhost:6789/testruns) - -![testruns](/src/main/resources/test-runs-fail.png "Recent testruns") - - -**In case of any query, refer to video below,** - -[![java-sample](https://user-images.githubusercontent.com/74777863/217864311-94a3dc0c-90bc-4551-aca2-87e82e3d24cb.png)](https://youtu.be/Ssm4TnTkbLs) +This updated README focuses solely on how to build and run the Employee-Manager App using Spring Boot and PostgreSQL. The Keploy-specific content has been removed. diff --git a/pom.xml b/pom.xml index 54dd38d..56913d1 100644 --- a/pom.xml +++ b/pom.xml @@ -22,21 +22,11 @@ spring-context 5.3.22 - org.springframework spring-webmvc 5.3.22 - - - - io.keploy - keploy-sdk - 1.2.8 - - - org.mariadb.jdbc mariadb-java-client diff --git a/src/main/java/com/example/demo/SamplesJavaApplication.java b/src/main/java/com/example/demo/SamplesJavaApplication.java index 76dd17b..5ac43eb 100644 --- a/src/main/java/com/example/demo/SamplesJavaApplication.java +++ b/src/main/java/com/example/demo/SamplesJavaApplication.java @@ -1,15 +1,11 @@ package com.example.demo; -import io.keploy.servlet.KeployMiddleware; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Import; @SpringBootApplication -@Import(KeployMiddleware.class) public class SamplesJavaApplication { public static void main(String[] args) { SpringApplication.run(SamplesJavaApplication.class, args); } } - diff --git a/src/test/java/SamplesJavaApplication_Test.java b/src/test/java/SamplesJavaApplication_Test.java deleted file mode 100644 index da9d57e..0000000 --- a/src/test/java/SamplesJavaApplication_Test.java +++ /dev/null @@ -1,38 +0,0 @@ -import com.example.demo.SamplesJavaApplication; -import com.example.demo.controller.EmployeeController; -import com.example.demo.repository.EmployeeRepository; -import io.keploy.regression.Mode; -import io.keploy.utils.AssertKTests; -import io.keploy.utils.HaltThread; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; - -import java.util.concurrent.CountDownLatch; - -import static org.junit.jupiter.api.Assertions.*; - - -public class SamplesJavaApplication_Test { - - @Mock - private EmployeeRepository employeeRepository; - - @InjectMocks - private EmployeeController employeeController; - - @Test() - public void TestKeploy() throws InterruptedException { - - CountDownLatch countDownLatch = HaltThread.getInstance().getCountDownLatch(); - Mode.setTestMode(); - - new Thread(() -> { - SamplesJavaApplication.main(new String[]{""}); - countDownLatch.countDown(); - }).start(); - - countDownLatch.await(); - assertTrue(AssertKTests.result()); - } -}