From 9680d6a441d6b53dd2655e9844178868c5e47645 Mon Sep 17 00:00:00 2001 From: Edward Storlien Date: Tue, 7 Nov 2023 10:09:04 +0100 Subject: [PATCH 01/14] Issue #118: Add new module 'e2e-tests' --- vendmachtrack/e2e-tests/pom.xml | 20 +++++++++++++++++++ .../e2e-tests/src/main/java/gr2338/Main.java | 7 +++++++ vendmachtrack/pom.xml | 1 + 3 files changed, 28 insertions(+) create mode 100644 vendmachtrack/e2e-tests/pom.xml create mode 100644 vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java diff --git a/vendmachtrack/e2e-tests/pom.xml b/vendmachtrack/e2e-tests/pom.xml new file mode 100644 index 0000000..9125d93 --- /dev/null +++ b/vendmachtrack/e2e-tests/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + gr2338 + vendmachtrack + 1.0 + + + e2e-tests + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java b/vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java new file mode 100644 index 0000000..b1c0b21 --- /dev/null +++ b/vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java @@ -0,0 +1,7 @@ +package gr2338; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/vendmachtrack/pom.xml b/vendmachtrack/pom.xml index 83a006a..a3b234b 100644 --- a/vendmachtrack/pom.xml +++ b/vendmachtrack/pom.xml @@ -21,6 +21,7 @@ springboot ui jacoco-aggregator + e2e-tests From 95f26c71509255602679dbe1bf9cac5a62cd8f61 Mon Sep 17 00:00:00 2001 From: Edward Storlien Date: Tue, 7 Nov 2023 10:12:38 +0100 Subject: [PATCH 02/14] Issue #118: Update pom.xml file to version 3.0 --- vendmachtrack/e2e-tests/pom.xml | 2 +- vendmachtrack/jacoco-aggregator/pom.xml | 5 +++++ vendmachtrack/pom.xml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/vendmachtrack/e2e-tests/pom.xml b/vendmachtrack/e2e-tests/pom.xml index 9125d93..ea84050 100644 --- a/vendmachtrack/e2e-tests/pom.xml +++ b/vendmachtrack/e2e-tests/pom.xml @@ -6,7 +6,7 @@ gr2338 vendmachtrack - 1.0 + 3.0 e2e-tests diff --git a/vendmachtrack/jacoco-aggregator/pom.xml b/vendmachtrack/jacoco-aggregator/pom.xml index a3b7d43..8130378 100644 --- a/vendmachtrack/jacoco-aggregator/pom.xml +++ b/vendmachtrack/jacoco-aggregator/pom.xml @@ -39,6 +39,11 @@ springboot ${project.version} + + + + + diff --git a/vendmachtrack/pom.xml b/vendmachtrack/pom.xml index 91cdbca..c39d5b0 100644 --- a/vendmachtrack/pom.xml +++ b/vendmachtrack/pom.xml @@ -20,8 +20,8 @@ jsonio springboot ui - jacoco-aggregator e2e-tests + jacoco-aggregator From 5f5c30b858f4375645f9770c57d55723bf56f6e2 Mon Sep 17 00:00:00 2001 From: Carl Edward Storlien Date: Wed, 8 Nov 2023 00:21:08 +0100 Subject: [PATCH 03/14] Issue #124: Update .gitlab-ci.yml file to delete job artifacts after 1 week --- .gitlab-ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b8c202c..883e52b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -28,6 +28,7 @@ checkstyle: when: always paths: - $CI_PROJECT_DIR/vendmachtrack/test-results/checkstyle/*.xml + expire_in: 1 week needs: - build @@ -42,6 +43,7 @@ spotbugs: when: always paths: - $CI_PROJECT_DIR/vendmachtrack/test-results/spotbugs/**/spotbugs.xml + expire_in: 1 week needs: - build @@ -56,6 +58,7 @@ test: when: always reports: junit: $CI_PROJECT_DIR/vendmachtrack/test-results/junit/**/*xml + expire_in: 1 week needs: - build @@ -72,7 +75,7 @@ verify: when: always paths: - $CI_PROJECT_DIR/vendmachtrack/test-results/jacoco/ - expire_in: 1 hour + expire_in: 1 week needs: - build - checkstyle From 44d4e5088b5c427efbe070836486e143120e79fc Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 01:09:32 +0100 Subject: [PATCH 04/14] Issue #123: create Integration test for SPringboot module - Made an integrationtests verifying that the different layers in th springboot module are integrated correctly - Made some changes in the module info so the tests could run --- .../springboot/src/main/java/module-info.java | 5 +- .../springboot/SpringBootIntegrationTest.java | 364 ++++++++++++++++++ 2 files changed, 367 insertions(+), 2 deletions(-) create mode 100644 vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java diff --git a/vendmachtrack/springboot/src/main/java/module-info.java b/vendmachtrack/springboot/src/main/java/module-info.java index 3ba932e..5fd92d9 100644 --- a/vendmachtrack/springboot/src/main/java/module-info.java +++ b/vendmachtrack/springboot/src/main/java/module-info.java @@ -11,7 +11,8 @@ // Necessary 'opens' for Spring Boot framework to work. opens vendmachtrack.springboot to spring.core, spring.beans, spring.context; - opens vendmachtrack.springboot.service to spring.core, org.mockito; + opens vendmachtrack.springboot.service to spring.core, org.mockito, spring.beans; + // These 'opens' are weak encapsulation, but we haven't managed to find which modules that are dependent on the 'controller' and 'exception' packages. // The error log says it's an unnamed module and our indications are that it is a Jackson module. This will be worked more on in a later release. @@ -22,6 +23,6 @@ // The reason why is that the Spring framework, especially the 'ReflectionTestUtils' class, needs // to gain access to the classes and their private fields within the 'springboot.repository' package. // This access is required for setting private fields during testing, such as mocking dependencies. - opens vendmachtrack.springboot.repository to org.mockito, spring.core; + opens vendmachtrack.springboot.repository to org.mockito, spring.core, spring.beans; } \ No newline at end of file diff --git a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java new file mode 100644 index 0000000..b849c75 --- /dev/null +++ b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java @@ -0,0 +1,364 @@ +package vendmachtrack.springboot; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.HashMap; + +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * Integration tests for a the springboot module. + * + * This class contains integration tests that validate the behavior of our Spring Boot application with REST API endpoints. The tests cover various aspects of the application, including HTTP requests and responses. + * + *

+ * The class is annotated with Spring Boot annotations to configure the test environment: + *

+ *
    + *
  • {@link ExtendWith(SpringExtension.class)}: Extends JUnit 5 with Spring support.
  • + *
  • {@link SpringBootTest}: Configures the test environment with a random port and starts the Spring application context.
  • + *
  • {@link TestMethodOrder(MethodOrderer.OrderAnnotation.class)}: Orders test methods based on annotated order.
  • + *
+ * + *

+ * The class includes instance variables for the server port and a TestRestTemplate for making HTTP requests to the application. + *

+ * + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class SpringBootIntegrationTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + + + + + + /** + * Tests adding a new vending machine via a REST API endpoint. + * + *

+ * This test verifies that a new vending machine can be successfully added by making a POST request + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the base URL, ID, and location for the new vending machine.
  2. + *
  3. Act: Make a POST request to add the new vending machine.
  4. + *
  5. Assert: Check that the response status code is HttpStatus.OK (200 OK) and the response body contains the added vending machine with the expected ID and location.
  6. + *
+ */ + @Test + @Order(1) + public void testAddVendMach() { + + // Arrange + String baseUrl = "http://localhost:" + port; + String newMachineId = "100"; + String newLocation = "Test Location"; + + // Act + ResponseEntity> response = restTemplate.exchange( + baseUrl + "/vendmachtrack/add?id=" + newMachineId + "&location=" + newLocation, + HttpMethod.POST, + null, + new ParameterizedTypeReference>() {} + ); + + // Assert + assertThat(response.getStatusCode()).as("Response body: " + response.getBody()) + .isEqualTo(HttpStatus.OK); + assertThat(response.getBody().get(Integer.parseInt(newMachineId))).isEqualTo(newLocation); + } + + /** + * Tests if we can get the location of an existing vending machine. + * + *

+ * + * This test checks if we can successfully find out where a vending machine is by making a GET request + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: prepare the necessary information; the service's URL and the ID of an existing vending machine.
  2. + *
  3. Act: Make a GET request to get the vending machine's location.
  4. + *
  5. Assert: Check that we receive a positive response (HTTP 200 OK) and that the location we receive matches the expected location ("Test Location").
  6. + *
+ */ + @Test + @Order(2) + public void testGetVendMachLocation() { + + // Arrange + String baseUrl = "http://localhost:" + port; + int existingMachineId = 100; + // Act + ResponseEntity response = restTemplate.getForEntity( + baseUrl + "/vendmachtrack/" + existingMachineId + "/name", + String.class + ); + + // Assert + assertThat(response.getStatusCode()).as("Response body: " + response.getBody()) + .isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).isNotNull(); + String location = response.getBody(); + assertThat(location).isEqualTo("Test Location"); + } + + /** + * Tests adding an item to an existing vending machine. + * + *

+ * + * This test checks if we can successfully put an item into a vending machine and verify that it has the correct quantity using a PUT request + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Prepare the necessary information; service's URL, vending machine ID, item name ("Cola") and quantity (5).
  2. + *
  3. Act: Make a PUT request to add the item to the vending machine.
  4. + *
  5. Assert: Check that we receive a positive response (HTTP 200 OK), and that the vending machine now contains the added item with the expected quantity.
  6. + *
+ */ + @Test + @Order(3) + public void testAddItem() { + + // Arrange + String baseUrl = "http://localhost:" + port; + int machineId = 100; + String item = "Cola"; + int quantity = 5; + + // Act + ResponseEntity> response = restTemplate.exchange( + baseUrl + "/vendmachtrack/" + machineId + "/add?item=" + item + "&quantity=" + quantity, + HttpMethod.PUT, + null, + new ParameterizedTypeReference>() {} + ); + + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).containsKey(item); + assertThat(response.getBody().get(item)).isEqualTo(quantity); + } + + /** + * Tests getting the quantity of a specific item from an existing vending machine. + * + *

+ * + * This test checks if we can successfully get the quantity of a specific item ("Cola") + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Prepare the necessary information; service's URL, vending machine ID, item name ("Cola") and quantity (5).
  2. + *
  3. Act: Make a GET request to the vending machine's inventory.
  4. + *
  5. Assert: Check that the response contains the expected item name and quantity ("Cola" with a quantity of 5).
  6. + *
+ */ + @Test + @Order(4) + public void testGetItem(){ + + String baseUrl = "http://localhost:" + port; + int machineId = 100; + String item = "Cola"; + int quantity = 5; + + ResponseEntity> inventoryResponse = restTemplate.exchange( + baseUrl + "/vendmachtrack/" + machineId, + HttpMethod.GET, + null, + new ParameterizedTypeReference>() {} + ); + assertThat(inventoryResponse.getBody()).containsEntry(item, quantity); + } + + /** + * Tests removing a specific quantity of an item from a vending machine. + * + *

+ * + * This test checks if we can successfully take a specific quantity of an item (e.g., "Cola") from a vending machine and verify the remaining quantity. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Prepare the necessary information; service's URL, vending machine ID, item name ("Cola") and quantity (3).
  2. + *
  3. Act: Make a PUT request to remove the quantity of the item from the vending machine.
  4. + *
  5. Assert: Check that the response contains the expected item name and quantity
  6. + *
+ */ + @Test + @Order(5) + public void testRemoveItem() { + + // Arrange + String baseUrl = "http://localhost:" + port; + int machineId = 100; + String item = "Cola"; + int quantityToRemove = 3; + + // Act + ResponseEntity> response = restTemplate.exchange( + baseUrl + "/vendmachtrack/" + machineId + "/remove?item=" + item + "&quantity=" + quantityToRemove, + HttpMethod.PUT, + null, + new ParameterizedTypeReference>() {} + ); + + // Assert + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.getBody()).containsKey(item); + int remainingQuantity = 2; + assertThat(remainingQuantity).isEqualTo(5 - quantityToRemove); + } + + /** + * Tests verifying the removal of a specific quantity of an item from an existing vending machine. + * + *

+ * + * This test checks if we can successfully confirm that a specific quantity of an item (e.g., "Cola") has been removed from a vending machine and that the remaining quantity matches the expected value. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Prepare the necessary information; service's URL, vending machine ID, item name ("Cola") and quantity (3).
  2. + *
  3. Act: Make a GET request the vending machine's inventory after the removal.
  4. + *
  5. Assert: Check that the response contains the expected item name and the remaining quantity matches the expected value
  6. + *
+ */ + @Test + @Order(6) + public void testremovedItem(){ + + // Arrange + String baseUrl = "http://localhost:" + port; + int machineId = 100; + String item = "Cola"; + int quantityToRemove = 3; + + //Act + ResponseEntity> inventoryResponse = restTemplate.exchange( + baseUrl + "/vendmachtrack/" + machineId, + HttpMethod.GET, + null, + new ParameterizedTypeReference>() {} + ); + + //Assert + assertThat(inventoryResponse.getBody()).containsEntry(item, 5 - quantityToRemove); + } + + + /** + * Tests removing a vending machine + * + *

+ * + * This test checks if we can successfully remove a vending machine by making a DELETE request. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Prepare the necessary information; the service's URL and the ID of the vending machine to remove.
  2. + *
  3. Act: Make a DELETE request to remove the vending machine.
  4. + *
  5. Assert: Check that the response indicates success (HTTP 200 OK), the response body is not null, and the removed vending machine's ID is no longer present in the response.
  6. + *
+ */ + @Test + @Order(7) + public void testRemoveVendMach() { + // Arrange + String baseUrl = "http://localhost:" + port; + String machineIdToRemove = "100"; + + // Act + ResponseEntity> removeResponse = restTemplate.exchange( + baseUrl + "/vendmachtrack/" + machineIdToRemove, + HttpMethod.DELETE, + null, + new ParameterizedTypeReference>() {} + ); + + // Assert + assertThat(removeResponse.getStatusCode()).as("Response body: " + removeResponse.getBody()) + .isEqualTo(HttpStatus.OK); + assertThat(removeResponse.getBody()).isNotNull(); + assertThat(removeResponse.getBody()).doesNotContainKey(machineIdToRemove); + } + + /** + * Tests verifying the removal of a vending machine. + * + *

+ * + * This test checks if we can successfully confirm that a vending machine has been removed by making a GET request to the specified endpoint and verifying that it's no longer found (HTTP 404 NOT FOUND). + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Prepare the necessary information; service's URL and the ID of the vending machine that was previously removed.
  2. + *
  3. Act: Make a GET request to check if the removed vending machine is no longer present.
  4. + *
  5. Assert: Check that the response status code indicates that the vending machine is not found (HTTP 404 NOT FOUND).
  6. + *
+ */ + @Test + @Order(8) + public void testVendMachRemoved(){ + + // Arrange + String baseUrl = "http://localhost:" + port; + String machineIdToRemove = "100"; + + // Act + ResponseEntity getResponse = restTemplate.getForEntity( + baseUrl + "/vendmachtrack/" + machineIdToRemove, + String.class + ); + + // Assert + assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } +} + + + + + + From af5b9968d28183c23235c87ef4a4244a3615b8bd Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 01:18:19 +0100 Subject: [PATCH 05/14] Issue #123: Fix linespace --- .../vendmachtrack/springboot/SpringBootIntegrationTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java index b849c75..3bdeefd 100644 --- a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java +++ b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/SpringBootIntegrationTest.java @@ -49,12 +49,7 @@ public class SpringBootIntegrationTest { @Autowired private TestRestTemplate restTemplate; - - - - - /** * Tests adding a new vending machine via a REST API endpoint. * From 2678974768f905045b6c98d5c4fd6aae78b68bc8 Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 03:13:25 +0100 Subject: [PATCH 06/14] Issue #125: Update JavaDocs in springboot Module --- .../MachineTrackerControllerTest.java | 335 +++++++++++++--- .../MachineTrackerRepositoryTest.java | 269 +++++++++---- .../service/MachineTrackerServiceTest.java | 377 +++++++++++++++--- 3 files changed, 804 insertions(+), 177 deletions(-) diff --git a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/controller/MachineTrackerControllerTest.java b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/controller/MachineTrackerControllerTest.java index b9f4830..d77d899 100644 --- a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/controller/MachineTrackerControllerTest.java +++ b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/controller/MachineTrackerControllerTest.java @@ -11,7 +11,6 @@ import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; -import vendmachtrack.core.model.MachineTracker; import vendmachtrack.core.model.VendingMachine; import vendmachtrack.springboot.exception.IllegalInputException; import vendmachtrack.springboot.exception.ResourceNotFoundException; @@ -29,6 +28,17 @@ import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +/** + * Test class for the {@link MachineTrackerController} using Spring's WebMvcTest. + * + *

+ * This test class contains a suite of test cases that verify the behavior of the {@link MachineTrackerController} by mocking HTTP requests and responses. The tests cover various methods and scenarios, including adding and removing vending machines, changing machine locations, and handling exceptions like {@link ResourceNotFoundException} and {@link IllegalInputException}. + *

+ * + *

+ * To conduct these tests, Spring's {@link WebMvcTest} annotation is used to focus on testing the controller layer in isolation. Various mock configurations are applied to test different scenarios. + *

+ */ @WebMvcTest(controllers = MachineTrackerController.class) public class MachineTrackerControllerTest { @@ -40,7 +50,6 @@ public class MachineTrackerControllerTest { private final VendingMachine vendingmachine = new VendingMachine(); private final List machines = new ArrayList<>(); - private final MachineTracker machineTracker = new MachineTracker(); /** * Sets up the test fixture before each test method runs. @@ -55,9 +64,20 @@ public void setUp() { } /** - * Tests the getVendMachList method of the MachineTrackerController class. - * Expects the response to be a JSON object containing the list of vending - * machines and the HTTP response status to be 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#getVendMachList()} method in the {@link MachineTrackerController} class. + * + * This test verifies that the {@link MachineTrackerController#getVendMachList()} method returns a JSON object containing the list of vending machines and ensures that the HTTP response status is 200 (OK). + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up a mock response containing a HashMap of vending machines and their locations.
  2. + *
  3. Act: Make a GET request to the "/vendmachtrack" endpoint using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 200 (OK) and that the returned JSON matches the expected format.
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_getVendMachList_returnVendmachList() throws Exception { @@ -77,9 +97,22 @@ public void MachineTrackerController_getVendMachList_returnVendmachList() throws .andExpect(content().json("{\"1\":\"Oslo\"}")); } + /** - * Tests the scenario where the MachineTrackerController throws a ResourceNotFoundException when trying to get the list of vending machines. - * Expects the HTTP response status to be 404 Not Found and the exception to be of type ResourceNotFoundException with the message "Should throw". + * Tests the scenario where the {@link MachineTrackerController} throws a {@link ResourceNotFoundException} when trying to get the list of vending machines. + * + * This test expects the HTTP response status to be 404 (Not Found) and the exception to be of type {@link ResourceNotFoundException} with the message "Should throw". + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw a {@link ResourceNotFoundException} with the message "Should throw" when called.
  2. + *
  3. Act: Make a GET request to the "/vendmachtrack" endpoint using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 404 (Not Found), the resolved exception is of type {@link ResourceNotFoundException}, and the exception message matches "Should throw".
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_getVendMachList_throwsResourceNotFoundException() throws Exception { @@ -97,10 +130,24 @@ public void MachineTrackerController_getVendMachList_throwsResourceNotFoundExcep .andExpect(result -> assertEquals("Should throw", result.getResolvedException().getMessage())); } + /** - * Test case for the MachineTrackerController's getVendMachLocation method. - * It tests if the method returns the correct location of a vending machine and - * the HTTP response status is 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#getVendMachLocation(int)} method in the {@link MachineTrackerController} class. + * + *

+ * This test verifies that the {@link MachineTrackerController#getVendMachLocation(int)} method returns the expected location ("Oslo") for a vending machine with a given ID and ensures a 200 (OK) response status. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to return the location "Oslo" when the {@link MachineTrackerService#getVendMachLocation(int)} method is called with any integer argument.
  2. + *
  3. Act: Make a GET request to the "/vendmachtrack/1/name" endpoint using MockMvc to retrieve the location of vending machine ID 1.
  4. + *
  5. Assert: Verify that the response status is 200 (OK) and that the returned content matches the expected location "Oslo".
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_getVendMachLocation_returnLocation() throws Exception { @@ -120,8 +167,22 @@ public void MachineTrackerController_getVendMachLocation_returnLocation() throws } /** - * Test case for the MachineTrackerController's getVendMachLocation method when it throws a ResourceNotFoundException. - * Expects the HTTP response status to be 404 Not Found and the exception to be of type ResourceNotFoundException with the message "Should throw". + * Tests the behavior of the {@link MachineTrackerController#getVendMachLocation(int)} method in the {@link MachineTrackerController} class when it throws a {@link ResourceNotFoundException}. + * + *

+ * This test verifies that the {@link MachineTrackerController#getVendMachLocation(int)} method throws a {@link ResourceNotFoundException} with the message "Should throw" and ensures that the HTTP response status is 404 (Not Found). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw a {@link ResourceNotFoundException} with the message "Should throw" when the {@link MachineTrackerService#getVendMachLocation(int)} method is called with any integer argument.
  2. + *
  3. Act: Make a GET request to the "/vendmachtrack/1/name" endpoint using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 404 (Not Found), the resolved exception is of type {@link ResourceNotFoundException}, and the exception message matches "Should throw".
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_getVendMachLocation_throwsResourceNotFoundException() throws Exception { @@ -142,9 +203,22 @@ public void MachineTrackerController_getVendMachLocation_throwsResourceNotFoundE } /** - * Test case for the MachineTrackerController's getInventory method. - * Expects the response to be a JSON object containing the inventory of the - * vending machine and the HTTP response status to be 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#getInventory(int)} method in the {@link MachineTrackerController} class. + * + *

+ * This test verifies that the {@link MachineTrackerController#getInventory(int)} method returns a JSON object containing the inventory of a vending machine ("Cola": 10) and ensures that the HTTP response status is 200 (OK). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to return an inventory HashMap with the item "Cola" and quantity 10 when the {@link MachineTrackerService#getInventory(int)} method is called with any integer argument.
  2. + *
  3. Act: Make a GET request to the "/vendmachtrack/1" endpoint using MockMvc to get the inventory of vending machine ID 1.
  4. + *
  5. Assert: Verify that the response status is 200 (OK) and that the returned content matches the expected inventory JSON.
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_getInventory_returnInventory() throws Exception { @@ -165,8 +239,22 @@ public void MachineTrackerController_getInventory_returnInventory() throws Excep } /** - * Tests the MachineTrackerController's getInventory method when it throws an IllegalInputException. - * Expects the HTTP response status to be 400 Bad Request and the exception to be of type IllegalInputException. + * Tests the behavior of the {@link MachineTrackerController#getInventory(int)} method in the {@link MachineTrackerController} class when it throws an {@link IllegalInputException}. + * + *

+ * This test verifies that the {@link MachineTrackerController#getInventory(int)} method throws an {@link IllegalInputException} with the message "Should throw" and ensures that the HTTP response status is 400 (Bad Request). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw an {@link IllegalInputException} with the message "Should throw" when the {@link MachineTrackerService#getInventory(int)} method is called with any integer argument.
  2. + *
  3. Act: Make a GET request to the "/vendmachtrack/1" endpoint using MockMvc to retrieve the inventory of vending machine ID 1.
  4. + *
  5. Assert: Verify that the response status is 400 (Bad Request), the resolved exception is of type {@link IllegalInputException}, and the exception message matches "Should throw".
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_getInventory_throwsIllegalInputException() throws Exception { @@ -186,9 +274,22 @@ public void MachineTrackerController_getInventory_throwsIllegalInputException() } /** - * Test case for the MachineTrackerController's addItem method. - * Expects the response to be a JSON object containing the updated inventory of - * the vending machine and the HTTP response status to be 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#addItem(int, String, int)} method in the {@link MachineTrackerController} class. + * + *

+ * This test verifies that the {@link MachineTrackerController#addItem(int, String, int)} method returns a JSON object containing the updated inventory of the vending machine ("Coke": 10) and ensures that the HTTP response status is 200 (OK). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to return an updated inventory HashMap with the item "Coke" and quantity 10 when the {@link MachineTrackerService#addItem(int, String, int)} method is called.
  2. + *
  3. Act: Make a PUT request to the "/vendmachtrack/1/add" endpoint using MockMvc with the parameters "item" set to "Coke" and "quantity" set to 10.
  4. + *
  5. Assert: Verify that the response status is 200 (OK).
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_addItem_returnUpdatedInventory() throws Exception { @@ -209,10 +310,24 @@ public void MachineTrackerController_addItem_returnUpdatedInventory() throws Exc response.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(content().json("{\"Coke\":10}")); } - + /** - * Tests the addItem method of the MachineTrackerController class when an IllegalInputException is thrown. - * Expects the HTTP response status to be 400 Bad Request and the exception to be of type IllegalInputException. + * Tests the behavior of the {@link MachineTrackerController#addItem(int, String, int)} method in the {@link MachineTrackerController} class when it throws an {@link IllegalInputException}. + * + *

+ * This test verifies that the {@link MachineTrackerController#addItem(int, String, int)} method throws an {@link IllegalInputException} with the message "Should throw" and ensures that the HTTP response status is 400 (Bad Request). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw an {@link IllegalInputException} with the message "Should throw" when the {@link MachineTrackerService#addItem(int, String, int)} method is called.
  2. + *
  3. Act: Make a PUT request to the "/vendmachtrack/1/add" endpoint using MockMvc with the parameters "item" set to "Coke" and "quantity" set to 10.
  4. + *
  5. Assert: Verify that the response status is 400 (Bad Request), the resolved exception is of type {@link IllegalInputException}, and the exception message matches "Should throw".
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_addItem_throwsIllegalInputException() throws Exception { @@ -234,10 +349,22 @@ public void MachineTrackerController_addItem_throwsIllegalInputException() throw } /** - * Tests the removeItem method of the MachineTrackerController class by mocking - * the HTTP request and response. - * It verifies that the updated inventory is returned in JSON format with a - * status code of 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#removeItem(int, String, int)} method in the {@link MachineTrackerController} class. + * + *

+ * This test verifies that the {@link MachineTrackerController#removeItem(int, String, int)} method returns a JSON object containing the updated inventory of the vending machine (e.g., "Coke": 5) and ensures that the HTTP response status is 200 (OK). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to return an updated inventory HashMap with the item "Coke" and quantity 5 when the {@link MachineTrackerService#removeItem(int, String, int)} method is called.
  2. + *
  3. Act: Make a PUT request to the "/vendmachtrack/1/remove" endpoint using MockMvc with the parameters "item" set to "Coke" and "quantity" set to 5.
  4. + *
  5. Assert: Verify that the response status is 200 (OK).
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_removeItem_returnUpdatedInventory() throws Exception { @@ -259,9 +386,24 @@ public void MachineTrackerController_removeItem_returnUpdatedInventory() throws .andExpect(content().json("{\"Coke\":5}")); } + /** - * Tests the removeItem method of the MachineTrackerController class when an IllegalInputException is thrown. - * Expects the HTTP response status to be 400 Bad Request and the exception to be of type IllegalInputException. + * Tests the behavior of the {@link MachineTrackerController#removeItem(int, String, int)} method in the {@link MachineTrackerController} class when it throws an {@link IllegalInputException}. + * + *

+ * This test verifies that the {@link MachineTrackerController#removeItem(int, String, int)} method throws an {@link IllegalInputException} with the message "Should throw" and ensures that the HTTP response status is 400 (Bad Request). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw an {@link IllegalInputException} with the message "Should throw" when the {@link MachineTrackerService#removeItem(int, String, int)} method is called.
  2. + *
  3. Act: Make a PUT request to the "/vendmachtrack/1/remove" endpoint using MockMvc with the parameters "item" set to "Coke" and "quantity" set to 5.
  4. + *
  5. Assert: Verify that the response status is 400 (Bad Request), the resolved exception is of type {@link IllegalInputException}, and the exception message matches "Should throw".
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_removeItem_throwsIllegalInputException() throws Exception { @@ -284,10 +426,24 @@ public void MachineTrackerController_removeItem_throwsIllegalInputException() th } + /** - * Tests the addVendMach method of the MachineTrackerController class. - * Expects the method to return a HashMap with the added vending machine's ID - * and location and the HTTP response status to be 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#addVendMach(int, String)} method in the {@link MachineTrackerController} class. + * + *

+ * This test verifies that the {@link MachineTrackerController#addVendMach(int, String)} method returns a {@link HashMap} with the added vending machine's ID and location ("1": "Oslo") and ensures that the HTTP response status is 200 (OK). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to return a {@link HashMap} with the added vending machine's ID and location (e.g., "1": "Oslo") when the {@link MachineTrackerService#addVendMach(int, String)} method is called.
  2. + *
  3. Act: Make a POST request to the "/vendmachtrack/add" endpoint using MockMvc with the parameters "id" set to 1 and "location" set to "Oslo".
  4. + *
  5. Assert: Verify that the response status is 200 (OK) and the response content matches the expected JSON format (e.g., "{\"1\":\"Oslo\"}").
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_addVendMach_returnAdded() throws Exception { @@ -311,9 +467,24 @@ public void MachineTrackerController_addVendMach_returnAdded() throws Exception .andExpect(content().json("{\"1\":\"Oslo\"}")); } + /** - * Tests the addVendMach method in the MachineTrackerController class when an IllegalInputException is thrown. - * Expects the HTTP response status to be 400 Bad Request and the exception to be of type IllegalInputException. + * Tests the behavior of the {@link MachineTrackerController#addVendMach(int, String)} method in the {@link MachineTrackerController} class when an {@link IllegalInputException} is thrown. + * + *

+ * This test verifies that the {@link MachineTrackerController#addVendMach(int, String)} method throws an {@link IllegalInputException} and ensures that the HTTP response status is 400 Bad Request. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw an {@link IllegalInputException} with the message "Should throw" when the {@link MachineTrackerService#addVendMach(int, String)} method is called with any integer and string arguments.
  2. + *
  3. Act: Make a POST request to the "/vendmachtrack/add" endpoint using MockMvc with the parameters "id" set to 1 and "location" set to "Oslo".
  4. + *
  5. Assert: Verify that the response status is 400 Bad Request and the exception thrown is of type {@link IllegalInputException} with the message "Should throw".
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_addVendMach_throwsIllegalInputException() throws Exception { @@ -334,10 +505,24 @@ public void MachineTrackerController_addVendMach_throwsIllegalInputException() t .andExpect(result -> assertEquals("Should throw", result.getResolvedException().getMessage())); } + /** - * Tests the removeVendMach method of the MachineTrackerController class - * Expects the method to return a HashMap with the updated list of vending - * machines and the HTTP response status to be 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#removeVendMach(int)} method in the {@link MachineTrackerController} class. + * + *

+ * This test verifies that the {@link MachineTrackerController#removeVendMach(int)} method returns an updated list of vending machines as a {@link HashMap} and ensures that the HTTP response status is 200 (OK). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to return an updated list of vending machines as a {@link HashMap} when the {@link MachineTrackerService#removeVendMach(int)} method is called.
  2. + *
  3. Act: Make a DELETE request to the "/vendmachtrack/1" endpoint using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 200 OK and the returned JSON matches the expected updated list of vending machines.
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_removeVendMach_returnUpdatedList() throws Exception { @@ -358,8 +543,22 @@ public void MachineTrackerController_removeVendMach_returnUpdatedList() throws E } /** - * Tests the removeVendMach method of the MachineTrackerController class when an IllegalInputException is thrown. - * Expects the HTTP response status to be 400 Bad Request and the exception to be of type IllegalInputException. + * Tests the behavior of the {@link MachineTrackerController#removeVendMach(int)} method in the {@link MachineTrackerController} class when an {@link IllegalInputException} is thrown. + * + *

+ * This test verifies that the {@link MachineTrackerController#removeVendMach(int)} method handles the scenario where an {@link IllegalInputException} is thrown. It expects the HTTP response status to be 400 Bad Request, and the thrown exception to be of type {@link IllegalInputException} with the message "Should throw." + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw an {@link IllegalInputException} with the message "Should throw" when the {@link MachineTrackerService#removeVendMach(int)} method is called.
  2. + *
  3. Act: Make a DELETE request to the "/vendmachtrack/1" endpoint using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 400 Bad Request and that the thrown exception is of type {@link IllegalInputException} with the expected message.
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_removeVendMach_throwsIllegalInputException() throws Exception { @@ -379,10 +578,24 @@ public void MachineTrackerController_removeVendMach_throwsIllegalInputException( .andExpect(result -> assertEquals("Should throw", result.getResolvedException().getMessage())); } + /** - * Tests the changeLocation method of the MachineTrackerController class - * Expects the method to return a HashMap with the updated location of the - * vending machine and the HTTP response status to be 200 (OK). + * Tests the behavior of the {@link MachineTrackerController#changeLocation(int, String)} method in the {@link MachineTrackerController} class. + * + *

+ * This test verifies that the {@link MachineTrackerController#changeLocation(int, String)} method correctly updates the location of a vending machine and returns a HashMap with the updated location. It expects the HTTP response status to be 200 (OK). + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to return a HashMap with the updated location when the {@link MachineTrackerService#changeLocation(int, String)} method is called.
  2. + *
  3. Act: Make a PUT request to the "/vendmachtrack/1" endpoint with the updated location using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 200 (OK) and that the response body contains the expected updated location.
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_changeLocation_returnUpdatedLocation() throws Exception { @@ -403,9 +616,24 @@ public void MachineTrackerController_changeLocation_returnUpdatedLocation() thro .andExpect(content().json("{\"1\":\"Trondheim\"}")); } + /** - * Tests the behavior of the MachineTrackerController when the changeLocation method throws an IllegalInputException. - * Expect the HTTP response status to be 400 Bad Request and the exception to be of type IllegalInputException. + * Tests the behavior of the {@link MachineTrackerController#changeLocation(int, String)} method in the {@link MachineTrackerController} class when it throws an {@link IllegalInputException}. + * + *

+ * This test verifies how the {@link MachineTrackerController#changeLocation(int, String)} method handles an {@link IllegalInputException} when trying to update the location of a vending machine. It expects the HTTP response status to be 400 Bad Request and the exception to be of type {@link IllegalInputException}. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw an {@link IllegalInputException} with the message "Should throw" when the {@link MachineTrackerService#changeLocation(int, String)} method is called.
  2. + *
  3. Act: Make a PUT request to the "/vendmachtrack/1" endpoint with the updated location using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 400 Bad Request and that the exception is of type {@link IllegalInputException} with the expected message.
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_changeLocation_throwsIllegalInputException() throws Exception { @@ -426,9 +654,24 @@ public void MachineTrackerController_changeLocation_throwsIllegalInputException( .andExpect(result -> assertEquals("Should throw", result.getResolvedException().getMessage())); } + /** - * Tests the scenario where the changeLocation method in MachineTrackerController throws a ResourceNotFoundException. - * Expects the HTTP response status to be 404 Not Found and the exception to be of type ResourceNotFoundException. + * Tests the behavior of the {@link MachineTrackerController#changeLocation(int, String)} method in the {@link MachineTrackerController} class when it throws a {@link ResourceNotFoundException}. + * + *

+ * This test verifies how the {@link MachineTrackerController#changeLocation(int, String)} method handles a {@link ResourceNotFoundException} when trying to update the location of a vending machine. It expects the HTTP response status to be 404 Not Found and the exception to be of type {@link ResourceNotFoundException}. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Configure the {@link MachineTrackerService} to throw a {@link ResourceNotFoundException} with the message "Should throw" when the {@link MachineTrackerService#changeLocation(int, String)} method is called.
  2. + *
  3. Act: Make a PUT request to the "/vendmachtrack/1" endpoint with the updated location using MockMvc.
  4. + *
  5. Assert: Verify that the response status is 404 Not Found and that the exception is of type {@link ResourceNotFoundException} with the expected message.
  6. + *
+ * + * @throws Exception if an exception occurs during test execution. */ @Test public void MachineTrackerController_changeLocation_throwsResourceNotFoundException() throws Exception { diff --git a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/repository/MachineTrackerRepositoryTest.java b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/repository/MachineTrackerRepositoryTest.java index d111734..4ae194f 100644 --- a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/repository/MachineTrackerRepositoryTest.java +++ b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/repository/MachineTrackerRepositoryTest.java @@ -17,6 +17,11 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; +/** + * This class contains unit tests for the {@link MachineTrackerRepository} class. It focuses on testing various methods of the repository that interact with vending machines and the {@link MachineTracker} object. + *

+ * The tests are implemented using the JUnit 5 framework and Mockito. The {@link MachineTrackerRepository} class is tested in isolation from the rest of the application. + */ public class MachineTrackerRepositoryTest { private final VendmachtrackPersistence persistence = Mockito.mock(VendmachtrackPersistence.class); @@ -44,8 +49,20 @@ public void setUp() { } /** - * Test case for MachineTrackerRepository's getVendmachtrack method. - * It Verifies that the method returns the expected MachineTracker object. + * Tests the {@link MachineTrackerRepository#getVendmachtrack()} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test case verifies that the {@link MachineTrackerRepository#getVendmachtrack()} method returns the expected {@link MachineTracker} object. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a {@link MachineTracker} object with machines.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#getVendmachtrack()} method to retrieve the actual {@link MachineTracker} object.
  4. + *
  5. Assert: Verify that the actual {@link MachineTracker} object matches the expected one and that the {@link MachinePersistence} is called once with the expected method.
  6. + *
*/ @Test public void MachineTrackerRepository_getVendmachtrack_ReturnMachineTracker() { @@ -63,8 +80,20 @@ public void MachineTrackerRepository_getVendmachtrack_ReturnMachineTracker() { } /** - * Tests the getVendMach method of the MachineTrackerRepository class. - * It verifies that the method returns the expected VendingMachine object. + * Tests the {@link MachineTrackerRepository#getVendMach(int)} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test case verifies that the {@link MachineTrackerRepository#getVendMach(int)} method returns the expected {@link VendingMachine} object when given a valid machine ID. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a {@link MachineTracker} object with machines and a valid machine ID.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#getVendMach(int)} method to retrieve the actual {@link VendingMachine} object.
  4. + *
  5. Assert: Verify that the actual {@link VendingMachine} object matches the expected one.
  6. + *
*/ @Test public void MachineTrackerRepository_getVendMach_ReturnVendingMachine() { @@ -81,11 +110,21 @@ public void MachineTrackerRepository_getVendMach_ReturnVendingMachine() { assertEquals(vendingmachine, actualVendingMachine); } - /** - * Tests the getVendMach method of the MachineTrackerRepository class when the - * machine is not found. - * Given an id, the method should return null if the machine is not found in the - * database. + /** + * Tests the {@link MachineTrackerRepository#getVendMach(int)} method of the {@link MachineTrackerRepository} class when the machine is not found. + * + *

+ * Given an ID, this test case verifies that the {@link MachineTrackerRepository#getVendMach(int)} method returns {@code null} if the vending machine is not found in the database. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a {@link MachineTracker} object with an empty list of machines and an ID for a non-existent machine.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#getVendMach(int)} method to retrieve the actual {@link VendingMachine} object.
  4. + *
  5. Assert: Verify that the actual {@link VendingMachine} object is {@code null} as expected.
  6. + *
*/ @Test public void MachineTrackerRepository_getVendMach_retrunsnull() { @@ -102,9 +141,20 @@ public void MachineTrackerRepository_getVendMach_retrunsnull() { } /** - * Tests the saveVendmachtrack method of the MachineTrackerRepository class. - * It verifies that the method saves and returns a MachineTracker object - * correctly. + * Tests the {@link MachineTrackerRepository#saveVendmachtrack(MachineTracker)} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test case verifies that the {@link MachineTrackerRepository#saveVendmachtrack(MachineTracker)} method correctly saves and returns a {@link MachineTracker} object. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a {@link MachineTracker} object with a list of machines.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#saveVendmachtrack(MachineTracker)} method to save the {@link MachineTracker} object.
  4. + *
  5. Assert: Verify that the method was called once with the correct parameter and that the returned {@link MachineTracker} object is the same as the one passed in.
  6. + *
*/ @Test public void MachineTrackerRepository_SaveVendmachtrack_savesAndRetursMAchinetracker() { @@ -116,18 +166,26 @@ public void MachineTrackerRepository_SaveVendmachtrack_savesAndRetursMAchinetrac MachineTracker returnedMachineTracker = machineTrackerRepository.saveVendmachtrack(machineTracker); // Assert - verify(persistence, times(1)).saveVendmachtrack(machineTracker); // check that saveVendmachtrack was called once - // with the correct parameter - assertSame(machineTracker, returnedMachineTracker); // check that the returned object is the same as the one - // passed in + verify(persistence, times(1)).saveVendmachtrack(machineTracker); + assertSame(machineTracker, returnedMachineTracker); } /** - * Tests the addVendMach method of the MachineTrackerRepository class. - * It verifies that the method adds a vending machine to the list of machines in - * the MachineTracker object. - * It also verifies that the added vending machine has the correct id and - * location. + * Tests the {@link MachineTrackerRepository#addVendMach(int, String)} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test case verifies that the {@link MachineTrackerRepository#addVendMach(int, String)} method correctly adds a vending machine to the list of machines in the {@link MachineTracker} object. + * It also ensures that the added vending machine has the correct ID and location. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the initial {@link MachineTracker} object with an empty list of machines and configure mock behavior for persistence methods.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#addVendMach(int, String)} method to add a vending machine.
  4. + *
  5. Assert: Verify that the {@link MachineTracker#saveVendmachtrack(MachineTracker)} method was called with the correct parameter, and ensure that the added vending machine has the expected ID and location.
  6. + *
*/ @Test public void MachineTrackerRepository_addVendMach_addsVendingMachine() { @@ -155,10 +213,22 @@ public void MachineTrackerRepository_addVendMach_addsVendingMachine() { assertEquals(location, addedVendingMachine.getLocation()); } + /** - * Tests the addItem method of the MachineTrackerRepository class. - * It verifies that the method adds an item to an existing vending machine and - * updates the MachineTracker object accordingly. + * Tests the {@link MachineTrackerRepository#addItem(int, String, int)} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test case verifies that the {@link MachineTrackerRepository#addItem(int, String, int)} method correctly adds an item to an existing vending machine and updates the {@link MachineTracker} object accordingly. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up initial data, including the vending machine, item, and quantity, and configure mock behavior for persistence methods.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#addItem(int, String, int)} method to add an item to the vending machine.
  4. + *
  5. Assert: Verify that the method call returns a non-null result with the expected ID and quantity, and ensure that the updated {@link MachineTracker} object contains the modified vending machine.
  6. + *
*/ @Test public void MachineTrackerRepository_addItem_addsItemToExistingVendingmachine() { @@ -186,8 +256,20 @@ public void MachineTrackerRepository_addItem_addsItemToExistingVendingmachine() } /** - * Tests the addItem method of the MachineTrackerRepository class when the - * vending machine does not exist. + * Tests the {@link MachineTrackerRepository#addItem(int, String, int)} method of the {@link MachineTrackerRepository} class when the vending machine does not exist. + * + *

+ * This test case verifies that the {@link MachineTrackerRepository#addItem(int, String, int)} method returns {@code null} when attempting to add an item to a non-existent vending machine. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the test by configuring mock behavior to simulate an empty list of vending machines for the {@link MachineTracker} object.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#addItem(int, String, int)} method to add an item to the vending machine.
  4. + *
  5. Assert: Verify that the method returns {@code null} since the vending machine does not exist.
  6. + *
*/ @Test public void MachineTrackerRepository_addItem_returnNullVendingMachinenotexist() { @@ -207,11 +289,21 @@ public void MachineTrackerRepository_addItem_returnNullVendingMachinenotexist() } /** - * Tests the changeLocation method of MachineTrackerRepository class. - * It verifies that the method updates the location of a vending machine with - * the given id. - * It also verifies that the updated vending machine is saved in the - * MachineTracker object. + * Tests the {@link MachineTrackerRepository#changeLocation(int, String)} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test case verifies that the {@link MachineTrackerRepository#changeLocation(int, String)} method correctly updates the location of a vending machine with the given ID + * and ensures that the updated vending machine is saved in the {@link MachineTracker} object. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the test by configuring mock behavior to simulate an existing vending machine with the specified ID, as well as setting the new location and configuring save behavior for the {@link MachineTracker} object.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#changeLocation(int, String)} method to change the location of the vending machine.
  4. + *
  5. Assert: Verify that the method returns the updated vending machine with the correct ID and location. Also, ensure that the updated vending machine is saved in the {@link MachineTracker} object.
  6. + *
*/ @Test public void MachineTrackerRepository_changeLocation_changeLocationOnVendingMachine() { @@ -235,13 +327,24 @@ public void MachineTrackerRepository_changeLocation_changeLocationOnVendingMachi MachineTracker capturedMachineTracker = machineTrackerCaptor.getValue(); // verify that the captured MachineTracker contains the updated VendingMachine assertEquals(vendingmachine, capturedMachineTracker.getMachines().get(0)); - } /** - * Test case for the changeLocation method of MachineTrackerRepository class. - * It tests the scenario where the vending machine with the given id does not - * exist in the system. + * Test case for the {@link MachineTrackerRepository#changeLocation(int, String)} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test case covers the scenario where the vending machine with the given ID does not exist in the system. + * It verifies that the method correctly returns null when attempting to change the location of a non-existent vending machine. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the test by configuring mock behavior to simulate an existing list of vending machines and specify a non-existent vending machine ID and a new location.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#changeLocation(int, String)} method to attempt to change the location of the non-existent vending machine.
  4. + *
  5. Assert: Verify that the method returns null, indicating that no changes were made since the vending machine does not exist in the system.
  6. + *
*/ @Test public void MachineTrackerRepository_changeLocation_returnNullVendingMachineNotExist() { @@ -257,18 +360,23 @@ public void MachineTrackerRepository_changeLocation_returnNullVendingMachineNotE // Assert assertNull(result); - } - + /** - * Tests the removeVendMach method of the MachineTrackerRepository class. - * It verifies that the method removes a vending machine from the list of - * machines in the MachineTracker object, - * saves the updated MachineTracker object to the persistence layer, and returns - * the updated MachineTracker object. - * It also verifies that the returned and saved MachineTracker objects are the - * same and that the list of machines - * in the updated MachineTracker object is empty. + * Tests the {@link MachineTrackerRepository#removeVendMach(int)} method of the {@link MachineTrackerRepository} class. + * + *

+ * This test verifies that the method removes a vending machine from the list of machines in the {@link MachineTracker} object.. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the test by configuring mock behavior to simulate an existing list of vending machines, specifying a vending machine ID to remove, and configuring mock behavior for saving the updated {@link MachineTracker} object.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#removeVendMach(int)} method to remove the vending machine.
  4. + *
  5. Assert: Verify that the vending machine is reoved form the machinetracker.
  6. + *
*/ @Test public void MachineTrackerRepository_removeVendMach_removesVendingmachine() { @@ -288,21 +396,29 @@ public void MachineTrackerRepository_removeVendMach_removesVendingmachine() { assertTrue(result.getMachines().isEmpty()); verify(persistence).saveVendmachtrack(machineTrackerCaptor.capture()); MachineTracker capturedMachineTracker = machineTrackerCaptor.getValue(); - assertEquals(machineTracker, capturedMachineTracker); // ensure the returned and saved MachineTrackers are the - // same + assertEquals(machineTracker, capturedMachineTracker); } /** - * Test case for the removeVendMach method of the MachineTrackerRepository - * class. - * Tests the scenario where the vendmach to be removed does not exist in the - * MachineTracker. + * Test case for the {@link MachineTrackerRepository#removeVendMach(int)} method of the {@link MachineTrackerRepository} class. + * + *

+ * Tests the scenario where the vending machine to be removed does not exist in the {@link MachineTracker}. + *

+ * + *

+ * The test sets up the necessary mocks and data, calls the {@link MachineTrackerRepository#removeVendMach(int)} method with a non-existent vending machine ID, + * and verifies that the returned {@link MachineTracker} is the same as the one saved. + *

+ * *

- * The test sets up the necessary mocks and data, calls the removeVendMach - * method with a non-existent VendindgMachineTracker id, - * and verifies that the returned VendingMachineTracker is the same as the one - * saved, and that the saved VendingMachineTracker - * has the same machines as the original one. + * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the test by configuring mock behavior to simulate an existing list of vending machines, specifying a vending machine ID that does not exist in the list, and configuring mock behavior for saving the updated {@link MachineTracker} object.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#removeVendMach(int)} method to attempt to remove a non-existent vending machine.
  4. + *
  5. Assert: Verify that the method returns the same {@link MachineTracker} object as the one saved.
  6. + *
*/ @Test public void MachineTrackerRepository_removeVendMach_removeVendmachNotExist() { @@ -319,20 +435,24 @@ public void MachineTrackerRepository_removeVendMach_removeVendmachNotExist() { // Assert assertEquals(machineTracker, result); - assertNotNull(result.getMachines()); verify(persistence).saveVendmachtrack(machineTrackerCaptor.capture()); MachineTracker capturedMachineTracker = machineTrackerCaptor.getValue(); - assertEquals(machineTracker, capturedMachineTracker); // ensure the returned and saved MachineTrackers are the - // same + assertEquals(machineTracker, capturedMachineTracker); } + /** - * Tests the removeItem method of the MachineTrackerRepository class. - * It verifies that the method removes the specified item from the vending - * machine and updates the status accordingly. - * It also verifies that the updated vending machine is saved correctly in the - * persistence layer. - */ + * Test case for the {@link MachineTrackerRepository#removeItem(int, String, int)} method of the {@link MachineTrackerRepository} class. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the test by configuring mock behavior to simulate an existing vending machine with a specified item and quantity, configuring mock behavior for saving the updated {@link MachineTracker} object, and providing an existing vending machine ID.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#removeItem(int, String, int)} method to attempt to remove the specified quantity of the item from the vending machine.
  4. + *
  5. Assert: Verify that the method returns a non-null {@link VendingMachine} object with the updated status, the status of the removed item is as expected, and the {@link MachineTracker} object is saved correctly in the persistence layer.
  6. + *
+ */ @Test public void MachineTrackerRepository_removeitem_removesItemFromVendigMachine() { @@ -356,17 +476,24 @@ public void MachineTrackerRepository_removeitem_removesItemFromVendigMachine() { // Assert assertNotNull(result); assertEquals(id, result.getId()); - assertEquals(1, result.getStatus().get(item)); // 1 item should remain + assertEquals(1, result.getStatus().get(item)); verify(persistence).saveVendmachtrack(machineTrackerCaptor.capture()); MachineTracker capturedMachineTracker = machineTrackerCaptor.getValue(); - assertEquals(machineTracker, capturedMachineTracker); // ensure the returned and saved MachineTrackers are the - // same - + assertEquals(machineTracker, capturedMachineTracker); + } /** - * Tests the removeItem method of the MachineTrackerRepository class when the - * vending machine does not exist. + * Test case for the {@link MachineTrackerRepository#removeItem(int, String, int)} method of the {@link MachineTrackerRepository} class when the vending machine does not exist. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the test by configuring mock behavior to simulate an empty list of vending machines, specifying a vending machine ID that does not exist, and configuring mock behavior for retrieving the {@link MachineTracker} object.
  2. + *
  3. Act: Call the {@link MachineTrackerRepository#removeItem(int, String, int)} method to attempt to remove an item from a non-existent vending machine.
  4. + *
  5. Assert: Verify that the method returns a null {@link VendingMachine} object, indicating that the vending machine does not exist.
  6. + *
*/ @Test public void MachineTrackerRepository_removeItem_removesItemVendingMachineNotExist() { diff --git a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/service/MachineTrackerServiceTest.java b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/service/MachineTrackerServiceTest.java index 0dc73b0..a65f18c 100644 --- a/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/service/MachineTrackerServiceTest.java +++ b/vendmachtrack/springboot/src/test/java/vendmachtrack/springboot/service/MachineTrackerServiceTest.java @@ -14,10 +14,21 @@ import vendmachtrack.core.model.VendingMachine; import vendmachtrack.springboot.exception.IllegalInputException; import vendmachtrack.springboot.exception.ResourceNotFoundException; +import vendmachtrack.springboot.repository.MachineTrackerRepository; import java.util.Collections; import java.util.HashMap; +/** + * Unit tests for the {@link MachineTrackerService} class. + * This class contains test cases to verify the behavior of the {@link MachineTrackerService} methods. + * Each test case focuses on specific functionality and expected outcomes of the service. + * + *

+ * Test cases cover various scenarios including valid inputs, invalid inputs, exceptions, and edge cases to ensure the service behaves as expected. + *

+ * + */ public class MachineTrackerServiceTest { @Mock @@ -44,11 +55,25 @@ public void setUp() { } /** - * Test case to verify the getVendMachList method of the MachineTrackerService class. - * Verifies that the method returns a list of machines when the machine tracker is present. + * Tests the {@link MachineTrackerService#getVendMachList()} method of the {@link MachineTrackerService} class. + * + *

+ * This test case verifies that the {@link MachineTrackerService#getVendMachList()} method returns a list of machines when the machine tracker is present. + *

+ * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a {@link MachineTracker} object with machines and configure mock behavior for the repository.
  2. + *
  3. Act: Call the {@link MachineTrackerService#getVendMachList()} method to retrieve the actual list of machines.
  4. + *
  5. Assert: Verify that the actual list of machines is not null, contains the expected machine(s), and has the correct mapping of machine IDs to locations. + * Also, ensure that the {@link MachineTrackerRepository#getVendmachtrack()} method is called once.
  6. + *
*/ @Test public void getVendMachList_whenMachineTrackerIsPresent_returnsListOfMachines() { + // Arrange when(repository.getVendmachtrack()).thenReturn(machineTracker); @@ -63,8 +88,16 @@ public void getVendMachList_whenMachineTrackerIsPresent_returnsListOfMachines() } /** - * Test case for the getVendMachList method of the MachineTrackerService class when the machine tracker is not present. - * Expects a ResourceNotFoundException to be thrown. + * Test case for the {@link MachineTrackerService#getVendMachList()} method of the {@link MachineTrackerService} class when the machine tracker is not present. + * Expects a {@link ResourceNotFoundException} to be thrown. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a null {@link MachineTracker} object and configure mock behavior for the repository.
  2. + *
  3. Act & Assert: Verify that calling the {@link MachineTrackerService#getVendMachList()} method results in a {@link ResourceNotFoundException} being thrown.
  4. + *
*/ @Test public void getVendMachList_whenMachineTrackerIsNotPresent_thowsException() { @@ -78,8 +111,17 @@ public void getVendMachList_whenMachineTrackerIsNotPresent_thowsException() { } /** - * Test case for the getVendMachLocation method of the MachineTrackerService class. + * Test case for the {@link MachineTrackerService#getVendMachLocation(int)} method of the {@link MachineTrackerService} class. * Verifies that the method returns the correct location for a valid machine ID. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a valid machine ID and configure mock behavior for the repository.
  2. + *
  3. Act: Call the {@link MachineTrackerService#getVendMachLocation(int)} method to retrieve the actual location.
  4. + *
  5. Assert: Verify that the actual location matches the expected location.
  6. + *
*/ @Test public void getVendMachLocation_validId_returnsLocation() { @@ -92,13 +134,22 @@ public void getVendMachLocation_validId_returnsLocation() { // Assert assertEquals("Oslo", location); } - + /** - * Test case for the getVendMachLocation method of the MachineTrackerService class when an invalid id is provided. - * Expects a ResourceNotFoundException to be thrown. + * Test case for the {@link MachineTrackerService#getVendMachLocation(int)} method of the {@link MachineTrackerService} class. + * Verifies that the method throws a {@link ResourceNotFoundException} when an invalid ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an invalid machine ID and configure mock behavior for the repository.
  2. + *
  3. Act & Assert: Verify that calling the {@link MachineTrackerService#getVendMachLocation(int)} method with an invalid ID throws a {@link ResourceNotFoundException}.
  4. + *
*/ @Test public void getVendMachLocation_invalidId_throwsResourceNotFoundException() { + // Arrange when(repository.getVendmachtrack()).thenReturn(machineTracker); @@ -107,9 +158,17 @@ public void getVendMachLocation_invalidId_throwsResourceNotFoundException() { } /** - * Test case for the getInventory method of the MachineTrackerService class. - * Verifies that the method returns the correct inventory for a valid machine - * ID. + * Test case for the {@link MachineTrackerService#getInventory(int)} method of the {@link MachineTrackerService} class. + * Verifies that the method returns the correct inventory for a valid machine ID. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a valid machine ID, inventory data, and configure mock behavior for the repository.
  2. + *
  3. Act: Call the {@link MachineTrackerService#getInventory(int)} method to retrieve the actual inventory.
  4. + *
  5. Assert: Verify that the actual inventory matches the expected one.
  6. + *
*/ @Test public void getInventory_validId_returnsInventory() { @@ -128,8 +187,16 @@ public void getInventory_validId_returnsInventory() { } /** - * Test case for the getInventory method of the MachineTrackerService class when an invalid id is provided. - * Expects a ResourceNotFoundException to be thrown. + * Test case for the {@link MachineTrackerService#getInventory(int)} method of the {@link MachineTrackerService} class when an invalid ID is provided. + * Expects a {@link ResourceNotFoundException} to be thrown. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an invalid machine ID, and configure mock behavior for the repository.
  2. + *
  3. Act & Assert: Call the {@link MachineTrackerService#getInventory(int)} method with an invalid ID and assert that it throws a {@link ResourceNotFoundException}.
  4. + *
*/ @Test public void getInventory_invalidId_throwsResourceNotFoundException() { @@ -141,9 +208,17 @@ public void getInventory_invalidId_throwsResourceNotFoundException() { } /** - * Tests the addItem method of the MachineTrackerService class with valid input. - * Verifies that the method adds an item to the machine successfully when a - * valid id, item, and quantity are provided. + * Test case for the {@link MachineTrackerService#addItem(int, String, int)} method of the {@link MachineTrackerService} class with valid input. + * Verifies that the method adds an item to the machine successfully when a valid ID, item, and quantity are provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a valid machine ID, item, quantity, and configure mock behavior for the repository.
  2. + *
  3. Act: Call the {@link MachineTrackerService#addItem(int, String, int)} method with valid input.
  4. + *
  5. Assert: Verify that the method returns the expected updated inventory.
  6. + *
*/ @Test public void addItem_validInput_addsItemSuccessfully() { @@ -163,9 +238,17 @@ public void addItem_validInput_addsItemSuccessfully() { assertEquals(inventory, updatedInventory); } + /** - * Tests that an IllegalInputException is thrown when an invalid item is added - * to the machine. + * Test case for the {@link MachineTrackerService#addItem(int, String, int)} method of the {@link MachineTrackerService} class when an invalid item is added to the machine. + * Verifies that an {@link IllegalInputException} is thrown when an invalid item is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid item is added to the machine.
  2. + *
*/ @Test public void addItem_invalidItem_throwsIllegalInputException() { @@ -174,9 +257,15 @@ public void addItem_invalidItem_throwsIllegalInputException() { } /** - * Tests the addItem method of the MachineTrackerService class when an invalid - * quantity is provided. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#addItem(int, String, int)} method of the {@link MachineTrackerService} class when an invalid quantity is provided. + * Verifies that an {@link IllegalInputException} is thrown when an invalid quantity is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid quantity is provided.
  2. + *
*/ @Test public void addItem_invalidQuantity_throwsIllegalInputException() { @@ -185,8 +274,16 @@ public void addItem_invalidQuantity_throwsIllegalInputException() { } /** - * Tests the addItem method of the MachineTrackerService class when an invalid is is provided. - * Expects a ResourceNotFoundException to be thrown. + * Test case for the {@link MachineTrackerService#addItem(int, String, int)} method of the {@link MachineTrackerService} class when an invalid ID is provided. + * Verifies that a {@link ResourceNotFoundException} is thrown when an invalid ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary mock behavior to simulate a non-existent vending machine with the given ID.
  2. + *
  3. Act & Assert: Verify that a {@link ResourceNotFoundException} is thrown when an invalid ID is provided.
  4. + *
*/ @Test public void addItem_invalidId_throwsResourceNotFoundException() { @@ -198,9 +295,18 @@ public void addItem_invalidId_throwsResourceNotFoundException() { } /** - * Tests the addItem method of the MachineTrackerService class when a negative - * quantity is provided. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#addItem(int, String, int)} method of the {@link MachineTrackerService} class + * when an invalid quantity is provided. + * Verifies that an {@link IllegalInputException} is thrown when an invalid quantity is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid quantity is provided.
  2. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void addItem_negativeQuantity_throwsIllegalInputException() { @@ -210,8 +316,19 @@ public void addItem_negativeQuantity_throwsIllegalInputException() { } /** - * Tests the addVendMach method of the MachineTrackerService class when an existing id is provided. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#addVendMach(int, String)} method of the {@link MachineTrackerService} class + * when an existing ID is provided. + * Verifies that the method throws an {@link IllegalInputException} when an existing ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an existing machine with the same ID.
  2. + *
  3. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an existing ID is provided.
  4. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void addVendMach_existingId_throwsIllegalInputException() { @@ -224,8 +341,19 @@ public void addVendMach_existingId_throwsIllegalInputException() { /** * Test case for adding a new vending machine to the machine tracker service. - * It verifies that the method adds a new vending machine successfully when a - * valid id and location are provided. + * Verifies that the method adds a new vending machine successfully when a + * valid ID and location are provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a new vending machine with a valid ID and location, and configure mock behavior for the repository.
  2. + *
  3. Act: Call the {@link MachineTrackerService#addVendMach(int, String)} method with valid input.
  4. + *
  5. Assert: Verify that the method returns the expected updated machine list with the new vending machine.
  6. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void addVendMach_validInput_addsMachineSuccessfully() { @@ -247,8 +375,19 @@ public void addVendMach_validInput_addsMachineSuccessfully() { } /** - * Tests the addVendMach method of the MachineTrackerService class when an invalid location is provided. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#addVendMach(int, String)} method of the {@link MachineTrackerService} class + * when an invalid location is provided. + * Verifies that the method throws an {@link IllegalInputException} when an invalid location is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including no existing machine with the same ID.
  2. + *
  3. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid location is provided.
  4. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void addVendMach_invalidLocation_throwsIllegalInputException() { @@ -260,9 +399,19 @@ public void addVendMach_invalidLocation_throwsIllegalInputException() { } /** - * Tests the addVendMach method of the MachineTrackerService class when an - * invalid id is provided. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#addVendMach(int, String)} method of the {@link MachineTrackerService} class + * when an invalid ID is provided. + * Verifies that the method throws an {@link IllegalInputException} when an invalid ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a new vending machine with an invalid ID and a location, and configure mock behavior for the repository.
  2. + *
  3. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid ID is provided.
  4. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void addVendMach_invalidId_throwsIllegalInputException() { @@ -277,8 +426,20 @@ public void addVendMach_invalidId_throwsIllegalInputException() { } /** - * Tests the removeVendMach method of the MachineTrackerService class. - * It verifies that the method removes a vending machine successfully when a valid id is provided. + * Test case for the {@link MachineTrackerService#removeVendMach(int)} method of the {@link MachineTrackerService} class + * when a valid ID is provided. + * Verifies that the method removes a vending machine successfully when a valid ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an existing vending machine with a valid ID, and configure mock behavior for the repository.
  2. + *
  3. Act: Call the {@link MachineTrackerService#removeVendMach(int)} method with a valid ID.
  4. + *
  5. Assert: Verify that the method removes the vending machine from the machine list.
  6. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void removeVendMach_validId_removesMachineSuccessfully() { @@ -296,8 +457,19 @@ public void removeVendMach_validId_removesMachineSuccessfully() { } /** - * Tests the removeVendMach method of the MachineTrackerService class when an invalid ID is provided. - * Expects a ResourceNotFoundException to be thrown. + * Test case for the {@link MachineTrackerService#removeVendMach(int)} method of the {@link MachineTrackerService} class + * when an invalid ID is provided. + * Verifies that the method throws a {@link ResourceNotFoundException} when an invalid ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data.
  2. + *
  3. Act & Assert: Verify that a {@link ResourceNotFoundException} is thrown when an invalid ID is provided.
  4. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void removeVendMach_invalidId_throwsResourceNotFoundException() { @@ -309,9 +481,20 @@ public void removeVendMach_invalidId_throwsResourceNotFoundException() { } /** - * Tests the changeLocation method of the MachineTrackerService class. - * It verifies that the method changes the location of a vending machine - * successfully. + * Test case for the {@link MachineTrackerService#changeLocation(int, String)} method of the {@link MachineTrackerService} class + * when valid input is provided. + * Verifies that the method changes the location of a vending machine successfully. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an existing vending machine with a valid ID and a new location, and configure mock behavior for the repository.
  2. + *
  3. Act: Call the {@link MachineTrackerService#changeLocation(int, String)} method with valid input.
  4. + *
  5. Assert: Verify that the method returns the expected updated machine list with the new location.
  6. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void changeLocation_validInput_changesLocationSuccessfully() { @@ -333,9 +516,21 @@ public void changeLocation_validInput_changesLocationSuccessfully() { assertEquals("Trondheim", updatedMachineList.get(1)); } + /** - * Tests the changeLocation method of the MachineTrackerService class when an invalid location is provided. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#changeLocation(int, String)} method of the {@link MachineTrackerService} class + * when an invalid location is provided. + * Verifies that the method throws an {@link IllegalInputException} when an invalid location is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an existing vending machine with a valid ID.
  2. + *
  3. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid location is provided.
  4. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void changeLocation_invalidLocation_throwsIllegalInputException() { @@ -347,8 +542,19 @@ public void changeLocation_invalidLocation_throwsIllegalInputException() { } /** - * Tests the changeLocation method of the MachineTrackerService class when an invalid id is passed as input. - * Expects a ResourceNotFoundException to be thrown. + * Test case for the {@link MachineTrackerService#changeLocation(int, String)} method of the {@link MachineTrackerService} class + * when an invalid ID is passed as input. + * Verifies that the method throws a {@link ResourceNotFoundException} when an invalid ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including no existing vending machine with the provided invalid ID.
  2. + *
  3. Act & Assert: Verify that a {@link ResourceNotFoundException} is thrown when an invalid ID is provided.
  4. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void changeLocation_invalidId_throwsResourceNotFoundException() { @@ -360,9 +566,18 @@ public void changeLocation_invalidId_throwsResourceNotFoundException() { } /** - * Tests the removeItem method of the MachineTrackerService class when an - * invalid item is passed as input. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#removeItem(int, String, int)} method of the {@link MachineTrackerService} class + * when an invalid item is passed as input. + * Verifies that the method throws an {@link IllegalInputException} when an invalid item is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid item is provided.
  2. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void removeItem_invalidItem_throwsIllegalInputException() { @@ -371,9 +586,18 @@ public void removeItem_invalidItem_throwsIllegalInputException() { } /** - * Tests the removeItem method of the MachineTrackerService class when an - * invalid quantity is provided. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#removeItem(int, String, int)} method of the {@link MachineTrackerService} class + * when an invalid quantity is provided. + * Verifies that the method throws an {@link IllegalInputException} when an invalid quantity is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act & Assert: Verify that an {@link IllegalInputException} is thrown when an invalid quantity is provided.
  2. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void removeItem_invalidQuantity_throwsIllegalInputException() { @@ -382,8 +606,19 @@ public void removeItem_invalidQuantity_throwsIllegalInputException() { } /** - * Tests the removeItem method of the MachineTrackerService class when an invalid id is provided. - * Expects a ResourceNotFoundException to be thrown. + * Test case for the {@link MachineTrackerService#removeItem(int, String, int)} method of the {@link MachineTrackerService} class + * when an invalid ID is provided. + * Verifies that the method throws a {@link ResourceNotFoundException} when an invalid ID is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including no existing vending machine with the provided invalid ID.
  2. + *
  3. Act & Assert: Verify that a {@link ResourceNotFoundException} is thrown when an invalid ID is provided.
  4. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void removeItem_invalidId_throwsResourceNotFoundException() { @@ -395,9 +630,20 @@ public void removeItem_invalidId_throwsResourceNotFoundException() { } /** - * Tests the removeItem method of the MachineTrackerService class when the item - * is not in the inventory. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#removeItem(int, String, int)} method of the {@link MachineTrackerService} class + * when the item is not in the inventory. + * Verifies that the method throws an {@link IllegalInputException} when the item is not present in the inventory. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an empty inventory for the vending machine.
  2. + *
  3. Act: Call the {@link MachineTrackerService#removeItem(int, String, int)} method.
  4. + *
  5. Assert: Verify that an {@link IllegalInputException} is thrown when the item is not in the inventory.
  6. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void removeItem_itemNotInInventory_throwsIllegalInputException() { @@ -413,9 +659,20 @@ public void removeItem_itemNotInInventory_throwsIllegalInputException() { } /** - * Tests the removeItem method of the MachineTrackerService class when the - * removal quantity is greater than the inventory. - * Expects an IllegalInputException to be thrown. + * Test case for the {@link MachineTrackerService#removeItem(int, String, int)} method of the {@link MachineTrackerService} class + * when the removal quantity is greater than the inventory. + * Verifies that the method throws an {@link IllegalInputException} when the removal quantity exceeds the available quantity in the inventory. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including an inventory with a lower quantity than the removal quantity.
  2. + *
  3. Act: Call the {@link MachineTrackerService#removeItem(int, String, int)} method.
  4. + *
  5. Assert: Verify that an {@link IllegalInputException} is thrown when the removal quantity is greater than the inventory.
  6. + *
+ * + * @throws Exception if any error occurs during the test. */ @Test public void removeItem_removalQuantityGreaterThanInventory_throwsIllegalInputException() { From f7752ae1f9b34147aaeeb51ca21e9cfcd5ce2301 Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 03:56:52 +0100 Subject: [PATCH 07/14] Issue #126: Update JavaDocs in Ui module --- .../ui/controller/RefillControllerUITest.java | 97 ++++++++ .../controller/VendAppControllerUITest.java | 233 +++++++++++++++++- 2 files changed, 329 insertions(+), 1 deletion(-) diff --git a/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/RefillControllerUITest.java b/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/RefillControllerUITest.java index fe506ba..f8bae2a 100644 --- a/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/RefillControllerUITest.java +++ b/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/RefillControllerUITest.java @@ -24,6 +24,19 @@ import vendmachtrack.ui.access.AccessService; import vendmachtrack.ui.access.MachineTrackerAccessible; +/** + * UI tests for the {@link RefillController} class. + * This class contains a suite of UI tests to verify the behavior of the RefillController when interacting with the application's user interface. + * Each test case focuses on specific UI interactions and expected outcomes. + * + *

+ * Test cases cover various scenarios including valid input, invalid input, edge cases, and error handling to ensure the UI behaves as expected. + *

+ * + *

+ * The tests in this class extend {@link ApplicationTest} + *

+ */ public class RefillControllerUITest extends ApplicationTest { private RefillController refillController; @@ -35,6 +48,24 @@ public class RefillControllerUITest extends ApplicationTest { @Mock private MachineTrackerAccessible mockAccess; + + /** + * Sets up the testing environment and initializes the JavaFX application for testing the RefillController class. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Mock the service and access objects to simulate interactions with external components.
  2. + *
  3. Mock a vending machine list and inventory data to provide test data for the application.
  4. + *
  5. Load the FXML file and initialize the RefillController with the mocked service and selected machine ID.
  6. + *
  7. Create and set up the JavaFX scene with the loaded parent and necessary styles.
  8. + *
  9. Show the JavaFX stage for testing.
  10. + *
+ * + * @param stage The JavaFX stage for the application. + * @throws Exception if any error occurs during the setup. + */ @Override public void start(Stage stage) throws Exception { @@ -65,6 +96,21 @@ public void start(Stage stage) throws Exception { stage.show(); } + /** + * Test case for the {@link RefillController} class's {@code testRefill} method with valid input. + * Verifies that the method correctly refills an item in the inventory when valid input is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a mock object for the access service with expected behavior, and configure input fields and UI elements.
  2. + *
  3. Act: Simulate user interaction by entering valid refill item information and clicking the refill button.
  4. + *
  5. Assert: Verify that the UI elements are updated as expected, including the text area displaying the updated inventory and the answer text field being empty.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void RefillController_testRefill_validInput() throws ConnectException { @@ -87,6 +133,18 @@ public void RefillController_testRefill_validInput() throws ConnectException { assertEquals("", textField.getText()); } + /** + * Test case for the {@link RefillController} class's {@code testRefill} method when an invalid number is provided. + * Verifies that the method handles the scenario of an invalid number input and displays the appropriate error message. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate user interaction by entering an invalid refill number (empty string) and a valid refill item, and clicking the refill button.
  2. + *
  3. Assert: Verify that the answer text field displays the expected error message indicating that the input is invalid.
  4. + *
+ */ @Test public void RefillController_testRefill_invalidNumber() { @@ -100,6 +158,18 @@ public void RefillController_testRefill_invalidNumber() { assertEquals("Invalid input: Please enter a valid number and item", textField.getText()); } + /** + * Test case for the {@link RefillController} class's {@code testRefill} method when an invalid item is provided. + * Verifies that the method handles the scenario of an invalid item input and displays the appropriate error message. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate user interaction by entering a valid refill number and an invalid refill item (empty string), and clicking the refill button.
  2. + *
  3. Assert: Verify that the answer text field displays the expected error message indicating that the input is invalid.
  4. + *
+ */ @Test public void RefillController_testRefill_invalidItem() { @@ -113,6 +183,18 @@ public void RefillController_testRefill_invalidItem() { assertEquals("Invalid input: Please enter a valid number and item", textField.getText()); } + /** + * Test case for the {@link RefillController} class's {@code testRefill} method when a negative integer is provided as the refill number. + * Verifies that the method handles the scenario of a negative integer input and displays the appropriate error message. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate user interaction by entering a negative integer as the refill number and a valid refill item, and clicking the refill button.
  2. + *
  3. Assert: Verify that the answer text field displays the expected error message indicating that the input is invalid.
  4. + *
+ */ @Test public void RefillController_testRefill_negativeInteger() { @@ -126,6 +208,21 @@ public void RefillController_testRefill_negativeInteger() { assertEquals("Invalid input: Please enter a valid number and item", textField.getText()); } + /** + * Test case for the {@link RefillController} class's {@code testRefill} method when refilling a new item. + * Verifies that the method correctly adds a new item to the inventory when valid input is provided. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Set up the necessary test data, including a mock object for the access service with expected behavior, and configure input fields and UI elements.
  2. + *
  3. Act: Simulate user interaction by entering valid refill information for a new item and clicking the refill button.
  4. + *
  5. Assert: Verify that the UI elements are updated as expected, including the text area displaying the updated inventory and the answer text field being empty.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void RefillController_testRefill_newItem() throws ConnectException { diff --git a/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/VendAppControllerUITest.java b/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/VendAppControllerUITest.java index 507d9ca..fdba590 100644 --- a/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/VendAppControllerUITest.java +++ b/vendmachtrack/ui/src/test/java/vendmachtrack/ui/controller/VendAppControllerUITest.java @@ -27,6 +27,16 @@ import java.net.ConnectException; import java.util.HashMap; +/** + * This class contains UI test cases for the {@link VendAppController} class using JavaFX Application Test. + * It focuses on testing the user interface and interaction with the application's GUI components. + *

+ * Mocks are used to simulate the behavior of the AccessService and MachineTrackerAccessible interfaces. + * The tests cover various scenarios related to the vending machine management application. + * + * @see VendAppController + * @see ApplicationTest + */ public class VendAppControllerUITest extends ApplicationTest { private VendAppController controller; @@ -38,6 +48,14 @@ public class VendAppControllerUITest extends ApplicationTest { @Mock private MachineTrackerAccessible mockAccess; + /** + * Initializes the test environment and sets up mocks for testing the {@link AppController} class. + * It mocks the behavior of the AccessService and MachineTrackerAccessible, loads the FXML, + * sets the mocked service to the controller, and initializes the scene for testing. + * + * @param stage The primary stage for the test application. + * @throws Exception If an exception occurs during the test setup. + */ @Override public void start(Stage stage) throws Exception { // Initialize mocks @@ -68,11 +86,35 @@ public void start(Stage stage) throws Exception { stage.show(); } + /** + * Test case for the {@link VenpAppController} class's initialization. + * Verifies that the controller instance is not null after initialization. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Assert: Verify that the controller instance is not null after initialization.
  2. + *
+ */ @Test public void testController_VenpAppController() { assertNotNull(this.controller); } + /** + * Test case for the {@link VendAppController} class's menu bar population. + * Verifies that the menu bar is populated with the expected number of items after updating the vending machine list. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Update the vending machine list using the controller.
  2. + *
  3. Act: Simulate clicking on the menu bar to trigger its population.
  4. + *
  5. Assert: Verify that the menu bar contains the expected number of items (choices).
  6. + *
+ */ @Test public void VendAppController_testMenuBarPopulation() { @@ -87,6 +129,21 @@ public void VendAppController_testMenuBarPopulation() { assertEquals(2, menuBar.getItems().size()); } + /** + * Test case for the {@link VendAppController} class's menu bar population and selection of a vending machine. + * Verifies that the menu bar displays available vending machines, allows selecting one, and shows its inventory when selected. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Mock the inventory data for a vending machine and set up the initial test conditions.
  2. + *
  3. Act: Simulate selecting a vending machine from the menu bar and clicking the OK button to display its inventory.
  4. + *
  5. Assert: Verify that the text area displays the expected inventory of the selected vending machine.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void VendAppController_testMenuBarPopulation_selectVendingMachine() throws ConnectException { @@ -97,7 +154,7 @@ public void VendAppController_testMenuBarPopulation_selectVendingMachine() throw when(mockAccess.getInventory(1)).thenReturn(mockInventory); // Act - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried object is of type ChoiceBox. ChoiceBox choiceBox = (ChoiceBox) lookup("#menuBar").queryAs(ChoiceBox.class); interact(() -> choiceBox.getSelectionModel().select(0)); @@ -108,6 +165,19 @@ public void VendAppController_testMenuBarPopulation_selectVendingMachine() throw FxAssert.verifyThat(textArea, TextInputControlMatchers.hasText("Inventory:\nCola: 5\nPepsi: 3\n")); } + /** + * Test case for the {@link VendAppController} class's menu bar population and attempting to select no vending machine. + * Verifies that when the user does not select any vending machine from the menu bar and clicks the OK button, + * an message is displayed indicating that no vending machine has been selected. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate clicking the OK button without selecting any vending machine from the menu bar.
  2. + *
  3. Assert: Verify that the text area displays the message indicating that no vending machine has been selected.
  4. + *
+ */ @Test public void VendAppController_testMenuBarPopulation_selectNoVendingMachine() { @@ -119,6 +189,19 @@ public void VendAppController_testMenuBarPopulation_selectNoVendingMachine() { FxAssert.verifyThat(textArea, TextInputControlMatchers.hasText("No vending machine selected")); } + /** + * Test case for the {@link VendAppController} class's refill functionality when no vending machine is selected. + * Verifies that when the user clicks the refill button without selecting any vending machine, + * an appropriate message is displayed indicating that a vending machine should be selected first. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate clicking the refill button without selecting any vending machine.
  2. + *
  3. Assert: Verify that the text area displays the message indicating that a vending machine should be selected first.
  4. + *
+ */ @Test public void VendAppController_testRefill_noVendingMachineSelected() { @@ -130,6 +213,19 @@ public void VendAppController_testRefill_noVendingMachineSelected() { FxAssert.verifyThat(textArea, TextInputControlMatchers.hasText("Please select a vending machine.")); } + /** + * Test case for the {@link VendAppController} class's refill functionality when no vending machine is selected. + * Verifies that when the user clicks the refill button without selecting any vending machine, + * an appropriate message is displayed indicating that a vending machine should be selected first. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate clicking the refill button without selecting any vending machine.
  2. + *
  3. Assert: Verify that the text area displays the message indicating that a vending machine should be selected first.
  4. + *
+ */ @Test public void VendAppController_testUserview_noVendingMachineSelected() { @@ -141,6 +237,22 @@ public void VendAppController_testUserview_noVendingMachineSelected() { FxAssert.verifyThat(textArea, TextInputControlMatchers.hasText("Please select a vending machine.")); } + /** + * Test case for the {@link VendAppController} when adding a valid vending machine. + * Verifies that when the user adds a new vending machine with a valid ID and location, + * the vending machine is successfully added to the tracker, and the menu bar is updated with the new vending machine. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Mock the behavior to add a new vending machine and set up initial test conditions.
  2. + *
  3. Act: Simulate entering a valid ID and location, clicking the add button, and checking the menu bar and output text.
  4. + *
  5. Assert: Verify that the vending machine is added successfully, and the menu bar and output text are updated accordingly.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void VendAppController_testAddVendingMachine_validVendingMachine() throws ConnectException { @@ -166,6 +278,22 @@ public void VendAppController_testAddVendingMachine_validVendingMachine() throws assertTrue(menuBar.getItems().contains("id: 3 (Bergen)")); } + /** + * Test case for the {@link VendAppController} class's addition of a vending machine with an existing ID. + * Verifies that when the user attempts to add a new vending machine with an existing ID, + * an appropriate error message is displayed indicating that the machine with that ID already exists. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Mock the behavior to throw an exception when attempting to add a vending machine with an existing ID.
  2. + *
  3. Act: Simulate entering an existing ID, a different location, clicking the add button, and checking the output text.
  4. + *
  5. Assert: Verify that the appropriate error message is displayed in the output text.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void VendAppController_testAddVendingMachine_ExistingID() throws ConnectException { @@ -183,6 +311,19 @@ public void VendAppController_testAddVendingMachine_ExistingID() throws ConnectE assertEquals("Machine with ID: 2 already exists", outputTextLabel.getText()); } + /** + * Test case for the {@link VendAppController} class's addition of a vending machine with an invalid ID. + * Verifies that when the user attempts to add a new vending machine with an invalid (non-integer) ID, + * an appropriate error message is displayed indicating that the machine ID must be an integer. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate entering an invalid (non-integer) ID, a location, clicking the add button, and checking the output text.
  2. + *
  3. Assert: Verify that the appropriate error message is displayed in the output text.
  4. + *
+ */ @Test public void VendAppController_testAddVendingMachine_InvalidID() { @@ -196,6 +337,22 @@ public void VendAppController_testAddVendingMachine_InvalidID() { assertEquals("Machine ID must be an integer", outputTextLabel.getText()); } + /** + * Test case for the {@link VendAppController} class's addition of a vending machine with an invalid location. + * Verifies that when the user attempts to add a new vending machine with an invalid location name, + * an appropriate error message is displayed indicating that the location name is not valid. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Mock the behavior to throw an exception when attempting to add a vending machine with an invalid location.
  2. + *
  3. Act: Simulate entering a valid ID, an invalid location name, clicking the add button, and checking the output text.
  4. + *
  5. Assert: Verify that the appropriate error message is displayed in the output text.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void VendAppController_testAddVendingMachine_InvalidLocation() throws ConnectException { @@ -213,6 +370,19 @@ public void VendAppController_testAddVendingMachine_InvalidLocation() throws Con assertEquals("Location Name not valid", outputTextLabel.getText()); } + /** + * Test case for the {@link VendAppController} class's addition of a vending machine when no ID is written. + * Verifies that when the user attempts to add a new vending machine without entering an ID, + * an appropriate error message is displayed indicating that the machine ID must be an integer. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate not entering an ID, entering a location, clicking the add button, and checking the output text.
  2. + *
  3. Assert: Verify that the appropriate error message is displayed in the output text.
  4. + *
+ */ @Test public void VendAppController_testAddVendingMachine_NoIdWritten() { @@ -225,6 +395,22 @@ public void VendAppController_testAddVendingMachine_NoIdWritten() { assertEquals("Machine ID must be an integer", outputTextLabel.getText()); } + /** + * Test case for the {@link VendAppController} class's addition of a vending machine when no location is written. + * Verifies that when the user attempts to add a new vending machine without entering a location name, + * an appropriate error message is displayed indicating that the location name is not valid. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Mock the behavior to throw an exception when attempting to add a vending machine with an empty location.
  2. + *
  3. Act: Simulate entering a valid ID, not entering a location, clicking the add button, and checking the output text.
  4. + *
  5. Assert: Verify that the appropriate error message is displayed in the output text.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void VendAppController_testAddVendingMachine_NoLocationWritten() throws ConnectException { @@ -241,6 +427,22 @@ public void VendAppController_testAddVendingMachine_NoLocationWritten() throws C assertEquals("Location Name not valid", outputTextLabel.getText()); } + /** + * Test case for the {@link VendAppController} class's removal of a vending machine. + * Verifies that when the user attempts to remove an existing vending machine, + * it is successfully removed from the tracker, and the appropriate confirmation message is displayed. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Mock the behavior to remove a vending machine from the tracker when requested.
  2. + *
  3. Act: Simulate entering the ID of an existing vending machine, clicking the remove button, and checking the output text and menu bar.
  4. + *
  5. Assert: Verify that the appropriate confirmation message is displayed, and the vending machine is removed from the menu bar.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void VendAppController_testRemoveVendingMachine_removesVendingmachine() throws ConnectException { @@ -261,6 +463,22 @@ public void VendAppController_testRemoveVendingMachine_removesVendingmachine() t assertFalse(menuBar.getItems().contains("id: 1 (Oslo)")); } + /** + * Test case for the {@link VendAppController} class's removal of a vending machine with an invalid ID. + * Verifies that when the user attempts to remove a vending machine with an invalid ID, + * an appropriate error message is displayed indicating that there is no such vending machine with the given ID. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Arrange: Mock the behavior to throw an exception when attempting to remove a vending machine with an invalid ID.
  2. + *
  3. Act: Simulate entering an invalid ID, clicking the remove button, and checking the output text.
  4. + *
  5. Assert: Verify that the appropriate error message is displayed in the output text.
  6. + *
+ * + * @throws ConnectException if there is a connection issue during the test. + */ @Test public void VendAppController_testRemoveVendingMachine_invalidID() throws ConnectException { @@ -276,6 +494,19 @@ public void VendAppController_testRemoveVendingMachine_invalidID() throws Connec assertEquals("No such Vending Machine with ID: 5", outputTextLabel.getText()); } + /** + * Test case for the {@link VendAppController} class's removal of a vending machine with a non-integer ID. + * Verifies that when the user attempts to remove a vending machine with a non-integer ID, + * an appropriate error message is displayed indicating that the machine ID must be an integer. + * + *

+ * Test Steps: + *

+ *
    + *
  1. Act: Simulate entering a non-integer ID, clicking the remove button, and checking the output text.
  2. + *
  3. Assert: Verify that the appropriate error message is displayed in the output text.
  4. + *
+ */ @Test public void VendAppController_testRemoveVendingMachine_notInteger() { From eb30f0ac9cd9813c128d7217f0e6df7665e4e4d3 Mon Sep 17 00:00:00 2001 From: Carl Edward Storlien Date: Wed, 8 Nov 2023 11:05:38 +0100 Subject: [PATCH 08/14] Issue #127: Update .gitlab-ci.yml file to copy .json file to home directory --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 883e52b..f2b4d10 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,5 @@ before_script: + - cp tracker.json /home/$(whoami)/ - cd vendmachtrack/ - mvn clean install -DskipTests From 3bcc7c6095b2f8d3a6fcbfa195f6369c0775b095 Mon Sep 17 00:00:00 2001 From: Carl Edward Storlien Date: Wed, 8 Nov 2023 11:08:32 +0100 Subject: [PATCH 09/14] Issue #127: Update .gitlab-ci.yml file to copy .json file to /root/ --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f2b4d10..d355985 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,5 @@ before_script: - - cp tracker.json /home/$(whoami)/ + - cp tracker.json /root/ - cd vendmachtrack/ - mvn clean install -DskipTests From b784f964bfc151e66334d20e974ea7e1b1a5c20f Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 16:03:46 +0100 Subject: [PATCH 10/14] Issue #118: Make new module for Integration tests In this commit the following has been done: - Made new Module named integrationtest - Added dependencies and plugins that was necessary in the moudles pom.xml - Updated the root Pom.xml - Made Integration Tests for the Application Co-Authored-By: Carl Edward Storlien --- vendmachtrack/e2e-tests/pom.xml | 20 -- .../e2e-tests/src/main/java/gr2338/Main.java | 7 - vendmachtrack/integrationtest/pom.xml | 189 +++++++++++++ .../integrationtest/IntegrationUITestIT.java | 254 ++++++++++++++++++ vendmachtrack/jacoco-aggregator/pom.xml | 10 +- vendmachtrack/pom.xml | 10 +- 6 files changed, 453 insertions(+), 37 deletions(-) delete mode 100644 vendmachtrack/e2e-tests/pom.xml delete mode 100644 vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java create mode 100644 vendmachtrack/integrationtest/pom.xml create mode 100644 vendmachtrack/integrationtest/src/test/java/vendmachtrack/integrationtest/IntegrationUITestIT.java diff --git a/vendmachtrack/e2e-tests/pom.xml b/vendmachtrack/e2e-tests/pom.xml deleted file mode 100644 index ea84050..0000000 --- a/vendmachtrack/e2e-tests/pom.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - 4.0.0 - - gr2338 - vendmachtrack - 3.0 - - - e2e-tests - - - 17 - 17 - UTF-8 - - - \ No newline at end of file diff --git a/vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java b/vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java deleted file mode 100644 index b1c0b21..0000000 --- a/vendmachtrack/e2e-tests/src/main/java/gr2338/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -package gr2338; - -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/vendmachtrack/integrationtest/pom.xml b/vendmachtrack/integrationtest/pom.xml new file mode 100644 index 0000000..6e7f6fe --- /dev/null +++ b/vendmachtrack/integrationtest/pom.xml @@ -0,0 +1,189 @@ + + + 4.0.0 + integrationtest + jar + + + gr2338 + vendmachtrack + 3.0 + + + + 17 + 17 + UTF-8 + ${project.basedir}/../test-results/ + + + + + + + org.junit.jupiter + junit-jupiter-api + + + org.junit.jupiter + junit-jupiter-engine + + + org.junit.jupiter + junit-jupiter-params + + + + gr2338 + core + ${project.version} + + + gr2338 + jsonio + ${project.version} + + + + gr2338 + ui + ${project.version} + + + + gr2338 + springboot + ${project.version} + + + + org.springframework.boot + spring-boot-starter-web + 3.1.4 + + + + org.springframework.boot + spring-boot-starter-test + 3.1.4 + test + + + + org.testfx + testfx-core + 4.0.16-alpha + test + + + org.testfx + testfx-junit5 + 4.0.16-alpha + test + + + + ch.qos.logback + logback-classic + 1.4.11 + + + + + + + + skip-graphical-ui-tests + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + + **/*UITest* + **/ui/controller/*Controller* + **/ui/App* + **/ui/Main* + + + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0 + + true + + + + + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0 + + + + integration-test + verify + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.1.4 + + vendmachtrack.springboot.SpringbootApplication + 7070 + true + + + + start-springrestserver + pre-integration-test + + start + + + 7070 + true + + test + + + + + stop-springrestserver + post-integration-test + + stop + + + 7070 + true + + + + + + + + diff --git a/vendmachtrack/integrationtest/src/test/java/vendmachtrack/integrationtest/IntegrationUITestIT.java b/vendmachtrack/integrationtest/src/test/java/vendmachtrack/integrationtest/IntegrationUITestIT.java new file mode 100644 index 0000000..92a5f1c --- /dev/null +++ b/vendmachtrack/integrationtest/src/test/java/vendmachtrack/integrationtest/IntegrationUITestIT.java @@ -0,0 +1,254 @@ +package vendmachtrack.integrationtest; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testfx.api.FxAssert; +import org.testfx.framework.junit5.ApplicationTest; +import org.testfx.matcher.base.NodeMatchers; +import org.testfx.matcher.control.TextInputControlMatchers; +import vendmachtrack.ui.App; + + + +import javafx.scene.Node; +import javafx.scene.control.ChoiceBox; +import javafx.scene.control.TextArea; +import javafx.stage.Stage; +import vendmachtrack.springboot.SpringbootApplication; +import org.junit.jupiter.api.TestInstance; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.testfx.api.FxAssert.verifyThat; +import static org.testfx.matcher.control.LabeledMatchers.hasText; + +import org.springframework.web.client.RestTemplate; + +/** + * This JUnit test class, `IntegrationUITestIT`, serves as an Integration test. + * It tests the integration between the UI and the Spring Boot application. + * + *

+ * + * Before the Tests are started the Spring Boot application is started. + * This ensures that the UI module uses RemoteAccess to communicate with the Spring Boot Rest API. + * And that the application works properly while the server is running. + * + *

+ * + * The test class simulates a user scenario by interacting with the UI and verifying the expected outcomes. + * this is done using TestFX, a testing framework for JavaFX applications. + *

+ * + * Note: Test method order is controlled using `@Order` annotations to ensure that the tests are run in + * a specific sequence, as some tests depend on the outcome of previous tests. + * + */ + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class IntegrationUITestIT extends ApplicationTest { + + /** + * Initializes and starts the JavaFX application. + */ + @Override + public void start(Stage stage) throws Exception { + + new vendmachtrack.ui.App().start(stage); + } + + /** + * Centers the current JavaFX stage (window) on the screen. + * This ensures that TestFx works properly + */ + private void centerCurrentStage() { + Stage currentStage = (Stage) lookup(".root").queryParent().getScene().getWindow(); + + double screenWidth = javafx.stage.Screen.getPrimary().getBounds().getWidth(); + double screenHeight = javafx.stage.Screen.getPrimary().getBounds().getHeight(); + double windowWidth = currentStage.getWidth(); + double windowHeight = currentStage.getHeight(); + + currentStage.setX((screenWidth - windowWidth) / 2); + currentStage.setY((screenHeight - windowHeight) / 2); + } + + + /** + * This JUnit test method represents an integration test for our vendmachtrack APplication. It simulates + * a user's interaction with different controllers and scenes in the application. The test is divided into + * several steps. + * + *

+ * + * Step 1: ServerController + * - Set the server URL and tracker file name. + * - Navigate to the VendAppController scene. + * - Asser that the "#Add" button is visible. + * + *

+ * + * + * Step 2: VendAppController + * - Add a vending machine with ID 1001 and location "Beijing" in the VendAppController. + * - Verify that the vending machine was added + * - Choose the newly added vending machine. + * - Press the "Refill" button to navigate to the RefillController scene. + * + *

+ * + * Step 3: RefillController + * - Add a "Cola" item with a quantity of 1 to the vending machine. + * - Assert that the item was successfully added to the vending machine's inventory. + * - Return to the VendAppController scene. + * + *

+ * + * Step 4: UserAppController + * - Navigate to the UserAppController scene. By pressing the "#UserView" button. + * - Verify that the the "Cola" item is visible in the user interface. + * - Buy a "Cola" item from the vending machine. + * - Assert that the item was bought and removed from the user interface. + * - Navigate to the PasswordHandlerController scene. By pressing the "#Back" button. + * + *

+ * + * Step 5: passwordHandlerController + * - Enter a wrong password and attempt to confirm it. + * - Verify that an error message indicates an incorrect password. + * - Return to the UserAppController scene. + * - Assert that the bougth item remains removed. + * + *

+ * + * @throws InterruptedException if any thread-related issues occur during test execution. + */ + @Test + @Order(1) + public void integrationTestFlow() throws InterruptedException { + + // STEP 1: ServerController: Write the server url and the tracker file name + clickOn("#serverUrlField").write("http://localhost:8080"); + clickOn("#trackerFileNameField").write("tracker.json"); + + // GO to VendAppController scene + clickOn("Submit"); + + verifyThat("#addButton", NodeMatchers.isVisible()); + + centerCurrentStage(); // Center the stage on the screen + + // STEP 2: VendAppController: Add a Vending Machine with Id: 1001 and location: Beijing + clickOn("#idTextFieldAdd").write("1001"); + clickOn("#locationTextField").write("Beijing"); + clickOn("#addButton"); + clickOn("#menuBar"); + + // Assert that the vending machine was added to the choice box + @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried object is of type ChoiceBox. + ChoiceBox choiceBox = (ChoiceBox) lookup("#menuBar").queryAs(ChoiceBox.class); + assertTrue(choiceBox.getItems().contains("id: 1001 (Beijing)")); + + // Choose the newly added VendingMachine + int lastIndex = choiceBox.getItems().size() - 1; // Get the index of the last item + interact(() -> choiceBox.getSelectionModel().select(lastIndex)); // Select the last item + + // GO to RefillController scene + clickOn("#refillButton"); + + // STEP 3: RefillController: Add a Cola item with 1 quantity to the vendingmachine + clickOn("#refillItem").write("Cola"); + clickOn("#refillNumber").write("1"); + clickOn("Refill"); + + // Assert that the item was added to the vending machine + TextArea textArea = lookup("#textArea").queryAs(TextArea.class); + FxAssert.verifyThat(textArea, TextInputControlMatchers.hasText("Inventory:\nCola: 1\n")); + + // GO back to VendAppController scene + clickOn("Back"); + + // STEP 4: Go to UserAppController scene + clickOn("#userView"); + + + // Assert that the vending machine was added + assertNotNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); + + // Buy the Cola item. + Node colaButton = lookup("#buttonContainer").lookup(".button").match(hasText("Cola")).query(); + clickOn(colaButton); + clickOn("#buy"); + + // Assert that the item was bought and removed from the UserController scene + assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); + + // Go to the PasswordHandler Scene + clickOn("#backButton"); + + // STEP 5: PasswordHandlerController: Write the Wrong password + clickOn("#passwordField").write("WRONG"); + clickOn("#confirmButton"); + + // Assert that the password was wrong + verifyThat("#label", hasText("Incorrect password. Please try again")); + + + // GO back to the Userview + clickOn("#backToUserView"); + + // Assert that the Item still is removed + assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); + } + + + // The reason for the split in the unit tests is because i dont want to Write the correct password in plain text for security reasons. + // Workaround for now, but will be fixed in the next release. + + + /** + *This Junit test will tests that the that you can remove a vending machine from the application. + * + *

+ * + * The test is divided into several steps. + * + *

+ * + *

  • Set the server URL and tracker file name.
  • + *
  • Navigate to the VendAppController scene.
  • + *
  • Center the stage on the screen.
  • + *
  • Remove the vending machine with ID 1001 from the application.
  • + *
  • Assert that the vending machine was removed from the choice box.
  • + * + */ + @Test + @Order(2) + public void test(){ + + // STEP 1: ServerController: Write the server url and the tracker file name + clickOn("#serverUrlField").write("http://localhost:8080"); + clickOn("#trackerFileNameField").write("tracker.json"); + clickOn("Submit"); + + // Center the stage on the screen + centerCurrentStage(); + + // Remove the Added Vending Machine + clickOn("#idTextFieldRemove").write("1001"); + clickOn("#removeButton"); + + // Assert that the vending machine was removed from the choice box + @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried object is of type ChoiceBox. + ChoiceBox choiceBox = (ChoiceBox) lookup("#menuBar").queryAs(ChoiceBox.class); + assertFalse(choiceBox.getItems().contains("id: 1001 (Beijing)")); + } +} diff --git a/vendmachtrack/jacoco-aggregator/pom.xml b/vendmachtrack/jacoco-aggregator/pom.xml index 1bae55e..cfee43f 100644 --- a/vendmachtrack/jacoco-aggregator/pom.xml +++ b/vendmachtrack/jacoco-aggregator/pom.xml @@ -39,11 +39,11 @@ springboot ${project.version} - - - - - + + gr2338 + integrationtest + ${project.version} + diff --git a/vendmachtrack/pom.xml b/vendmachtrack/pom.xml index 1c83311..4afefac 100644 --- a/vendmachtrack/pom.xml +++ b/vendmachtrack/pom.xml @@ -1,5 +1,5 @@ - + 4.0.0 gr2338 vendmachtrack @@ -20,7 +20,7 @@ jsonio springboot ui - e2e-tests + integrationtest jacoco-aggregator @@ -75,7 +75,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.8.1 @@ -157,5 +157,5 @@
    - + From fa1e2d39821b51139db7ab3a69f9948968f1ade9 Mon Sep 17 00:00:00 2001 From: Edward Storlien Date: Wed, 8 Nov 2023 17:00:22 +0100 Subject: [PATCH 11/14] Issue #118: Restructure 'integrationtests' module, update pom.xml files, skip SpotBugs Renamed and restructured the 'integrationtests' module such that it is now set correctly up as a Java module. This includes adding a module-info file. Updated pom.xml files accordingly. Also skipped SpotBugs testing for this module since it contains no classes other than integration test class. --- .../pom.xml | 24 ++- .../src/main/java/module-info.java | 3 + .../IntegrationUITestIT.java | 141 ++++++++---------- vendmachtrack/jacoco-aggregator/pom.xml | 7 +- vendmachtrack/pom.xml | 5 +- 5 files changed, 88 insertions(+), 92 deletions(-) rename vendmachtrack/{integrationtest => integrationtests}/pom.xml (87%) create mode 100644 vendmachtrack/integrationtests/src/main/java/module-info.java rename vendmachtrack/{integrationtest/src/test/java/vendmachtrack/integrationtest => integrationtests/src/test/java/vendmachtrack/integrationtests}/IntegrationUITestIT.java (81%) diff --git a/vendmachtrack/integrationtest/pom.xml b/vendmachtrack/integrationtests/pom.xml similarity index 87% rename from vendmachtrack/integrationtest/pom.xml rename to vendmachtrack/integrationtests/pom.xml index 6e7f6fe..ca5720e 100644 --- a/vendmachtrack/integrationtest/pom.xml +++ b/vendmachtrack/integrationtests/pom.xml @@ -1,8 +1,9 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - integrationtest + integrationtests jar @@ -86,7 +87,8 @@ ch.qos.logback logback-classic - 1.4.11 + 1.4.11 + @@ -131,6 +133,16 @@ + + + com.github.spotbugs + spotbugs-maven-plugin + 4.7.3.6 + + true + + + org.apache.maven.plugins maven-failsafe-plugin @@ -153,7 +165,7 @@ vendmachtrack.springboot.SpringbootApplication 7070 - true + @@ -164,7 +176,7 @@ 7070 - true + test @@ -178,7 +190,7 @@ 7070 - true + diff --git a/vendmachtrack/integrationtests/src/main/java/module-info.java b/vendmachtrack/integrationtests/src/main/java/module-info.java new file mode 100644 index 0000000..8d99758 --- /dev/null +++ b/vendmachtrack/integrationtests/src/main/java/module-info.java @@ -0,0 +1,3 @@ +module vendmachtrack.integrationtests { + +} \ No newline at end of file diff --git a/vendmachtrack/integrationtest/src/test/java/vendmachtrack/integrationtest/IntegrationUITestIT.java b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java similarity index 81% rename from vendmachtrack/integrationtest/src/test/java/vendmachtrack/integrationtest/IntegrationUITestIT.java rename to vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java index 92a5f1c..0eea2f4 100644 --- a/vendmachtrack/integrationtest/src/test/java/vendmachtrack/integrationtest/IntegrationUITestIT.java +++ b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java @@ -1,69 +1,51 @@ -package vendmachtrack.integrationtest; - -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.testfx.api.FxAssert; -import org.testfx.framework.junit5.ApplicationTest; -import org.testfx.matcher.base.NodeMatchers; -import org.testfx.matcher.control.TextInputControlMatchers; -import vendmachtrack.ui.App; - - +package vendmachtrack.integrationtests; import javafx.scene.Node; import javafx.scene.control.ChoiceBox; import javafx.scene.control.TextArea; import javafx.stage.Stage; -import vendmachtrack.springboot.SpringbootApplication; -import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.*; +import org.testfx.api.FxAssert; +import org.testfx.framework.junit5.ApplicationTest; +import org.testfx.matcher.base.NodeMatchers; +import org.testfx.matcher.control.TextInputControlMatchers; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.testfx.api.FxAssert.verifyThat; import static org.testfx.matcher.control.LabeledMatchers.hasText; -import org.springframework.web.client.RestTemplate; - /** * This JUnit test class, `IntegrationUITestIT`, serves as an Integration test. * It tests the integration between the UI and the Spring Boot application. - * + * + *

    *

    - * * Before the Tests are started the Spring Boot application is started. * This ensures that the UI module uses RemoteAccess to communicate with the Spring Boot Rest API. - * And that the application works properly while the server is running. - * - *

    + * And that the application works properly while the server is running. * + *

    + *

    * The test class simulates a user scenario by interacting with the UI and verifying the expected outcomes. * this is done using TestFX, a testing framework for JavaFX applications. *

    - * + *

    * Note: Test method order is controlled using `@Order` annotations to ensure that the tests are run in * a specific sequence, as some tests depend on the outcome of previous tests. - * */ @TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class IntegrationUITestIT extends ApplicationTest { - /** + /** * Initializes and starts the JavaFX application. */ @Override public void start(Stage stage) throws Exception { - new vendmachtrack.ui.App().start(stage); - } + new vendmachtrack.ui.App().start(stage); + } /** * Centers the current JavaFX stage (window) on the screen. @@ -87,31 +69,31 @@ private void centerCurrentStage() { * a user's interaction with different controllers and scenes in the application. The test is divided into * several steps. * - *

    - * + *

    + *

    * Step 1: ServerController * - Set the server URL and tracker file name. * - Navigate to the VendAppController scene. * - Asser that the "#Add" button is visible. * - *

    - * - * - * Step 2: VendAppController + *

    + *

    + *

    + * Step 2: VendAppController * - Add a vending machine with ID 1001 and location "Beijing" in the VendAppController. * - Verify that the vending machine was added * - Choose the newly added vending machine. * - Press the "Refill" button to navigate to the RefillController scene. - * - *

    - * + * + *

    + *

    * Step 3: RefillController * - Add a "Cola" item with a quantity of 1 to the vending machine. * - Assert that the item was successfully added to the vending machine's inventory. * - Return to the VendAppController scene. * - *

    - * + *

    + *

    * Step 4: UserAppController * - Navigate to the UserAppController scene. By pressing the "#UserView" button. * - Verify that the the "Cola" item is visible in the user interface. @@ -119,38 +101,38 @@ private void centerCurrentStage() { * - Assert that the item was bought and removed from the user interface. * - Navigate to the PasswordHandlerController scene. By pressing the "#Back" button. * - *

    - * + *

    + *

    * Step 5: passwordHandlerController * - Enter a wrong password and attempt to confirm it. * - Verify that an error message indicates an incorrect password. * - Return to the UserAppController scene. * - Assert that the bougth item remains removed. * - *

    + *

    * * @throws InterruptedException if any thread-related issues occur during test execution. */ @Test @Order(1) public void integrationTestFlow() throws InterruptedException { - + // STEP 1: ServerController: Write the server url and the tracker file name - clickOn("#serverUrlField").write("http://localhost:8080"); + clickOn("#serverUrlField").write("http://localhost:8080"); clickOn("#trackerFileNameField").write("tracker.json"); - + // GO to VendAppController scene - clickOn("Submit"); + clickOn("Submit"); verifyThat("#addButton", NodeMatchers.isVisible()); centerCurrentStage(); // Center the stage on the screen - + // STEP 2: VendAppController: Add a Vending Machine with Id: 1001 and location: Beijing clickOn("#idTextFieldAdd").write("1001"); clickOn("#locationTextField").write("Beijing"); clickOn("#addButton"); - clickOn("#menuBar"); + clickOn("#menuBar"); // Assert that the vending machine was added to the choice box @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried object is of type ChoiceBox. @@ -168,26 +150,26 @@ public void integrationTestFlow() throws InterruptedException { clickOn("#refillItem").write("Cola"); clickOn("#refillNumber").write("1"); clickOn("Refill"); - + // Assert that the item was added to the vending machine TextArea textArea = lookup("#textArea").queryAs(TextArea.class); FxAssert.verifyThat(textArea, TextInputControlMatchers.hasText("Inventory:\nCola: 1\n")); - + // GO back to VendAppController scene - clickOn("Back"); + clickOn("Back"); // STEP 4: Go to UserAppController scene - clickOn("#userView"); + clickOn("#userView"); + - - // Assert that the vending machine was added + // Assert that the vending machine was added assertNotNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); // Buy the Cola item. Node colaButton = lookup("#buttonContainer").lookup(".button").match(hasText("Cola")).query(); clickOn(colaButton); clickOn("#buy"); - + // Assert that the item was bought and removed from the UserController scene assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); @@ -201,46 +183,45 @@ public void integrationTestFlow() throws InterruptedException { // Assert that the password was wrong verifyThat("#label", hasText("Incorrect password. Please try again")); - + // GO back to the Userview clickOn("#backToUserView"); // Assert that the Item still is removed - assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); + assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); } // The reason for the split in the unit tests is because i dont want to Write the correct password in plain text for security reasons. - // Workaround for now, but will be fixed in the next release. - + // Workaround for now, but will be fixed in the next release. + /** - *This Junit test will tests that the that you can remove a vending machine from the application. + * This Junit test will tests that the that you can remove a vending machine from the application. * - *

    - * + *

    + *

    * The test is divided into several steps. - * + * *

    - * - *

  • Set the server URL and tracker file name.
  • - *
  • Navigate to the VendAppController scene.
  • - *
  • Center the stage on the screen.
  • - *
  • Remove the vending machine with ID 1001 from the application.
  • - *
  • Assert that the vending machine was removed from the choice box.
  • * + *
  • Set the server URL and tracker file name.
  • + *
  • Navigate to the VendAppController scene.
  • + *
  • Center the stage on the screen.
  • + *
  • Remove the vending machine with ID 1001 from the application.
  • + *
  • Assert that the vending machine was removed from the choice box.
  • */ @Test @Order(2) - public void test(){ + public void test() { // STEP 1: ServerController: Write the server url and the tracker file name - clickOn("#serverUrlField").write("http://localhost:8080"); + clickOn("#serverUrlField").write("http://localhost:8080"); clickOn("#trackerFileNameField").write("tracker.json"); - clickOn("Submit"); - + clickOn("Submit"); + // Center the stage on the screen - centerCurrentStage(); + centerCurrentStage(); // Remove the Added Vending Machine clickOn("#idTextFieldRemove").write("1001"); diff --git a/vendmachtrack/jacoco-aggregator/pom.xml b/vendmachtrack/jacoco-aggregator/pom.xml index cfee43f..0c753a4 100644 --- a/vendmachtrack/jacoco-aggregator/pom.xml +++ b/vendmachtrack/jacoco-aggregator/pom.xml @@ -8,7 +8,6 @@ gr2338 vendmachtrack 3.0 - ..
    @@ -39,11 +38,11 @@ springboot ${project.version} - + gr2338 - integrationtest + integrationtests ${project.version} - + diff --git a/vendmachtrack/pom.xml b/vendmachtrack/pom.xml index 4afefac..5ddf931 100644 --- a/vendmachtrack/pom.xml +++ b/vendmachtrack/pom.xml @@ -1,5 +1,6 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 gr2338 vendmachtrack @@ -20,7 +21,7 @@ jsonio springboot ui - integrationtest + integrationtests jacoco-aggregator From 1893aa27e1034fdedb38419367889084a63825fa Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 19:14:06 +0100 Subject: [PATCH 12/14] Issue #118: Change how vendingmachine is selected in menubar - changed how the vendingmachine is selected in the menubar, making the tests more stable - Made all new scenes go to the center of the screen, trying to make the tests more stable. --- .../integrationtests/IntegrationUITestIT.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java index 0eea2f4..6701f32 100644 --- a/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java +++ b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java @@ -137,15 +137,17 @@ public void integrationTestFlow() throws InterruptedException { // Assert that the vending machine was added to the choice box @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried object is of type ChoiceBox. ChoiceBox choiceBox = (ChoiceBox) lookup("#menuBar").queryAs(ChoiceBox.class); - assertTrue(choiceBox.getItems().contains("id: 1001 (Beijing)")); + String expectedItem = "id: 1001 (Beijing)"; + assertTrue(choiceBox.getItems().contains(expectedItem)); - // Choose the newly added VendingMachine - int lastIndex = choiceBox.getItems().size() - 1; // Get the index of the last item - interact(() -> choiceBox.getSelectionModel().select(lastIndex)); // Select the last item + // Click on the item in the ChoiceBox by its value + clickOn(expectedItem); // GO to RefillController scene clickOn("#refillButton"); + centerCurrentStage(); // Center the stage on the screen + // STEP 3: RefillController: Add a Cola item with 1 quantity to the vendingmachine clickOn("#refillItem").write("Cola"); clickOn("#refillNumber").write("1"); @@ -158,9 +160,12 @@ public void integrationTestFlow() throws InterruptedException { // GO back to VendAppController scene clickOn("Back"); + centerCurrentStage(); // Center the stage on the screen + // STEP 4: Go to UserAppController scene clickOn("#userView"); + centerCurrentStage(); // Center the stage on the screen // Assert that the vending machine was added assertNotNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); @@ -176,6 +181,8 @@ public void integrationTestFlow() throws InterruptedException { // Go to the PasswordHandler Scene clickOn("#backButton"); + centerCurrentStage(); // Center the stage on the screen + // STEP 5: PasswordHandlerController: Write the Wrong password clickOn("#passwordField").write("WRONG"); clickOn("#confirmButton"); @@ -187,6 +194,8 @@ public void integrationTestFlow() throws InterruptedException { // GO back to the Userview clickOn("#backToUserView"); + centerCurrentStage(); // Center the stage on the screen + // Assert that the Item still is removed assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); } From 9379d83d6c19ee690b230da5d7708fe93466aa85 Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 22:35:10 +0100 Subject: [PATCH 13/14] Issue #118: Add verification that ensures that Remote Access is used - Redirected the standard outputstream to capture the consule output to verify that the outputstream given indicates that remote acces is used. --- .../integrationtests/IntegrationUITestIT.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java index 6701f32..f6abe6d 100644 --- a/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java +++ b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java @@ -10,6 +10,9 @@ import org.testfx.matcher.base.NodeMatchers; import org.testfx.matcher.control.TextInputControlMatchers; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + import static org.junit.jupiter.api.Assertions.*; import static org.testfx.api.FxAssert.verifyThat; import static org.testfx.matcher.control.LabeledMatchers.hasText; @@ -72,9 +75,12 @@ private void centerCurrentStage() { *

    *

    * Step 1: ServerController + * - Redirect the standard output stream to capture console output. * - Set the server URL and tracker file name. * - Navigate to the VendAppController scene. - * - Asser that the "#Add" button is visible. + * - Assert that the captured console output indicates the use of remote access due to the healthy server connection. + * - Restore the original standard output stream. + * - Assert that the "#Add" button is visible. * *

    *

    @@ -117,6 +123,11 @@ private void centerCurrentStage() { @Order(1) public void integrationTestFlow() throws InterruptedException { + // Redirect the standard output stream to capture console output. + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + PrintStream originalOut = System.out; + System.setOut(new PrintStream(outContent)); + // STEP 1: ServerController: Write the server url and the tracker file name clickOn("#serverUrlField").write("http://localhost:8080"); clickOn("#trackerFileNameField").write("tracker.json"); @@ -124,6 +135,14 @@ public void integrationTestFlow() throws InterruptedException { // GO to VendAppController scene clickOn("Submit"); + // Restore the original standard output stream. + System.setOut(originalOut); + + // Assert that the captured console output indicates the use of remote access due to the healthy server connection. + assertTrue(outContent.toString().contains("Using remote access"), + "Expected remote access but got local access"); + + verifyThat("#addButton", NodeMatchers.isVisible()); centerCurrentStage(); // Center the stage on the screen @@ -195,7 +214,7 @@ public void integrationTestFlow() throws InterruptedException { clickOn("#backToUserView"); centerCurrentStage(); // Center the stage on the screen - + // Assert that the Item still is removed assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); } From 365cf0b0450b1ae0a4b105c8c375da5060d98bbc Mon Sep 17 00:00:00 2001 From: Alexander Grannes Date: Wed, 8 Nov 2023 23:02:25 +0100 Subject: [PATCH 14/14] Issue #118: Update javadocs --- .../integrationtests/IntegrationUITestIT.java | 125 +++++++++++------- 1 file changed, 75 insertions(+), 50 deletions(-) diff --git a/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java index f6abe6d..8832112 100644 --- a/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java +++ b/vendmachtrack/integrationtests/src/test/java/vendmachtrack/integrationtests/IntegrationUITestIT.java @@ -24,16 +24,19 @@ *

    *

    * Before the Tests are started the Spring Boot application is started. - * This ensures that the UI module uses RemoteAccess to communicate with the Spring Boot Rest API. + * This ensures that the UI module uses RemoteAccess to communicate with the + * Spring Boot Rest API. * And that the application works properly while the server is running. * *

    *

    - * The test class simulates a user scenario by interacting with the UI and verifying the expected outcomes. + * The test class simulates a user scenario by interacting with the UI and + * verifying the expected outcomes. * this is done using TestFX, a testing framework for JavaFX applications. *

    *

    - * Note: Test method order is controlled using `@Order` annotations to ensure that the tests are run in + * Note: Test method order is controlled using `@Order` annotations to ensure + * that the tests are run in * a specific sequence, as some tests depend on the outcome of previous tests. */ @@ -66,58 +69,76 @@ private void centerCurrentStage() { currentStage.setY((screenHeight - windowHeight) / 2); } - /** - * This JUnit test method represents an integration test for our vendmachtrack APplication. It simulates - * a user's interaction with different controllers and scenes in the application. The test is divided into + * This JUnit test method represents an integration test for our vendmachtrack + * APplication. It simulates + * a user's interaction with different controllers and scenes in the + * application. The test is divided into * several steps. * *

    *

    - * Step 1: ServerController - * - Redirect the standard output stream to capture console output. - * - Set the server URL and tracker file name. - * - Navigate to the VendAppController scene. - * - Assert that the captured console output indicates the use of remote access due to the healthy server connection. - * - Restore the original standard output stream. - * - Assert that the "#Add" button is visible. + * Step 1: ServerController: + *

      + *
    1. Redirect the standard output stream to capture console output.
    2. + *
    3. Set the server URL and tracker file name.
    4. + *
    5. Navigate to the VendAppController scene.
    6. + *
    7. Assert that the captured console output indicates the use of remote + * access due to the healthy server connection.
    8. + *
    9. Restore the original standard output stream.
    10. + *
    11. Assert that the "#Add" button is visible.
    12. + *
    * *

    *

    *

    * Step 2: VendAppController - * - Add a vending machine with ID 1001 and location "Beijing" in the VendAppController. - * - Verify that the vending machine was added - * - Choose the newly added vending machine. - * - Press the "Refill" button to navigate to the RefillController scene. + * + *

      + *
    1. Add a vending machine with ID 1001 and location "Beijing".
    2. + *
    3. Verify that the vending machine was added.
    4. + *
    5. Choose the newly added vending machine.
    6. + *
    7. Press the "Refill" button to navigate to the RefillController scene.
    8. + *
    * *

    *

    * Step 3: RefillController - * - Add a "Cola" item with a quantity of 1 to the vending machine. - * - Assert that the item was successfully added to the vending machine's inventory. - * - Return to the VendAppController scene. + *

      + *
    1. Add a "Cola" item with a quantity of 1 to the vending machine.
    2. + *
    3. Assert that the item was successfully added to the vending machine's + * inventory.
    4. + *
    5. Return to the VendAppController scene.
    6. + *
    * *

    *

    * Step 4: UserAppController - * - Navigate to the UserAppController scene. By pressing the "#UserView" button. - * - Verify that the the "Cola" item is visible in the user interface. - * - Buy a "Cola" item from the vending machine. - * - Assert that the item was bought and removed from the user interface. - * - Navigate to the PasswordHandlerController scene. By pressing the "#Back" button. + *

      + *
    1. Navigate to the UserAppController scene by pressing the "#UserView" + * button.
    2. + *
    3. Verify that the "Cola" item is visible in the user interface.
    4. + *
    5. Buy a "Cola" item from the vending machine.
    6. + *
    7. Assert that the item was bought and removed from the user interface.
    8. + *
    9. Navigate to the PasswordHandlerController scene by pressing the "#Back" + * button.
    10. + *
    * *

    - *

    - * Step 5: passwordHandlerController - * - Enter a wrong password and attempt to confirm it. - * - Verify that an error message indicates an incorrect password. - * - Return to the UserAppController scene. - * - Assert that the bougth item remains removed. + * + * + * Step 5: passwordHandlerController: + *

      + *
    1. Enter a wrong password and attempt to confirm it.
    2. + *
    3. Verify that an error message indicates an incorrect password.
    4. + *
    5. Return to the UserAppController scene.
    6. + *
    7. Assert that the bought item remains removed.
    8. + *
    * *

    * - * @throws InterruptedException if any thread-related issues occur during test execution. + * @throws InterruptedException if any thread-related issues occur during test + * execution. */ @Test @Order(1) @@ -138,23 +159,25 @@ public void integrationTestFlow() throws InterruptedException { // Restore the original standard output stream. System.setOut(originalOut); - // Assert that the captured console output indicates the use of remote access due to the healthy server connection. + // Assert that the captured console output indicates the use of remote access + // due to the healthy server connection. assertTrue(outContent.toString().contains("Using remote access"), "Expected remote access but got local access"); - verifyThat("#addButton", NodeMatchers.isVisible()); centerCurrentStage(); // Center the stage on the screen - // STEP 2: VendAppController: Add a Vending Machine with Id: 1001 and location: Beijing + // STEP 2: VendAppController: Add a Vending Machine with Id: 1001 and location: + // Beijing clickOn("#idTextFieldAdd").write("1001"); clickOn("#locationTextField").write("Beijing"); clickOn("#addButton"); clickOn("#menuBar"); // Assert that the vending machine was added to the choice box - @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried object is of type ChoiceBox. + @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried + // object is of type ChoiceBox. ChoiceBox choiceBox = (ChoiceBox) lookup("#menuBar").queryAs(ChoiceBox.class); String expectedItem = "id: 1001 (Beijing)"; assertTrue(choiceBox.getItems().contains(expectedItem)); @@ -167,7 +190,8 @@ public void integrationTestFlow() throws InterruptedException { centerCurrentStage(); // Center the stage on the screen - // STEP 3: RefillController: Add a Cola item with 1 quantity to the vendingmachine + // STEP 3: RefillController: Add a Cola item with 1 quantity to the + // vendingmachine clickOn("#refillItem").write("Cola"); clickOn("#refillNumber").write("1"); clickOn("Refill"); @@ -209,7 +233,6 @@ public void integrationTestFlow() throws InterruptedException { // Assert that the password was wrong verifyThat("#label", hasText("Incorrect password. Please try again")); - // GO back to the Userview clickOn("#backToUserView"); @@ -219,29 +242,30 @@ public void integrationTestFlow() throws InterruptedException { assertNull(lookup(".button").match(hasText("Cola")).tryQuery().orElse(null)); } - - // The reason for the split in the unit tests is because i dont want to Write the correct password in plain text for security reasons. + // The reason for the split in the unit tests is because i dont want to Write + // the correct password in plain text for security reasons. // Workaround for now, but will be fixed in the next release. - /** - * This Junit test will tests that the that you can remove a vending machine from the application. + * This Junit test will tests that the that you can remove a vending machine + * from the application. * *

    *

    * The test is divided into several steps. * *

    - * - *

  • Set the server URL and tracker file name.
  • - *
  • Navigate to the VendAppController scene.
  • - *
  • Center the stage on the screen.
  • - *
  • Remove the vending machine with ID 1001 from the application.
  • - *
  • Assert that the vending machine was removed from the choice box.
  • + *
      + *
    1. Set the server URL and tracker file name.
    2. + *
    3. Navigate to the VendAppController scene.
    4. + *
    5. Center the stage on the screen.
    6. + *
    7. Remove the vending machine with ID 1001 from the application.
    8. + *
    9. Assert that the vending machine was removed from the choice box.
    10. + *
    */ @Test @Order(2) - public void test() { + public void testIntegrationFlow2() { // STEP 1: ServerController: Write the server url and the tracker file name clickOn("#serverUrlField").write("http://localhost:8080"); @@ -256,7 +280,8 @@ public void test() { clickOn("#removeButton"); // Assert that the vending machine was removed from the choice box - @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried object is of type ChoiceBox. + @SuppressWarnings("unchecked") // Suppressing unchecked cast warning because we are certain that the queried + // object is of type ChoiceBox. ChoiceBox choiceBox = (ChoiceBox) lookup("#menuBar").queryAs(ChoiceBox.class); assertFalse(choiceBox.getItems().contains("id: 1001 (Beijing)")); }