aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/CarAddContTest.java
blob: a4c7d071ef011826c94d433127c23140646f0e3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package at.ac.tuwien.sepm.assignment.groupphase;




import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service.VehicleService;
import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidEmployeeException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javafx.scene.control.DialogPane;
import javafx.scene.input.MouseButton;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.testfx.api.FxToolkit;
import org.testfx.robot.Motion;

public class CarAddContTest extends HighDpiAwareApplicationTest{
    private VehicleService vehicleService;
    @Before
    public void setup() throws Exception {
        // TODO: check if testfx can be run in headless mode on Jenkins
        FxToolkit.registerPrimaryStage();
        FxToolkit.setupApplication(CarAddApp.class);
        vehicleService = CarAddApp.context.getBean(VehicleService.class);
    }
    @After
    public void cleanup() throws Exception {
        FxToolkit.cleanupStages();
    }
    @Test
    public void testClickAddValidEmployee() throws InvalidVehicleException, ServiceException {
        when(vehicleService.add(any())).thenReturn(1L);
        //clickOn("#cmb_Ctype", Motion.DIRECT, MouseButton.PRIMARY);


        clickOn("#btn_create", Motion.DIRECT, MouseButton.PRIMARY);
        Stage alertDialog = getTopModalStage();
        Assert.assertNotNull(alertDialog);
        DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot();
        Assert.assertEquals("Erfolgreich angelegt", dialogPane.getHeaderText());
    }
    @Test
    public void testClickAddInvalidEmployee()
            throws InvalidVehicleException, ServiceException {
        when(vehicleService.add(any())).thenThrow(InvalidEmployeeException.class);
        moveTo("#inputName");
        clickOn("#btnCreate", Motion.DIRECT, MouseButton.PRIMARY);
        Stage alertDialog = getTopModalStage();
        Assert.assertNotNull(alertDialog);
        DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot();
        Assert.assertEquals("Ungültige Eingabe", dialogPane.getHeaderText());
    }
    @Test
    public void testClickAddEmployeeWithPersistenceException()
            throws InvalidVehicleException, ServiceException{
        when(vehicleService.add(any())).thenThrow(PersistenceException.class);
        clickOn("#inputName", Motion.DIRECT, MouseButton.PRIMARY);
        write("Test");
        clickOn("#btnCreate", Motion.DIRECT, MouseButton.PRIMARY);
        Stage alertDialog = getTopModalStage();
        Assert.assertNotNull(alertDialog);
        DialogPane dialogPane = (DialogPane) alertDialog.getScene().getRoot();
        Assert.assertEquals("Speicherfehler", dialogPane.getHeaderText());
    }
    private Stage getTopModalStage() {
        List<Window> allWindows = new ArrayList<>(robotContext().getWindowFinder().listWindows());
        Collections.reverse(allWindows);
        return (Stage)
                allWindows
                        .stream()
                        .filter(window -> window instanceof Stage)
                        .filter(
                                window ->
                                        ((Stage) window).getModality()
                                                == Modality.APPLICATION_MODAL)
                        .findFirst()
                        .orElse(null);
    }

}