This is a Spring Boot application that showcases the case study. It provides endpoints to retrieve and sync instrument and market data from the Robinhood API.
A Postman collection is provided along with this documentation, which contains sample requests for testing the application's endpoints. Import the Postman collection and execute the requests to interact with the application. For sync requests, please refer to the database to verify that the data has been synced or check the console for the log.
The application consists of the following entities:
- Represents an instrument with its symbol, name, custom name, and associated market.
- The fields include:
id
: Unique identifier for the instrument.symbol
: Unique symbol of the instrument.name
: Name of the instrument.customName
: Custom name for the instrument.market
: Associated market for the instrument.
- Represents a DTO (Data Transfer Object) for the Instrument entity.
- The fields include:
id
: Unique identifier for the instrument.symbol
: Symbol of the instrument.name
: Name of the instrument.customName
: Custom name for the instrument.marketId
: ID of the associated market.
- Represents a market with its code, symbol, name, country, and website.
- The fields include:
MarketId
: Unique identifier for the market.Code
: Unique code of the market.Symbol
: Symbol of the market.Name
: Name of the market.Country
: Country of the market.Website
: Website of the market.
The application includes the following repositories:
- Provides CRUD operations for the Instrument entity.
- Includes the following methods:
findBySymbol(symbol)
: Retrieves an instrument by its symbol.findByMarket(market)
: Retrieves instruments associated with a specific market.
- Provides CRUD operations for the Market entity.
The application includes the following services:
- Handles business logic related to instruments.
- Includes the following methods:
syncInstruments()
: Synchronizes instruments by retrieving data from the Robinhood API and saving it to the database.getAllInstruments()
: Retrieves all instruments from the database.getInstrumentBySymbol(symbol)
: Retrieves an instrument by its symbol using cache for improved performance.
- Handles business logic related to markets.
- Includes the following methods:
syncMarkets()
: Synchronizes markets by retrieving data from the Robinhood API and saving it to the database.
The application includes the following controllers:
- Exposes endpoints related to instruments.
- Includes the following routes:
GET /instruments/sync
: Syncs instruments by calling thesyncInstruments()
method of theInstrumentService
.GET /instruments/getall
: Retrieves all instruments by calling thegetAllInstruments()
method of theInstrumentService
.GET /instruments/{symbol}
: Retrieves an instrument by its symbol by calling thegetInstrumentBySymbol(symbol)
method of theInstrumentService
.
- Exposes endpoints related to markets.
- Includes the following route:
GET /markets/sync
: Syncs markets by calling thesyncMarkets()
method of theMarketService
.
The application includes the following configuration:
- Configures caching for instrument retrieval.
- Includes a cache manager bean that utilizes the
SimpleCacheManager
and sets up a cache named "instruments" with a cache expiration time of 1 hour.
The application properties include the configuration for the database connection:
spring.datasource.url=jdbc:postgresql://localhost:5436/dbname
spring.datasource.username=postgres
spring.datasource.password=123
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
Please ensure that you have a PostgreSQL database running on localhost:5436
with the specified credentials and database name.
To run the Spring Boot application, follow these steps:
- Ensure that you have a PostgreSQL database running on
localhost:5436
with the specified credentials and database name. - Import the data on seed.sql into your empty database.
- Build the application using Maven:
mvn clean install
. - Run the application:
java -jar target/appname-0.0.1-SNAPSHOT.jar
. - The application will be accessible at
http://localhost:8080
. - Import the Postman collection and execute the requests to interact with the application.
Note: In order to use instrument sync endpoint, you have to insert the seed.sql into the database. The seed.sql file is located in the main folder. This is due to sync endpoint fetching only instruments available in the database. This is due to Robinhood API restrictions. Since the API does not provide a way to fetch all instruments, we have to fetch them one by one. This is why we have to insert the seed.sql into the database to get instrument symbols one by one.
GET /instruments/sync
: Synchronizes instruments by retrieving data from the Robinhood API and saving it to the database.GET /instruments/getall
: Retrieves all instruments.GET /instruments/{symbol}
: Retrieves an instrument by its symbol.GET /markets/sync
: Synchronizes markets by retrieving data from the Robinhood API and saving it to the database.
The application uses the following dependencies:
- Spring Boot Starter Data JPA
- Spring Boot Starter Web
- Spring Boot Starter Webflux
- PostgreSQL Driver
- Lombok
- Spring Boot Starter Test
- Jakarta Persistence API
- Spring Boot Starter Cache
Make sure to include these dependencies in your pom.xml
file.
The application includes unit tests to ensure the correctness of the implemented functionality. The tests cover various scenarios, including repository, service, and controller tests.
The application includes unit tests to ensure the correctness of repository methods and service functionality:
- InstrumentRepositoryTest: Tests the repository methods for retrieving instruments.
- MarketRepositoryTest: Tests the repository methods for retrieving markets.
- InstrumentServiceTest: Tests the service methods for retrieving instruments.
- MarketServiceTest: Tests the service methods for syncing markets.
Please note that the Dockerization of this application was attempted, but encountered an error when the application container tried to connect to the PostgreSQL database. As a result, it was not possible to fully test the functionality of the Dockerized application.
To build and run the Dockerized application, use the following commands:
docker build -t appname .
docker run -d -p 8080:8080 --name=testapp_con appname