From 0b44a7be72d88c56741519619088bc57a9920c88 Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:01:59 -0500 Subject: [PATCH 01/11] Created MergeCarAndOffer --- .../api/utils/MergeCarAndOffer.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java diff --git a/src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java b/src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java new file mode 100644 index 0000000..6d7a920 --- /dev/null +++ b/src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java @@ -0,0 +1,36 @@ +package tech.autodirect.api.utils; + +import tech.autodirect.api.entities.EntCar; +import tech.autodirect.api.entities.EntOffer; + +import java.util.HashMap; +import java.util.Map; + +public class MergeCarAndOffer { + /** + * Merge car and offer entities into a single map. + */ + public static Map mergeCarAndOffer(EntCar car, EntOffer offer) { + return new HashMap<>() {{ + // Car info + put("car_id", car.getCarId()); + put("brand", car.getBrand()); + put("model", car.getModel()); + put("year", car.getYear()); + put("price", car.getPrice()); + put("kms", car.getKms()); + // Offer info + put("offer_id", offer.getOfferId()); + put("loan_amount", offer.getLoanAmount()); + put("capital_sum", offer.getCapitalSum()); + put("interest_sum", offer.getInterestSum()); + put("total_sum", offer.getTotalSum()); + put("apr", offer.getInterestRate()); // TODO: why "apr" + put("term_mo", offer.getTermMo()); // TODO: clean term_length/term_mo and all these terms + put("installments", offer.getInstallments()); + put("claimed", offer.isClaimed()); + // Computed stuff + put("payment_mo", offer.getTotalSum() / offer.getTermMo()); + }}; + } +} From 200837ab548fc974852dce1f9939751b822da20c Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:02:23 -0500 Subject: [PATCH 02/11] changed id column in car table to be int and renamed to car_id --- .../java/tech/autodirect/api/database/InitDatabase.java | 2 +- src/main/java/tech/autodirect/api/database/TableCars.java | 7 ++----- .../tech/autodirect/api/interfaces/TableCarsInterface.java | 6 +++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/main/java/tech/autodirect/api/database/InitDatabase.java b/src/main/java/tech/autodirect/api/database/InitDatabase.java index 1eed7ea..cce515c 100644 --- a/src/main/java/tech/autodirect/api/database/InitDatabase.java +++ b/src/main/java/tech/autodirect/api/database/InitDatabase.java @@ -105,7 +105,7 @@ private static void exitWithHelp(int exitCode) { private static void createCarsTable(Connection conn) throws SQLException { PreparedStatement stmt = conn.prepareStatement( "CREATE TABLE IF NOT EXISTS public.cars (" + - "id serial NOT NULL PRIMARY KEY, " + + "car_id integer NOT NULL PRIMARY KEY, " + "brand varchar(50) NOT NULL, " + "model varchar(50) NOT NULL, " + "year integer NOT NULL, " + diff --git a/src/main/java/tech/autodirect/api/database/TableCars.java b/src/main/java/tech/autodirect/api/database/TableCars.java index de4a807..d870ce8 100644 --- a/src/main/java/tech/autodirect/api/database/TableCars.java +++ b/src/main/java/tech/autodirect/api/database/TableCars.java @@ -16,7 +16,6 @@ limitations under the License. */ -import org.springframework.http.HttpStatus; import org.springframework.web.server.ResponseStatusException; import tech.autodirect.api.interfaces.TableCarsInterface; @@ -26,7 +25,6 @@ import java.util.Map; public class TableCars extends Table implements TableCarsInterface { - private final String dbName; private final Connection dbConn; private final String schemaName = "public"; private final String tableName = "cars"; @@ -37,7 +35,6 @@ public class TableCars extends Table implements TableCarsInterface { * @param dbName : name of the database to connect to */ public TableCars(String dbName) throws SQLException, ClassNotFoundException { - this.dbName = dbName; this.dbConn = Conn.getConn(dbName); } @@ -47,11 +44,11 @@ public List> getAllCars() throws SQLException { } @Override - public Map getCarById(String carId) throws SQLException, ResponseStatusException { + public Map getCarById(int carId) throws SQLException, ResponseStatusException { return getEntryById(carId, schemaName, tableName, dbConn, "car"); } - public boolean checkCarExists(String carId) throws SQLException { + public boolean checkCarExists(int carId) throws SQLException { return checkEntryExists(carId, schemaName, tableName, dbConn, "car"); } } diff --git a/src/main/java/tech/autodirect/api/interfaces/TableCarsInterface.java b/src/main/java/tech/autodirect/api/interfaces/TableCarsInterface.java index 08ce2d4..8cd6d27 100644 --- a/src/main/java/tech/autodirect/api/interfaces/TableCarsInterface.java +++ b/src/main/java/tech/autodirect/api/interfaces/TableCarsInterface.java @@ -23,7 +23,7 @@ public interface TableCarsInterface { /** - * Gets all cars from the cars table in the database, using full-text keyword search (TODO). + * Gets all cars from the cars table in the database. * * @return A List of Maps where each Map is a single entry in the JDBC query result. */ @@ -34,10 +34,10 @@ public interface TableCarsInterface { * * @return A Map representing a car entry in the database. */ - Map getCarById(String carId) throws SQLException; + Map getCarById(int carId) throws SQLException; /** * Check if car exists in database. */ - boolean checkCarExists(String carId) throws SQLException; + boolean checkCarExists(int carId) throws SQLException; } From 5a56af1740389bd22425d30b9ee61b49c088cb2c Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:05:34 -0500 Subject: [PATCH 03/11] renamed test method --- .../java/tech/autodirect/api/services/SvcSearchTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/tech/autodirect/api/services/SvcSearchTest.java b/src/test/java/tech/autodirect/api/services/SvcSearchTest.java index 4ba1060..b696832 100644 --- a/src/test/java/tech/autodirect/api/services/SvcSearchTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcSearchTest.java @@ -55,7 +55,7 @@ void testSearchCarsPreLoginWithAllEmpty() { } @Test - void testSearchCarsPreLoginNonPriceSortBy() { + void testSearchCarsPreLoginInvalidSortBy() { try { tableUsers.addUser(testUserId, 700, 1000, 200); List> carsResult = svcSearch.search("", "1000", "200", "term_length", "true"); @@ -66,7 +66,8 @@ void testSearchCarsPreLoginNonPriceSortBy() { } catch (ResponseStatusException e) { // pre-login search must take "price" sortBy assert Objects.requireNonNull(e.getMessage()).startsWith("400 BAD_REQUEST"); - } } + } + } @Test void testSearchCarsPreLoginCheckSorting() { From 751be3fc19857ff2c0e72f47038b916be283099f Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:07:42 -0500 Subject: [PATCH 04/11] cleaned code and fixed bugs --- .../tech/autodirect/api/entities/EntCar.java | 10 +++--- .../autodirect/api/services/SvcSearch.java | 36 +++---------------- .../autodirect/api/entities/EntCarTest.java | 2 +- 3 files changed, 11 insertions(+), 37 deletions(-) diff --git a/src/main/java/tech/autodirect/api/entities/EntCar.java b/src/main/java/tech/autodirect/api/entities/EntCar.java index 38cf867..5d998b2 100644 --- a/src/main/java/tech/autodirect/api/entities/EntCar.java +++ b/src/main/java/tech/autodirect/api/entities/EntCar.java @@ -27,7 +27,7 @@ * This entity represents a car in our program. */ public class EntCar { - private int id; + private int carId; private String brand; private String model; private int year; @@ -40,7 +40,7 @@ public class EntCar { * @param entry : A Map containing representing a car entry in the database. */ public void loadFromMap(Map entry) throws SQLException { - id = (int) entry.get("id"); + carId = (int) entry.get("car_id"); brand = (String) entry.get("brand"); model = (String) entry.get("model"); year = (int) entry.get("year"); @@ -48,8 +48,8 @@ public void loadFromMap(Map entry) throws SQLException { kms = UnitConv.mileToKm(((Float) entry.get("mileage")).doubleValue()); } - public int getId() { - return id; + public int getCarId() { + return carId; } public String getBrand() { @@ -77,7 +77,7 @@ public boolean equals(Object o) { if (o instanceof EntCar) { EntCar car = (EntCar) o; - return getId() == car.getId() && + return getCarId() == car.getCarId() && Objects.equals(getBrand(), car.getBrand()) && getKms() == car.getKms() && Objects.equals(getModel(), car.getModel()) && diff --git a/src/main/java/tech/autodirect/api/services/SvcSearch.java b/src/main/java/tech/autodirect/api/services/SvcSearch.java index 3425445..6ef598b 100644 --- a/src/main/java/tech/autodirect/api/services/SvcSearch.java +++ b/src/main/java/tech/autodirect/api/services/SvcSearch.java @@ -25,6 +25,7 @@ import tech.autodirect.api.interfaces.TableCarsInterface; import tech.autodirect.api.interfaces.TableOffersInterface; import tech.autodirect.api.interfaces.TableUsersInterface; +import tech.autodirect.api.utils.MergeCarAndOffer; import tech.autodirect.api.utils.ParseChecker; import java.io.IOException; @@ -217,7 +218,7 @@ private List> searchCarsWithOfferNewParams( // carMap in the loop. if (offer != null) { // Merge car and offer to create a carAndOfferInfoMap - Map carAndOfferInfoMap = mergeCarAndOffer(car, offer); + Map carAndOfferInfoMap = MergeCarAndOffer.mergeCarAndOffer(car, offer); carAndOfferInfoMaps.add(carAndOfferInfoMap); } } @@ -248,12 +249,12 @@ private List> searchCarsWithOfferOldParams( EntOffer offer = new EntOffer(); offer.loadFromMap(offerMap); - Map carMap = tableCars.getCarById(Integer.toString(offer.getCarId())); + Map carMap = tableCars.getCarById(offer.getCarId()); EntCar car = new EntCar(); car.loadFromMap(carMap); // Merge car and offer to create a carAndOfferInfoMap - Map carAndOfferInfoMap = mergeCarAndOffer(car, offer); + Map carAndOfferInfoMap = MergeCarAndOffer.mergeCarAndOffer(car, offer); carAndOfferInfoMaps.add(carAndOfferInfoMap); } // Return a sorted version of carAndOfferInfoMaps according to the sort settings @@ -291,7 +292,7 @@ private EntOffer createOfferFromUserAndCar ( // offer entity. Otherwise, there was no loan offer for this car and the current search params, so return null. if (queryResult.get("status").equals(200)) { Map queryBody = (Map) queryResult.get("body"); - int carId = car.getId(); + int carId = car.getCarId(); double loanAmount = (double) queryBody.get("amount"); double capitalSum = (double) queryBody.get("capitalSum"); double interestSum = (double) queryBody.get("interestSum"); @@ -326,33 +327,6 @@ private EntOffer createOfferFromUserAndCar ( } } - /** - * Merge car and offer entities into a single map. - */ - private Map mergeCarAndOffer(EntCar car, EntOffer offer) { - return new HashMap<>() {{ - // Car info - put("car_id", car.getId()); - put("brand", car.getBrand()); - put("model", car.getModel()); - put("year", car.getYear()); - put("price", car.getPrice()); - put("kms", car.getKms()); - // Offer info - put("offer_id", offer.getOfferId()); - put("loan_amount", offer.getLoanAmount()); - put("capital_sum", offer.getCapitalSum()); - put("interest_sum", offer.getInterestSum()); - put("total_sum", offer.getTotalSum()); - put("apr", offer.getInterestRate()); // TODO: why "apr" - put("term_length", offer.getTermMo()); // TODO: clean term_length/term_mo and all these terms - put("installments", offer.getInstallments()); - put("claimed", offer.isClaimed()); - // Computed stuff - put("payment_mo", offer.getTotalSum() / offer.getTermMo()); - }}; - } - /** * Sort a list of maps according to key and sortAsc. We use the corresponding value of key for each map * (which is assumed to be castable to double) to sort the list of maps. sortAsc defines whether we sort diff --git a/src/test/java/tech/autodirect/api/entities/EntCarTest.java b/src/test/java/tech/autodirect/api/entities/EntCarTest.java index 29083e2..ecf589c 100644 --- a/src/test/java/tech/autodirect/api/entities/EntCarTest.java +++ b/src/test/java/tech/autodirect/api/entities/EntCarTest.java @@ -28,7 +28,7 @@ void testLoadFromMap() { EntCar car = new EntCar(); car.loadFromMap(carMap); - assert car.getId() == 1; + assert car.getCarId() == 1; assert car.getBrand().equals("nissan"); assert car.getModel().equals("sedan"); assert car.getYear() == 2017; From 234c34baec67a992c74ce3260cc7106b0aa62ae4 Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:08:06 -0500 Subject: [PATCH 05/11] Updated svcs to return offer and car info maps and associated tests (WIP) --- .../api/services/SvcGetClaimedOffers.java | 24 +++++++++--- .../api/services/SvcGetOfferDetails.java | 17 +++++++- .../api/services/SvcGetClaimedOffersTest.java | 39 +++++++++++++------ .../api/services/SvcGetOfferDetailsTest.java | 27 +++++++------ 4 files changed, 77 insertions(+), 30 deletions(-) diff --git a/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java b/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java index 310c008..e7d271d 100644 --- a/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java +++ b/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java @@ -18,8 +18,12 @@ import org.springframework.http.HttpStatus; import org.springframework.web.server.ResponseStatusException; +import tech.autodirect.api.database.TableCars; +import tech.autodirect.api.entities.EntCar; import tech.autodirect.api.entities.EntOffer; +import tech.autodirect.api.interfaces.TableCarsInterface; import tech.autodirect.api.interfaces.TableOffersInterface; +import tech.autodirect.api.utils.MergeCarAndOffer; import java.sql.SQLException; import java.util.ArrayList; @@ -34,7 +38,8 @@ public class SvcGetClaimedOffers { /** * Get all claimed offers for the specified user. */ - public List getClaimedOffers( + public List> getClaimedOffers( + TableCarsInterface tableCars, TableOffersInterface tableOffers, String userId ) throws SQLException, ResponseStatusException { @@ -44,16 +49,25 @@ public List getClaimedOffers( ); } + // Set user in offers table and get all offers tableOffers.setUser(userId); List> offersList = tableOffers.getAllOffers(); - List offers = new ArrayList<>(); + List> carAndOfferInfoMaps = new ArrayList<>(); for (Map offerMap : offersList) { + // Get offer entity EntOffer offer = new EntOffer(); offer.loadFromMap(offerMap); - offers.add(offer); - } - return offers; + // Get car entity (TODO: why is carId string and not int?) + Map carMap = tableCars.getCarById(offer.getCarId()); + EntCar car = new EntCar(); + car.loadFromMap(carMap); + + // Merge car and offer entities into a map that has both car and offer info to return to frontend + Map carAndOfferInfo = MergeCarAndOffer.mergeCarAndOffer(car, offer); + carAndOfferInfoMaps.add(carAndOfferInfo); + } + return carAndOfferInfoMaps; } } diff --git a/src/main/java/tech/autodirect/api/services/SvcGetOfferDetails.java b/src/main/java/tech/autodirect/api/services/SvcGetOfferDetails.java index 314d0b9..87292c0 100644 --- a/src/main/java/tech/autodirect/api/services/SvcGetOfferDetails.java +++ b/src/main/java/tech/autodirect/api/services/SvcGetOfferDetails.java @@ -17,9 +17,13 @@ */ import org.springframework.http.HttpStatus; +import org.springframework.validation.ObjectError; import org.springframework.web.server.ResponseStatusException; +import tech.autodirect.api.entities.EntCar; import tech.autodirect.api.entities.EntOffer; +import tech.autodirect.api.interfaces.TableCarsInterface; import tech.autodirect.api.interfaces.TableOffersInterface; +import tech.autodirect.api.utils.MergeCarAndOffer; import java.sql.SQLException; import java.util.Map; @@ -32,7 +36,8 @@ public class SvcGetOfferDetails { /** * Get details for an offer (as an EntOffer object). */ - public EntOffer getOfferDetails( + public Map getOfferDetails( + TableCarsInterface tableCars, TableOffersInterface tableOffers, String userId, String offerId @@ -43,12 +48,20 @@ public EntOffer getOfferDetails( ); } + // Set the user in the offers table tableOffers.setUser(userId); + // Get offer entity Map offerMap = tableOffers.getOfferByOfferId(Integer.parseInt(offerId)); EntOffer offer = new EntOffer(); offer.loadFromMap(offerMap); - return offer; + // Get car entity + Map carMap = tableCars.getCarById(offer.getCarId()); + EntCar car = new EntCar(); + car.loadFromMap(carMap); + + // Merge car and offer entities into a map that has both car and offer info to return to frontend + return MergeCarAndOffer.mergeCarAndOffer(car, offer); } } diff --git a/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java b/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java index 6343f20..7d78b58 100644 --- a/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java @@ -4,13 +4,17 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; +import tech.autodirect.api.database.TableCars; import tech.autodirect.api.database.TableOffers; import tech.autodirect.api.entities.EntOffer; +import tech.autodirect.api.interfaces.TableCarsInterface; import tech.autodirect.api.interfaces.TableOffersInterface; import javax.management.InstanceAlreadyExistsException; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; +import java.util.Map; // This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) @@ -23,26 +27,37 @@ class SvcGetClaimedOffersTest { void testGetClaimedOffers() { try { SvcGetClaimedOffers svcGetClaimedOffers = new SvcGetClaimedOffers(); + TableCarsInterface tableCars = new TableCars(dbName); TableOffersInterface tableOffers = new TableOffers(dbName); tableOffers.setUser(testUserId); - // Add some offers - int offerId1 = tableOffers.addOffer(1, 2, 3, 4, 5, 6, 7, "TEST", false); - int offerId2 = tableOffers.addOffer(2, 2, 3, 4, 5, 6, 7, "TEST", true); - int offerId3 = tableOffers.addOffer(3, 2, 3, 4, 5, 6, 7, "TEST", false); - int offerId4 = tableOffers.addOffer(4, 2, 3, 4, 5, 6, 7, "TEST", true); + // Add carIds for 4 cars from the database + List> carMaps = tableCars.getAllCars(); + assert carMaps.size() >= 4; + List fourCarIds = new ArrayList<>(); + for (Map carMap : carMaps) { + int carId = (int) carMap.get("car_id"); + fourCarIds.add(carId); + } + + // Add offers corresponding to the cars + int offerId1 = tableOffers.addOffer(fourCarIds.get(0), 2, 3, 4, 5, 6, 7, "TEST", false); + int offerId2 = tableOffers.addOffer(fourCarIds.get(1), 2, 3, 4, 5, 6, 7, "TEST", true); + int offerId3 = tableOffers.addOffer(fourCarIds.get(2), 2, 3, 4, 5, 6, 7, "TEST", false); + int offerId4 = tableOffers.addOffer(fourCarIds.get(3), 2, 3, 4, 5, 6, 7, "TEST", true); - // - List offers = svcGetClaimedOffers.getClaimedOffers(tableOffers, testUserId); + // Check that carAndOfferInfoMaps from getClaimedOffers has all the offers + List> carAndOfferInfoMaps + = svcGetClaimedOffers.getClaimedOffers(tableCars, tableOffers, testUserId); boolean offerId1InOffers = false; boolean offerId2InOffers = false; boolean offerId3InOffers = false; boolean offerId4InOffers = false; - for (EntOffer offer : offers) { - if (offer.getOfferId() == offerId1) { offerId1InOffers = true; } - if (offer.getOfferId() == offerId2) { offerId2InOffers = true; } - if (offer.getOfferId() == offerId3) { offerId3InOffers = true; } - if (offer.getOfferId() == offerId4) { offerId4InOffers = true; } + for (Map carAndOfferInfo : carAndOfferInfoMaps) { + if ((int) carAndOfferInfo.get("offer_id") == offerId1) { offerId1InOffers = true; } + if ((int) carAndOfferInfo.get("offer_id") == offerId2) { offerId2InOffers = true; } + if ((int) carAndOfferInfo.get("offer_id") == offerId3) { offerId3InOffers = true; } + if ((int) carAndOfferInfo.get("offer_id") == offerId4) { offerId4InOffers = true; } } assert offerId1InOffers && offerId2InOffers && offerId3InOffers && offerId4InOffers; diff --git a/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java b/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java index b52d209..ad11380 100644 --- a/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java @@ -4,11 +4,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; +import tech.autodirect.api.database.TableCars; import tech.autodirect.api.database.TableOffers; import tech.autodirect.api.entities.EntOffer; +import tech.autodirect.api.interfaces.TableCarsInterface; import tech.autodirect.api.interfaces.TableOffersInterface; import java.sql.SQLException; +import java.util.Map; import java.util.Objects; import static org.junit.jupiter.api.Assertions.*; @@ -23,6 +26,7 @@ class SvcGetOfferDetailsTest { void getOfferDetails() { try { SvcGetOfferDetails svcGetOfferDetails = new SvcGetOfferDetails(); + TableCarsInterface tableCars = new TableCars(dbName); TableOffersInterface tableOffers = new TableOffers(dbName); tableOffers.setUser(testUserId); @@ -30,19 +34,20 @@ void getOfferDetails() { int offerId = tableOffers.addOffer(1, 2, 3, 4, 5, 6, 7, "TEST", true); // Get offer details - EntOffer offer = svcGetOfferDetails.getOfferDetails(tableOffers, testUserId, Integer.toString(offerId)); + Map carAndOfferInfo + = svcGetOfferDetails.getOfferDetails(tableCars, tableOffers, testUserId, Integer.toString(offerId)); // Checks - assert offer.getOfferId() == offerId; - assert offer.getCarId() == 1; - assert offer.getLoanAmount() == 2; - assert offer.getCapitalSum() == 3; - assert offer.getInterestSum() == 4; - assert offer.getTotalSum() == 5; - assert offer.getInterestRate() == 6; - assert offer.getTermMo() == 7; - assert Objects.equals(offer.getInstallments(), "TEST"); - assert offer.isClaimed(); + assert (int) carAndOfferInfo.get("offer_id") == offerId; + assert (int) carAndOfferInfo.get("car_id") == 1; + assert (double) carAndOfferInfo.get("loan_amount") == 2; + assert (double) carAndOfferInfo.get("capital_sum") == 3; + assert (double) carAndOfferInfo.get("interest_sum") == 4; + assert (double) carAndOfferInfo.get("total_sum") == 5; + assert (double) carAndOfferInfo.get("apr") == 6; + assert (double) carAndOfferInfo.get("term_mo") == 7; + assert Objects.equals(carAndOfferInfo.get("installments"), "TEST"); + assert (boolean) carAndOfferInfo.get("claimed"); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); assert false; From 1c312db9eb8db43849ac9cfe6b9607bb38445f3f Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:08:51 -0500 Subject: [PATCH 06/11] removed todos that are done --- src/main/java/tech/autodirect/api/ApiEndpoints.java | 5 ++--- .../tech/autodirect/api/services/SvcGetClaimedOffers.java | 2 +- src/main/java/tech/autodirect/api/services/SvcSearch.java | 7 +++---- .../java/tech/autodirect/api/utils/MergeCarAndOffer.java | 4 ++-- .../tech/autodirect/api/database/TableOffersTests.java | 2 +- .../java/tech/autodirect/api/database/TableUsersTest.java | 2 +- .../tech/autodirect/api/services/SvcClaimOfferTest.java | 2 +- .../autodirect/api/services/SvcGetClaimedOffersTest.java | 2 +- .../autodirect/api/services/SvcGetOfferDetailsTest.java | 4 ++-- .../java/tech/autodirect/api/services/SvcSearchTest.java | 6 +++--- .../tech/autodirect/api/services/SvcUnclaimOfferTest.java | 2 +- .../tech/autodirect/api/services/SvcUserLoginTest.java | 2 +- 12 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/tech/autodirect/api/ApiEndpoints.java b/src/main/java/tech/autodirect/api/ApiEndpoints.java index 5782ccc..9d65c52 100644 --- a/src/main/java/tech/autodirect/api/ApiEndpoints.java +++ b/src/main/java/tech/autodirect/api/ApiEndpoints.java @@ -54,7 +54,6 @@ public class ApiEndpoints extends SpringBootServletInitializer { private final String dbName = "autodirect"; // Initialize Frameworks & Drivers - // TODO: Add explanation, change to more specific? private TableCarsInterface tableCars; private TableUsersInterface tableUsers; private TableOffersInterface tableOffers; @@ -175,7 +174,7 @@ public Object unclaimOffer( @GetMapping("/getClaimedOffers") public Object getClaimedOffers(@RequestParam(name = "user_id") String userId) { try { - return svcGetClaimedOffers.getClaimedOffers(tableOffers, userId); + return svcGetClaimedOffers.getClaimedOffers(tableCars, tableOffers, userId); } catch (SQLException e) { e.printStackTrace(); throw SERVER_ERROR; @@ -188,7 +187,7 @@ public Object getOfferDetails( @RequestParam(name = "offer_id") String offerId ) { try { - return svcGetOfferDetails.getOfferDetails(tableOffers, userId, offerId); + return svcGetOfferDetails.getOfferDetails(tableCars, tableOffers, userId, offerId); } catch (SQLException e) { e.printStackTrace(); throw SERVER_ERROR; diff --git a/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java b/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java index e7d271d..6be459e 100644 --- a/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java +++ b/src/main/java/tech/autodirect/api/services/SvcGetClaimedOffers.java @@ -59,7 +59,7 @@ public List> getClaimedOffers( EntOffer offer = new EntOffer(); offer.loadFromMap(offerMap); - // Get car entity (TODO: why is carId string and not int?) + // Get car entity Map carMap = tableCars.getCarById(offer.getCarId()); EntCar car = new EntCar(); car.loadFromMap(carMap); diff --git a/src/main/java/tech/autodirect/api/services/SvcSearch.java b/src/main/java/tech/autodirect/api/services/SvcSearch.java index 6ef598b..c925308 100644 --- a/src/main/java/tech/autodirect/api/services/SvcSearch.java +++ b/src/main/java/tech/autodirect/api/services/SvcSearch.java @@ -41,7 +41,8 @@ public class SvcSearch { private final TableUsersInterface tableUsers; private final TableOffersInterface tableOffers; private final SensoApiInterface sensoApi; - private final List valuesOfSortBy = Arrays.asList("price", "payment_mo", "apr", "total_sum", "term_length"); + private final List valuesOfSortBy + = Arrays.asList("price", "payment_mo", "interest_rate", "total_sum", "term_mo"); private final List valuesOfSortAsc = Arrays.asList("true", "false"); public SvcSearch( @@ -140,8 +141,6 @@ private List> searchAllCars( } List> carsMapsAll = this.tableCars.getAllCars(); - // TODO: Tell Samm that pre-login should only have price filtering (hardcode "price" so not care about - // sortBy when pre-login?) return sortCars(carsMapsAll, sortBy, sortAsc); } @@ -277,7 +276,7 @@ private EntOffer createOfferFromUserAndCar ( // Query senso Api for this car and user information Map queryResult = this.sensoApi.getLoanOffer( - Double.toString(car.getPrice()), // loanAmount (TODO: verify correct) + Double.toString(car.getPrice()), // loanAmount Integer.toString(user.getCreditScore()), // creditScore Double.toString(budgetMo), // budget car.getBrand(), // vehicleMake diff --git a/src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java b/src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java index 6d7a920..cf39e5e 100644 --- a/src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java +++ b/src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java @@ -25,8 +25,8 @@ public static Map mergeCarAndOffer(EntCar car, EntOffer offer) { put("capital_sum", offer.getCapitalSum()); put("interest_sum", offer.getInterestSum()); put("total_sum", offer.getTotalSum()); - put("apr", offer.getInterestRate()); // TODO: why "apr" - put("term_mo", offer.getTermMo()); // TODO: clean term_length/term_mo and all these terms + put("interest_rate", offer.getInterestRate()); + put("term_mo", offer.getTermMo()); put("installments", offer.getInstallments()); put("claimed", offer.isClaimed()); // Computed stuff diff --git a/src/test/java/tech/autodirect/api/database/TableOffersTests.java b/src/test/java/tech/autodirect/api/database/TableOffersTests.java index 1002e18..7fa904a 100644 --- a/src/test/java/tech/autodirect/api/database/TableOffersTests.java +++ b/src/test/java/tech/autodirect/api/database/TableOffersTests.java @@ -17,7 +17,7 @@ import java.util.Objects; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class TableOffersTests { private static final String dbName = "testing"; diff --git a/src/test/java/tech/autodirect/api/database/TableUsersTest.java b/src/test/java/tech/autodirect/api/database/TableUsersTest.java index 529ae8a..7664b6a 100644 --- a/src/test/java/tech/autodirect/api/database/TableUsersTest.java +++ b/src/test/java/tech/autodirect/api/database/TableUsersTest.java @@ -13,7 +13,7 @@ import java.util.Map; import java.util.Objects; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TableUsersTest { private static final String dbName = "testing"; diff --git a/src/test/java/tech/autodirect/api/services/SvcClaimOfferTest.java b/src/test/java/tech/autodirect/api/services/SvcClaimOfferTest.java index e63b37f..ae026d7 100644 --- a/src/test/java/tech/autodirect/api/services/SvcClaimOfferTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcClaimOfferTest.java @@ -11,7 +11,7 @@ import java.sql.SQLException; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SvcClaimOfferTest { private static final String dbName = "testing"; diff --git a/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java b/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java index 7d78b58..457a36e 100644 --- a/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcGetClaimedOffersTest.java @@ -17,7 +17,7 @@ import java.util.Map; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SvcGetClaimedOffersTest { private static final String dbName = "testing"; diff --git a/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java b/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java index ad11380..edf6564 100644 --- a/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcGetOfferDetailsTest.java @@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.*; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SvcGetOfferDetailsTest { private static final String dbName = "testing"; @@ -44,7 +44,7 @@ void getOfferDetails() { assert (double) carAndOfferInfo.get("capital_sum") == 3; assert (double) carAndOfferInfo.get("interest_sum") == 4; assert (double) carAndOfferInfo.get("total_sum") == 5; - assert (double) carAndOfferInfo.get("apr") == 6; + assert (double) carAndOfferInfo.get("interest_rate") == 6; assert (double) carAndOfferInfo.get("term_mo") == 7; assert Objects.equals(carAndOfferInfo.get("installments"), "TEST"); assert (boolean) carAndOfferInfo.get("claimed"); diff --git a/src/test/java/tech/autodirect/api/services/SvcSearchTest.java b/src/test/java/tech/autodirect/api/services/SvcSearchTest.java index b696832..d711b35 100644 --- a/src/test/java/tech/autodirect/api/services/SvcSearchTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcSearchTest.java @@ -19,7 +19,7 @@ import java.util.Map; import java.util.Objects; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SvcSearchTest { private static final String dbName = "testing"; @@ -58,7 +58,7 @@ void testSearchCarsPreLoginWithAllEmpty() { void testSearchCarsPreLoginInvalidSortBy() { try { tableUsers.addUser(testUserId, 700, 1000, 200); - List> carsResult = svcSearch.search("", "1000", "200", "term_length", "true"); + List> carsResult = svcSearch.search("", "1000", "200", "term_mo", "true"); assert carsResult.size() > 0; } catch (IOException | InterruptedException | SQLException | ClassNotFoundException e) { e.printStackTrace(); @@ -84,7 +84,7 @@ void testSearchCarsPreLoginCheckSorting() { @Test void testSearchCarsPostLoginCheckSorting() { try { - List valuesOfSortBy = Arrays.asList("price", "payment_mo", "apr", "total_sum", "term_length"); + List valuesOfSortBy = Arrays.asList("price", "payment_mo", "interest_rate", "total_sum", "term_mo"); List valuesOfSortAsc = Arrays.asList("true", "false"); testSorting(testUserId, valuesOfSortBy, valuesOfSortAsc); } catch (IOException | InterruptedException | SQLException | ClassNotFoundException e) { diff --git a/src/test/java/tech/autodirect/api/services/SvcUnclaimOfferTest.java b/src/test/java/tech/autodirect/api/services/SvcUnclaimOfferTest.java index b55612a..69b684b 100644 --- a/src/test/java/tech/autodirect/api/services/SvcUnclaimOfferTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcUnclaimOfferTest.java @@ -10,7 +10,7 @@ import java.sql.SQLException; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SvcUnclaimOfferTest { private static final String dbName = "testing"; diff --git a/src/test/java/tech/autodirect/api/services/SvcUserLoginTest.java b/src/test/java/tech/autodirect/api/services/SvcUserLoginTest.java index f6ce392..acc20b7 100644 --- a/src/test/java/tech/autodirect/api/services/SvcUserLoginTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcUserLoginTest.java @@ -13,7 +13,7 @@ import java.util.Map; -// This annotation allows us to use a non-static BeforeAll/AfterAll methods (TODO: check if ok) +// This annotation allows us to use a non-static BeforeAll/AfterAll methods @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class SvcUserLoginTest { private static final String dbName = "testing"; From e779b9d02b94b3249e51973467c1130b2abecd85 Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:24:36 -0500 Subject: [PATCH 07/11] fixed some tests that were not updated --- .../java/tech/autodirect/api/database/InitDatabase.java | 2 +- .../java/tech/autodirect/api/database/TableCarsTest.java | 4 ++-- .../java/tech/autodirect/api/entities/EntCarTest.java | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/tech/autodirect/api/database/InitDatabase.java b/src/main/java/tech/autodirect/api/database/InitDatabase.java index cce515c..552dab1 100644 --- a/src/main/java/tech/autodirect/api/database/InitDatabase.java +++ b/src/main/java/tech/autodirect/api/database/InitDatabase.java @@ -28,7 +28,7 @@ * Responsible for initializing the populating the databse with schemas, tables, and entries. */ public class InitDatabase { - private static final String dbName = "autodirect"; + private static final String dbName = "testing"; /** * Populate the database with the cars and users tables. diff --git a/src/test/java/tech/autodirect/api/database/TableCarsTest.java b/src/test/java/tech/autodirect/api/database/TableCarsTest.java index 4164654..001a52d 100644 --- a/src/test/java/tech/autodirect/api/database/TableCarsTest.java +++ b/src/test/java/tech/autodirect/api/database/TableCarsTest.java @@ -21,7 +21,7 @@ void testGetAllCars() { EntCar car1 = new EntCar(); car1.loadFromMap(new HashMap<>() { { - put("id", 1); + put("car_id", 1); put("brand", "nissan"); put("model", "sedan"); put("year", 2017); @@ -33,7 +33,7 @@ void testGetAllCars() { EntCar car2 = new EntCar(); car2.loadFromMap(new HashMap<>() { { - put("id", 7); + put("car_id", 7); put("brand", "ford"); put("model", "mustang"); put("year", 2019); diff --git a/src/test/java/tech/autodirect/api/entities/EntCarTest.java b/src/test/java/tech/autodirect/api/entities/EntCarTest.java index ecf589c..835ee1b 100644 --- a/src/test/java/tech/autodirect/api/entities/EntCarTest.java +++ b/src/test/java/tech/autodirect/api/entities/EntCarTest.java @@ -16,7 +16,7 @@ void testLoadFromMap() { try { Map carMap = new HashMap<>() { { - put("id", 1); + put("car_id", 1); put("brand", "nissan"); put("model", "sedan"); put("year", 2017); @@ -44,7 +44,7 @@ void testEqualsWhenSame() { try { Map carMap1 = new HashMap<>() { { - put("id", 1); + put("car_id", 1); put("brand", "nissan"); put("model", "sedan"); put("year", 2017); @@ -70,7 +70,7 @@ void testEqualsWhenNotSame() { try { Map carMap1 = new HashMap<>() { { - put("id", 1); + put("car_id", 1); put("brand", "nissan"); put("model", "sedan"); put("year", 2017); @@ -80,7 +80,7 @@ void testEqualsWhenNotSame() { }; Map carMap2 = new HashMap<>() { { - put("id", 7); + put("car_id", 7); put("brand", "ford"); put("model", "mustang"); put("year", 2019); From 9a8e0011f644a27388f4ced1535d40af588c59b8 Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:28:08 -0500 Subject: [PATCH 08/11] updated javadocs --- .../java/tech/autodirect/api/services/SvcSearch.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/tech/autodirect/api/services/SvcSearch.java b/src/main/java/tech/autodirect/api/services/SvcSearch.java index c925308..1a2c6cf 100644 --- a/src/main/java/tech/autodirect/api/services/SvcSearch.java +++ b/src/main/java/tech/autodirect/api/services/SvcSearch.java @@ -176,7 +176,7 @@ private List> searchOnlyCarsWithOffer( /** * Resets user's offers table, queries Senso Api for new loan offer information using search params, * adds approved offers to the user's offers table, and return maps that contain car and offers information - * (made using mergeCarOffer()) using the approved offers that were added to the user's offers table. + * (made using MergeCarAndOffer.mergeCarAndOffer()) using the approved offers that were added to the user's offers table. * * Run this method if user's search params are different to previous search (what's currently in the database). * Thus, we need to reset the offers table and check loan offer approval for each car with the senso /rate api @@ -226,10 +226,10 @@ private List> searchCarsWithOfferNewParams( } /** - * Returns maps that contain car and offers information (made using mergeCarOffer()) using the offers that - * are currently in the user's offers table (does not reset the offers table or re-call senso Api to - * check whether loan offers are approved, since these are already assumed to be approved in a previous - * search query with the same search params). + * Returns maps that contain car and offers information (made using MergeCarAndOffer.mergeCarAndOffer()) + * using the offers that are currently in the user's offers table (does not reset the offers table or re-call + * senso Api to check whether loan offers are approved, since these are already assumed to be approved + * in a previous search query with the same search params). */ private List> searchCarsWithOfferOldParams( EntUser user, From 8c3aebe329b738524a6667b9e7e207238d3cb5df Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:29:22 -0500 Subject: [PATCH 09/11] renamed vars --- .../tech/autodirect/api/services/SvcSearchTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/tech/autodirect/api/services/SvcSearchTest.java b/src/test/java/tech/autodirect/api/services/SvcSearchTest.java index d711b35..9d50ebf 100644 --- a/src/test/java/tech/autodirect/api/services/SvcSearchTest.java +++ b/src/test/java/tech/autodirect/api/services/SvcSearchTest.java @@ -107,15 +107,15 @@ void testSorting( tableUsers.addUser(userId, 700, 1000, 200); } - List> carsAndOffers = svcSearch.search( + List> carAndOfferInfoMaps = svcSearch.search( userId, "1000", "200", sortBy, sortAscString ); - assert carsAndOffers.size() > 0; + assert carAndOfferInfoMaps.size() > 0; boolean sortAsc = Boolean.parseBoolean(sortAscString); - double prev = TypeConvert.toDouble(carsAndOffers.get(0).get(sortBy)); - for (Map carAndOffer : carsAndOffers) { - double valueDouble = TypeConvert.toDouble(carAndOffer.get(sortBy)); + double prev = TypeConvert.toDouble(carAndOfferInfoMaps.get(0).get(sortBy)); + for (Map carAndOfferInfo : carAndOfferInfoMaps) { + double valueDouble = TypeConvert.toDouble(carAndOfferInfo.get(sortBy)); if (sortAsc) { assert prev <= valueDouble; From 841431fc9e64e809a5c67494f0d48251b24f1941 Mon Sep 17 00:00:00 2001 From: Shalev Lifshitz Date: Thu, 9 Dec 2021 04:34:04 -0500 Subject: [PATCH 10/11] accidentally changed InitDatabase dbName to "testing", put back to "autodirect" --- src/main/java/tech/autodirect/api/database/InitDatabase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/tech/autodirect/api/database/InitDatabase.java b/src/main/java/tech/autodirect/api/database/InitDatabase.java index 552dab1..cce515c 100644 --- a/src/main/java/tech/autodirect/api/database/InitDatabase.java +++ b/src/main/java/tech/autodirect/api/database/InitDatabase.java @@ -28,7 +28,7 @@ * Responsible for initializing the populating the databse with schemas, tables, and entries. */ public class InitDatabase { - private static final String dbName = "testing"; + private static final String dbName = "autodirect"; /** * Populate the database with the cars and users tables. From 4658ca5b063aacb9cafe03f9448230403a7c4d8b Mon Sep 17 00:00:00 2001 From: sammdu Date: Thu, 9 Dec 2021 04:46:12 -0500 Subject: [PATCH 11/11] fixed DB ingest issue --- src/main/java/tech/autodirect/api/database/InitDatabase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/tech/autodirect/api/database/InitDatabase.java b/src/main/java/tech/autodirect/api/database/InitDatabase.java index cce515c..76f2ed8 100644 --- a/src/main/java/tech/autodirect/api/database/InitDatabase.java +++ b/src/main/java/tech/autodirect/api/database/InitDatabase.java @@ -105,7 +105,7 @@ private static void exitWithHelp(int exitCode) { private static void createCarsTable(Connection conn) throws SQLException { PreparedStatement stmt = conn.prepareStatement( "CREATE TABLE IF NOT EXISTS public.cars (" + - "car_id integer NOT NULL PRIMARY KEY, " + + "car_id serial NOT NULL PRIMARY KEY, " + "brand varchar(50) NOT NULL, " + "model varchar(50) NOT NULL, " + "year integer NOT NULL, " +