From 4b96820d36d2b07154313396f7275341bc2896e8 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 20 May 2020 10:14:17 +0200 Subject: [PATCH] Tested Automation FastUpdate --- .../dto/automation/BooleanConditionDTO.java | 4 + .../dto/automation/BooleanTriggerDTO.java | 4 + .../dto/automation/ScenePriorityDTO.java | 4 + .../controller/AutomationControllerTests.java | 92 +++++++++++++++++-- 4 files changed, 98 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanConditionDTO.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanConditionDTO.java index eb3dbdb..fd5e807 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanConditionDTO.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanConditionDTO.java @@ -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.Condition; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; +@NoArgsConstructor +@AllArgsConstructor public class BooleanConditionDTO extends ConditionDTO { @NotNull @Getter @Setter private boolean on; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanTriggerDTO.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanTriggerDTO.java index 43a3950..c73de71 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanTriggerDTO.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/BooleanTriggerDTO.java @@ -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.Trigger; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; +@NoArgsConstructor +@AllArgsConstructor public class BooleanTriggerDTO extends TriggerDTO { @NotNull @Getter @Setter private boolean on; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/ScenePriorityDTO.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/ScenePriorityDTO.java index 4d4b251..65761cb 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/ScenePriorityDTO.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/automation/ScenePriorityDTO.java @@ -3,9 +3,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto.automation; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; +@NoArgsConstructor +@AllArgsConstructor public class ScenePriorityDTO { @NotNull @Getter @Setter private long sceneId; diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java index b0e3a68..452ffc7 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationControllerTests.java @@ -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.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; +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.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.models.Automation; -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 ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.security.Principal; import java.util.List; import java.util.Optional; @@ -33,6 +33,12 @@ public class AutomationControllerTests { @Mock private Principal mockPrincipal; + @Mock private TriggerRepository> triggerRepository; + + @Mock private ConditionRepository> conditionRepository; + + @Mock private ScenePriorityRepository scenePriorityRepository; + private final User u; public AutomationControllerTests() { @@ -100,6 +106,80 @@ public class AutomationControllerTests { .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 public void testDelete() { final Automation old = new Automation();