@tooniez

Published

- 4 min read

Evaluating Air Quality with JUnit and RestAssured

img of Evaluating Air Quality with JUnit and RestAssured

Evaluating Air Quality with JUnit and RestAssured

In today’s world, air quality is a growing concern for many cities. As software developers and QA engineers, we can leverage APIs and testing frameworks to monitor and evaluate air quality data programmatically. In this post, we’ll explore how to use JUnit and RestAssured to create automated tests for air quality data from the OpenWeatherAPI.

Project Overview

We’ll be building a Maven project that uses RestAssured and JUnit to interact with the OpenWeatherAPI. Our main goals are to:

  1. Fetch weather data for specific cities
  2. Extract latitude and longitude coordinates
  3. Use these coordinates to get air quality information
  4. Evaluate the Air Quality Index (AQI) against a threshold

You can find the complete source code for this project on GitHub.

Prerequisites

Before we dive in, make sure you have the following installed:

  • Java Development Kit (JDK)
  • Apache Maven
  • An OpenWeatherAPI key (sign up at OpenWeatherMap)

Project Structure

Our project will have the following key components:

  1. BaseTest.java: A base class for common test configurations
  2. CityProvider.java: A data provider for city information
  3. Endpoints.java: A class to manage API endpoints
  4. TestAirQuality.java: Our main test class

Let’s break down each component and see how they work together.

Base Test Configuration

We’ll start with a BaseTest class to handle common configurations:

   public class BaseTest {
    protected static Dotenv dotenv;
    protected static String API_KEY;

    @BeforeClass
    public static void setUp() {
        dotenv = Dotenv.load();
        API_KEY = dotenv.get("API_KEY");
    }
}

This class loads our API key from a .env file, ensuring we keep sensitive information out of our codebase.

City Data Provider

Next, we’ll create a CityProvider to supply our tests with city data:

   public class CityProvider {
    public static List<String> getCities() {
        return Arrays.asList("Melbourne", "Manchester");
    }
}

This simple provider allows us to easily add or remove cities from our tests.

API Endpoints

We’ll manage our API endpoints in a separate class:

   public class Endpoints extends Base {
    public static String getWeatherEndpoint(String city, String apiKey) {
        return "/weather?q=" + city + "&appid=" + apiKey;
    }

    public static String getAirPollutionEndpoint(double lat, double lon, String apiKey) {
        return "/air_pollution/forecast?lat=" + lat + "&lon=" + lon + "&appid=" + apiKey;
    }
}

This class provides methods to construct the appropriate URLs for weather and air pollution data.

Main Test Class

Now, let’s look at our main test class, TestAirQuality:

   public class TestAirQuality extends BaseTest {

    @Test
    public void testAirQualityForCities() {
        for (String city : CityProvider.getCities()) {
            // Step 1: Get coordinates for the city
            Response weatherResponse = given()
                .when()
                .get(Endpoints.getWeatherEndpoint(city, API_KEY))
                .then()
                .statusCode(200)
                .extract().response();

            double lat = weatherResponse.path("coord.lat");
            double lon = weatherResponse.path("coord.lon");

            System.out.println("Coordinates for " + city + ": " + lat + ", " + lon);

            // Step 2: Get air quality data using coordinates
            Response airQualityResponse = given()
                .when()
                .get(Endpoints.getAirPollutionEndpoint(lat, lon, API_KEY))
                .then()
                .statusCode(200)
                .extract().response();

            int aqi = airQualityResponse.path("list[0].main.aqi");
            System.out.println("AQI for " + city + ": " + aqi);

            // Step 3: Evaluate AQI
            if (aqi >= 2) {
                System.out.println("City with AQI 2 or above: " + city + ", AQI: " + aqi);
            } else {
                System.out.println("City with AQI below 2: " + city + ", AQI: " + aqi);
            }
        }
    }
}

This test method does the following for each city:

  1. Fetches weather data to get coordinates
  2. Uses these coordinates to fetch air quality data
  3. Evaluates the Air Quality Index (AQI) against a threshold of 2

Running the Tests

To run the tests, you can use the following Maven command:

   mvn test

The output will show the coordinates, AQI, and evaluation for each city.

Conclusion

In this post, we’ve explored how to use JUnit and RestAssured to create automated tests for air quality data. This approach allows us to:

  • Easily test multiple cities
  • Chain API calls to get the data we need
  • Evaluate air quality based on specific criteria

By automating these tests, we can quickly and reliably monitor air quality across different locations, helping to identify areas that may need attention or further investigation.

Remember to check out the full project on GitHub for more details and to see how you can extend this concept for your own testing needs.

Happy testing, and here’s to cleaner air for all!