Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions spring-boot-modules/spring-boot-openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<version>${openapi-generator.version}</version>
<executions>
<execution>
<id>generate-quotes-api</id>
<goals>
<goal>generate</goal>
</goals>
Expand All @@ -80,13 +81,40 @@
<modelPackage>com.baeldung.tutorials.openapi.quotes.api.model</modelPackage>
<documentationProvider>source</documentationProvider>
</configOptions>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
</configuration>
</execution>
<execution>
<id>generate-weather-api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api/weatherapi.yaml</inputSpec>
<generatorName>spring</generatorName>
<configOptions>
<openApiNullable>false</openApiNullable>
<useJakartaEe>false</useJakartaEe>
<annotationLibrary>none</annotationLibrary>
<documentationProvider>none</documentationProvider>
<apiPackage>com.baeldung.tutorials.openapi.generatehttpclients.api</apiPackage>
<modelPackage>com.baeldung.tutorials.openapi.generatehttpclients.api.model</modelPackage>
</configOptions>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.tutorials.openapi.generatehttpclients.GenerateHttpClientSpringApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.tutorials.openapi.generatehttpclients;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GenerateHttpClientSpringApplication {

public static void main(String[] args) {
SpringApplication.run(GenerateHttpClientSpringApplication.class, args);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.tutorials.openapi.generatehttpclients.service;

import org.springframework.stereotype.Service;

import com.baeldung.tutorials.openapi.generatehttpclients.api.WeatherApi;
import com.baeldung.tutorials.openapi.generatehttpclients.api.model.WeatherResponse;

@Service
public class GetWeatherService {

private final WeatherApi weatherApi;

public GetWeatherService(WeatherApi weatherApi) {
this.weatherApi = weatherApi;
}

public WeatherResponse getCurrentWeather(String city, String units) {
var response = weatherApi.getCurrentWeather(city, units);

if (response.getStatusCodeValue() < 399) {
return response.getBody();
}

throw new RuntimeException("Failed to get current weather for " + city);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
openapi: 3.0.3
info:
title: Current Weather API
description: |
Get real-time weather information for cities worldwide.
version: 1.0.0

paths:
/weather:
get:
summary: Get current weather data
description: Retrieve current weather information for a specified city
operationId: getCurrentWeather
parameters:
- name: city
in: query
required: true
schema:
type: string
- name: units
in: query
required: false
schema:
type: string
enum: [ celsius, fahrenheit ]
default: celsius
responses:
'200':
description: Successful weather data retrieval
content:
application/json:
schema:
$ref: '#/components/schemas/WeatherResponse'
'404':
description: City not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'

components:
schemas:
WeatherResponse:
type: object
required:
- location
properties:
current:
type: object
required:
- temperature
- units
properties:
temperature:
type: number
format: double
units:
type: string
enum: [ celsius, fahrenheit ]
timestamp:
type: string
format: date-time

ErrorResponse:
type: object
required:
- code
- message
properties:
code:
type: string
message:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
logging:
level:
root: INFO
org.springframework: INFO
org.springframework: INFO

openapi:
currentWeather:
base-path: https://localhost:8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.baeldung.tutorials.openapi.generatehttpclients;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.baeldung.tutorials.openapi.generatehttpclients.api.WeatherApi;
import com.baeldung.tutorials.openapi.generatehttpclients.api.model.WeatherResponse;
import com.baeldung.tutorials.openapi.generatehttpclients.api.model.WeatherResponseCurrent;
import com.baeldung.tutorials.openapi.generatehttpclients.service.GetWeatherService;

@ExtendWith(MockitoExtension.class)
class WeatherApiUnitTest {

private GetWeatherService weatherService;

@Mock
private WeatherApi weatherApi;

@BeforeEach
void setUp() {
weatherService = new GetWeatherService(weatherApi);
}

@Test
void givenOkStatus_whenCallingWeatherApi_thenReturnCurrentWeather() {
when(weatherApi.getCurrentWeather("London", "celsius")).thenReturn(
new ResponseEntity<>(new WeatherResponse().current(new WeatherResponseCurrent(455d, WeatherResponseCurrent.UnitsEnum.CELSIUS)), HttpStatus.OK));

var weather = weatherService.getCurrentWeather("London", "celsius");

assertThat(weather.getCurrent()
.getTemperature()).isEqualTo(455d);
assertThat(weather.getCurrent()
.getUnits()).isEqualTo(WeatherResponseCurrent.UnitsEnum.CELSIUS);
}

@Test
void givenBadRequestStatus_whenCallingWeatherApi_thenReturnError() {
when(weatherApi.getCurrentWeather("London", "celsius")).thenReturn(new ResponseEntity<>(HttpStatus.BAD_REQUEST));

assertThrows(RuntimeException.class, () -> weatherService.getCurrentWeather("London", "celsius"));
}
}