diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c647bf8..19bf084 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -362,4 +362,82 @@ jobs: # with tag from the build-and-publish-docker-image job in the output_tags step image_tag: "${{ needs.build-and-publish-docker-image.outputs.image_tag }}" debug: "${{ github.event.inputs.debug_deployment }}" - secrets: inherit \ No newline at end of file + secrets: inherit + + integration-tests: + name: INTEGRATION-TESTS + runs-on: ubuntu-latest + needs: [build] + container: + image: ghcr.io/tsviz/tsvi-spring-test:v2.0.0 + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + ports: + - 5432:5432 + redis: + image: redis + ports: + - 6379:6379 + permissions: + contents: read + packages: write + id-token: write + steps: + - name: Checkout repository + uses: actions/checkout@v4.2.2 + - name: Cache Maven packages + uses: actions/cache@v4.1.2 + with: + path: /root/.m2 + key: ${{ runner.os }}-integration-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-integration- + - name: Run integration tests + run: | + mvn verify -Dspring.datasource.url=${{ secrets.LIQUIBASE_COMMAND_URL }} -Dspring.datasource.username=${{ secrets.LIQUIBASE_COMMAND_USERNAME }} -Dspring.datasource.password=${{ secrets.LIQUIBASE_COMMAND_PASSWORD }} -Dspring.liquibase.change-log=classpath:db/changelog/changelog_version-3.3.xml -Dserver.port=8086 -Dspring.redis.host=redis -Dspring.redis.port=6379 -Dspring.redis.mode=standalone + - uses: actions/upload-artifact@v4.4.3 + with: + name: integration-test-results + path: ./target/surefire-reports + retention-days: 90 + + end-to-end-tests: + name: END-TO-END-TESTS + runs-on: ubuntu-latest + needs: [build] + container: + image: ghcr.io/tsviz/tsvi-spring-test:v2.0.0 + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + ports: + - 5432:5432 + redis: + image: redis + ports: + - 6379:6379 + permissions: + contents: read + packages: write + id-token: write + steps: + - name: Checkout repository + uses: actions/checkout@v4.2.2 + - name: Cache Maven packages + uses: actions/cache@v4.1.2 + with: + path: /root/.m2 + key: ${{ runner.os }}-e2e-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-e2e- + - name: Run end-to-end tests + run: | + mvn verify -Dspring.datasource.url=${{ secrets.LIQUIBASE_COMMAND_URL }} -Dspring.datasource.username=${{ secrets.LIQUIBASE_COMMAND_USERNAME }} -Dspring.datasource.password=${{ secrets.LIQUIBASE_COMMAND_PASSWORD }} -Dspring.liquibase.change-log=classpath:db/changelog/changelog_version-3.3.xml -Dserver.port=8086 -Dspring.redis.host=redis -Dspring.redis.port=6379 -Dspring.redis.mode=standalone + - uses: actions/upload-artifact@v4.4.3 + with: + name: e2e-test-results + path: ./target/surefire-reports + retention-days: 90 diff --git a/src/test/java/net/codejava/JUnit5ExampleTest1.java b/src/test/java/net/codejava/JUnit5ExampleTest1.java old mode 100755 new mode 100644 index 75e98a4..5ef7d2a --- a/src/test/java/net/codejava/JUnit5ExampleTest1.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest1.java @@ -1,17 +1,72 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest1 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest1 successful\n\n"); + System.out.println("\n\nTest1 successful\n\n"); + } + + @Test + void contextLoads() { } @Test - void contextLoads() { - } + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest1-2 Successful!\n\n"); + } + + @Test + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest1-3 Successful!\n\n"); + } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest10.java b/src/test/java/net/codejava/JUnit5ExampleTest10.java index 30613ef..21e471b 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest10.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest10.java @@ -1,33 +1,72 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest10 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest10-1 Successful!\n\n"); + System.out.println("\n\nTest10-1 Successful!\n\n"); } @Test - void contextLoads() { - } + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest10-2 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest10-2 Successful!\n\n"); + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest10-3 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest11.java b/src/test/java/net/codejava/JUnit5ExampleTest11.java index 4e665e4..e8545b6 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest11.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest11.java @@ -1,70 +1,148 @@ package net.codejava; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.Calendar; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertNotNull; @SpringBootTest public class JUnit5ExampleTest11 { @Autowired - // create private instance of SalesDAO - private SalesDAO salesDAO = new SalesDAO(); - // This field is used to inject an instance of the AppController class. + private SalesDAO salesDAO; + @Autowired private AppController appController; @Test void testInsert() { - Calendar calendar = Calendar.getInstance(); - calendar.set(2021, Calendar.FEBRUARY, 1); - java.util.Date utilDate = calendar.getTime(); - java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); - - // Generate a unique serial number based on the current timestamp - String serialNumber = String.valueOf(System.currentTimeMillis()); - - Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); - salesDAO.save(sale); - - // list all the records - List listSale = salesDAO.list(10, 0); - - // Find the sale with the matching serial number - Sale insertedSale = listSale.stream() - .filter(s -> s.getSerialNumber().equals(serialNumber)) - .findFirst() - .orElse(null); - - System.out.println("\n\n"); - System.out.println("--------------------------------------------------------------------------------"); - System.out.println("Expected value of item: Laptop"); - System.out.println("Actual value of item: " + insertedSale.getItem()); - System.out.println("--------------------------------------------------------------------------------"); - assertNotNull(insertedSale, "Inserted sale not found"); - assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); - - // clean up the database - salesDAO.delete(serialNumber); - System.out.println("\n\nTest11-1 Successful!\n\n"); + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest11-1 Successful!\n\n"); } - // test the variable enableSearchFeature in AppController.java @Test void testEnableSearchFeature() { - // print a comment about the value of enableSearchFeature - System.out.println("\n\n"); - System.out.println("--------------------------------------------------------------------------------"); - System.out.println("Expected value of enableSearchFeature: true"); - System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); - System.out.println("--------------------------------------------------------------------------------"); + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest11-2 Successful!\n\n"); + } + + @Test + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest11-3 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest11-4 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); - // assert that the value of enableSearchFeature is true - assertEquals(true, appController.getEnableSearchFeature()); + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); - System.out.println("\n\nTest11-2 Successful!\n\n"); + salesDAO.delete(serialNumber); + System.out.println("\n\nTest11-5 Successful!\n\n"); } - } \ No newline at end of file +} diff --git a/src/test/java/net/codejava/JUnit5ExampleTest2.java b/src/test/java/net/codejava/JUnit5ExampleTest2.java old mode 100755 new mode 100644 index cfb3884..bde3103 --- a/src/test/java/net/codejava/JUnit5ExampleTest2.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest2.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest2 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest2-1 Successful!\n\n"); + System.out.println("\n\nTest2-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest2-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest2-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest2-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest2-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest2-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest2-6 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest3.java b/src/test/java/net/codejava/JUnit5ExampleTest3.java index 07e7a3a..7d7ef4e 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest3.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest3.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest3 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest3-1 Successful!\n\n"); + System.out.println("\n\nTest3-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest3-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest3-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest3-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest3-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest3-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest3-6 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest4.java b/src/test/java/net/codejava/JUnit5ExampleTest4.java index 158763f..e2b4609 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest4.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest4.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest4 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest4-1 Successful!\n\n"); + System.out.println("\n\nTest4-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest4-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest4-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest4-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest4-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest4-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest4-6 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest5.java b/src/test/java/net/codejava/JUnit5ExampleTest5.java index 08268b7..1f13ba7 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest5.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest5.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest5 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest5-1 Successful!\n\n"); + System.out.println("\n\nTest5-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest5-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest5-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest5-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest5-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest5-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest5-6 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest6.java b/src/test/java/net/codejava/JUnit5ExampleTest6.java index 8f6bf3d..7444cda 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest6.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest6.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest6 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest6-1 Successful!\n\n"); + System.out.println("\n\nTest6-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest6-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest6-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest6-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest6-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest6-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest6-6 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest7.java b/src/test/java/net/codejava/JUnit5ExampleTest7.java index d496dc7..2604044 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest7.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest7.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest7 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest7-1 Successful!\n\n"); + System.out.println("\n\nTest7-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest7-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest7-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest7-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest7-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest7-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest7-6 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest8.java b/src/test/java/net/codejava/JUnit5ExampleTest8.java index dd6884a..7aaea00 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest8.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest8.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest8 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest8-1 Successful!\n\n"); + System.out.println("\n\nTest8-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest8-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest8-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest8-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest8-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest8-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest8-6 Successful!\n\n"); } } diff --git a/src/test/java/net/codejava/JUnit5ExampleTest9.java b/src/test/java/net/codejava/JUnit5ExampleTest9.java index a5de2e3..f5cf66a 100644 --- a/src/test/java/net/codejava/JUnit5ExampleTest9.java +++ b/src/test/java/net/codejava/JUnit5ExampleTest9.java @@ -1,33 +1,157 @@ package net.codejava; -import org.junit.jupiter.api.Test; -// import org.springframework.boot.test.context.SpringBootTest; -// import java.util.*; -// import static org.junit.Assert.*; -// import org.junit.Test; -// import org.junit.jupiter.api.DisplayName; -// import org.junit.jupiter.api.Nested; -// import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; -// import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Calendar; +import java.util.List; -// @SpringBootTest +@SpringBootTest class JUnit5ExampleTest9 { + @Autowired + private SalesDAO salesDAO; + + @Autowired + private AppController appController; + @Test void justAnExample() { - System.out.println("\n\nTest9-1 Successful!\n\n"); + System.out.println("\n\nTest9-1 Successful!\n\n"); + } + + @Test + void contextLoads() { + } + + @Test + void testInsert() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + List listSale = salesDAO.list(10, 0); + + Sale insertedSale = listSale.stream() + .filter(s -> s.getSerialNumber().equals(serialNumber)) + .findFirst() + .orElse(null); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Laptop"); + System.out.println("Actual value of item: " + insertedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(insertedSale, "Inserted sale not found"); + assertEquals("Laptop", insertedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest9-2 Successful!\n\n"); } @Test - void contextLoads() { - } + void testEnableSearchFeature() { + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of enableSearchFeature: true"); + System.out.println("Actual value of enableSearchFeature: " + appController.getEnableSearchFeature()); + System.out.println("--------------------------------------------------------------------------------"); + + assertEquals(true, appController.getEnableSearchFeature()); + + System.out.println("\n\nTest9-3 Successful!\n\n"); + } - int ACTUAL = 9; - int EXPECTED = 4; @Test - void shouldNotBeEqual() { - assertEquals(EXPECTED, ACTUAL-5); - System.out.println("\n\nTest9-2 Successful!\n\n"); + void testUpdate() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + sale.setItem("Updated Laptop"); + salesDAO.update(sale); + + Sale updatedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of item: Updated Laptop"); + System.out.println("Actual value of item: " + updatedSale.getItem()); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(updatedSale, "Updated sale not found"); + assertEquals("Updated Laptop", updatedSale.getItem(), "Item name does not match"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest9-4 Successful!\n\n"); + } + + @Test + void testDelete() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.delete(serialNumber); + + Sale deletedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of deleted sale: null"); + System.out.println("Actual value of deleted sale: " + deletedSale); + System.out.println("--------------------------------------------------------------------------------"); + assertNotNull(deletedSale, "Deleted sale should be null"); + + System.out.println("\n\nTest9-5 Successful!\n\n"); + } + + @Test + void testClearRecord() { + Calendar calendar = Calendar.getInstance(); + calendar.set(2021, Calendar.FEBRUARY, 1); + java.util.Date utilDate = calendar.getTime(); + java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); + + String serialNumber = String.valueOf(System.currentTimeMillis()); + + Sale sale = new Sale(serialNumber, "Laptop", 1, 1500.00f, sqlDate); + salesDAO.save(sale); + + salesDAO.clearRecord(serialNumber); + + Sale clearedSale = salesDAO.get(serialNumber); + + System.out.println("\n\n"); + System.out.println("--------------------------------------------------------------------------------"); + System.out.println("Expected value of quantity: 0"); + System.out.println("Actual value of quantity: " + clearedSale.getQuantity()); + System.out.println("Expected value of amount: 0.0"); + System.out.println("Actual value of amount: " + clearedSale.getAmount()); + System.out.println("--------------------------------------------------------------------------------"); + assertEquals(0, clearedSale.getQuantity(), "Quantity should be 0"); + assertEquals(0.0f, clearedSale.getAmount(), "Amount should be 0.0"); + + salesDAO.delete(serialNumber); + System.out.println("\n\nTest9-6 Successful!\n\n"); } }