https://github.com/saravanan81java/blind-auction-system
Blind auction system
https://github.com/saravanan81java/blind-auction-system
java jpa jpa-hibernate repository springboot
Last synced: 3 months ago
JSON representation
Blind auction system
- Host: GitHub
- URL: https://github.com/saravanan81java/blind-auction-system
- Owner: saravanan81java
- Created: 2024-10-27T01:50:37.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-21T23:43:38.000Z (11 months ago)
- Last Synced: 2025-02-22T00:26:32.592Z (11 months ago)
- Topics: java, jpa, jpa-hibernate, repository, springboot
- Language: Java
- Homepage:
- Size: 74.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Blind auction system
# Project Basic Structure
The folder structure of the Blind Auction System repository is as follows:
```
root
├── user-service
│ ├── src
│ │ └── main
│ │ └── java/com/yourcompany/users
│ │ └── UserController.java
│ │ └── User.java
│ │ └── UserRepository.java
│ └── pom.xml
│
├── auction-service
│ ├── src
│ │ └── main
│ │ └── java/com/yourcompany/auctions
│ │ └── AuctionController.java
│ │ └── ProductController.java
│ │ └── Auction.java
│ │ └── Product.java
│ │ └── Bid.java
│ │ └── AuctionRepository.java
│ │ └── BidRepository.java
│ │ └── ProductRepository.java
│ └── pom.xml
│
└── pom.xml
```
This structure includes two services: user-service and auction-service, each with its respective Java files and Maven configuration (pom.xml).
# Run the Services
1) Start the user-service:
```
cd user-service
mvn spring-boot:run
```
2) Start the auction-service:
```
cd ../auction-service
mvn spring-boot:run
```
Now, both services are up and running. You can interact with the API endpoints (e.g., /auctions/ping, /users/ping, /users/validate) using tools like Postman or curl.
API Details and Endpoints explanation:
# Auction Service
1) createAuction:
- Validates the seller's token by calling the User Service.
- If validation passes, saves the auction details and sets the seller’s token.
2) placeBid:
- Validates the buyer’s token.
- Ensures that the bid amount meets the minimum bid price.
- Associates the bid with the auction and saves it.
3) getAllAuctions:
- Returns a list of all auctions available for bidding.
4) endAuction:
- Ends the auction by finding the highest bid.
- In case of a tie on the bid amount, the earliest bid (timestamp) is selected as the winner.
5) validateUserToken:
- Calls the User Service to validate user tokens by role type (buyer or seller).
- Throws an error if the token is invalid or the validation service fails.
6) Auctions Endpoints:
- /auctions/register : Creates a new auction with a seller’s token and minimum bid requirement.
- /auctions/list : Lists all open auctions.
- /auctions/{auctionId}/bid : Places a bid on an auction if the bid meets the minimum amount and the auction is open.
- /auctions/{auctionId}/end : Closes the auction and returns the winning bid, if any. The first highest bid in descending order is considered the winner.
8) getAllProducts:
- Returns a list of all products.
Notes
- The RestTemplate in validateUserToken simulates a call to the UserService to verify tokens.
- Error Handling: Error messages are thrown if auction or bid validation fails.
- Comparators: endAuction selects the highest bid and resolves ties by timestamp.
# User Service
1) Token Map:
- userTokens is a Map that simulates a database of user tokens
mapped to user types (e.g., "seller" or "buyer").
- For this example, tokens are pre-populated. In a real application, these would likely be stored
in a database or managed by an authentication service.
2) validateUserToken Endpoint:
- URL: /users/validate
- Parameters:
- token: The token to validate.
- type: Expected type of user ("seller" or "buyer").
3) Validation Logic:
- Checks if the provided token exists in the userTokens map.
- Verifies that the token corresponds to the correct user type (buyer or seller).
4) Return:
- Returns "Valid User" if the token and type match.
- Returns "Invalid Token" if the token doesn’t exist in the map.
- Returns "Invalid User Type" if the token exists but is for the wrong user type.
# Sample Data Pre-population
To pre-populate the database, Add a CommandLineRunner bean to insert sample users and auction when the application starts:
@Bean
CommandLineRunner initDatabase(UserRepository userRepository) {
}
@Bean
CommandLineRunner initDatabase(ProductRepository productRepository, AuctionRepository auctionRepository) {
return args -> {
List listOfProducts = new ArrayList<>();
.....
productRepository.saveAll(listOfProducts);
auctionRepository.save(new Auction(....));
};
}
# Test Cases
User service API endpoints JUnit Test cases
- testGetPing(): Validates that the /ping endpoint returns the expected string.
- testValidateToken_ValidUser(): Mocks a valid user return from userService.validateToken() and checks if the response is "Valid User".
- testValidateToken_InvalidUser(): Mocks a null return from userService.validateToken() and verifies that the response is "Invalid User".
Make sure JUnit and Mockito dependencies to successfully execute these tests.
User service services layer JUnit Test cases
- testValidateToken_Valid(): Verifies that a valid token returns a non-null user.
- testValidateToken_Invalid(): Tests that an invalid token throws a RuntimeException with the message "Invalid token".
This structure uses Mockito to mock the UserRepository and validate the behavior of validateToken().
Auction service API endpoints JUnit Test cases
...