Merge branch '92-inputdeviceconnectioncontroller-tests' into 'dev'

Resolve "inputDeviceConnectionController Tests"

Closes #92

See merge request sa4-2020/the-sanmarinoes/backend!210
This commit is contained in:
Claudio Maggioni 2020-05-27 22:51:56 +02:00
commit 3eea344b6f
5 changed files with 73 additions and 7 deletions

View file

@ -29,7 +29,7 @@ public class ButtonDimmerController
ButtonDimmerRepository inputRepository,
DimmableRepository<Dimmable> outputRepository,
DeviceService deviceService) {
super(inputRepository, outputRepository);
super(inputRepository, outputRepository, deviceService);
this.deviceService = deviceService;
this.buttonDimmerRepository = inputRepository;
}

View file

@ -12,7 +12,6 @@ import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@ -28,7 +27,7 @@ import org.springframework.web.bind.annotation.RequestBody;
public abstract class InputDeviceConnectionController<
I extends InputDevice & Connectable<O>, O extends OutputDevice> {
private class Connection {
protected class Connection {
private final I input;
private final List<O> outputs;
@ -54,7 +53,7 @@ public abstract class InputDeviceConnectionController<
return outputReposiory;
}
@Autowired private DeviceService deviceService;
private final DeviceService deviceService;
private final DeviceRepository<I> inputRepository;
@ -67,9 +66,12 @@ public abstract class InputDeviceConnectionController<
* @param outputRepository the output device repository
*/
protected InputDeviceConnectionController(
DeviceRepository<I> inputRepository, DeviceRepository<O> outputRepository) {
DeviceRepository<I> inputRepository,
DeviceRepository<O> outputRepository,
DeviceService deviceService) {
this.inputRepository = inputRepository;
this.outputReposiory = outputRepository;
this.deviceService = deviceService;
}
private Connection checkConnectionIDs(Long inputId, List<Long> outputs, String username)

View file

@ -28,7 +28,7 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
KnobDimmerRepository inputRepository,
DimmableRepository<Dimmable> outputRepository,
DeviceService deviceService) {
super(inputRepository, outputRepository);
super(inputRepository, outputRepository, deviceService);
this.knobDimmerRepository = inputRepository;
this.deviceService = deviceService;
}

View file

@ -34,7 +34,7 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
SwitchRepository inputRepository,
SwitchableRepository<Switchable> outputRepository,
DeviceService deviceService) {
super(inputRepository, outputRepository);
super(inputRepository, outputRepository, deviceService);
this.deviceService = deviceService;
this.switchRepository = inputRepository;
}

View file

@ -0,0 +1,64 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.anyIterable;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class InputDeviceConnectionControllerTests {
@Mock private DeviceService deviceService;
@Mock private KnobDimmerRepository inputRepository;
@Mock private DimmableRepository<Dimmable> outputRepository;
private KnobDimmerController knobDimmerController;
@Mock private Principal mockPrincipal;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
}
@Test
public void testConnection() throws NotFoundException {
KnobDimmer knobDimmer = new KnobDimmer();
DimmableLight dimmableLight = new DimmableLight();
knobDimmer.addDimmable(dimmableLight);
when(inputRepository.findByIdAndUsername(anyLong(), anyString()))
.thenReturn(java.util.Optional.of(knobDimmer));
when(outputRepository.findByIdAndUsername(anyLong(), anyString()))
.thenReturn(java.util.Optional.of(dimmableLight));
when(deviceService.saveAllAsOwner(anyIterable(), anyString()))
.thenReturn(List.of(new DimmableLight()));
List<Long> l = new ArrayList<>();
l.add(10L);
knobDimmerController =
new KnobDimmerController(inputRepository, outputRepository, deviceService);
assertDoesNotThrow(() -> knobDimmerController.addLight(1L, l, mockPrincipal));
}
}