From ec93ff22cc8a7da3707eaaca10f90de653f111a4 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 27 May 2020 10:34:48 +0200 Subject: [PATCH] Tests --- build.gradle | 1 + .../smarthut/models/ConfirmationToken.java | 5 ++--- .../sa4/sanmarinoes/smarthut/models/User.java | 3 +++ .../controller/RegularLightControllerTests.java | 2 +- .../controller/RoomControllerTests.java | 2 +- .../smarthut/models/ConfirmationTokenTests.java | 17 ++++++++++++++++- .../service/DevicePopulationServiceTests.java | 3 ++- 7 files changed, 26 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index a933dc9..abd4c31 100644 --- a/build.gradle +++ b/build.gradle @@ -37,6 +37,7 @@ dependencies { testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } + testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.3' testImplementation 'org.springframework.security:spring-security-test' testImplementation 'com.h2database:h2:1.4.200' // Fixes https://stackoverflow.com/a/60455550 diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java index 74028cb..7d73b41 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java @@ -9,7 +9,7 @@ import lombok.NonNull; @Data @Entity -public class ConfirmationToken { +public final class ConfirmationToken { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -55,7 +55,6 @@ public class ConfirmationToken { if (o == null || getClass() != o.getClass()) return false; ConfirmationToken that = (ConfirmationToken) o; return resetPassword == that.resetPassword - && Objects.equals(id, that.id) && confirmToken.equals(that.confirmToken) && createdDate.equals(that.createdDate) && Objects.equals(user, that.user); @@ -63,6 +62,6 @@ public class ConfirmationToken { @Override public int hashCode() { - return Objects.hash(id, confirmToken, createdDate, user, resetPassword); + return Objects.hash(confirmToken, createdDate, user, resetPassword); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java index fc575b5..95b30d1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -6,6 +6,7 @@ import java.util.HashSet; import java.util.Objects; import java.util.Set; import javax.persistence.*; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -56,6 +57,7 @@ public class User { @GsonExclude @Getter @ToString.Exclude + @EqualsAndHashCode.Exclude private Set guests = new HashSet<>(); @ManyToMany(cascade = CascadeType.DETACH) @@ -66,6 +68,7 @@ public class User { @GsonExclude @Getter @ToString.Exclude + @EqualsAndHashCode.Exclude private Set hosts = new HashSet<>(); /** Determines whether a guest can access security cameras */ diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java index 8d9e548..6b093d7 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightControllerTests.java @@ -58,7 +58,7 @@ public class RegularLightControllerTests { @Test public void testGetAll() { when(regularLightRepository.findAll()).thenReturn(List.of()); - assertThat(regularLightController.findAll().isEmpty()); + assertThat(regularLightController.findAll()).isEmpty(); } @Test diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java index fb4e105..38cd498 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomControllerTests.java @@ -171,6 +171,6 @@ public class RoomControllerTests { public void testGetDevices() throws NotFoundException { when(mockPrincipal.getName()).thenReturn("user"); when(deviceService.findAll(2020L, 10L, "user")).thenReturn(List.of()); - assertThat(roomController.getDevices(2020L, mockPrincipal, 10L)); + assertThat(roomController.getDevices(2020L, mockPrincipal, 10L)).isNotNull(); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenTests.java index 85e8c33..a46e35d 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenTests.java @@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import static org.junit.jupiter.api.Assertions.*; import java.util.Date; +import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -66,7 +67,7 @@ public class ConfirmationTokenTests { public void equals() { User user = new User(); user.setName("Tizio Sempronio"); - user.setId(42l); + user.setId(42L); user.setUsername("xXCoolUserNameXx"); user.setEmail("realMail@service.ext"); user.setPassword("alpaca"); @@ -75,4 +76,18 @@ public class ConfirmationTokenTests { assertNotEquals(t, token); } + + @Test + public void testEqualsHashCode() { + final User u = new User(); + u.setId(1L); + final User t = new User(); + t.setId(2L); + EqualsVerifier.forClass(ConfirmationToken.class) + .withNonnullFields("createdDate") + .withNonnullFields("confirmToken") + .withPrefabValues(User.class, u, t) + .verify(); + assertDoesNotThrow(() -> new ConfirmationToken().hashCode()); + } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java index a40ee49..3aa5d9c 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationServiceTests.java @@ -1,5 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.mockito.Mockito.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; @@ -38,6 +39,6 @@ public class DevicePopulationServiceTests { list.add(t1); list.add(light2); doNothing().when(populationService).populateMeasuredTemperature(t1); - service.populateComputedFields(list); + assertDoesNotThrow(() -> service.populateComputedFields(list)); } }