This is a Spring Boot REST API to fetch and store weather information for a particular day and pincode. The system:
- Retrieves latitude and longitude for a given pincode using the OpenWeather Geocoding API.
- Fetches weather information based on latitude and longitude using the OpenWeather API.
- Saves the pincode's latitude, longitude, and weather information in a MySQL database for optimized subsequent calls.
- Input: Pincode (e.g.,
411014) and date (e.g.,2020-10-15). - Storage:
- Stores latitude and longitude of the pincode.
- Caches weather information to reduce external API calls.
- Optimization:
- If weather data for a pincode and date is already present, it fetches from the database.
- External API calls are minimized.
- Testing: The API has been tested using Postman.
- Postman Collection Documentation: View Collection
- Java 17+
- Maven 3.6+
- MySQL 8.0+
- Postman (for testing)
- Sign up on OpenWeather to get an API key.
- Add your API key in
Constantsclass forGEOCODING_API_URLandWEATHER_API_URL.
git clone <repository-url>
cd weather-information-api-
Update
application.propertieswith your MySQL credentials:spring.datasource.url=jdbc:mysql://localhost:3306/weather_api_db spring.datasource.username=your_username spring.datasource.password=your_password
-
Create the database manually:
CREATE DATABASE weather_api_db;
- Open
Constants.javaand update:public static final String GEOCODING_API_URL = "https://api.openweathermap.org/geo/1.0/zip?zip=%s,IN&appid={YOUR_API_KEY}"; public static final String WEATHER_API_URL = "https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid={YOUR_API_KEY}";
mvn clean installmvn spring-boot:run- The application will run on
http://localhost:8080/api/v1.
- Endpoint:
/weather - Method:
POST - Description: Fetches weather data for a given pincode and date.
- Request Params:
pincode(String): Pincode for the location (e.g.,411014).forDate(LocalDate): Date for which weather is requested (e.g.,2020-10-15).
- Sample Request:
curl -X POST "http://localhost:8080/api/v1/weather" \ -d "pincode=411014" \ -d "forDate=2020-10-15"
| Field | Type | Description |
|---|---|---|
id |
BIGINT (PK) | Auto-generated ID |
pincode |
VARCHAR(255) | Pincode |
latitude |
DOUBLE | Latitude |
longitude |
DOUBLE | Longitude |
| Field | Type | Description |
|---|---|---|
id |
BIGINT (PK) | Auto-generated ID |
location_id |
BIGINT (FK) | References Location table |
forDate |
DATE | Weather date |
temperatureCurrent |
DOUBLE | Current temperature (Celsius) |
temperatureFeelsLike |
DOUBLE | Feels-like temperature (Celsius) |
humidity |
INT | Humidity (%) |
weatherInfo |
JSON | Raw weather info JSON |
- Open Postman Collection.
- Update variables (
baseURL, API key) if necessary. - Test the
/weatherendpoint with required parameters.
- Optimized Caching: Weather data is stored in the database to avoid repeated API calls.
- Error Handling: Comprehensive error handling for API failures and database issues.
- Modular Design: Service and repository layers for maintainability.
Our project incorporates JUnit for comprehensive testing and follows a Test-Driven Development (TDD) methodology to ensure code reliability, maintainability, and alignment with project requirements.
- JUnit: For writing and running test cases.
- Mockito: For mocking dependencies and simulating behaviors in unit tests.
- AssertJ: For fluent and expressive assertions.
- H2 Database: For in-memory integration testing with a clean, isolated database state.
-
Unit Tests:
- Focused on individual components like services and repositories.
- Mocked external dependencies using
Mockitoto test isolated logic. - Example:
WeatherServiceImplTestverifies weather data retrieval with and without cached data.
-
Integration Tests:
- Validated end-to-end functionality of components interacting with each other.
- Used the H2 in-memory database to simulate real database behavior.
- Example:
LocationRepositoryTestandWeatherDataRepositoryTesttest CRUD operations and custom queries.
-
Test Coverage:
- Comprehensive coverage of all major components.
- Assured validation logic, database interactions, and service flows are functioning as expected.
A Maven project structure with the appropriate placement of test files:
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ ├── controller
│ │ ├── service
│ │ ├── repository
│ │ ├── model
│ │ └── config
│ └── resources
│ └── application.properties
│
└── test
└── java
└── com
└── example
├── service
│ └── WeatherServiceImplTest.java
└── repository
├── LocationRepositoryTest.java
└── WeatherDataRepositoryTest.java
- The
application.propertiesfile ensures isolated testing with in-memory or dedicated test databases:spring.datasource.url=jdbc:h2:mem:testdb spring.jpa.hibernate.ddl-auto=create-drop spring.datasource.driver-class-name=org.h2.Driver spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
To run all test cases:
mvn clean test