Merge branch 'tests' into 'dev'
Tested Automation FastUpdate See merge request sa4-2020/the-sanmarinoes/backend!157
This commit is contained in:
commit
6f43b6289f
4 changed files with 98 additions and 6 deletions
|
@ -3,9 +3,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Condition;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Condition;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class BooleanConditionDTO extends ConditionDTO {
|
public class BooleanConditionDTO extends ConditionDTO {
|
||||||
|
|
||||||
@NotNull @Getter @Setter private boolean on;
|
@NotNull @Getter @Setter private boolean on;
|
||||||
|
|
|
@ -3,9 +3,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class BooleanTriggerDTO extends TriggerDTO {
|
public class BooleanTriggerDTO extends TriggerDTO {
|
||||||
@NotNull @Getter @Setter private boolean on;
|
@NotNull @Getter @Setter private boolean on;
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class ScenePriorityDTO {
|
public class ScenePriorityDTO {
|
||||||
@NotNull @Getter @Setter private long sceneId;
|
@NotNull @Getter @Setter private long sceneId;
|
||||||
|
|
||||||
|
|
|
@ -3,15 +3,15 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.doNothing;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationFastUpdateRequest;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationSaveRequest;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationSaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation.BooleanConditionDTO;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation.BooleanTriggerDTO;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation.ScenePriorityDTO;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -33,6 +33,12 @@ public class AutomationControllerTests {
|
||||||
|
|
||||||
@Mock private Principal mockPrincipal;
|
@Mock private Principal mockPrincipal;
|
||||||
|
|
||||||
|
@Mock private TriggerRepository<Trigger<?>> triggerRepository;
|
||||||
|
|
||||||
|
@Mock private ConditionRepository<Condition<?>> conditionRepository;
|
||||||
|
|
||||||
|
@Mock private ScenePriorityRepository scenePriorityRepository;
|
||||||
|
|
||||||
private final User u;
|
private final User u;
|
||||||
|
|
||||||
public AutomationControllerTests() {
|
public AutomationControllerTests() {
|
||||||
|
@ -100,6 +106,80 @@ public class AutomationControllerTests {
|
||||||
.isInstanceOf(NotFoundException.class);
|
.isInstanceOf(NotFoundException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFastUpdate() throws NotFoundException {
|
||||||
|
when(mockPrincipal.getName()).thenReturn("user");
|
||||||
|
when(userRepository.findByUsername("user")).thenReturn(u);
|
||||||
|
|
||||||
|
final Automation a = new Automation();
|
||||||
|
a.setId(42L);
|
||||||
|
a.setName("Old Name");
|
||||||
|
|
||||||
|
final RangeTrigger rt = new RangeTrigger();
|
||||||
|
final RangeCondition co = new RangeCondition();
|
||||||
|
|
||||||
|
a.getTriggers().add(rt);
|
||||||
|
a.getConditions().add(co);
|
||||||
|
|
||||||
|
final AutomationFastUpdateRequest f = new AutomationFastUpdateRequest();
|
||||||
|
f.setName("New name");
|
||||||
|
f.setId(43L);
|
||||||
|
f.setScenes(List.of(new ScenePriorityDTO(30L, 1)));
|
||||||
|
|
||||||
|
final BooleanConditionDTO b = new BooleanConditionDTO(true);
|
||||||
|
b.setDeviceId(1L);
|
||||||
|
f.setConditions(List.of(b));
|
||||||
|
|
||||||
|
final BooleanTriggerDTO c = new BooleanTriggerDTO(true);
|
||||||
|
c.setDeviceId(1L);
|
||||||
|
f.setTriggers(List.of(c));
|
||||||
|
|
||||||
|
doAnswer(
|
||||||
|
i -> {
|
||||||
|
a.getTriggers().clear();
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.when(triggerRepository)
|
||||||
|
.deleteAllByAutomationId(42L);
|
||||||
|
doAnswer(
|
||||||
|
i -> {
|
||||||
|
a.getConditions().clear();
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.when(conditionRepository)
|
||||||
|
.deleteAllByAutomationId(42L);
|
||||||
|
doAnswer(
|
||||||
|
i -> {
|
||||||
|
a.getScenes().clear();
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.when(scenePriorityRepository)
|
||||||
|
.deleteAllByAutomationId(42L);
|
||||||
|
|
||||||
|
when(automationRepository.findByIdAndUserId(42L, 1L)).thenReturn(Optional.of(a));
|
||||||
|
when(automationRepository.findByIdAndUserId(43L, 1L)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
when(conditionRepository.saveAll(any())).thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
when(triggerRepository.saveAll(any())).thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
when(scenePriorityRepository.saveAll(any())).thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
when(automationRepository.save(any())).thenAnswer(i -> i.getArguments()[0]);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> automationController.fastUpdate(f, mockPrincipal))
|
||||||
|
.isInstanceOf(NotFoundException.class);
|
||||||
|
|
||||||
|
f.setId(42L);
|
||||||
|
|
||||||
|
Automation saved = automationController.fastUpdate(f, mockPrincipal);
|
||||||
|
|
||||||
|
assertThat(saved.getId()).isEqualTo(42L);
|
||||||
|
assertThat(saved.getName()).isEqualTo("New name");
|
||||||
|
assertThat(saved.getTriggers()).doesNotContain(rt);
|
||||||
|
assertThat(saved.getConditions()).doesNotContain(co);
|
||||||
|
assertThat(saved.getTriggers().size()).isEqualTo(1);
|
||||||
|
assertThat(saved.getConditions().size()).isEqualTo(1);
|
||||||
|
assertThat(saved.getScenes().size()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelete() {
|
public void testDelete() {
|
||||||
final Automation old = new Automation();
|
final Automation old = new Automation();
|
||||||
|
|
Loading…
Reference in a new issue