diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 90175b6..ea1aa99 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -59,7 +59,7 @@ jobs: uses: actions/checkout@v3 - name: Set up JDK 17 - uses: actions/setup-java@v2 + uses: actions/setup-java@v3 with: java-version: '17' distribution: 'adopt' @@ -68,10 +68,10 @@ jobs: run: mvn -Dtest=com.napier.sem.AppUnitTest test - name: Upload results to CodeCov - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v3 with: directory: ./target/site/jacoco - flags: Unit Tests + flags: Unit_Tests verbose: true integration-test: @@ -84,7 +84,7 @@ jobs: uses: actions/checkout@v3 - name: Set up JDK 17 - uses: actions/setup-java@v2 + uses: actions/setup-java@v3 with: java-version: '17' distribution: 'adopt' @@ -103,10 +103,10 @@ jobs: docker image prune -f - name: Upload results to CodeCov - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v3 with: directory: ./target/site/jacoco - flags: Integration Tests + flags: Integration_Tests verbose: true build: @@ -124,7 +124,7 @@ jobs: uses: actions/checkout@v3 - name: Set up JDK 17 - uses: actions/setup-java@v2 + uses: actions/setup-java@v3 with: java-version: '17' distribution: 'adopt' diff --git a/src/main/java/com/napier/sem/App.java b/src/main/java/com/napier/sem/App.java index afb822c..197d2d7 100644 --- a/src/main/java/com/napier/sem/App.java +++ b/src/main/java/com/napier/sem/App.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Scanner; +//TODO: Look into making the Cities and Countries class methods all non static public class App { public static void main(String[] args) { // Initiate database Class diff --git a/src/main/java/com/napier/sem/database/Cities.java b/src/main/java/com/napier/sem/database/Cities.java index dcb5237..b7f694f 100644 --- a/src/main/java/com/napier/sem/database/Cities.java +++ b/src/main/java/com/napier/sem/database/Cities.java @@ -17,7 +17,7 @@ public class Cities { private static final String baseFrom = " FROM city INNER JOIN country ON country.Code=city.CountryCode "; private static final String baseWhere = "WHERE city.ID=country.Capital "; - private static List parseExtendedCityResults(DB db, String query) throws SQLException { + public static List parseExtendedCityResults(DB db, String query) { List countries = new ArrayList<>(); try (ResultSet results = db.runQuery(query)) { @@ -33,11 +33,13 @@ private static List parseExtendedCityResults(DB db, String q ); countries.add(report); } + } catch (SQLException e) { + System.out.println("Cities#parseExtendedCityResults failed: " + e.getMessage()); } return countries; } - private static List parseCapitalCityResults(DB db, String query) throws SQLException { + public static List parseCapitalCityResults(DB db, String query) { List countries = new ArrayList<>(); try (ResultSet results = db.runQuery(query)) { @@ -52,11 +54,13 @@ private static List parseCapitalCityResults(DB db, St ); countries.add(report); } + } catch (SQLException e) { + System.out.println("Cities#parseCapitalCityResults failed: " + e.getMessage()); } return countries; } - private static List parsePopulationResults(DB db, String query) throws SQLException { + public static List parsePopulationResults(DB db, String query) { List countries = new ArrayList<>(); try (ResultSet results = db.runQuery(query)) { @@ -72,17 +76,19 @@ private static List parsePopulationResults(DB db, String query ); countries.add(report); } + } catch (SQLException e) { + System.out.println("Cities#parsePopulationResults failed: " + e.getMessage()); } return countries; } - public static List getAllCitiesByPopulation(DB db, int limit) throws SQLException { + public static List getAllCitiesByPopulation(DB db, int limit) { String query = baseSelect + baseFrom + "ORDER BY city.Population DESC"; if(limit > 0) query += " LIMIT " + limit; return new ArrayList<>(parseExtendedCityResults(db, query)); } - public static HashMap> getCitiesInContinentByPopulation(DB db, int limit) throws SQLException { + public static HashMap> getCitiesInContinentByPopulation(DB db, int limit) { String query = baseSelect + ", ROW_NUMBER() OVER (PARTITION BY Continent ORDER BY city.Population DESC) AS continent_rank" + baseFrom + "ORDER BY country.Continent, city.Population DESC"; if(limit > 0) query = "SELECT * FROM (" + query + ") AS T WHERE continent_rank <= " + limit; @@ -98,7 +104,7 @@ public static HashMap> getCitiesInContinentByPopulation return continentMap; } - public static HashMap> getCitiesInRegionByPopulation(DB db, int limit) throws SQLException { + public static HashMap> getCitiesInRegionByPopulation(DB db, int limit) { String query = baseSelect + ", ROW_NUMBER() OVER (PARTITION BY Region ORDER BY city.Population DESC) AS region_rank" + baseFrom + "ORDER BY country.Region, city.Population DESC"; if(limit > 0) query = "SELECT * FROM (" + query + ") AS T WHERE region_rank <= " + limit; @@ -114,8 +120,8 @@ public static HashMap> getCitiesInRegionByPopulation(DB return regionMap; } - public static HashMap> getCitiesInCountryByPopulation(DB db, int limit) throws SQLException { - String query = baseSelect + ", ROW_NUMBER() OVER (PARTITION BY Country ORDER BY city.Population DESC) AS country_rank" + + public static HashMap> getCitiesInCountryByPopulation(DB db, int limit) { + String query = baseSelect + ", ROW_NUMBER() OVER (PARTITION BY country.Name ORDER BY city.Population DESC) AS country_rank" + baseFrom + "ORDER BY Country, city.Population DESC"; if(limit > 0) query = "SELECT * FROM (" + query + ") AS T WHERE country_rank <= " + limit; @@ -130,7 +136,7 @@ public static HashMap> getCitiesInCountryByPopulation(D return countryMap; } - public static HashMap> getCitiesInDistrictByPopulation(DB db, int limit) throws SQLException { + public static HashMap> getCitiesInDistrictByPopulation(DB db, int limit) { String query = baseSelect + ", ROW_NUMBER() OVER (PARTITION BY District ORDER BY city.Population DESC) AS district_rank" + baseFrom + "ORDER BY city.District, city.Population DESC"; if(limit > 0) query = "SELECT * FROM (" + query + ") AS T WHERE district_rank <= " + limit; @@ -146,14 +152,14 @@ public static HashMap> getCitiesInDistrictByPopulation( return districtMap; } - public static List getCapitalCitiesByPopulation(DB db, int limit) throws SQLException { + public static List getCapitalCitiesByPopulation(DB db, int limit) { String query = baseSelect + baseFrom + baseWhere + "ORDER BY city.Population DESC"; if(limit > 0) query += " LIMIT " + limit; return new ArrayList<>(parseCapitalCityResults(db, query)); } - public static HashMap> getCapitalCitiesInContinentByPopulation(DB db, int limit) throws SQLException { + public static HashMap> getCapitalCitiesInContinentByPopulation(DB db, int limit) { String query = baseSelect + ", ROW_NUMBER() OVER (PARTITION BY Continent ORDER BY city.Population DESC) AS continent_rank" + baseFrom + baseWhere + "ORDER BY country.Continent, city.Population DESC"; @@ -170,7 +176,7 @@ public static HashMap> getCapitalCitiesInContine return continentMap; } - public static HashMap> getCapitalCitiesInRegionByPopulation(DB db, int limit) throws SQLException { + public static HashMap> getCapitalCitiesInRegionByPopulation(DB db, int limit) { String query = baseSelect + ", ROW_NUMBER() OVER (PARTITION BY country.Region ORDER BY city.Population DESC) AS region_rank " + baseFrom + baseWhere + "ORDER BY country.Region, city.Population DESC"; @@ -187,9 +193,9 @@ public static HashMap> getCapitalCitiesInRegionB return regionMap; } - public static HashMap getPopulationByContinent(DB db) throws SQLException { - String query = "SELECT country.Continent AS Name, SUM(DISTINCT country.population) AS Population, SUM(city.Population) AS PopulationInCities " + - baseFrom + "GROUP BY country.Continent " + "ORDER BY city.Population DESC"; + public static HashMap getPopulationByContinent(DB db) { + String query = "SELECT country.Continent AS Name, SUM(DISTINCT country.Population) AS Population, SUM(city.Population) AS PopulationInCities " + + baseFrom + "GROUP BY country.Continent " + "ORDER BY Population DESC"; List results = parsePopulationResults(db, query); HashMap continentMap = new HashMap<>(); @@ -199,9 +205,9 @@ public static HashMap getPopulationByContinent(DB db) return continentMap; } - public static HashMap getPopulationByRegion(DB db) throws SQLException { - String query = "SELECT country.Region AS Name, SUM(DISTINCT country.population) AS Population, SUM(city.Population) AS PopulationInCities " + - baseFrom + "GROUP BY country.Region " + "ORDER BY city.Population DESC"; + public static HashMap getPopulationByRegion(DB db) { + String query = "SELECT country.Region AS Name, SUM(DISTINCT country.Population) AS Population, SUM(city.Population) AS PopulationInCities " + + baseFrom + "GROUP BY country.Region " + "ORDER BY Population DESC"; List results = parsePopulationResults(db, query); HashMap regionMap = new HashMap<>(); @@ -211,9 +217,9 @@ public static HashMap getPopulationByRegion(DB db) thr return regionMap; } - public static HashMap getPopulationByCountry(DB db) throws SQLException { - String query = "SELECT country.Name AS Name, country.Population AS Population, SUM(city.Population) AS PopulationInCities " + - baseFrom + "GROUP BY country.Name " + "ORDER BY city.Population DESC"; + public static HashMap getPopulationByCountry(DB db) { + String query = "SELECT country.Name AS Name, SUM(DISTINCT country.Population) AS Population, SUM(city.Population) AS PopulationInCities " + + baseFrom + "GROUP BY country.Name " + "ORDER BY Population DESC"; List results = parsePopulationResults(db, query); HashMap countryMap = new HashMap<>(); diff --git a/src/main/java/com/napier/sem/database/Countries.java b/src/main/java/com/napier/sem/database/Countries.java index 10b2835..5b59d29 100644 --- a/src/main/java/com/napier/sem/database/Countries.java +++ b/src/main/java/com/napier/sem/database/Countries.java @@ -12,7 +12,7 @@ public class Countries { private static final String baseSelect = "SELECT country.Name, country.Population, country.Code, country.Continent, country.Region, city.name AS Capital"; private static final String baseFrom = " FROM country INNER JOIN city ON country.Capital=city.ID WHERE country.Capital IS NOT NULL "; - private static List parseResults(DB db, String query) throws SQLException { + public static List parseResults(DB db, String query) { List countries = new ArrayList<>(); try (ResultSet results = db.runQuery(query)) { @@ -28,16 +28,18 @@ private static List parseResults(DB db, String query) throws SQLE ); countries.add(report); } + } catch (SQLException e) { + System.out.println("Countries#parseResults failed: " + e.getMessage()); } return countries; } - public static List getAllCountriesByPopulation(DB db, int limit) throws SQLException { + public static List getAllCountriesByPopulation(DB db, int limit) { String query = baseSelect + baseFrom + "ORDER BY country.Population DESC"; if(limit > 0) query += " LIMIT " + limit; return parseResults(db, query); } - public static HashMap> getCountriesInContinentByPopulation(DB db, int limit) throws SQLException { + public static HashMap> getCountriesInContinentByPopulation(DB db, int limit) { String query = baseSelect + ", ROW_NUMBER() over (PARTITION BY Continent order by Population DESC) AS continent_rank" + baseFrom + "ORDER BY country.Continent, country.Population DESC"; if(limit > 0) query = "SELECT * FROM (" + query + ") AS T WHERE continent_rank <= " + limit; @@ -53,7 +55,7 @@ public static HashMap> getCountriesInContinentByPopu return continentMap; } - public static HashMap> getCountriesInRegionByPopulation(DB db, int limit) throws SQLException { + public static HashMap> getCountriesInRegionByPopulation(DB db, int limit) { String query = baseSelect + ", ROW_NUMBER() over (PARTITION BY Region ORDER BY Population DESC) AS region_rank" + baseFrom + "ORDER BY country.Region, country.Population DESC"; if(limit > 0) query = "SELECT * FROM (" + query + ") AS T WHERE region_rank <= " + limit; diff --git a/src/main/java/com/napier/sem/database/DB.java b/src/main/java/com/napier/sem/database/DB.java index 29a96b2..3e748d3 100644 --- a/src/main/java/com/napier/sem/database/DB.java +++ b/src/main/java/com/napier/sem/database/DB.java @@ -23,17 +23,16 @@ public void connect() { System.out.println("Connected to database"); } catch (SQLException e) { System.out.println("Failed to connect to database"); - System.out.println(e.getMessage()); } } public void disconnect() { - try { - if (connection != null) { + if (connection != null) { + try { connection.close(); + } catch (SQLException ex) { + System.out.println("Database failed to disconnect"); } - } catch (SQLException ex) { - System.out.println(ex.getMessage()); } } @@ -46,7 +45,7 @@ public ResultSet runQuery(String query) { var statement = connection.createStatement(); return statement.executeQuery(query); } catch (SQLException e) { - System.out.println(e.getMessage()); + System.out.println("DB#runQuery failed: " + e.getMessage()); } return null; } diff --git a/src/test/java/com/napier/sem/AppIntegrationTest.java b/src/test/java/com/napier/sem/AppIntegrationTest.java index 5aef92c..fce19a2 100644 --- a/src/test/java/com/napier/sem/AppIntegrationTest.java +++ b/src/test/java/com/napier/sem/AppIntegrationTest.java @@ -1,11 +1,15 @@ package com.napier.sem; +import com.napier.sem.database.Cities; +import com.napier.sem.database.Countries; +import com.napier.sem.database.CountryLanguage; import com.napier.sem.database.DB; + import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class AppIntegrationTest { static DB db; @@ -25,4 +29,199 @@ static void cleanUp() { void basicTest() { assertEquals(1, 1); } + + @Test + void getDbConnectionReturnConnection() { + assertNotNull(db.getConnection()); + } + + @Test + void testRunQuery() { + String query = "SELECT * FROM city LIMIT 1"; + assertNotNull(db.runQuery(query)); + } + + @Test + void testRunQueryWithNull() { + String query = "SELECT city.ID2 FROM city LIMIT 1"; + assertNull(db.runQuery(query)); + } + + @Test + void fakeDbConnection() { + DB db2 = new DB("localhost", "33061", "world", "root", "group6Pass"); + db2.connect(); + } + + @Test + void testDisconnectWithoutConnection() { + DB db2 = new DB("localhost", "33060", "world", "root", "group6Pass"); + db2.disconnect(); + } + + @Test + void countriesClassTest() { + new Countries(); + } + @Test + void testCountriesByPopulation() { + Countries.getAllCountriesByPopulation(db, 1); + } + + @Test + void testCountriesByPopulationWithNoLimit() { + Countries.getAllCountriesByPopulation(db, 0); + } + + @Test + void testContinentCountriesByPopulation() { + Countries.getCountriesInContinentByPopulation(db, 1); + } + + @Test + void testContinentCountriesByPopulationWithNoLimit() { + Countries.getCountriesInContinentByPopulation(db, 0); + } + + @Test + void testRegionCountriesByPopulation() { + Countries.getCountriesInRegionByPopulation(db, 1); + } + + @Test + void testRegionCountriesByPopulationWithNoLimit() { + Countries.getCountriesInRegionByPopulation(db, 0); + } + + @Test + void testInvalidQuery() { + assertTrue(Countries.parseResults(db, "SELECT * FROM city2").isEmpty()); + } + + @Test + void testCitiesClass() { + new Cities(); + } + + @Test + void testCitiesByPopulation() { + Cities.getAllCitiesByPopulation(db, 1); + } + + @Test + void testCitiesByPopulationWithNoLimit() { + Cities.getAllCitiesByPopulation(db, 0); + } + + @Test + void testCitiesInContinentByPopulation() { + Cities.getCitiesInContinentByPopulation(db, 1); + } + + @Test + void testCitiesInContinentByPopulationWithNoLimit() { + Cities.getCitiesInContinentByPopulation(db, 0); + } + + @Test + void testCitiesInRegionByPopulation() { + Cities.getCitiesInRegionByPopulation(db, 1); + } + + @Test + void testCitiesInRegionByPopulationWithNoLimit() { + Cities.getCitiesInRegionByPopulation(db, 0); + } + + @Test + void testCitiesInCountryByPopulation() { + Cities.getCitiesInCountryByPopulation(db, 1); + } + + @Test + void testCitiesInCountryByPopulationWithNoLimit() { + Cities.getCitiesInCountryByPopulation(db, 0); + } + + @Test + void testCitiesInDistrictByPopulation() { + Cities.getCitiesInDistrictByPopulation(db, 1); + } + + @Test + void testCitiesInDistrictByPopulationWithNoLimit() { + Cities.getCitiesInDistrictByPopulation(db, 0); + } + + @Test + void testCapitalCitiesByPopulation() { + Cities.getCapitalCitiesByPopulation(db, 1); + } + + @Test + void testCapitalCitiesByPopulationWithNoLimit() { + Cities.getCapitalCitiesByPopulation(db, 0); + } + + @Test + void testCapitalCitiesInContinentByPopulation() { + Cities.getCapitalCitiesInContinentByPopulation(db, 1); + } + + @Test + void testCapitalCitiesInContinentByPopulationWithNoLimit() { + Cities.getCapitalCitiesInContinentByPopulation(db, 0); + } + + @Test + void testCapitalCitiesInRegionByPopulation() { + Cities.getCapitalCitiesInRegionByPopulation(db, 1); + } + + @Test + void testCapitalCitiesInRegionByPopulationWithNoLimit() { + Cities.getCapitalCitiesInRegionByPopulation(db, 0); + } + + @Test + void testCitiesPopulationByContinent() { + Cities.getPopulationByContinent(db); + } + + @Test + void testCitiesPopulationByRegion() { + Cities.getPopulationByRegion(db); + } + + @Test + void testCitiesPopulationByCountry() { + Cities.getPopulationByCountry(db); + } + + @Test + void testParseExtendedCityResults() { + assertTrue(Cities.parseExtendedCityResults(db, "SELECT ID2 FROM city LIMIT 1").isEmpty()); + } + + @Test + void testParseCapitalCityResults() { + assertTrue(Cities.parseCapitalCityResults(db, "SELECT ID2 FROM city LIMIT 1").isEmpty()); + } + + @Test + void testPopulationResults() { + assertTrue(Cities.parsePopulationResults(db, "SELECT ID2 FROM city LIMIT 1").isEmpty()); + } + + @Test + void testCountryLanguageClass() { + new CountryLanguage(); + } + + + @Test + void testMainApp() { + // TODO: MAIN METHOD NOT BEING TESTED DUE TO IMMINENT REFACTOR OF DRIVER CODE. TESTS WILL BE WRITTEN AFTER REFACTOR. + // App.main("0"); + } }