diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 974a18b..bcb7f3e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,32 +1,68 @@ -# GitHub Actions workflow for building and running a Java application -# Triggers on push to the repository name: A workflow for my Hello World App on: - push: # Runs the workflow when changes are pushed - + push: + branches: + - develop jobs: - build: # Job for building application - name: Hello world action + UnitTests: + name: Unit Tests runs-on: ubuntu-20.04 - steps: - - name: Checkout # Check out the code + - name: Checkout uses: actions/checkout@v2 with: - submodules: recursive # Includes submodules - - # Install JDK 11 + submodules: recursive - name: Set up JDK 11 uses: actions/setup-java@v2 with: java-version: '11' distribution: 'adopt' - - name: Unit Tests - run: mvn -Dtest=com.napier.sem.AppTest test - - - name: Build with Maven # Build the application - run: mvn package -DskipTests # Compiles and packages the application + run: mvn -Dtest='com.napier.sem.AppTest,com.napier.sem.CityTest,com.napier.sem.CountryLanguageTest,com.napier.sem.CountryTest,com.napier.sem.DatabaseTest' test - - name: Run docker compose # Run Docker Compose - run: docker compose up --abort-on-container-exit # Starts services and aborts on container exit + IntegrationTests: + name: Integration Tests + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: recursive + - name: Set up JDK 11 + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + - name: Integration Tests and CodeCov + run: | + docker build -t database ./db + docker run --name world -dp 33060:3306 database + mvn -Dtest=com.napier.sem.AppIntegrationTest test + docker stop world + docker rm world + docker image rm database + - name: CodeCov + uses: codecov/codecov-action@v4.0.1 + with: + token: ${{ secrets.CODECOV_TOKEN }} # now required for public repos too + directory: ./target/site/jacoco + flags: Integration Tests # optional + verbose: true # optional (default = false) + slug: NikitaEdin/SET08103_2024-5 + build: + name: Build and Start Using docker compose + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: recursive + - name: Set up JDK 11 + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + - name: Package and Run docker compose + run: | + mvn package -DskipTests + docker compose up --abort-on-container-exit diff --git a/Dockerfile b/Dockerfile index f29f752..f3012cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ # Use the latest OpenJDK image FROM openjdk:latest # Copy the packaged JAR file from local machine to the container -COPY ./target/seMethods-0.2.0.1-jar-with-dependencies.jar /tmp +COPY ./target/seMethods-0.3.0.0.jar /tmp # Makes tmp directory the working directory WORKDIR /tmp # Command to run the jarfile that was copied earlier -ENTRYPOINT ["java", "-jar", "seMethods-0.2.0.1-jar-with-dependencies.jar"] \ No newline at end of file +ENTRYPOINT ["java", "-jar", "seMethods-0.3.0.0.jar", "db:3306", "10000"] \ No newline at end of file diff --git a/README.md b/README.md index 96e977b..c62e264 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ Napier University Team project for SET08103 module 2024/2025 * Release ![GitHub Release](https://img.shields.io/github/v/release/nikitaedin/SET09103_2024-5?include_prereleases) +* Codecov Coverage [![codecov](https://codecov.io/gh/NikitaEdin/SET08103_2024-5/branch/develop/graph/badge.svg?token=380HR1UHWD)](https://codecov.io/gh/NikitaEdin/SET08103_2024-5) + ## Team Members - **Stephen Banks** - **Rachael Banks** diff --git a/docker-compose.yml b/docker-compose.yml index ce6c047..8476186 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,10 +4,10 @@ services: app: build: . - # db is db folder + # db is is db folder db: build: db/. command: --default-authentication-plugin=mysql_native_password restart: always ports: - - "3306:3306" \ No newline at end of file + - "33060:3306" diff --git a/pom.xml b/pom.xml index 4b7c3ed..afd273a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.napier.sem seMethods - 0.2.0.1 + 0.3.0.0 @@ -61,6 +61,7 @@ jar-with-dependencies + false @@ -90,6 +91,26 @@ + + + org.jacoco + jacoco-maven-plugin + 0.8.2 + + + + prepare-agent + + + + report + test + + report + + + + diff --git a/src/main/java/com/napier/sem/App.java b/src/main/java/com/napier/sem/App.java index b2c85b1..ac605f9 100644 --- a/src/main/java/com/napier/sem/App.java +++ b/src/main/java/com/napier/sem/App.java @@ -4,6 +4,9 @@ import java.util.ArrayList; import java.util.List; +/** + * This is class is to run the program and print the reports that are needed. + */ public class App { /** @@ -14,24 +17,37 @@ public static void main(String[] args) { // Create new Application App a = new App(); - - - // Connect to database - a.connect(); + if (args.length < 1) { + a.connect("localhost:33060", 10000); + } else { + a.connect(args[0], Integer.parseInt(args[1])); + } // ######## REPORTS BEGIN HERE ######## /// + generateAllReports(a); + // Disconnect from database before termination + a.disconnect(); + } - ////// Testing Data and Examples ////// - + public static void generateAllReports(App a){ + // World Reports + System.out.println("\nreport_PopulationDESC: "); + print_Items(a.report_PopulationDESC()); + System.out.println("\nreport_PopulationByContinentDESC: "); + print_Items(a.report_PopulationByContinentDESC("Asia")); + // report_CountriesByRegionDESC + System.out.println("\nreport_CountriesByRegionDESC: "); + print_Items(a.report_CountriesByRegionDESC("South America")); + + // TopN Reports System.out.println("\nreport_TopN_PopulatedCountries: "); print_Items(a.report_TopN_PopulatedCountries(3)); System.out.println("\nreport_TopN_PopulatedCountriesByContinent: "); print_Items(a.report_TopN_PopulatedCountriesByContinent("North America", 4)); System.out.println("\nreport_TopN_PopulatedCountriesByRegion: "); print_Items(a.report_TopN_PopulatedCountriesByRegion("Central Africa", 5)); - System.out.println("\nreport_TopN_PopulatedCities: "); print_Items(a.report_TopN_PopulatedCities(3)); System.out.println("\nreport_TopN_PopulatedCitiesByContinent: "); @@ -43,6 +59,7 @@ public static void main(String[] args) { System.out.println("\nreport_TopN_PopulatedCitiesByDistrict: "); print_Items(a.report_TopN_PopulatedCitiesByDistrict("Western", 6)); + // City Reports System.out.println("\nreport_CitiesInWorldDESC: "); print_Items(a.report_CitiesInWorldDESC()); System.out.println("\nreport_CitiesInContinentDESC: "); @@ -53,21 +70,14 @@ public static void main(String[] args) { print_Items(a.report_CitiesInCountryDESC("Angola")); System.out.println("\nreport_CitiesInDistrictDESC: "); print_Items(a.report_CitiesInDistrictDESC("Scotland")); - System.out.println("\nreport_CapitalCitiesInWorldDESC: "); print_Items_Capitals(a.report_CapitalCitiesInWorldDESC()); System.out.println("\nreport_CapitalCitiesInContinentDESC: "); print_Items_Capitals(a.report_CapitalCitiesInContinentDESC("North America")); System.out.println("\nreport_CapitalCitiesInRegionDESC: "); print_Items_Capitals(a.report_CapitalCitiesInRegionDESC("Western Europe")); - - - - // Disconnect from database before termination - a.disconnect(); } - ///////////////////// REPORTS ///////////////////// // @@ -87,9 +97,15 @@ public List report_PopulationDESC() { * @return Return a list of Country type in DESC order */ public List report_PopulationByContinentDESC(String continent) { - if(continent.isEmpty()) return null; - String query = "SELECT * FROM country WHERE continent = '" + continent + "' ORDER BY population DESC"; - return getReport_Country(query); + if(continent != null && !continent.isEmpty()){ + String query = "SELECT * FROM country WHERE continent = '" + continent + "' ORDER BY population DESC"; + return getReport_Country(query); + } + else{ + System.out.println("Invalid continent"); + return null; + } + } /** @@ -98,9 +114,12 @@ public List report_PopulationByContinentDESC(String continent) { * @return Returns a list of Country type in DESC order */ public List report_CountriesByRegionDESC(String region) { - if(region.isEmpty()) return null; - String query = "SELECT * FROM country WHERE region = '"+ region +"' ORDER BY population DESC"; - return getReport_Country(query); + if (region !=null && !region.isEmpty()){ + String query = "SELECT * FROM country WHERE region = '"+ region +"' ORDER BY population DESC"; + return getReport_Country(query); + } + System.out.println("Invalid region"); + return null; } @@ -121,7 +140,7 @@ public List report_TopN_PopulatedCountries(int N){ * @return List of top N populated countries in the specified continent */ public List report_TopN_PopulatedCountriesByContinent(String continent, int N) { - if( N < 1 || continent.isEmpty()) return null; + if( N < 1 || continent == null || continent.isEmpty()) return null; String query = "SELECT * FROM country WHERE continent = '"+ continent +"' ORDER BY population DESC LIMIT "+ N; return getReport_Country(query); } @@ -133,7 +152,7 @@ public List report_TopN_PopulatedCountriesByContinent(String continent, * @return A list of top N populated countries in the specified region */ public List report_TopN_PopulatedCountriesByRegion(String region, int N) { - if( N < 1 || region.isEmpty()) return null; + if( N < 1 || region == null || region.isEmpty()) return null; String query = "SELECT * FROM country WHERE region = '" + region + "' ORDER BY population DESC LIMIT "+ N; return getReport_Country(query); } @@ -156,7 +175,7 @@ public List report_TopN_PopulatedCities(int N) { * @return List of top N populated cities in the specified continent, sorted in descending order by population */ public List report_TopN_PopulatedCitiesByContinent(String continent, int N) { - if (N < 1 || continent.isEmpty()) return null; + if (N < 1 || continent == null || continent.isEmpty()) return null; String query = "SELECT city.* FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Continent = '" + continent + "' ORDER BY city.Population DESC LIMIT " + N; return getReport_City(query); } @@ -169,7 +188,7 @@ public List report_TopN_PopulatedCitiesByContinent(String continent, int N * @return List of top N populated cities in specified region, sorted in descending order by population */ public List report_TopN_PopulatedCitiesByRegion(String region, int N) { - if (N < 1 || region.isEmpty()) return null; + if (N < 1 || region == null || region.isEmpty()) return null; String query = "SELECT city.* FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Region = '" + region + "' ORDER BY city.Population DESC LIMIT " + N; return getReport_City(query); } @@ -181,7 +200,7 @@ public List report_TopN_PopulatedCitiesByRegion(String region, int N) { * @return List of top N populated cities in specified country, sorted in descending order by population */ public List report_TopN_PopulatedCitiesByCountry(String country, int N) { - if (N < 1 || country.isEmpty()) return null; + if (N < 1 || country == null || country.isEmpty()) return null; String query = "SELECT city.* FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Name = '" + country + "' ORDER BY city.Population DESC LIMIT " + N; return getReport_City(query); } @@ -193,7 +212,7 @@ public List report_TopN_PopulatedCitiesByCountry(String country, int N) { * @return List of top N populated cities in specified district, sorted in descending order by population */ public List report_TopN_PopulatedCitiesByDistrict(String district, int N) { - if (N < 1 || district.isEmpty()) return null; + if (N < 1 || district == null || district.isEmpty()) return null; String query = "SELECT * FROM city WHERE District = '" + district + "' ORDER BY Population DESC LIMIT " + N; return getReport_City(query); } @@ -213,7 +232,7 @@ public List report_CitiesInWorldDESC() { * @return List of cities in specified continent, sorted by descending order by population. */ public List report_CitiesInContinentDESC(String continent) { - if(continent.isEmpty()) return null; + if(continent == null || continent.isEmpty()) return null; String query = "SELECT * FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Continent = '" + continent + "' ORDER BY city.population DESC"; return getReport_City(query); } @@ -224,7 +243,7 @@ public List report_CitiesInContinentDESC(String continent) { * @return List of cities in specified region, sorted by descending order by population. */ public List report_CitiesInRegionDESC(String region) { - if(region.isEmpty()) return null; + if(region == null || region.isEmpty()) return null; String query = "SELECT * FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Region = '" + region + "' ORDER BY city.population DESC"; return getReport_City(query); } @@ -235,7 +254,7 @@ public List report_CitiesInRegionDESC(String region) { * @return List of cities in the specified country, sorted by descending order by population. */ public List report_CitiesInCountryDESC(String country) { - if(country.isEmpty()) return null; + if(country == null || country.isEmpty()) return null; String query = "SELECT * FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Name = '" + country + "' ORDER BY city.population DESC"; return getReport_City(query); } @@ -246,7 +265,7 @@ public List report_CitiesInCountryDESC(String country) { * @return List of cities in the specified country, sorted by descending order by population. */ public List report_CitiesInDistrictDESC(String district) { - if(district.isEmpty()) return null; + if(district == null || district.isEmpty()) return null; String query = "SELECT * FROM city WHERE District = '" + district + "' ORDER BY city.population DESC"; return getReport_City(query); } @@ -266,7 +285,7 @@ public List report_CapitalCitiesInWorldDESC() { * @return List of capital cities in the specified continent, sorted by descending order by population. */ public List report_CapitalCitiesInContinentDESC(String continent) { - if(continent.isEmpty()) return null; + if(continent == null || continent.isEmpty()) return null; String query = "SELECT * FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Continent = '" + continent + "' AND country.Capital = city.ID ORDER BY city.population DESC"; return getReport_City(query); } @@ -277,11 +296,42 @@ public List report_CapitalCitiesInContinentDESC(String continent) { * @return List of capital cities in the specified region, sorted by descending order by population. */ public List report_CapitalCitiesInRegionDESC(String region) { - if(region.isEmpty()) return null; - String query = "SELECT * FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Region = '" + region + "' AND country.Capital = city.ID ORDER BY city.population DESC"; - return getReport_City(query); + if (region != null && !region.isEmpty()){ + String query = "SELECT * FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Region = '" + region + "' AND country.Capital = city.ID ORDER BY city.population DESC"; + return getReport_City(query); + } + System.out.println("Invalid Region"); + return null; } + // Reports to be implemented + +// public List report_TopN_PopulatedCapitalCitiesInWorld(int N) {} +// +// public List report_TopN_PopulatedCapitalCitiesInContinent(int N, String continent) {} +// +// public List report_TopN_PopulatedCapitalCitiesInRegion(int N, String region) {} +// +// public void report_PopulationBreakdown_AllContinents() {} +// +// public void report_PopulationBreakdown_AllRegions() {} +// +// public void report_PopulationBreakdown_AllCountries() {} +// +// public void report_TotalPopulation_World() {} +// +// public void report_TotalPopulation_Continent(String continent) {} +// +// public void report_TotalPopulation_Region(String region) {} +// +// public void report_TotalPopulation_Country(String country) {} +// +// public void report_TotalPopulation_District(String district) {} +// +// public void report_TotalPopulation_City(String city) {} +// +// public void report_WorldLanguagesBreakdown() {} + // ///////////////////// UTILS AND DATABASE CONNECTIONS ///////////////////// // @@ -456,33 +506,46 @@ public static void setConnection(Connection connection) { /** * Connect to the MySQL database. */ - public void connect() { - try { - // Load Database driver - Class.forName("com.mysql.cj.jdbc.Driver"); - } catch (ClassNotFoundException e) { - System.out.println("Could not load SQL driver"); - System.exit(-1); - } - - int retries = 10; - for (int i = 0; i < retries; ++i) { - System.out.println("Connecting to database..."); + public void connect(String location, int delay) { + if (location != null) { try { - // Wait a bit for db to start - Thread.sleep(10000); - // Connect to database - con = DriverManager.getConnection("jdbc:mysql://db:3306/world?useSSL=false", "root", "example"); - System.out.println("Successfully connected"); - break; - } catch (SQLException sqle) { - System.out.println("Failed to connect to database attempt " + Integer.toString(i)); - System.out.println(sqle.getMessage()); + // Load Database driver + Class.forName("com.mysql.cj.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("Could not load SQL driver"); + System.exit(-1); } - catch (InterruptedException ie) { - System.out.println("Thread interrupted? Should not happen."); + + int retries = 10; + boolean shouldWait = false; + for (int i = 0; i < retries; ++i) { + System.out.println("Connecting to database..."); + try { + if (shouldWait) { + // Wait a bit for db to start + Thread.sleep(delay); + } + + // Connect to database + con = DriverManager.getConnection("jdbc:mysql://" + location + + "/world?allowPublicKeyRetrieval=true&useSSL=false", + "root", "example"); + System.out.println("Successfully connected"); + break; + } catch (SQLException sqle) { + System.out.println("Failed to connect to database attempt " + i); + System.out.println(sqle.getMessage()); + + // Let's wait before attempting to reconnect + shouldWait = true; + } catch (InterruptedException ie) { + System.out.println("Thread interrupted? Should not happen."); + } } } + else { + System.out.println("Failed to connect to database"); + } } /** diff --git a/src/main/java/com/napier/sem/City.java b/src/main/java/com/napier/sem/City.java index 2dda631..1ca6312 100644 --- a/src/main/java/com/napier/sem/City.java +++ b/src/main/java/com/napier/sem/City.java @@ -45,6 +45,30 @@ public int getID() { return ID; } + /** + * Method to retrieve the Name of the city. + * @return Name of the City. + */ + public String getName() {return Name;} + + /** + * Method to retrieve the Country Code of the object. + * @return Country Code of the object. + */ + public String getCountryCode() {return CountryCode;} + + /** + * Method to retrieve the District of the object. + * @return District of the object. + */ + public String getDistrict() {return District;} + + /** + * Method to retrieve the Population of the object. + * @return Population of the object. + */ + public int getPopulation() {return Population;} + /** * Returns a string representation of the City object. * diff --git a/src/main/java/com/napier/sem/Country.java b/src/main/java/com/napier/sem/Country.java index f15245d..310d0b2 100644 --- a/src/main/java/com/napier/sem/Country.java +++ b/src/main/java/com/napier/sem/Country.java @@ -98,6 +98,92 @@ public String getCode() { return Code; } + /** + * Method to retrieve the country name. + * @return Name of the country. + */ + public String getName() { + return Name; + } + + /** + * Method to retrieve the continent of a Country object. + * @return Continent of the object. + */ + public String getContinent() {return Continent;} + + /** + * Method to retrieve the region of a Country object. + * @return Region of the object. + */ + public String getRegion() {return Region;} + + /** + * Method to retrieve the surface area of a Country object. + * @return Surface area of the object. + */ + public double getSurfaceArea() {return SurfaceArea;} + + /** + * Method to retrieve the Independence year of a Country object. + * @return Independence year of the object. + */ + public Integer getIndepYear() {return IndepYear;} + + /** + * Method to retrieve the total population of this country. + * @return Number of population + */ + public int getPopulation() { return Population;} + + /** + * Method to retrieve the life expectancy of a Country object. + * @return Life expectancy of the object. + */ + public Double getLifeExpectancy() {return LifeExpectancy;} + + /** + * Method to retrieve the GNP of a Country object. + * @return GNP of the object. + */ + public Double getGNP() {return GNP;} + + /** + * Method to retrieve the old GNP of a Country object. + * @return Old GNP of the object. + */ + public Double getGNPOld() {return GNPOld;} + + /** + * Method to retrieve the local name of a Country object. + * @return Local name of the object. + */ + public String getLocalName() {return LocalName;} + + /** + * Method to retrieve the form of government of a Country object. + * @return Form of government of the object. + */ + public String getGovernmentForm() {return GovernmentForm;} + + /** + * Method to retrieve the head of state of a Country object. + * @return Head of state of the object. + */ + public String getHeadOfState() {return HeadOfState;} + + /** + * Method to retrieve the capital of a Country object. + * @return Capital of the object. + */ + public Integer getCapital() {return Capital;} + + /** + * Method to retrieve the second country code of a Country object. + * @return Second country code of the object. + */ + public String getCode2() {return Code2;} + /** * Returns a string representation of the Country object. * diff --git a/src/main/java/com/napier/sem/CountryLanguage.java b/src/main/java/com/napier/sem/CountryLanguage.java index e1d6eda..799f25d 100644 --- a/src/main/java/com/napier/sem/CountryLanguage.java +++ b/src/main/java/com/napier/sem/CountryLanguage.java @@ -32,6 +32,38 @@ public CountryLanguage(String CountryCode, String Language, String IsOfficial, d this.Percentage = Percentage; } + /** + * Method to retrieve the Country Code of a CountryLanguage object. + * @return Country Code of the object. + */ + public String getCountryCode() { + return CountryCode; + } + + /** + * Method to retrieve the Language of a CountryLanguage object. + * @return Language of the object. + */ + public String getLanguage() { + return Language; + } + + /** + * Method to retrieve if the language is official or not of a CountryLanguage object. + * @return If the language is official or not of the object. + */ + public String getIsOfficial() { + return IsOfficial; + } + + /** + * Method to retrieve the Percentage of a CountryLanguage object. + * @return Percentage of the object. + */ + public double getPercentage() { + return Percentage; + } + /** * Returns a string representation of the CountryLanguage object * diff --git a/src/test/java/com/napier/sem/AppIntegrationTest.java b/src/test/java/com/napier/sem/AppIntegrationTest.java new file mode 100644 index 0000000..794f322 --- /dev/null +++ b/src/test/java/com/napier/sem/AppIntegrationTest.java @@ -0,0 +1,665 @@ +package com.napier.sem; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class AppIntegrationTest { + static App app; + + /** + * Method to initialise required logic before the tests are executed + */ + @BeforeAll + static void init() { + app = new App(); + app.connect("localhost:33060", 30000); + } + + /** + * Tests the report_TopN_PopulatedCountries method to ensure it returns the top N populated Countries. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCountries(){ + List countries = app.report_TopN_PopulatedCountries(3); + + // Not Null + assertNotNull(countries, "The result should not be null"); + + // Exact value of items + assertEquals(3, countries.size(), "There should be 3 countries returned"); + + // Verify countries are in descending order + for (int i = 0; i < countries.size() - 1; i++) { + assertTrue(countries.get(i).Population >= countries.get(i + 1).Population, + "Countries should be ordered in descending population"); + } + } + + /** + * Test method TopN_PopulateCountries to check if it can handle a null parameter + */ + @Test + void test_nullInvalid_TopN_PopulatedCountries(){ + List countries = app.report_TopN_PopulatedCountries(0); + assertNull(countries, "The result should be null"); + countries = app.report_TopN_PopulatedCountries(-2); + assertNull(countries, "The result should be null"); + } + + /** + * Tests the report_TopN_PopulatedCountriesByContinent method to ensure it returns the top N populated Countries in a continent. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCountriesByContinent(){ + List countries = app.report_TopN_PopulatedCountriesByContinent("North America", 5); + + // Not Null + assertNotNull(countries, "The result should not be null"); + + // Exact value of items + assertEquals(5, countries.size(), "There should be 5 countries returned"); + + // Verify countries are in descending order + for (int i = 0; i < countries.size() - 1; i++) { + assertTrue(countries.get(i).Population >= countries.get(i + 1).Population, + "Countries should be ordered in descending population for the specified continent"); + } + } + + /** + * Test method TopN_PopulateCountriesByContinent to check if it can handle null parameters + */ + @Test + void test_nullEmpty_TopN_PopulatedCountriesByContinent(){ + List countries = app.report_TopN_PopulatedCountriesByContinent("",0); + assertNull(countries, "The result should be null"); + countries = app.report_TopN_PopulatedCountriesByContinent(null,0); + assertNull(countries, "The result should be null"); + } + + /** + * Test method for TopN_PopulatedCountriesByContinent to check if it can handle invalid inputs in its parameters + */ + @Test + void test_invalid_TopN_PopulatedCountriesByContinent(){ + List countries = app.report_TopN_PopulatedCountriesByContinent("Central Europe", -2); + assertNull(countries, "The result should be null"); + } + + /** + * Tests the report_TopN_PopulatedCountriesByRegion method to ensure it returns the top N populated Countries in a region. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCountriesByRegion(){ + List countries = app.report_TopN_PopulatedCountriesByRegion("Central Africa", 5); + + // Not Null + assertNotNull(countries, "The result should not be null"); + + // Exact value of items + assertEquals(5, countries.size(), "There should be 5 countries returned"); + + // Verify countries are in descending order + for (int i = 0; i < countries.size() - 1; i++) { + assertTrue(countries.get(i).Population >= countries.get(i + 1).Population, + "Countries should be ordered in descending population for the specified region"); + } + } + + /** + * Test method TopN_PopulateCountriesByRegion to check if it can handle null parameters + */ + @Test + void test_nullEmpty_TopN_PopulatedCountriesByRegion(){ + List countries = app.report_TopN_PopulatedCountriesByRegion("",0); + assertNull(countries, "The result should be null"); + countries = app.report_TopN_PopulatedCountriesByRegion(null,0); + assertNull(countries, "The result should be null"); + } + + /** + * Test method for TopN_PopulatedCountriesByContinent to check if it can handle invalid inputs in its parameters + */ + @Test + void test_invalid_TopN_PopulatedCountriesByRegion(){ + List countries = app.report_TopN_PopulatedCountriesByRegion("Central Europe", -2); + assertNull(countries, "The result should be null"); + } + + /** + * Tests the report_TopN_PopulatedCities method to ensure it returns the top N populated cities. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCities(){ + List cities = app.report_TopN_PopulatedCities(3); + + // Not Null + assertNotNull(cities, "The result should not be null"); + + // Exact value of items + assertEquals(3, cities.size(), "There should be 3 cities returned"); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population."); + } + } + + /** + * Test method TopN_PopulatedCities to check if it can handle a null parameter + */ + @Test + void test_nullInvalid_TopN_PopulatedCities(){ + List cities = app.report_TopN_PopulatedCities(0); + assertNull(cities, "The result should be null"); + cities = app.report_TopN_PopulatedCities(-2); + assertNull(cities, "The result should be null"); + } + + + /** + * Tests the report_TopN_PopulatedCitiesByContinent method to ensure it returns the top N populated cities in a continent. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCitiesByContinent(){ + List cities = app.report_TopN_PopulatedCitiesByContinent("North America", 5); + + // Not Null + assertNotNull(cities, "The result should not be null"); + + // Exact value of items + assertEquals(5, cities.size(), "There should be 5 cities returned"); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + /** + * Test method TopN_PopulatedCitiesByContinent to check if it can handle two null parameters + */ + @Test + void test_nullEmpty_TopN_PopulatedCitiesByContinent(){ + List cities = app.report_TopN_PopulatedCitiesByContinent("", 0); + assertNull(cities, "The result should be null"); + cities = app.report_TopN_PopulatedCitiesByContinent(null, 0); + assertNull(cities, "The result should be null"); + } + + /** + * Test method for TopN_PopulatedCitiesByContinent to check if it can handle 2 invalid inputs in its parameter + */ + @Test + void test_invalid_TopN_PopulatedCitiesByContinent(){ + List cities = app.report_TopN_PopulatedCitiesByContinent("Eastern Europe",-2); + assertNull(cities, "The result should be null"); + } + + /** + * Tests the report_TopN_PopulatedCitiesByRegion method to ensure it returns the top N populated cities in a region. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCitiesByRegion(){ + List cities = app.report_TopN_PopulatedCitiesByRegion("Central America", 4); + + // Not Null + assertNotNull(cities, "The result should not be null"); + + // Exact value of items + assertEquals(4, cities.size(), "There should be 5 cities returned"); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Countries should be ordered in descending population for the specified region"); + } + } + + /** + * Test method TopN_PopulatedCitiesByRegion to check if it can handle two null parameters + */ + @Test + void test_nullEmpty_TopN_PopulatedCitiesByRegion(){ + List cities = app.report_TopN_PopulatedCitiesByRegion("", 0); + assertNull(cities, "The result should be null"); + cities = app.report_TopN_PopulatedCitiesByRegion(null, 0); + assertNull(cities, "The result should be null"); + } + + /** + * Test method for TopN_PopulatedCitiesByRegion to check if it can handle 2 invalid inputs in its parameter + */ + @Test + void test_invalid_TopN_PopulatedCitiesByRegion(){ + List cities = app.report_TopN_PopulatedCitiesByRegion("SouthWest Europe",-2); + assertNull(cities, "The result should be null"); + } + + /** + * Tests the report_TopN_PopulatedCitiesByCountry method to ensure it returns the top N populated cities in a Country. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCitiesByCountry(){ + List cities = app.report_TopN_PopulatedCitiesByCountry("Brazil",2); + + // Not Null + assertNotNull(cities, "The result should not be null"); + + // Exact value of items + assertEquals(2, cities.size(), "There should be 2 cities returned"); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population for the specified country"); + } + } + + /** + * Test method TopN_PopulatedCitiesByCountry to check if it can handle two null parameters + */ + @Test + void test_nullEmpty_TopN_PopulatedCitiesByCountry(){ + List cities = app.report_TopN_PopulatedCitiesByCountry("", 0); + assertNull(cities, "The result should be null"); + cities = app.report_TopN_PopulatedCitiesByCountry(null, 0); + assertNull(cities, "The result should be null"); + } + + /** + * Test method for TopN_PopulatedCitiesByCountry to check if it can handle 2 invalid inputs in its parameter + */ + @Test + void test_invalid_TopN_PopulatedCitiesByCountry(){ + List cities = app.report_TopN_PopulatedCitiesByCountry("Scotland",-2); + assertNull(cities, "The result should be null"); + } + + /** + * Tests the report_TopN_PopulatedCitiesByDistrict method to ensure it returns the top N populated cities in a District. + * sorted in descending order by population. + */ + @Test + void test_TopN_PopulatedCitiesByDistrict(){ + List cities = app.report_TopN_PopulatedCitiesByDistrict("Western",6); + + // Not Null + assertNotNull(cities, "The result should not be null"); + + // Exact value of items + assertEquals(6, cities.size(), "There should be 6 cities returned"); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population for the specified district"); + } + } + + /** + * Test method TopN_PopulatedCitiesByDistrict to check if it can handle two null parameters + */ + @Test + void test_nullEmpty_TopN_PopulatedCitiesByDistrict(){ + List cities = app.report_TopN_PopulatedCitiesByDistrict("", 0); + assertNull(cities, "The result should be null"); + cities = app.report_TopN_PopulatedCitiesByDistrict(null, 0); + assertNull(cities, "The result should be null"); + } + + /** + * Test method for TopN_PopulatedCitiesByDistrict to check if it can handle 2 invalid inputs in its parameter + */ + @Test + void test_invalid_TopN_PopulatedCitiesByDistrict(){ + List cities = app.report_TopN_PopulatedCitiesByDistrict("Centre",-2); + assertNull(cities, "The result should be null"); + } + + + + + + /** + * Tests the report_PopulationDESC method to ensure it returns a list of all countries + * sorted in descending order by population. + */ + @Test + void test_PopulationDESC(){ + // Get all country by population + List countries = app.report_PopulationDESC(); + + // Not Null + assertNotNull(countries, "The result should not be null"); + + // Has items + assertNotEquals(0, countries.size(), "Countries should not be empty"); + + // Verify items are in descending order + for (int i = 0; i < countries.size() - 1; i++) { + assertTrue(countries.get(i).Population >= countries.get(i + 1).Population, + "Countries should be ordered in descending order."); + } + } + + /** + * Tests the report_PopulationByContinentDESC method to ensure it return a list of countries + * within a given continent. + */ + @Test + void test_PopulationByContinentDESC(){ + // Get population + List countries = app.report_PopulationByContinentDESC("Asia"); + + // Not Null + assertNotNull(countries, "The result should not be null"); + + // Verify countries are in descending order + for (int i = 0; i < countries.size() - 1; i++) { + assertTrue(countries.get(i).getPopulation() >= countries.get(i + 1).getPopulation(), + "Countries should be ordered in descending population for the specified continent"); + } + } + + /** + * Test method for PopulationByContinentDESC to check if it can handle empty parameter + */ + @Test + void test_nullEmpty_PopulationByContinentDESC(){ + List countries = app.report_PopulationByContinentDESC(""); + assertNull(countries, "The result should be null"); + countries = app.report_PopulationByContinentDESC(null); + assertNull(countries, "The result should be null"); + } + + /** + * Test method for PopulationByContinentDESC to check if it can handle invalid input in its parameter + */ + @Test + void test_invalid_PopulationByContinentDESC(){ + List countries = app.report_PopulationByContinentDESC("Scotland"); + assertNotNull(countries, "The result should be not null"); + assertEquals(0, countries.size(), "The result should be empty."); + } + + /** + * Tests the report_CountriesByRegion method to ensure it returns a list of countries + * within a given region. + */ + @Test + void test_CountriesByRegionDESC(){ + // Get countries by region + List countries = app.report_CountriesByRegionDESC("South America"); + + // Not null + assertNotNull(countries, "The result should not be null"); + + // Verify countries returned in descending order by population + for (int i = 0; i < countries.size() - 1; i++) { + assertTrue(countries.get(i).getPopulation() >= countries.get(i + 1).getPopulation(), + "Countries should be ordered in descending population for the specified region"); + } + } + + + /** + * Test method CountriesByRegionDESC to check if it can handle a null parameter + */ + @Test + void test_nullEmpty_CountriesByRegionDESC(){ + List countries = app.report_CountriesByRegionDESC(null); + assertNull(countries, "The result should be null"); + countries = app.report_CountriesByRegionDESC(""); + assertNull(countries, "The result should be null"); + } + + /** + * Test method for CountriesByRegionDESC to check if it can handle invalid inpput + */ + @Test + void test_invalid_CountriesByRegionDESC(){ + List countries = app.report_CountriesByRegionDESC("Africa"); + assertNotNull(countries, "The result should not be null"); + assertEquals(0, countries.size(), "The result should be empty."); + } + + /** + * Tests the report_CapitalCitiesInRegionDESC method to ensure it returns + * a list of capital cities within the given region + */ + @Test + void test_CapitalCitiesInRegionDESC() { + List items = app.report_CapitalCitiesInRegionDESC("Western Europe"); + // Not empty + assertNotEquals(0, items.size()); + + // Check order of items + for (int i = 0; i < items.size() - 1; i++) { + assertTrue(items.get(i).Population >= items.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + /** + * Test if CapitalCitiesInRegionDESC can handle null parameter + */ + @Test + void test_nullEmpty_CapitalCitiesInRegionDESC(){ + List cities = app.report_CapitalCitiesInRegionDESC(null); + assertNull(cities, "The result should be null"); + cities = app.report_CapitalCitiesInRegionDESC(""); + assertNull(cities, "The result should be null"); + } + + /** + * Test if CapitalCitiesInRegionDESC can handle invalid parameter + */ + @Test + void test_invalid_CapitalCitiesInRegionDESC(){ + List cities = app.report_CapitalCitiesInRegionDESC("Scotland"); + assertEquals(0, cities.size()); + } + + /** + * Test report_CitiesInWorldDESC outputs correct number of reports & if it is pulling the correct data. + * And to also check if it's outputting in DESC order + */ + @Test + void test_report_CitiesInWorldDESC(){ + // Put the report into a list + List cities = app.report_CitiesInWorldDESC(); + + // Check if the size of cities is the same as expected + assertNotNull(cities, "The result should not be null"); + assertNotEquals(0, cities.size()); + + // Check if in desc + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + + /** + * Tests the report CitiesInContinentDESC method to ensure it returns a list of all cities + * sorted in descending order by population within a given continent. + */ + @Test + void test_report_CitiesInContinentDESC(){ + // Get cities in continent + List cities = app.report_CitiesInContinentDESC("Africa"); + + // Not nukll + assertNotNull(cities, "The result should not be null"); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + /** + * Tests report CitiesInContinentDESC for empty and null as parameters + */ + @Test + void test_emptyNull_CitiesInContinentDESC(){ + List cities = app.report_CitiesInContinentDESC(""); + assertNull(cities); + cities = app.report_CitiesInContinentDESC(null); + assertNull(cities); + } + + /** + * Tests the report CitiesInRegionDESC method to ensure it returns a list of all cities + * sorted in descending order by population within a given region. + */ + @Test + void test_report_CitiesInRegionDESC(){ + List cities = app.report_CitiesInRegionDESC("Western Europe"); + // Not null + assertNotNull(cities); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + /** + * Tests report CitiesInRegionDESC for empty and null as parameters + */ + @Test + void test_emptyNull_CitiesInRegionDESC(){ + List cities = app.report_CitiesInRegionDESC(""); + assertNull(cities); + cities = app.report_CitiesInRegionDESC(null); + assertNull(cities); + } + + /** + * Tests the report CitiesInCountryDESC method to ensure it returns a list of all cities + * sorted in descending order by population within a given country. + */ + @Test + void test_report_CitiesInCountryDESC(){ + List cities = app.report_CitiesInCountryDESC("France"); + + // Not null + assertNotNull(cities); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + /** + * Tests report CitiesInCountryDESC for empty and null as parameters + */ + @Test + void test_emptyNull_CitiesInCountryDESC(){ + List cities = app.report_CitiesInCountryDESC(""); + assertNull(cities); + cities = app.report_CitiesInCountryDESC(null); + assertNull(cities); + } + + /** + * Tests the report CitiesInDistrictDESC method to ensure it returns a list of all cities + * sorted in descending order by population within a given district. + */ + @Test + void test_report_CitiesInDistrictDESC(){ + List cities = app.report_CitiesInDistrictDESC("Scotland"); + + // Not Null + assertNotNull(cities); + + // Verify cities are in descending order + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + /** + * Tests report CitiesInDistrictDESC for empty and null as parameters + */ + @Test + void test_emptyNull_CitiesInDistrictDESC(){ + List cities = app.report_CitiesInDistrictDESC(""); + assertNull(cities); + cities = app.report_CitiesInDistrictDESC(null); + assertNull(cities); + } + + /** + * Tests the report CapitalCitiesInWorldDESC method to ensure it returns a list of all cities + * sorted in descending order by population in the world. + */ + @Test + void test_report_CapitalCitiesInWorldDESC(){ + List cities = app.report_CapitalCitiesInWorldDESC(); + + // Not null or empty + assertNotNull(cities); + assertNotEquals(0, cities.size()); + + // Check if in desc + for (int i = 0; i < cities.size() - 1; i++) { + assertTrue(cities.get(i).Population >= cities.get(i + 1).Population, + "Cities should be ordered in descending population"); + } + } + + + /** + * Test generic PrintItems with Null + */ + @Test + void test_emptyNull_printItems(){ + App.print_Items(null); + App.print_Items(Collections.emptyList()); + } + + /** + * Test generic PrintItems with single and multiple items + */ + @Test + void test_singleAndMany_printItems(){ + List items = List.of("item0"); + App.print_Items(items); + items = List.of("item1", "item2", "item3"); + App.print_Items(items); + System.out.println(); + } + + @Test + void test_allReports(){ + try{ + App.generateAllReports(app); + }catch (Exception e){ + throw new AssertionError(e); + } + } + +} \ No newline at end of file diff --git a/src/test/java/com/napier/sem/AppTest.java b/src/test/java/com/napier/sem/AppTest.java index 4390a44..1974834 100644 --- a/src/test/java/com/napier/sem/AppTest.java +++ b/src/test/java/com/napier/sem/AppTest.java @@ -416,6 +416,79 @@ void testReport_CapitalCitiesInRegionDESC_regionEmpty(){ assertNull(app.report_CapitalCitiesInRegionDESC(region)); } + // Tests for City reports still to be implemented. +// +// /** +// * Method to test the report_TopN_PopulatedCapitalCitiesInWorld method with an N value less than 1. +// */ +// @Test +// void testReport_TopN_PopulatedCapitalCitiesInWorld_NLessThan1(){ +// int n = 0; +// assertNull(app.report_TopN_PopulatedCapitalCitiesInWorld(n)); +// } +// +// /** +// * Method to test the report_TopN_PopulatedCapitalCitiesInContinent method with an N value less than 1. +// */ +// @Test +// void testReport_TopN_PopulatedCapitalCitiesInContinent_NLessThan1(){ +// int n = 0; +// String continent = "Africa"; +// assertNull(app.report_TopN_PopulatedCapitalCitiesInContinent(n, continent)); +// } +// +// /** +// * Method to test the report_TopN_PopulatedCapitalCitiesInContinent method to make sure it can handle an empty continent being passed in. +// */ +// @Test +// void testReport_TopN_PopulatedCapitalCitiesInContinent_continentEmpty(){ +// int n = 3; +// String continent = ""; +// assertNull(app.report_TopN_PopulatedCapitalCitiesInContinent(n, continent)); +// } +// +// /** +// * Method to test the report_TopN_PopulatedCapitalCitiesInContinent method to make sure it can handle an empty continent being passed in +// * as well as an N value less than 1. +// */ +// @Test +// void testReport_TopN_PopulatedCapitalCitiesInContinent_continentEmptyAndNLessThan1(){ +// int n = 0; +// String continent = ""; +// assertNull(app.report_TopN_PopulatedCapitalCitiesInContinent(n, continnt)); +// } +// +// /** +// * Method to test the report_TopN_PopulatedCapitalCitiesInRegion method with an N value less than 1. +// */ +// @Test +// void testReport_TopN_PopulatedCapitalCitiesInRegion_NLessThan1(){ +// int n = 0; +// String region = "Central America"; +// assertNull(app.report_TopN_PopulatedCapitalCitiesInRegion(n, region)); +// } +// +// /** +// * Method to test the report_TopN_PopulatedCapitalCitiesInRegion method to make sure it can handle an empty region being passed in. +// */ +// @Test +// void testReport_TopN_PopulatedCapitalCitiesInRegion_regionEmpty(){ +// int n = 3; +// String region = ""; +// assertNull(app.report_TopN_PopulatedCapitalCitiesInRegion(n, region)); +// } +// +// /** +// * Method to test the report_TopN_PopulatedCapitalCitiesInRegion method to make sure it can handle an empty region being passed in +// * as well as an N value less than 1. +// */ +// @Test +// void testReport_TopN_PopulatedCapitalCitiesInRegion_regionEmptyAndNLessThan1(){ +// int n = 0; +// String region = ""; +// assertNull(app.report_TopN_PopulatedCapitalCitiesInRegion(n, region)); +// } + @Test /** * Tests the method getReport_City method to test if it causes errors when no city is provided, @@ -485,6 +558,7 @@ void testPrint_ItemsCity(){ app.print_Items(cities); } + @Test /** * This is to test the method print_Items_Capitals and this is its normal use case */ @@ -497,6 +571,7 @@ void testPrint_Items_Capitals(){ app.print_Items_Capitals(capitals); } + @Test /** * This is to test the print_Items_Capitals method and to see if it can handle null being provided */ @@ -505,6 +580,7 @@ void testPrint_Items_CapitalsNull(){ app.print_Items_Capitals(capitals); } + @Test /** * This is to test the print_Items_Capitals method to see if it can handle no data being provided. */ @@ -514,13 +590,62 @@ void testPrint_Items_CapitalsEmpty(){ } // + ///////////////////// Total Population Tests ///////////////////// + // + + // Tests for Total Population reports still to be implemented. +// +// /** +// * Method to test the report_TotalPopulation_Continent method to make sure it can handle an empty continent being passed in. +// */ +// @Test +// void testReport_TotalPopulation_Continent_continentEmpty(){ +// String continent = ""; +// assertNull(app.report_TotalPopulation_Continent(continent)); +// } +// +// /** +// * Method to test the report_TotalPopulation_Region method to make sure it can handle an empty region being passed in. +// */ +// @Test +// void testReport_TotalPopulation_Region_regionEmpty(){ +// String region = ""; +// assertNull(app.report_TotalPopulation_Region(region)); +// } +// +// /** +// * Method to test the report_TotalPopulation_Country method to make sure it can handle an empty country being passed in. +// */ +// @Test +// void testReport_TotalPopulation_Country_countryEmpty(){ +// String country = ""; +// assertNull(app.report_TotalPopulation_Country(country)); +// } +// +// /** +// * Method to test the report_TotalPopulation_District method to make sure it can handle an empty district being passed in. +// */ +// @Test +// void testReport_TotalPopulation_District_districtEmpty(){ +// String district = ""; +// assertNull(app.report_TotalPopulation_District(district)); +// } +// +// @Test +// void testReport_TotalPopulation_City_cityEmpty(){ +// String city = ""; +// assertNull(app.report_TotalPopulation_City(city)); +// } + + // + ///////////////////// Country Language Tests ///////////////////// // + @Test /** * Tests the print_Items method to ensure it handles an empty list without errors. */ - void testPrint_ItemsCountryLanguageContainsNull(){ // Create ArrayList ArrayList countryLanguages = new ArrayList(); diff --git a/src/test/java/com/napier/sem/CityTest.java b/src/test/java/com/napier/sem/CityTest.java index 558352c..150390a 100644 --- a/src/test/java/com/napier/sem/CityTest.java +++ b/src/test/java/com/napier/sem/CityTest.java @@ -16,6 +16,7 @@ class CityTest { // Initialising City object and variables that will be used to create City object. static private City city; + static private City cityNull; static private int id = 64; static private String name = "Dubai"; static private String countryCode = "ARE"; @@ -28,6 +29,7 @@ class CityTest { @BeforeAll static void setUpBeforeClass(){ city = new City(id, name, countryCode, district, population); + cityNull = new City(0, null, null, null, 0); } /** @@ -41,4 +43,78 @@ void testCityConstructor() { assertEquals(district, city.District); assertEquals(population, city.Population); } + + /** + * Tests the City class constructor with null values. + */ + @Test + void testCityConstructorNull() { + assertNull(cityNull.Name); + assertNull(cityNull.CountryCode); + assertNull(cityNull.District); + } + + /** + * Tests the getID method. + */ + @Test + void testGetID() { + assertEquals(id, city.getID()); + } + + /** + * Tests the getName method. + */ + @Test + void testGetName() { + assertEquals(name, city.getName()); + } + + /** + * Tests the getName method with a null value. + */ + @Test + void testGetNameNull(){ + assertNull(cityNull.getName()); + } + + /** + * Tests the getCountryCode method. + */ + @Test + void testGetCountryCode() { + assertEquals(countryCode, city.getCountryCode()); + } + + /** + * Tests the getCountryCode method with a null value. + */ + @Test + void testGetCountryCodeNull(){ + assertNull(cityNull.getCountryCode()); + } + + /** + * Tests the getDistrict method. + */ + @Test + void testGetDistrict() { + assertEquals(district, city.getDistrict()); + } + + /** + * Tests the getDistrict method with a null value. + */ + @Test + void testGetDistrictNull(){ + assertNull(cityNull.getDistrict()); + } + + /** + * Tests the getPopulation method. + */ + @Test + void testGetPopulation() { + assertEquals(population, city.getPopulation()); + } } \ No newline at end of file diff --git a/src/test/java/com/napier/sem/CountryLanguageTest.java b/src/test/java/com/napier/sem/CountryLanguageTest.java index b67505c..1082e25 100644 --- a/src/test/java/com/napier/sem/CountryLanguageTest.java +++ b/src/test/java/com/napier/sem/CountryLanguageTest.java @@ -16,6 +16,7 @@ class CountryLanguageTest { // Initialising CountryLanguage object and variables that will be used to create CountryLanguage object. static private CountryLanguage cl; + static private CountryLanguage clNull; static private String countryCode = "ALB"; static private String language = "Macedonian"; static private String isOfficial = "F"; @@ -27,6 +28,7 @@ class CountryLanguageTest { @BeforeAll static void setUpBeforeClass(){ cl = new CountryLanguage(countryCode, language, isOfficial, percentage); + clNull = new CountryLanguage(null, null, null, 0); } /** @@ -39,4 +41,70 @@ void testCountryLanguageConstructor() { assertEquals(isOfficial, cl.IsOfficial); assertEquals(percentage, cl.Percentage); } + + /** + * Tests the CountryLanguage constructor with null values. + */ + @Test + void testCountryLanguageConstructorNull() { + assertNull(clNull.CountryCode); + assertNull(clNull.Language); + assertNull(clNull.IsOfficial); + } + + /** + * Tests the getCountryCode method. + */ + @Test + void testGetCountryCode() { + assertEquals(countryCode, cl.getCountryCode()); + } + + /** + * Tests the getCountryCode method with a null value. + */ + @Test + void testGetCountryCodeNull() { + assertNull(clNull.getCountryCode()); + } + + /** + * Tests the getLanguage method. + */ + @Test + void testGetLanguage() { + assertEquals(language, cl.getLanguage()); + } + + /** + * Tests the getLanguage method with a null value. + */ + @Test + void testGetLanguageNull() { + assertNull(clNull.getLanguage()); + } + + /** + * Tests the getIsOfficial method. + */ + @Test + void testGetIsOfficial() { + assertEquals(isOfficial, cl.getIsOfficial()); + } + + /** + * Tests the getIsOfficial method with a null value. + */ + @Test + void testGetIsOfficialNull() { + assertNull(clNull.getIsOfficial()); + } + + /** + * Tests the getPercentage method. + */ + @Test + void testGetPercentage() { + assertEquals(percentage, cl.getPercentage()); + } } \ No newline at end of file diff --git a/src/test/java/com/napier/sem/CountryTest.java b/src/test/java/com/napier/sem/CountryTest.java index 6076c22..c9f02d5 100644 --- a/src/test/java/com/napier/sem/CountryTest.java +++ b/src/test/java/com/napier/sem/CountryTest.java @@ -17,6 +17,7 @@ class CountryTest { // Initialising Country object and variables that will be used to create Country object. static private Country country; + static private Country countryNull; static private String code = "AND"; static private String name = "Andorra"; static private String continent = "Europe"; @@ -54,6 +55,23 @@ static void setUpBeforeClass(){ headOfState, capital, code2 ); + + countryNull = new Country( + null, + null, + null, + null, + 0, + null, + 0, + null, + null, + null, + null, + null, + null, + null, + null); } /** @@ -66,6 +84,8 @@ void testCountryConstructor(){ assertEquals(continent, country.Continent); assertEquals(region, country.Region); assertEquals(surfaceArea, country.SurfaceArea, 0.2); + assertEquals(indepYear, country.IndepYear, 4); + assertEquals(population, country.Population); assertEquals(lifeExpectancy, country.LifeExpectancy, 0.1); assertEquals(gnp, country.GNP, 0.2); assertEquals(gnpOld, country.GNPOld, 0.2); @@ -75,4 +95,248 @@ void testCountryConstructor(){ assertEquals(capital, country.Capital); assertEquals(code2, country.Code2); } + + /** + * Tests the Country class constructor with null values. + */ + @Test + void testCountryConstructorNull(){ + assertNull(countryNull.Code); + assertNull(countryNull.Name); + assertNull(countryNull.Continent); + assertNull(countryNull.Region); + assertNull(countryNull.IndepYear); + assertNull(countryNull.LifeExpectancy); + assertNull(countryNull.GNP); + assertNull(countryNull.GNPOld); + assertNull(countryNull.LocalName); + assertNull(countryNull.GovernmentForm); + assertNull(countryNull.HeadOfState); + assertNull(countryNull.Capital); + assertNull(countryNull.Code2); + } + + /** + * Tests the getCode method. + */ + @Test + void testGetCode(){ + assertEquals(code, country.getCode()); + } + + /** + * Tests the getCode method with a null value. + */ + @Test + void testGetCodeNull(){ + assertNull(countryNull.getCode()); + } + + /** + * Tests the getName method. + */ + @Test + void testGetName(){ + assertEquals(name, country.getName()); + } + + /** + * Tests the getName method with a null value. + */ + @Test + void testGetNameNull(){ + assertNull(countryNull.getName()); + } + + /** + * Tests the getContinent method. + */ + @Test + void testGetContinent(){ + assertEquals(continent, country.getContinent()); + } + + /** + * Tests the getContinent method with a null value. + */ + @Test + void testGetContinentNull(){ + assertNull(countryNull.getContinent()); + } + + /** + * Tests the getRegion method. + */ + @Test + void testGetRegion(){ + assertEquals(region, country.getRegion()); + } + + /** + * Tests the getRegion method with a null value. + */ + @Test + void testGetRegionNull(){ + assertNull(countryNull.getRegion()); + } + + /** + * Tests the getSurfaceArea method. + */ + @Test + void testGetSurfaceArea(){ + assertEquals(surfaceArea, country.getSurfaceArea(), 0.2); + } + + /** + * Tests the getIndepYear method. + */ + @Test + void testGetIndepYear(){ + assertEquals(indepYear, country.getIndepYear(), 4); + } + + /** + * Tests the getIndepYear method with a null value. + */ + @Test + void testGetIndepYearNull(){ + assertNull(countryNull.getIndepYear()); + } + + /** + * Tests the getPopulation method. + */ + @Test + void testGetPopulation(){ + assertEquals(population, country.getPopulation()); + } + + /** + * Tests the getLifeExpectancy method. + */ + @Test + void testGetLifeExpectancy(){ + assertEquals(lifeExpectancy, country.getLifeExpectancy(), 0.1); + } + + /** + * Tests the getLifeExpectancy method with a null value. + */ + @Test + void testGetLifeExpectancyNull(){ + assertNull(countryNull.getLifeExpectancy()); + } + + /** + * Tests the getGNP method. + */ + @Test + void testGetGNP(){ + assertEquals(gnp, country.getGNP(), 0.2); + } + + /** + * Tests the getGNP method with a null value. + */ + @Test + void testGetGNPNull(){ + assertNull(countryNull.getGNP()); + } + + /** + * Tests the getGNPOld method. + */ + @Test + void testGetGNPOld(){ + assertEquals(gnpOld, country.getGNPOld(), 0.2); + } + + /** + * Tests the getGNPOld method with a null value. + */ + @Test + void testGetGNPOldNull(){ + assertNull(countryNull.getGNPOld()); + } + + /** + * Tests the getLocalName method. + */ + @Test + void testGetLocalName(){ + assertEquals(localName, country.getLocalName()); + } + + /** + * Tests the getLocalName method with a null value. + */ + @Test + void testGetLocalNameNull(){ + assertNull(countryNull.getLocalName()); + } + + /** + * Tests the getGovernmentForm method. + */ + @Test + void testGetGovernmentForm(){ + assertEquals(governmentForm, country.getGovernmentForm()); + } + + /** + * Tests the getGovernmentForm method with a null value. + */ + @Test + void testGetGovernmentFormNull(){ + assertNull(countryNull.getGovernmentForm()); + } + + /** + * Tests the getHeadOfState method + */ + @Test + void testGetHeadOfState(){ + assertEquals(headOfState, country.getHeadOfState()); + } + + /** + * Tests the getHeadOfState method with a null value. + */ + @Test + void testGetHeadOfStateNull(){ + assertNull(countryNull.getHeadOfState()); + } + + /** + * Tests the getCapital method. + */ + @Test + void testGetCapital(){ + assertEquals(capital, country.getCapital()); + } + + /** + * Tests the getCapital method with a null value. + */ + @Test + void testGetCapitalNull(){ + assertNull(countryNull.getCapital()); + } + + /** + * Tests the getCode2 method. + */ + @Test + void testGetCode2(){ + assertEquals(code2, country.getCode2()); + } + + /** + * Tests the getCode2 method with a null value. + */ + @Test + void testGetCode2Null(){ + assertNull(countryNull.getCode2()); + } } \ No newline at end of file diff --git a/src/test/java/com/napier/sem/DatabaseTest.java b/src/test/java/com/napier/sem/DatabaseTest.java index 227816a..26fc54d 100644 --- a/src/test/java/com/napier/sem/DatabaseTest.java +++ b/src/test/java/com/napier/sem/DatabaseTest.java @@ -16,6 +16,10 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; + +/** + * This class is to the database using Mockito framework to test functionality of methods that require a database connection. + */ public class DatabaseTest { @Mock @@ -29,6 +33,10 @@ public class DatabaseTest { private App app; + /** + * This is a setup class which sets up the mock connection of the database with Mockito + * @throws SQLException + */ @BeforeEach void setUp() throws SQLException { App app = new App(); @@ -42,6 +50,10 @@ void setUp() throws SQLException { when(mockStatement.executeQuery(anyString())).thenReturn(mockResultSet); } + /** + * Tests executeQuery method using Mockito to test that it can execute a query. + * @throws SQLException + */ @Test void testExecuteQuery() throws SQLException { // Define the query to test