Car CRUD App - Spring Boot
PROBLEM STATEMENT
Implement Microservice using Spring Boot & RestFul Architecture style.
For below Bean class, with following fields public class Car { private String carName; private Integer carId; private double price; private String model; private Integer yearOfManufacture; private String fuelType; (possible values PETROL/DIESEL/ELECTRIC) }
- Microservice with CRUD operations to create Car.
- Create appropriate layers.
- Write custom validations such that a) car name should not contain spaces and special characters. b) car should not be more than 15 years old
- Enable Caching at Service Layer and demonstrate it for the same Car Service Layer
- Get car review from `. Add circuit breaker pattern
- Add JUnit Test cases including integration tests
HOW TO USE:
Build the application by skipping test cases(jUnits are yet to be completed due to technical error), as there are bugs in JUnits ,need to be fixed. Run the application and once the initialisation is complete,use postman/terminal to Use the API's as decribed below. This App also implements hystrix circuit breaker and caching enabled at service layer as described below. This app uses H2 database (packaged dependecy with the project),for storing and retrieving the data.
Application Property: spring.datasource.url=jdbc:h2:mem:cars_data spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sneha spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true
Connect to H2 db on link: http://localhost:8080/h2-console
use : jdbc:h2:mem:cars_data as jdbc url || username: sneha || password:
Click on Connect.
API description:
1)POST
URL:http://localhost:8080/cars Body: { "carid": "1", "carname": "Jazz", "price": "1000000", "model": "XUV", "yearOfManufacture" : "2020", "fuelType":"PETROL" } ContentType:json
Output: Returns the carid on succesful insertion
2)GET To get all cars:
Output: List of Cars
{{
"carid": "1",
"carname": "Jazz",
"price": "1000000",
"model": "XUV",
"yearOfManufacture" : "2020",
"fuelType":"PETROL"
},
{}}
To get a specific Car by ID:
URL:http://localhost:8080/car/1
Output: Car Description By ID
{
"carid": "1",
"carname": "Jazz",
"price": "1000000",
"model": "XUV",
"yearOfManufacture" : "2020",
"fuelType":"PETROL"
}
4)UPDATE(PUT): used same as POST
5)DELETE:
URL:http://localhost:8080/car/1
VALIDATIONS
This APP has some validations like:
a) car name should not contain spaces and special characters. b) car should not be more than 15 years old c) fuelType cannot be other than {PETROL/DIESEL/ELECTRIC}
HOW TO TEST CACHING:
This APP has enabled caching at getAllCars service.
@Cacheable("allCars") public List getAllCars() { List cars = new ArrayList();......
AND Cache evicts once there is an update or New entry:
@CacheEvict(allEntries = true) public void saveOrUpdate(Cars cars) {
carsRepository.save(cars);
}....
@CacheEvict(allEntries = true) public void update(Cars cars, int carid) {.....
Thread.sleep has been implemented for 5 seconds to demonstrate cache working.Once for first time data is retrieved from database<application waits for 5 seconds>. Post that same data would be retrieved from cache.
HOW TO TEST HYSTRIX:
This App has hystrix enabled at getCarById. Try fetching some data that does nnot exist.You should get a null object.
GET: http://localhost:8080/car/5
Output: { "carid": 0, "carname": null, "price": 0.0, "model": null, "yearOfManufacture": null, "fuelType": null }