diff --git a/spring-boot-redis/README.md b/spring-boot-redis/README.md new file mode 100644 index 00000000..34cf271d --- /dev/null +++ b/spring-boot-redis/README.md @@ -0,0 +1,114 @@ +Here’s a README for the Product application you developed using Spring Boot and Redis: + +```markdown +# Product Spring Application + +This is a simple Product application built with Spring Boot and Redis. The application provides RESTful APIs to manage Product items. + +## Prerequisites + +- Java 8 +- Maven +- Docker + +## Installation + +### 1. Clone the Repository + +```sh +git clone https://github.com/yourusername/product-spring-app.git +cd product-spring-app +``` + +### 2. Build the Application + +```sh +mvn clean install +``` + +### 3. Set Up Docker Containers + +#### Redis Container + +1. Pull the Redis Docker Image: + + ```sh + docker pull redis:latest + ``` + +2. Run the Redis Container: + + ```sh + docker run --name redis-product -p 6379:6379 -d redis:latest + ``` + +### 4. Configure Application Properties + +Update the `src/main/resources/application.properties` file with your Redis settings: + +```properties +spring.redis.host=localhost +spring.redis.port=6379 +``` + +### 5. Run the Application + +```sh +mvn spring-boot:run +``` + +## Usage + +### API Endpoints + +#### Create a Product Item + +```sh +curl -X POST http://localhost:8080/api/products \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Sample Product", + "description": "This is a sample product description", + "price": 29.99 + }' +``` + +#### Get All Product Items + +```sh +curl -X GET http://localhost:8080/api/products +``` + +#### Get a Product Item by ID + +```sh +curl -X GET http://localhost:8080/api/products/{id} +``` +Replace `{id}` with the actual ID of the Product item you want to retrieve. + +#### Update a Product Item + +```sh +curl -X PUT http://localhost:8080/api/products/{id} \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Updated Product", + "description": "This is an updated product description", + "price": 39.99 + }' +``` +Replace `{id}` with the actual ID of the Product item you want to update. + +#### Delete a Product Item + +```sh +curl -X DELETE http://localhost:8080/api/products/{id} +``` +Replace `{id}` with the actual ID of the Product item you want to delete. + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. +``` + +Feel free to adjust any details specific to your project, such as the repository link, product attributes, or additional instructions! \ No newline at end of file diff --git a/spring-boot-redis/keploy/test-set-0/mocks.yaml b/spring-boot-redis/keploy/test-set-0/mocks.yaml new file mode 100644 index 00000000..cb0a1827 --- /dev/null +++ b/spring-boot-redis/keploy/test-set-0/mocks.yaml @@ -0,0 +1,30 @@ +version: api.keploy.io/v1beta1 +kind: Mocks +name: product-mocks +spec: + mocks: + - req: + method: POST + url: http://localhost:8080/products + resp: + status_code: 201 + header: + Content-Type: application/json + body: '{"id":"1","name":"Product A","price":10.99}' + + - req: + method: GET + url: http://localhost:8080/products/1 + resp: + status_code: 200 + header: + Content-Type: application/json + body: '{"id":"1","name":"Product A","price":10.99}' + + - req: + method: DELETE + url: http://localhost:8080/products/1 + resp: + status_code: 204 + header: + Content-Type: application/json diff --git a/spring-boot-redis/keploy/test-set-0/tests/test-1.yaml b/spring-boot-redis/keploy/test-set-0/tests/test-1.yaml new file mode 100644 index 00000000..c74dc4e8 --- /dev/null +++ b/spring-boot-redis/keploy/test-set-0/tests/test-1.yaml @@ -0,0 +1,50 @@ +version: api.keploy.io/v1beta1 +kind: Http +name: test-1 +spec: + metadata: {} + req: + method: POST + proto_major: 1 + proto_minor: 1 + url: http://localhost:8080/products + header: + Accept: '*/*' + Content-Length: "160" + Content-Type: application/json + Host: localhost:8080 + User-Agent: curl/7.88.1 + body: |- + { + "id": "1", + "name": "Product A", + "price": 10.99 + } + timestamp: 2024-08-14T11:58:49.994804119+05:30 + resp: + status_code: 201 + header: + Content-Type: application/json + Date: Wed, 14 Aug 2024 06:28:51 GMT + body: '{"id":"1","name":"Product A","price":10.99}' + status_message: Created + proto_major: 0 + proto_minor: 0 + timestamp: 2024-08-14T11:58:53.957634007+05:30 + objects: [] + assertions: + noise: + header.Date: [] + created: 1723616933 +curl: |- + curl --request POST \ + --url http://localhost:8080/products \ + --header 'User-Agent: curl/7.88.1' \ + --header 'Accept: */*' \ + --header 'Content-Type: application/json' \ + --header 'Host: localhost:8080' \ + --data '{ + "id": "1", + "name": "Product A", + "price": 10.99 + }' diff --git a/spring-boot-redis/keploy/test-set-0/tests/test-2.yaml b/spring-boot-redis/keploy/test-set-0/tests/test-2.yaml new file mode 100644 index 00000000..6fbddcff --- /dev/null +++ b/spring-boot-redis/keploy/test-set-0/tests/test-2.yaml @@ -0,0 +1,36 @@ +version: api.keploy.io/v1beta1 +kind: Http +name: test-2 +spec: + metadata: {} + req: + method: GET + proto_major: 1 + proto_minor: 1 + url: http://localhost:8080/products/1 + header: + Accept: '*/*' + Host: localhost:8080 + User-Agent: curl/7.88.1 + timestamp: 2024-08-14T11:58:49.994804119+05:30 + resp: + status_code: 200 + header: + Content-Type: application/json + Date: Wed, 14 Aug 2024 06:28:51 GMT + body: '{"id":"1","name":"Product A","price":10.99}' + status_message: OK + proto_major: 0 + proto_minor: 0 + timestamp: 2024-08-14T11:58:53.957634007+05:30 + objects: [] + assertions: + noise: + header.Date: [] + created: 1723616933 +curl: |- + curl --request GET \ + --url http://localhost:8080/products/1 \ + --header 'User-Agent: curl/7.88.1' \ + --header 'Accept: */*' \ + --header 'Host: localhost:8080' diff --git a/spring-boot-redis/keploy/test-set-0/tests/test-3.yaml b/spring-boot-redis/keploy/test-set-0/tests/test-3.yaml new file mode 100644 index 00000000..e69de29b diff --git a/spring-boot-redis/pom.xml b/spring-boot-redis/pom.xml new file mode 100644 index 00000000..a7a24c97 --- /dev/null +++ b/spring-boot-redis/pom.xml @@ -0,0 +1,59 @@ + + 4.0.0 + + com.example + spring-boot-redis + 0.0.1-SNAPSHOT + jar + + spring-boot-redis + Sample application using Spring Boot and Redis + + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + + + 11 + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-redis/src/main/java/com/example/redis/SpringBootRedisApplication.java b/spring-boot-redis/src/main/java/com/example/redis/SpringBootRedisApplication.java new file mode 100644 index 00000000..2320bee9 --- /dev/null +++ b/spring-boot-redis/src/main/java/com/example/redis/SpringBootRedisApplication.java @@ -0,0 +1,11 @@ +package com.example.redis; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootRedisApplication { + public static void main(String[] args) { + SpringApplication.run(SpringBootRedisApplication.class, args); + } +} diff --git a/spring-boot-redis/src/main/java/com/example/redis/controller/ProductController.java b/spring-boot-redis/src/main/java/com/example/redis/controller/ProductController.java new file mode 100644 index 00000000..0d0491eb --- /dev/null +++ b/spring-boot-redis/src/main/java/com/example/redis/controller/ProductController.java @@ -0,0 +1,45 @@ +package com.example.redis.controller; + +import com.example.redis.model.Product; +import com.example.redis.repository.ProductRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.Optional; + +@RestController +@RequestMapping("/products") +public class ProductController { + + @Autowired + private ProductRepository productRepository; + + @PostMapping + public Product createProduct(@RequestBody Product product) { + return productRepository.save(product); + } + + @GetMapping("/{id}") + public Optional getProduct(@PathVariable String id) { + return productRepository.findById(id); + } + + @GetMapping + public Iterable getAllProducts() { + return productRepository.findAll(); + } + + @PutMapping("/{id}") + public Product updateProduct(@PathVariable String id, @RequestBody Product product) { + if (!productRepository.existsById(id)) { + return null; + } + product.setId(id); + return productRepository.save(product); + } + + @DeleteMapping("/{id}") + public void deleteProduct(@PathVariable String id) { + productRepository.deleteById(id); + } +} diff --git a/spring-boot-redis/src/main/java/com/example/redis/model/Product.java b/spring-boot-redis/src/main/java/com/example/redis/model/Product.java new file mode 100644 index 00000000..74a8a429 --- /dev/null +++ b/spring-boot-redis/src/main/java/com/example/redis/model/Product.java @@ -0,0 +1,19 @@ +package com.example.redis.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.redis.core.RedisHash; + +@RedisHash("products") +public class Product { + @Id + private String id; + private String name; + private double price; + + public String getId() { return id; } + public void setId(String id) { this.id = id; } + public String getName() { return name; } + public void setName(String name) { this.name = name; } + public double getPrice() { return price; } + public void setPrice(double price) { this.price = price; } +} diff --git a/spring-boot-redis/src/main/java/com/example/redis/repository/ProductRepository.java b/spring-boot-redis/src/main/java/com/example/redis/repository/ProductRepository.java new file mode 100644 index 00000000..68210dc8 --- /dev/null +++ b/spring-boot-redis/src/main/java/com/example/redis/repository/ProductRepository.java @@ -0,0 +1,7 @@ +package com.example.redis.repository; + +import com.example.redis.model.Product; +import org.springframework.data.repository.CrudRepository; + +public interface ProductRepository extends CrudRepository { +} diff --git a/spring-boot-redis/src/main/resources/application.properties b/spring-boot-redis/src/main/resources/application.properties new file mode 100644 index 00000000..dc19204b --- /dev/null +++ b/spring-boot-redis/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.redis.host=localhost +spring.redis.port=6379