package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException; import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationException; import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao.OperationDAO; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao.VehicleDAO; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation.Severity; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation.Status; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle; import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.ConstructionType; import java.time.Instant; import java.util.Collections; import java.util.Set; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class OperationServiceTest { @Mock private OperationDAO operationDAO; @Mock private VehicleDAO vehicleDAO; @InjectMocks private OperationServiceImpl operationService; private Set vehicles; private Vehicle v1, v2, v3, v4, v5; private Operation baseOp, o1, o2; @Before public void setUp() throws Exception { v1 = Vehicle.builder() .id(1) .name("RTW-1") .constructionType(ConstructionType.HOCHDACH) .type(Vehicle.VehicleType.RTW) .status(Vehicle.Status.FREI_FUNK) .hasNef(true) .build(); v2 = Vehicle.builder() .id(2) .name("KTW-1") .constructionType(ConstructionType.HOCHDACH) .type(Vehicle.VehicleType.KTW) .status(Vehicle.Status.FREI_WACHE) .hasNef(true) .build(); v3 = Vehicle.builder() .id(3) .name("KTW-2") .constructionType(ConstructionType.MITTELHOCHDACH) .type(Vehicle.VehicleType.KTW_B) .status(Vehicle.Status.FREI_FUNK) .hasNef(false) .build(); v4 = Vehicle.builder() .id(4) .name("BKTW-2") .constructionType(ConstructionType.HOCHDACH) .type(Vehicle.VehicleType.BKTW) .status(Vehicle.Status.FREI_FUNK) .hasNef(false) .build(); v5 = Vehicle.builder() .id(5) .name("NEF-1") .constructionType(ConstructionType.NORMAL) .type(Vehicle.VehicleType.NEF) .status(Vehicle.Status.FREI_WACHE) .hasNef(true) .build(); Vehicle v6 = Vehicle.builder() .id(6) .name("NAH-1") .constructionType(ConstructionType.MITTELHOCHDACH) .type(Vehicle.VehicleType.NAH) .status(Vehicle.Status.ABGEMELDET) .hasNef(true) .build(); vehicles = Set.of(v1, v2, v3, v4, v5, v6); baseOp = Operation.builder() .opCode("ALP-95E7") .severity(Severity.E) .status(Status.ACTIVE) .vehicles(Collections.singleton(v1)) .destination("Wiedner Hauptstraße 35, Wien") .build(); o1 = baseOp.toBuilder().id(1).created(Instant.now()).build(); o2 = o1.toBuilder().id(5).status(Status.CANCELLED).build(); when(operationDAO.get(anyLong())) .thenAnswer( ans -> { long arg = ans.getArgument(0); if (arg == 1L) return o1; else if (arg == 5L) return o2; else throw new ElementNotFoundException(""); }); when(vehicleDAO.get(anyLong())) .thenAnswer( ans -> { int arg = ((Long) ans.getArgument(0)).intValue(); return vehicles.stream() .filter(v -> v.id() == arg) .findFirst() .orElseThrow(() -> new ElementNotFoundException("")); }); } @Test public void requestNormal() throws Exception { Set vehicleIds = Set.of(2L, 3L, 4L, 5L); operationService.requestVehicles(1, vehicleIds); Operation result = operationDAO.get(1).toBuilder().vehicles(Set.of(v1, v2, v3, v4, v5)).build(); verify(operationDAO, times(1)).update(result); verify(operationDAO, times(0)).get(6L); } @Test public void requestExistingVehicle() throws Exception { operationService.requestVehicles(1, Set.of(1L)); Operation result = operationDAO.get(1); verify(operationDAO, times(0)).update(result); } @Test(expected = InvalidVehicleException.class) public void requestInvalidVehicle() throws Exception { operationService.requestVehicles(1, Set.of(5L, 6L)); } @Test(expected = InvalidOperationException.class) public void requestInvalidOperation() throws Exception { operationService.requestVehicles(2, Set.of(1L)); } @Test(expected = InvalidVehicleException.class) public void requestInactiveVehicle() throws Exception { operationService.requestVehicles(1, Set.of(6L)); } @Test(expected = InvalidOperationException.class) public void requestInactiveOperation() throws Exception { operationService.requestVehicles(5, Set.of(1L)); } @Test public void addOperation() throws Exception { operationService.add(baseOp.toBuilder().severity(null).build()); verify(operationDAO, times(1)) .add(baseOp.toBuilder().created(any()).status(Status.ACTIVE).build()); verify(vehicleDAO, times(1)).get(v1.id()); } @Test(expected = InvalidOperationException.class) public void addWithSeverity() throws Exception { operationService.add(baseOp.toBuilder().severity(Severity.E).build()); } @Test(expected = InvalidOperationException.class) public void addWithoutVehicles() throws Exception { operationService.add(baseOp.toBuilder().vehicles(Set.of()).build()); } @Test(expected = InvalidOperationException.class) public void addInvalidOpcode() throws Exception { operationService.add(baseOp.toBuilder().opCode("ABC").build()); } @Test(expected = InvalidOperationException.class) public void addWithSetCreated() throws Exception { operationService.add(baseOp.toBuilder().created(Instant.now()).build()); } }