Skip to content
Snippets Groups Projects

updated the auction tests so that they are tested correctly

Merged Osama Halabi requested to merge sub-task/ASDCBA-683-auction-dao-test into feature/ASDCBA-000-ui
Compare and
1 file
+ 70
11
Compare changes
  • Side-by-side
  • Inline
@@ -38,37 +38,73 @@ public class AuctionDAOImplUnitTest {
@Test
void testGetAuction() {
List<AuctionDTO> beforeDatabase = getAllAuctions();
// Act
AuctionDTO auction = sut.getAuction("auction1");
// Assert
assertNotNull(auction);
assertEquals("auction1", auction.getAuctionid());
assertEquals("auction1", auction.getAuctionType());
assertEquals("auction1", auction.getCategory());
List<AuctionDTO> afterDatabase = getAllAuctions();
for (int i = 0; i < afterDatabase.size(); i++) {
if(beforeDatabase.get(i).getAuctionid().equals(auction.getAuctionid())){
continue;
}
assertEquals(beforeDatabase.get(i).getAuctionName(),afterDatabase.get(i).getAuctionName());
assertEquals(beforeDatabase.get(i).getStartDate(),afterDatabase.get(i).getStartDate());
assertEquals(beforeDatabase.get(i).getEndDate(),afterDatabase.get(i).getEndDate());
assertEquals(beforeDatabase.get(i).getAuctionType(),afterDatabase.get(i).getAuctionType());
assertEquals(beforeDatabase.get(i).getCategory(),afterDatabase.get(i).getCategory());
}
assertEquals(beforeDatabase.size(), afterDatabase.size());
}
@Test
void testDeleteAuctionAuction1() {
List<AuctionDTO> beforeDatabase = getAllAuctions();
// Act
AuctionDTO auction = getAuctionForTesting("auction1");
sut.deleteAuction(auction);
auction = getAuctionForTesting("auction1");
// Assert
assertNull(auction);
List<AuctionDTO> afterDatabase = getAllAuctions();
for (int i = 0; i < afterDatabase.size(); i++) {
if(beforeDatabase.get(i).getAuctionid().equals(auction.getAuctionid())){
continue;
}
assertEquals(beforeDatabase.get(i).getAuctionName(),afterDatabase.get(i).getAuctionName());
assertEquals(beforeDatabase.get(i).getStartDate(),afterDatabase.get(i).getStartDate());
assertEquals(beforeDatabase.get(i).getEndDate(),afterDatabase.get(i).getEndDate());
assertEquals(beforeDatabase.get(i).getAuctionType(),afterDatabase.get(i).getAuctionType());
assertEquals(beforeDatabase.get(i).getCategory(),afterDatabase.get(i).getCategory());
}
assertEquals(beforeDatabase.size() - 1, afterDatabase.size());
}
@Test
void testDeleteAuctionAuction2() {
List<AuctionDTO> beforeDatabase = getAllAuctions();
// Act
sut.deleteAuction("auction2");
AuctionDTO auction = getAuctionForTesting("auction2");
sut.deleteAuction(auction);
// Assert
assertNull(auction);
List<AuctionDTO> afterDatabase = getAllAuctions();
for (int i = 0; i < afterDatabase.size(); i++) {
if(beforeDatabase.get(i).getAuctionid().equals(auction.getAuctionid())){
continue;
}
assertEquals(beforeDatabase.get(i).getAuctionName(),afterDatabase.get(i).getAuctionName());
assertEquals(beforeDatabase.get(i).getStartDate(),afterDatabase.get(i).getStartDate());
assertEquals(beforeDatabase.get(i).getEndDate(),afterDatabase.get(i).getEndDate());
assertEquals(beforeDatabase.get(i).getAuctionType(),afterDatabase.get(i).getAuctionType());
assertEquals(beforeDatabase.get(i).getCategory(),afterDatabase.get(i).getCategory());
}
assertEquals(beforeDatabase.size() - 1, afterDatabase.size());
}
/*
@@ -77,6 +113,8 @@ public class AuctionDAOImplUnitTest {
@Test
void testUpdateOrCreateAuctionUpdate() {
List<AuctionDTO> beforeDatabase = getAllAuctions();
// Act
AuctionDTO auction = getAuctionForTesting("auction1");
auction.setAuctionType("updatedAuction1");
@@ -85,8 +123,21 @@ public class AuctionDAOImplUnitTest {
auction = getAuctionForTesting("auction1");
// Assert
assertEquals("updatedAuction1", auction.getAuctionType());
assertEquals("updatedAuction1", auction.getCategory());
List<AuctionDTO> afterDatabase = getAllAuctions();
for (int i = 0; i < afterDatabase.size(); i++) {
assertEquals(beforeDatabase.get(i).getAuctionName(),afterDatabase.get(i).getAuctionName());
assertEquals(beforeDatabase.get(i).getStartDate(),afterDatabase.get(i).getStartDate());
assertEquals(beforeDatabase.get(i).getEndDate(),afterDatabase.get(i).getEndDate());
if(afterDatabase.get(i).getAuctionid().equals(auction.getAuctionid())) {
assertNotEquals(beforeDatabase.get(i).getAuctionType(),afterDatabase.get(i).getAuctionType());
assertNotEquals(beforeDatabase.get(i).getCategory(),afterDatabase.get(i).getCategory());
}
else {
assertEquals(beforeDatabase.get(i).getAuctionType(),afterDatabase.get(i).getAuctionType());
assertEquals(beforeDatabase.get(i).getCategory(),afterDatabase.get(i).getCategory());
}
}
}
@@ -97,6 +148,7 @@ public class AuctionDAOImplUnitTest {
void testUpdateOrCreateAuctionCreate() {
// Arrange
List<AuctionDTO> beforeDatabase = getAllAuctions();
AuctionDTO newAuction = new AuctionDTO();
newAuction.setAuctionid("auction3");
newAuction.setAuctionName("auction3");
@@ -110,7 +162,8 @@ public class AuctionDAOImplUnitTest {
newAuction = getAuctionForTesting("auction3");
// Assert
assertNotNull(newAuction);
List<AuctionDTO> afterDatabase = getAllAuctions();
assertEquals(beforeDatabase.size() + 1, afterDatabase.size());
}
@@ -147,4 +200,10 @@ public class AuctionDAOImplUnitTest {
return session.get(AuctionDTO.class, id);
}
}
private List<AuctionDTO> getAllAuctions() {
try (Session session = sessionFactory.openSession()) {
return session.createQuery("from Auction", AuctionDTO.class).list();
}
}
}