userEntityList = userRepository.findAll();
+ assertFalse(userEntityList.isEmpty());
+ }
+
+}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/persistence/UserRepositoryTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/persistence/UserRepositoryTest.java
index ae0acb38..28502872 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/persistence/UserRepositoryTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/persistence/UserRepositoryTest.java
@@ -1,25 +1,35 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
package de.bluewhale.sabi.persistence;
-import de.bluewhale.sabi.BasicDataFactory;
import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.persistence.model.UserEntity;
import de.bluewhale.sabi.persistence.repositories.UserRepository;
+import de.bluewhale.sabi.util.TestContainerVersions;
+import org.flywaydb.core.Flyway;
+import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.time.LocalDateTime;
import java.util.Locale;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertNotEquals;
import static org.springframework.test.util.AssertionErrors.assertNotNull;
@@ -31,40 +41,77 @@
* Date: 14.11.2015
*/
@SpringBootTest
+@Testcontainers
@ContextConfiguration(classes = AppConfig.class)
-@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
-public class UserRepositoryTest extends BasicDataFactory {
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("IntegrationTest")
+@Transactional
+@DirtiesContext
+// DirtiesContext: Spring context is refreshed after the test class is executed,
+// which includes reinitializing the HikariCP datasource (which is defined at spring level, while the testcontainer is not)
+public class UserRepositoryTest implements TestContainerVersions {
+
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+ @Autowired
+ private Flyway flyway;
@Autowired
UserRepository userRepository;
- /**
- * There seems to be a timing problem with H2, that causes, that the basic data is not available
- * for some test classes, while for others it worked out. Until we know what's going wrong...
- * we "double inject" by extending the BasicTestDataFactory and by calling it directly.
- * The different behaviour can be observed by e.g. calling the master test suite and as comparising
- * the measurement testsuite while this is method is deaktivated.
- */
@BeforeEach
- public void ensureBasicDataAvailability() {
- UserEntity byEmail = userRepository.getByEmail(P_USER1_EMAIL);
- if (byEmail == null) populateBasicData();
- UserEntity byEmail2 = userRepository.getByEmail(P_USER1_EMAIL);
- assertNotNull("H2-Basicdata injection did not work!" ,byEmail2);
+ public void setUp() {
+
+ // flyway.clean(); // Optional: Clean DB before each single test
+ // org.flywaydb.core.api.FlywayException: Unable to execute clean as it has been disabled with the 'flyway.cleanDisabled' property.
+ flyway.migrate();
+
+ }
+
+ @AfterAll
+ static void cleanup() {
+ mariaDBContainer.stop();
}
@Test
- @Transactional
- public void testProbeTracebleAttributeMappingsOnTestData() throws Exception {
+ void connectionEstablished(){
+ assertThat(mariaDBContainer.isCreated());
+ assertThat(mariaDBContainer.isRunning());
+ }
- UserEntity userEntity = userRepository.getByEmail("sabi@bluewhale.de");
+
+ @Test
+ @Rollback
+ public void testProbeTracebleAttributeMappingsHasBeenSet() throws Exception {
+
+ String email = "P_USER1_EMAIL@bluewhale.de";
+
+ // Given
+ UserEntity testuser1 = new UserEntity();
+ testuser1.setEmail(email);
+ testuser1.setPassword("098f6bcd4621d373cade4e832627b4f6");
+ testuser1.setUsername("Tim");
+ testuser1.setValidateToken("NO_IDEA");
+ testuser1.setValidated(true);
+ testuser1.setLanguage("de");
+ testuser1.setCountry("DE");
+ userRepository.save(testuser1);
+
+ // When
+ UserEntity userEntity = userRepository.getByEmail(email);
+
+ // Then
assertNotNull("Missing Default Testdata", userEntity);
assertNotNull("Temporal Column CreatedOn not mapped.", userEntity.getCreatedOn());
assertNotNull("Temporal Column LastmodOn not mapped.", userEntity.getLastmodOn());
}
@Test
- @Transactional
+ @Rollback
public void testCreateUser() throws Exception {
// given
@@ -88,7 +135,7 @@ public void testCreateUser() throws Exception {
}
@Test
- @Transactional
+ @Rollback
public void testModifierAttributesViaGenericDAO() throws Exception {
// given
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/AquariumIoTControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/AquariumIoTControllerTest.java
index 0bbf4f9c..7376e3b1 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/AquariumIoTControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/AquariumIoTControllerTest.java
@@ -1,27 +1,36 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
package de.bluewhale.sabi.rest.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.model.IoTMeasurementTo;
import de.bluewhale.sabi.persistence.repositories.UserRepository;
import de.bluewhale.sabi.services.TankService;
import de.bluewhale.sabi.util.RestHelper;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.client.ResourceAccessException;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -31,6 +40,10 @@
* @author Stefan Schubert
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class AquariumIoTControllerTest {
@@ -38,6 +51,22 @@ public class AquariumIoTControllerTest {
private static final String SECRET_ASSUMED_AS_VALID = "allowed api key";
private static final String SECRET_ASSUMED_AS_INVALID = "invalid api key";
+
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+
@Autowired
ObjectMapper objectMapper; // json mapper
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/MeasurementControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/MeasurementControllerTest.java
similarity index 90%
rename from sabi-server/src/test/java/de/bluewhale/sabi/services/rest/MeasurementControllerTest.java
rename to sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/MeasurementControllerTest.java
index 77f753c9..57348fb8 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/MeasurementControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/MeasurementControllerTest.java
@@ -1,12 +1,12 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
-package de.bluewhale.sabi.services.rest;
+package de.bluewhale.sabi.rest.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.bluewhale.sabi.TestDataFactory;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.mapper.AquariumMapper;
import de.bluewhale.sabi.mapper.MeasurementMapper;
import de.bluewhale.sabi.mapper.UserMapper;
@@ -21,19 +21,28 @@
import de.bluewhale.sabi.persistence.repositories.UserRepository;
import de.bluewhale.sabi.security.TokenAuthenticationService;
import de.bluewhale.sabi.util.RestHelper;
+import de.bluewhale.sabi.util.TestDataFactory;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -44,24 +53,35 @@
/**
* Demonstrate usage of the measurement REST API.
* NOTICE: This test mocks the DAO persistent layer, as it was not meant to run as an integration test.
- *
- * However notice the following drawbacks:
- *
- * (1) It still requires the database, as without it we get a java.lang.IllegalStateException:
- * Failed to load ApplicationContext, though this might be fixed by proper test configuration
- * (2) Lines of code! The mocked variant outweighs the implementation by far. Which slows down development progress.
- * I leave it to demonstrate the effect. For those cases it would be much better to leave this as real integration
- * tests (however against an H2 in memory database, or by manually control your test data).
- *
+ *
* @author Stefan Schubert
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MeasurementControllerTest {
// ------------------------------ FIELDS ------------------------------
final static String MOCKED_USER = "testsabi@bluewhale.de";
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+
@MockBean
UserRepository userRepository;
@@ -103,7 +123,7 @@ public void testListUsersMeasurements() throws Exception {
AquariumTo aquariumTo = testDataFactory.getTestAquariumFor(userTo);
AquariumEntity aquariumEntity = aquariumMapper.mapAquariumTo2Entity(aquariumTo);
- MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo.getId());
+ MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo);
MeasurementEntity measurementEntity = measurementMapper.mapMeasurementTo2EntityWithoutAquarium(measurementTo);
measurementEntity.setAquarium(aquariumEntity);
@@ -151,8 +171,8 @@ public void testListUsersTankMeasurements() throws Exception {
given(this.aquariumRepository.getAquariumEntityByIdAndUser_IdIs(aquariumTo.getId(), userTo.getId())).willReturn(aquariumEntity);
given(this.aquariumRepository.getOne(aquariumTo.getId())).willReturn(aquariumEntity);
- MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo.getId());
- MeasurementTo measurementTo2 = testDataFactory.getTestMeasurementTo(aquariumTo.getId());
+ MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo);
+ MeasurementTo measurementTo2 = testDataFactory.getTestMeasurementTo(aquariumTo);
MeasurementEntity measurementEntity = measurementMapper.mapMeasurementTo2EntityWithoutAquarium(measurementTo);
MeasurementEntity measurementEntity2 = measurementMapper.mapMeasurementTo2EntityWithoutAquarium(measurementTo2);
measurementEntity.setAquarium(aquariumEntity);
@@ -203,7 +223,7 @@ public void testListUsersTankMeasurementsForSpecificMeasurement() throws Excepti
given(this.aquariumRepository.getAquariumEntityByIdAndUser_IdIs(aquariumTo.getId(), userTo.getId())).willReturn(aquariumEntity);
given(this.aquariumRepository.getOne(aquariumTo.getId())).willReturn(aquariumEntity);
- MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo.getId());
+ MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo);
MeasurementEntity measurementEntity = measurementMapper.mapMeasurementTo2EntityWithoutAquarium(measurementTo);
measurementEntity.setAquarium(aquariumEntity);
@@ -279,7 +299,7 @@ public void testAddMeasurement() throws Exception {
given(this.aquariumRepository.getAquariumEntityByIdAndUser_IdIs(aquariumTo.getId(), userTo.getId())).willReturn(aquariumEntity);
given(this.aquariumRepository.findById(aquariumTo.getId())).willReturn(Optional.of(aquariumEntity));
- MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo.getId());
+ MeasurementTo measurementTo = testDataFactory.getTestMeasurementTo(aquariumTo);
MeasurementEntity measurementEntity = new MeasurementEntity();
measurementEntity.setId(88L);
measurementEntity.setAquarium(aquariumEntity);
@@ -321,7 +341,7 @@ public void testUpdateMeasurement() throws Exception {
AquariumTo aquariumTo = testDataFactory.getTestAquariumFor(userTo);
AquariumEntity aquariumEntity = aquariumMapper.mapAquariumTo2Entity(aquariumTo);
- MeasurementTo updatableMeasurementTo = testDataFactory.getTestMeasurementTo(aquariumTo.getId());
+ MeasurementTo updatableMeasurementTo = testDataFactory.getTestMeasurementTo(aquariumTo);
updatableMeasurementTo.setId(88L);
MeasurementEntity updatableMeasurementEntity = measurementMapper.mapMeasurementTo2EntityWithoutAquarium(updatableMeasurementTo);
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/MotdControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/MotdControllerTest.java
new file mode 100644
index 00000000..2e217fd5
--- /dev/null
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/MotdControllerTest.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
+ * See project LICENSE file for the detailed terms and conditions.
+ */
+
+package de.bluewhale.sabi.rest.controller;
+
+import de.bluewhale.sabi.configs.AppConfig;
+import de.bluewhale.sabi.services.AppService;
+import org.junit.Assert;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.boot.test.web.server.LocalServerPort;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.web.client.RestClient;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.reset;
+
+
+/**
+ * Checks Motd Service
+ */
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
+@Tag("ModuleTest")
+public class MotdControllerTest {
+
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+ @MockBean
+ AppService appService;
+
+ @LocalServerPort
+ private int port;
+
+ private RestClient restClient;
+
+ @BeforeEach
+ public void initRestClient() {
+ if (restClient == null) {
+ String url = String.format("http://localhost:%d/sabi", port);
+ restClient = RestClient
+ .builder()
+ .baseUrl(url) // Dynamischer Port
+ .build();
+ }
+ }
+
+ @AfterEach
+ public void cleanUpMocks() {
+ reset(appService);
+ }
+
+ /**
+ * Tests MOTD Rest API in case we have no content.
+ *
+ * @throws Exception
+ */
+ @Test // REST-API
+ public void testModtRetrievalWithNoNews() throws Exception {
+ given(this.appService.fetchMotdFor("xx")).willReturn(null);
+
+ restClient.get().uri("/api/app/motd/xx")
+ .retrieve() // führt den Request aus und ruft die Antwort ab
+ .onStatus(
+ status -> !status.isSameCodeAs(HttpStatus.NO_CONTENT),
+ (request, response) -> {
+ throw new RuntimeException("Retrieved wrong status code: " + response.getStatusCode());
+ }
+ )
+ .toEntity(String.class);
+
+ }
+
+
+ /**
+ * Tests MOTD Rest API in case we have news.
+ *
+ * @throws Exception
+ */
+ @Test // REST-API
+ public void testModtRetrieval() throws Exception {
+
+ String motd = "Junit Modt";
+ given(this.appService.fetchMotdFor("en")).willReturn(motd);
+
+ ResponseEntity stringResponseEntity = restClient.get().uri("/api/app/motd/en")
+ .retrieve()
+ .onStatus(status -> status.value() != 200, (request, response) -> {
+ throw new RuntimeException("Retrieved wrong status code: " + response.getStatusCode());
+ }).toEntity(String.class);
+
+ Assert.assertTrue(stringResponseEntity.toString().contains(motd));
+ }
+
+}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/PlagueCenterControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/PlagueCenterControllerTest.java
similarity index 70%
rename from sabi-server/src/test/java/de/bluewhale/sabi/services/rest/PlagueCenterControllerTest.java
rename to sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/PlagueCenterControllerTest.java
index 805edd37..30d59d02 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/PlagueCenterControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/PlagueCenterControllerTest.java
@@ -1,12 +1,12 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
-package de.bluewhale.sabi.services.rest;
+package de.bluewhale.sabi.rest.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.bluewhale.sabi.TestDataFactory;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.mapper.UserMapper;
import de.bluewhale.sabi.model.PlagueStatusTo;
import de.bluewhale.sabi.model.UserTo;
@@ -15,17 +15,26 @@
import de.bluewhale.sabi.security.TokenAuthenticationService;
import de.bluewhale.sabi.services.PlagueCenterService;
import de.bluewhale.sabi.util.RestHelper;
+import de.bluewhale.sabi.util.TestDataFactory;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.ArrayList;
import java.util.List;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.BDDMockito.given;
@@ -37,10 +46,29 @@
* @author Stefan Schubert
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class PlagueCenterControllerTest {
// ------------------------------ FIELDS ------------------------------
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+
final static String MOCKED_USER = "testsabi@bluewhale.de";
@MockBean
@@ -54,7 +82,9 @@ public class PlagueCenterControllerTest {
@Autowired
ObjectMapper objectMapper; // json mapper
+
TestDataFactory testDataFactory = TestDataFactory.getInstance();
+
@Autowired
private TestRestTemplate restTemplate;
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/StatsControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/StatsControllerTest.java
similarity index 68%
rename from sabi-server/src/test/java/de/bluewhale/sabi/services/rest/StatsControllerTest.java
rename to sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/StatsControllerTest.java
index 83160264..41abe565 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/StatsControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/StatsControllerTest.java
@@ -1,12 +1,12 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
-package de.bluewhale.sabi.services.rest;
+package de.bluewhale.sabi.rest.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.bluewhale.sabi.TestDataFactory;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.mapper.UserMapper;
import de.bluewhale.sabi.model.UserTo;
import de.bluewhale.sabi.persistence.model.UserEntity;
@@ -14,14 +14,23 @@
import de.bluewhale.sabi.persistence.repositories.UserRepository;
import de.bluewhale.sabi.security.TokenAuthenticationService;
import de.bluewhale.sabi.util.RestHelper;
+import de.bluewhale.sabi.util.TestDataFactory;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -30,24 +39,35 @@
/**
* Demonstrate usage of the unit REST API.
* NOTICE: This test mocks the DAO persistent layer, as it was not meant to run as an integration test.
- *
- * However notice the following drawbacks:
- *
- * (1) It still requires the database, as without it we get a java.lang.IllegalStateException:
- * Failed to load ApplicationContext, though this might be fixed by proper test configuration
- * (2) Lines of code! The mocked variant outweighs the implementation by far. Which slows down development progress.
- * I leave it to demonstrate the effect. For those cases it would be much better to leave this as real integration
- * tests (however against an H2 in memory database, or by manually control your test data).
*
* @author Stefan Schubert
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class StatsControllerTest {
// ------------------------------ FIELDS ------------------------------
final static String MOCKED_USER = "testsabi@bluewhale.de";
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+
@MockBean
MeasurementRepository measurementRepository;
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/TankControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/TankControllerTest.java
similarity index 89%
rename from sabi-server/src/test/java/de/bluewhale/sabi/services/rest/TankControllerTest.java
rename to sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/TankControllerTest.java
index fc9ae2e6..83747856 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/TankControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/TankControllerTest.java
@@ -1,12 +1,12 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
-package de.bluewhale.sabi.services.rest;
+package de.bluewhale.sabi.rest.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.bluewhale.sabi.TestDataFactory;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.mapper.AquariumMapper;
import de.bluewhale.sabi.mapper.UserMapper;
import de.bluewhale.sabi.model.AquariumTo;
@@ -17,19 +17,28 @@
import de.bluewhale.sabi.persistence.repositories.UserRepository;
import de.bluewhale.sabi.security.TokenAuthenticationService;
import de.bluewhale.sabi.util.RestHelper;
+import de.bluewhale.sabi.util.TestDataFactory;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.ArrayList;
import java.util.List;
import static de.bluewhale.sabi.api.HttpHeader.AUTH_TOKEN;
import static de.bluewhale.sabi.api.HttpHeader.TOKEN_PREFIX;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -43,12 +52,30 @@
* @author Stefan Schubert
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class TankControllerTest {
// ------------------------------ FIELDS ------------------------------
final static String MOCKED_USER = "testsabi@bluewhale.de";
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
@MockBean
UserRepository userRepository;
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UnitControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UnitControllerTest.java
similarity index 81%
rename from sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UnitControllerTest.java
rename to sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UnitControllerTest.java
index 9f2fd1d9..70f561bf 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UnitControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UnitControllerTest.java
@@ -3,11 +3,11 @@
* See project LICENSE file for the detailed terms and conditions.
*/
-package de.bluewhale.sabi.services.rest;
+package de.bluewhale.sabi.rest.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.bluewhale.sabi.TestDataFactory;
import de.bluewhale.sabi.api.Endpoint;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.mapper.ParameterMapper;
import de.bluewhale.sabi.mapper.UnitMapper;
import de.bluewhale.sabi.mapper.UserMapper;
@@ -24,18 +24,27 @@
import de.bluewhale.sabi.persistence.repositories.UserRepository;
import de.bluewhale.sabi.security.TokenAuthenticationService;
import de.bluewhale.sabi.util.RestHelper;
+import de.bluewhale.sabi.util.TestDataFactory;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -44,23 +53,34 @@
/**
* Demonstrate usage of the unit REST API.
* NOTICE: This test mocks the DAO persistent layer, as it was not meant to run as an integration test.
- *
- * However notice the following drawbacks:
- *
- * (1) It still requires the database, as without it we get a java.lang.IllegalStateException:
- * Failed to load ApplicationContext, though this might be fixed by proper test configuration
- * (2) Lines of code! The mocked variant outweighs the implementation by far. Which slows down development progress.
- * I leave it to demonstrate the effect. For those cases it would be much better to leave this as real integration
- * tests (however against an H2 in memory database, or by manually control your test data).
*
* @author Stefan Schubert
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class UnitControllerTest {
// ------------------------------ FIELDS ------------------------------
final static String MOCKED_USER = "testsabi@bluewhale.de";
+
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
@MockBean
UnitRepository unitRepository;
@MockBean
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UserAuthControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UserAuthControllerTest.java
similarity index 86%
rename from sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UserAuthControllerTest.java
rename to sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UserAuthControllerTest.java
index cb95ce8e..79407ccb 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UserAuthControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UserAuthControllerTest.java
@@ -1,15 +1,15 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
-package de.bluewhale.sabi.services.rest;
+package de.bluewhale.sabi.rest.controller;
import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.bluewhale.sabi.TestDataFactory;
import de.bluewhale.sabi.api.Endpoint;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.mapper.UserMapper;
import de.bluewhale.sabi.model.AccountCredentialsTo;
import de.bluewhale.sabi.model.UserTo;
@@ -17,19 +17,28 @@
import de.bluewhale.sabi.persistence.repositories.UserRepository;
import de.bluewhale.sabi.services.CaptchaAdapter;
import de.bluewhale.sabi.util.RestHelper;
+import de.bluewhale.sabi.util.TestDataFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.*;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import javax.naming.NamingException;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -43,9 +52,27 @@
* You may consult the test cases, while developing a specific client.
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class UserAuthControllerTest {
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
SimpleSmtpServer smtpServer;
@@ -156,7 +183,7 @@ public void testSuccessfulNewUserRegistration() throws Exception {
public void testWeakPasswordUserRegistration() throws Exception {
// given a test user
- UserTo userTo = new UserTo("test@bluewhale.de", "Tester",TestDataFactory.INVALID_PASSWORD);
+ UserTo userTo = new UserTo("test@bluewhale.de", "Tester", TestDataFactory.INVALID_PASSWORD);
userTo.setCaptchaCode("test");
UserEntity userEntity = userMapper.mapUserTo2Entity(userTo);
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UserProfileControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UserProfileControllerTest.java
similarity index 83%
rename from sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UserProfileControllerTest.java
rename to sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UserProfileControllerTest.java
index c8b29687..d004c767 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/UserProfileControllerTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/rest/controller/UserProfileControllerTest.java
@@ -1,17 +1,17 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
-package de.bluewhale.sabi.services.rest;
+package de.bluewhale.sabi.rest.controller;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.bluewhale.sabi.TestDataFactory;
import de.bluewhale.sabi.api.Endpoint;
+import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.exception.CommonMessageCodes;
import de.bluewhale.sabi.exception.Message;
import de.bluewhale.sabi.model.ResultTo;
@@ -20,17 +20,26 @@
import de.bluewhale.sabi.security.TokenAuthenticationService;
import de.bluewhale.sabi.services.UserService;
import de.bluewhale.sabi.util.RestHelper;
+import de.bluewhale.sabi.util.TestDataFactory;
import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.Date;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -41,9 +50,27 @@
* Checks UserProfile Rest API
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Testcontainers
+@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ModuleTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class UserProfileControllerTest {
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
@MockBean
UserService userService;
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/security/PasswordPolicyTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/security/PasswordPolicyTest.java
index 2075af6b..136d2a0d 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/security/PasswordPolicyTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/security/PasswordPolicyTest.java
@@ -1,11 +1,12 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
package de.bluewhale.sabi.security;
import org.eclipse.persistence.jpa.jpql.Assert;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@@ -17,6 +18,7 @@
* @author Stefan Schubert
*/
@ExtendWith(SpringExtension.class)
+@Tag("DeveloperTest")
public class PasswordPolicyTest {
@Test
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/CoralServiceTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/CoralServiceTest.java
index 3e45cee7..98eac7b4 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/CoralServiceTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/services/CoralServiceTest.java
@@ -1,25 +1,26 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
package de.bluewhale.sabi.services;
-import de.bluewhale.sabi.TestDataFactory;
import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.model.AquariumTo;
-import de.bluewhale.sabi.model.ResultTo;
-import org.junit.jupiter.api.Disabled;
-import org.junit.jupiter.api.MethodOrderer;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.TestMethodOrder;
+import de.bluewhale.sabi.util.TestDataFactory;
+import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
-import org.springframework.transaction.annotation.Transactional;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
-import static de.bluewhale.sabi.TestDataFactory.TESTUSER_EMAIL1;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.springframework.test.util.AssertionErrors.fail;
@@ -29,17 +30,34 @@
* Date: 16.06.2017
*/
@SpringBootTest
+@Testcontainers
@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
+@Tag("ServiceTest")
public class CoralServiceTest {
// ------------------------------ FIELDS ------------------------------
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
@Autowired
private TankService tankService;
- @Autowired
- private UserService userService;
+ static TestDataFactory testDataFactory = TestDataFactory.getInstance();
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
// -------------------------- OTHER METHODS --------------------------
@@ -49,18 +67,15 @@ public class CoralServiceTest {
* @throws Exception
*/
@Test
- @Transactional
+ @Rollback
@Disabled
public void testAddCoral() throws Exception {
// Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
- final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, TESTUSER_EMAIL1);
// When
fail("Implement me");
}
-
}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/FishServiceTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/FishServiceTest.java
index 5d733a92..fec5557b 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/FishServiceTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/services/FishServiceTest.java
@@ -1,29 +1,46 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
package de.bluewhale.sabi.services;
-import de.bluewhale.sabi.TestDataFactory;
import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.exception.Message.CATEGORY;
import de.bluewhale.sabi.model.AquariumTo;
import de.bluewhale.sabi.model.FishTo;
import de.bluewhale.sabi.model.ResultTo;
import de.bluewhale.sabi.model.UserTo;
+import de.bluewhale.sabi.persistence.model.AquariumEntity;
+import de.bluewhale.sabi.persistence.model.FishEntity;
+import de.bluewhale.sabi.persistence.model.UserEntity;
+import de.bluewhale.sabi.persistence.repositories.AquariumRepository;
+import de.bluewhale.sabi.persistence.repositories.FishRepository;
+import de.bluewhale.sabi.persistence.repositories.UserRepository;
+import de.bluewhale.sabi.util.TestDataFactory;
import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.time.LocalDate;
-
-import static de.bluewhale.sabi.TestDataFactory.TESTUSER_EMAIL1;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
+import static de.bluewhale.sabi.util.TestDataFactory.TESTUSER_EMAIL1;
+import static de.bluewhale.sabi.util.TestDataFactory.TEST_FISH_ID;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.when;
import static org.springframework.test.util.AssertionErrors.*;
@@ -33,18 +50,45 @@
* Date: 30.08.15
*/
@SpringBootTest
+@Testcontainers
@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+@Tag("ServiceTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class FishServiceTest {
// ------------------------------ FIELDS ------------------------------
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
@Autowired
private TankService tankService;
@Autowired
private FishService fishService;
- @Autowired
- private UserService userService;
+
+ static TestDataFactory testDataFactory = TestDataFactory.getInstance();
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+ @MockBean
+ private UserRepository userRepository;
+
+ @MockBean
+ private FishRepository fishRepository;
+
+ @MockBean
+ private AquariumRepository aquariumRepository;
+
// -------------------------- OTHER METHODS --------------------------
@@ -54,25 +98,26 @@ public class FishServiceTest {
* @throws Exception
*/
@Test
- @Transactional
+ @Rollback
public void testAddFish() throws Exception {
// Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
- final UserTo registeredUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
+ final UserTo testUser = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity testUserEntity = testDataFactory.getNewTestUserEntity(testUser);
final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
+ AquariumEntity testAquariumEntity = testDataFactory.getTestAquariumEntity(aquariumTo, testUserEntity);
+ FishTo testFishTo = testDataFactory.getTestFishTo(aquariumTo);
- final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, registeredUser.getEmail());
-
+ given(userRepository.getOne(testUser.getId())).willReturn(testUserEntity);
+ given(aquariumRepository.getOne(testFishTo.getAquariumId())).willReturn(testAquariumEntity);
+ when(fishRepository.save(any(FishEntity.class))).thenAnswer(invocation -> {
+ FishEntity fishEntity = invocation.getArgument(0);
+ fishEntity.setId(TEST_FISH_ID);
+ return fishEntity;
+ });
// When
- final FishTo fish = new FishTo();
- fish.setAddedOn(LocalDate.now());
- fish.setFishCatalogueId(1L); // existing default Data
- fish.setAquariumId(aquariumToResultTo.getValue().getId());
- fish.setNickname("Green Latern");
-
// The user is required to check that he or she really possesses the tank
- final ResultTo fishResultTo = fishService.registerNewFish(fish,registeredUser);
+ final ResultTo fishResultTo = fishService.registerNewFish(testFishTo,testUser);
// Then
assertNotNull("ResultObject must not be empty",fishResultTo);
@@ -85,25 +130,26 @@ public void testAddFish() throws Exception {
// A User cannot register a fish for a tank that he or she does not own.
@Test
- @Transactional
- public void testAddFishForWrongTank() throws Exception {
+ @Rollback
+ public void testAddFishForOtherUsersTank() throws Exception {
// Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
- final UserTo registeredUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
- final UserTo fraudUser = testDataFactory.getRegisterNewTestUser("I_Intent@No.good");
- final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
- final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, registeredUser.getEmail());
+ final UserTo testUser = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity testUserEntity = testDataFactory.getNewTestUserEntity(testUser);
+ AquariumTo testAquariumFor = testDataFactory.getTestAquariumFor(testUser);
+ AquariumEntity testAquariumEntity = testDataFactory.getTestAquariumEntity(testAquariumFor, testUserEntity);
- // When
- final FishTo fish = new FishTo();
- fish.setAddedOn(LocalDate.now());
- fish.setFishCatalogueId(1L); // existing default Data
- fish.setAquariumId(aquariumToResultTo.getValue().getId());
- fish.setNickname("Green Latern");
+ FishTo testFishTo = testDataFactory.getTestFishTo(testAquariumFor);
+ final UserTo fraudUser = testDataFactory.getNewTestUserTo("I_Intent@No.good");
+ fraudUser.setId(88L);
+ UserEntity testFraudUserEntity = testDataFactory.getNewTestUserEntity(fraudUser);
+
+ given(userRepository.getOne(fraudUser.getId())).willReturn(testFraudUserEntity);
+ given(aquariumRepository.getOne(testFishTo.getAquariumId())).willReturn(testAquariumEntity); // an Aquarium that is not owned by the fraud user
+ // When
// The the fraud user tries to place something in a different tank
- final ResultTo fishResultTo = fishService.registerNewFish(fish,fraudUser);
+ final ResultTo fishResultTo = fishService.registerNewFish(testFishTo, fraudUser);
// then
assertNull("ResultObject Value should be empty as creation was not permitted.",fishResultTo.getValue());
@@ -112,48 +158,31 @@ public void testAddFishForWrongTank() throws Exception {
}
-/*
- @BeforeClass
- public static void init() throws NamingException {
- }
-*/
-
-/* @AfterClass
- public static void tearDownClass() throws Exception {
- }
-*/
-
-
/**
* Remode a fish and write it automatically to tanks history via log entry
*
* @throws Exception
*/
@Test
- @Transactional
+ @Rollback
public void testRemoveFish() throws Exception {
// Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
- final UserTo registeredUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
+ final UserTo testUser = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity testUserEntity = testDataFactory.getNewTestUserEntity(testUser);
final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
+ AquariumEntity testAquariumEntity = testDataFactory.getTestAquariumEntity(aquariumTo, testUserEntity);
+ FishTo testFishTo = testDataFactory.getTestFishTo(aquariumTo);
+ FishEntity testFishEntity = testDataFactory.getTestFishEntity(testFishTo, testAquariumEntity.getId());
- final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, registeredUser.getEmail());
-
- final FishTo fish = new FishTo();
- fish.setAddedOn(LocalDate.now());
- fish.setFishCatalogueId(1L); // existing default Data
- fish.setAquariumId(aquariumToResultTo.getValue().getId());
- fish.setNickname("Green Latern");
-
- // The user is required to check that he your she really posses the tank
- final ResultTo fishResultTo = fishService.registerNewFish(fish,registeredUser);
+ Long fishId = testFishTo.getId();
+ given(fishRepository.findUsersFish(fishId, testUser.getId())).willReturn(testFishEntity).willReturn(null);
+ doNothing().when(fishRepository).delete(testFishEntity);
// When
- Long fishId = fishResultTo.getValue().getId();
- fishService.removeFish(fishId, registeredUser);
+ fishService.removeFish(fishId, testUser);
// Then
- FishTo removedFish = fishService.getUsersFish(fishId, registeredUser);
+ FishTo removedFish = fishService.getUsersFish(fishId, testUser);
assertNull("Fish was not removed!", removedFish);
}
}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/MeasurementServiceTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/MeasurementServiceTest.java
index 1748ae79..b636ffe7 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/MeasurementServiceTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/services/MeasurementServiceTest.java
@@ -5,26 +5,37 @@
package de.bluewhale.sabi.services;
-import de.bluewhale.sabi.BasicDataFactory;
-import de.bluewhale.sabi.TestDataFactory;
import de.bluewhale.sabi.configs.AppConfig;
import de.bluewhale.sabi.exception.Message;
-import de.bluewhale.sabi.exception.Message.CATEGORY;
import de.bluewhale.sabi.model.*;
-import jakarta.transaction.Transactional;
-import org.junit.jupiter.api.BeforeEach;
+import de.bluewhale.sabi.persistence.model.*;
+import de.bluewhale.sabi.persistence.repositories.*;
+import de.bluewhale.sabi.util.TestDataFactory;
import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
-import java.time.LocalDateTime;
import java.util.List;
-
-import static org.junit.jupiter.api.Assertions.assertNull;
+import java.util.Optional;
+
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
+import static de.bluewhale.sabi.util.TestDataFactory.TESTUSER_EMAIL1;
+import static de.bluewhale.sabi.util.TestDataFactory.TEST_TANK_ID;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.doNothing;
import static org.springframework.test.util.AssertionErrors.*;
@@ -34,241 +45,278 @@
* Date: 30.08.15
*/
@SpringBootTest
+@Testcontainers
@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
-@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
-public class MeasurementServiceTest extends BasicDataFactory {
-
+@Tag("ServiceTest")
+@DirtiesContext
+public class MeasurementServiceTest {
+
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
// ------------------------------ FIELDS ------------------------------
- @Autowired
- private TankService tankService;
+ static TestDataFactory testDataFactory = TestDataFactory.getInstance();
- @Autowired
- private UserService userService;
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
@Autowired
private MeasurementService measurementService;
-// -------------------------- OTHER METHODS --------------------------
+ @MockBean
+ private AquariumRepository aquariumRepository;
- /**
- * There seems to be a timing problem with H2, that causes, that the basic data is not available
- * for some test classes, while for others it worked out. Or the Annotation is not being processed through
- * inheritance. Until we know what's going wrong...
- * we "double inject" by extending the BasicTestDataFactory and by calling it directly.
- * The different behaviour can be observed by e.g. calling the master test suite and as comparising
- * the measurement testsuite while this is method is deaktivated.
- */
- @BeforeEach
- public void ensureBasicDataAvailability() {
- List list = measurementService.listMeasurements(P_USER1_EMAIL, 0);
- if (list.isEmpty()) {
- populateBasicData();
- list = measurementService.listMeasurements(P_USER1_EMAIL, 0);
- }
- assert(list.size() > 0);
-
- // PROBLEM here: We thought that stores User gets the ID wie set hard, but H2 Dirties context
- // drops data but not sequence values, which is why repeated user creation leads to a different user ID.
- // So any test which is relies on a certain user ID might run into trouble.
- // UserEntity storedTestUser = userRepository.getByEmail(P_USER1_EMAIL);
- // assertNotNull("Should not happen!","Precondition stored Test User failed!",storedTestUser);
- // assertEquals("Stored Test User got wrong ID!? ", 1l, storedTestUser.getId().longValue());
- }
+ @MockBean
+ private UserRepository userRepository;
+
+ @MockBean
+ private UnitRepository unitRepository;
+
+ @MockBean
+ private MeasurementRepository measurementRepository;
+
+ @MockBean
+ private LocalizedUnitRepository localizedUnitRepository;
+
+ @MockBean
+ ParameterRepository parameterRepository;
+
+// -------------------------- OTHER METHODS --------------------------
@Test
- @Transactional
public void testListMeasurements() throws Exception {
- // Given already stored testdata for measurements
- AquariumTo aquariumTo = tankService.listTanks(P_USER1_EMAIL).get(0);
- assertNotNull("Prepersisted Testdata is missing", aquariumTo);
- Long tankID = aquariumTo.getId();
+
+ // Given:
+ UserTo testUserTo = TestDataFactory.getInstance().getNewTestUserTo(TestDataFactory.TESTUSER_EMAIL1);
+ UserEntity userEntity = TestDataFactory.getInstance().getNewTestUserEntity(testUserTo);
+
+ AquariumTo testAquariumTo = testDataFactory.getTestAquariumTo();
+ AquariumEntity aquariumEntity = testDataFactory.getTestAquariumEntity(testAquariumTo, userEntity);
+
+ MeasurementTo testMeasurementTo = testDataFactory.getTestMeasurementTo(testAquariumTo);
+ MeasurementEntity measurementEntity = testDataFactory.getTestMeasurementEntity(testMeasurementTo, aquariumEntity);
+
+
+ given(aquariumRepository.getOne(TEST_TANK_ID)).willReturn(aquariumEntity);
+ given(measurementRepository.findByAquarium(aquariumEntity)).willReturn(List.of(measurementEntity));
+ given(userRepository.getByEmail(TestDataFactory.TESTUSER_EMAIL1)).willReturn(userEntity);
+ given(measurementRepository.findByUserOrderByMeasuredOnDesc(userEntity)).willReturn(List.of(measurementEntity));
// When
- List tank1Measurements = measurementService.listMeasurements(tankID);
- List usersMeasurements = measurementService.listMeasurements(P_USER1_EMAIL, 0);
+ List tank1Measurements = measurementService.listMeasurements(TEST_TANK_ID);
+ List usersMeasurements = measurementService.listMeasurements(TestDataFactory.TESTUSER_EMAIL1, 0);
// Then
- assertNotNull("Should not happen!",tank1Measurements);
- assertNotNull("Should not happen!",usersMeasurements);
- assertTrue("Testdata gone?", tank1Measurements.size() >= 1);
- assertTrue("Stored Testdata changed?", usersMeasurements.containsAll(tank1Measurements));
+ assertNotNull("Should not happen!", tank1Measurements);
+ assertNotNull("Should not happen!", usersMeasurements);
+ assertTrue("Mocks didn't worked?", tank1Measurements.size() >= 1);
+ assertTrue("Relationships brocken?", usersMeasurements.containsAll(tank1Measurements));
}
+
@Test
- @Transactional
- public void testFindMeasurementParameter() throws Exception {
- // Given already stored testdata for measurements
+ public void testFindLocalizedMeasurementParameter() throws Exception {
+ // Given parameter Entity with id 1
+ LocalizedParameterEntity localizedParameterEntity = new LocalizedParameterEntity();
+ localizedParameterEntity.setId(1L);
+ localizedParameterEntity.setLanguage("de");
+ localizedParameterEntity.setDescription("Test Parameter");
+
+ ParameterEntity parameterEntity = new ParameterEntity();
+ parameterEntity.setId(1);
+ parameterEntity.setLocalizedParameterEntities(List.of(localizedParameterEntity));
+
+ given(parameterRepository.findByBelongingUnitIdEquals(1)).willReturn(parameterEntity);
// When
ParameterTo parameterTo = measurementService.fetchParameterInfoFor(1, "de");
// Then
- assertNotNull("Should not happen!",parameterTo);
+ assertNotNull("Should not happen!", parameterTo);
}
@Test
- @Transactional
public void testFindInvalidMeasurementParameter() throws Exception {
- // Given already stored testdata for measurements
+ // Given
Integer nonExistingUnit = Integer.MAX_VALUE;
+ given(parameterRepository.findByBelongingUnitIdEquals(nonExistingUnit)).willReturn(null);
// When
- ParameterTo parameterTo = measurementService.fetchParameterInfoFor(nonExistingUnit,"de");
+ ParameterTo parameterTo = measurementService.fetchParameterInfoFor(nonExistingUnit, "de");
// Then
- assertNull(parameterTo);
+ assertNull("According to API we should get null but got: ", parameterTo);
}
-
@Test
- @Transactional
public void testListMeasurementsForSpecificTankAndUnit() throws Exception {
- // Given already stored testdata for measurements
- // for tank 2 only one measurement with unit id 1
+ // Given
+ AquariumEntity aquariumEntity = new AquariumEntity();
+ aquariumEntity.setId(2L);
+
+ MeasurementEntity measurementEntity = new MeasurementEntity();
+ measurementEntity.setAquarium(aquariumEntity);
+ measurementEntity.setUnitId(1);
+
+ given(aquariumRepository.getOne(2L)).willReturn(aquariumEntity); // Tank
+ given(measurementRepository.findByAquariumAndUnitIdOrderByMeasuredOnAsc(aquariumEntity, 1)).willReturn(List.of(measurementEntity));
// When
List measurements = measurementService.listMeasurementsFilteredBy(2L, 1);
// Then
- assertNotNull("Should not happen!",measurements);
- assertTrue("Testdata gone or changed? Received more or less than expected on measurement.", measurements.size() == 1);
+ assertNotNull("Should not happen!", measurements);
+ assertTrue("Mocked Testdata gone or changed? Received more or less than expected on measurement.", measurements.size() == 1);
}
@Test
- @Transactional
public void testListMeasurementUnits() throws Exception {
// Given already stored testdata for measurements
+ UnitEntity unitEntity = new UnitEntity();
+ unitEntity.setName("Happyness");
+ unitEntity.setId(1);
+ LocalizedUnitEntity localizedUnitEntity = new LocalizedUnitEntity();
+ localizedUnitEntity.setLanguage("de");
+ localizedUnitEntity.setDescription("Glück");
+
+ given(unitRepository.findAll()).willReturn(List.of(unitEntity));
+ given(localizedUnitRepository.findByLanguageAndUnitId("de",unitEntity.getId())).willReturn(localizedUnitEntity);
// When
List measurementUnits = measurementService.listAllMeasurementUnits("de");
// Then
- assertNotNull("Should not happen!",measurementUnits);
+ assertNotNull("Should not happen!", measurementUnits);
assertTrue("Testdata gone?", measurementUnits.size() >= 1);
+ assertTrue("Translation failed?", measurementUnits.get(0).getDescription().equals("Glück"));
}
@Test
- @Transactional
+ @Rollback
public void testAddNewMeasurement() throws Exception {
- // Given already store test data
- AquariumTo aquariumTo = tankService.listTanks(P_USER1_EMAIL).get(0);
- assertNotNull("Prepersisted Testdata is missing", aquariumTo);
- Long tankID = aquariumTo.getId();
+ // Given
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity userEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+ AquariumTo testAquariumTo = testDataFactory.getTestAquariumTo();
+ AquariumEntity aquariumEntity = testDataFactory.getTestAquariumEntity(testAquariumTo, userEntity);
+ MeasurementTo testMeasurementTo = testDataFactory.getTestMeasurementTo(testAquariumTo);
- // When adding a new Measurment
- TestDataFactory testDataFactory = TestDataFactory.getInstance();
- MeasurementTo testMeasurementTo = testDataFactory.getTestMeasurementTo(tankID);
- ResultTo measurementToResultTo = measurementService.addMeasurement(testMeasurementTo, P_USER1_EMAIL);
+ given(userRepository.getByEmail(TestDataFactory.TESTUSER_EMAIL1)).willReturn(userEntity);
+ given(aquariumRepository.getAquariumEntityByIdAndUser_IdIs(TEST_TANK_ID,userEntity.getId())).willReturn(aquariumEntity);
+ given(measurementRepository.saveAndFlush(any())).willReturn(new MeasurementEntity());
+
+ // When adding a new Measurement
+ ResultTo measurementToResultTo = measurementService.addMeasurement(testMeasurementTo, TestDataFactory.TESTUSER_EMAIL1);
// Then
assertNotNull("Should not happen!",measurementToResultTo);
- assertNotNull("Should not happen!",measurementToResultTo.getValue());
- assertEquals("Creating measurement failed? " + measurementToResultTo.getMessage().getCode(), CATEGORY.INFO, measurementToResultTo.getMessage().getType());
+ assertNotNull("Should not happen! Empty ResultTo",measurementToResultTo.getValue());
+ assertEquals("Creating measurement failed? " + measurementToResultTo.getMessage().getCode(), Message.CATEGORY.INFO, measurementToResultTo.getMessage().getType());
}
@Test
- @Transactional
- public void testGetLastetMeasurementEntryDateTime() throws Exception {
-
- // Given already store test data
- TestDataFactory testDataFactory = TestDataFactory.getInstance();
- AquariumTo aquariumTo = tankService.listTanks(P_USER1_EMAIL).get(0);
- assertNotNull("Prepersisted Testdata missing", aquariumTo);
- Long tankID = aquariumTo.getId();
+ @Rollback
+ public void testRemoveMeasurement() throws Exception {
+ // Given
- // Stored Measurement A
- MeasurementTo testMeasurementATo = testDataFactory.getTestMeasurementTo(tankID);
- testMeasurementATo.setMeasuredOn(LocalDateTime.now().minusYears(2));
- ResultTo measurementAToResultTo = measurementService.addMeasurement(testMeasurementATo, P_USER1_EMAIL);
- assertEquals("Failure storing test data A?: " + measurementAToResultTo.getMessage().getCode(), CATEGORY.INFO, measurementAToResultTo.getMessage().getType());
+ UserEntity userEntity = new UserEntity();
+ userEntity.setEmail(TestDataFactory.TESTUSER_EMAIL1);
+ userEntity.setId(99L);
+ MeasurementEntity measurementEntity = new MeasurementEntity();
+ measurementEntity.setId(42L);
- // And Afterwards created Measurement B
- MeasurementTo testMeasurementBTo = testDataFactory.getTestMeasurementTo(tankID);
- testMeasurementBTo.setId(testMeasurementATo.getId() + 1l);
- testMeasurementBTo.setMeasuredValue(99f);
- ResultTo measurementBToResultTo = measurementService.addMeasurement(testMeasurementBTo, P_USER1_EMAIL);
- assertEquals("Failure storing test data B?: " + measurementBToResultTo.getMessage().getCode(), CATEGORY.INFO, measurementBToResultTo.getMessage().getType());
+ given(userRepository.getByEmail(TestDataFactory.TESTUSER_EMAIL1)).willReturn(userEntity);
+ given(measurementRepository.getByIdAndUser(42L, userEntity)).willReturn(measurementEntity);
+ doNothing().when(measurementRepository).delete(measurementEntity);
// When
- LocalDateTime lastRecordedTime = measurementService.getLastTimeOfMeasurementTakenFilteredBy(measurementAToResultTo.getValue().getAquariumId(), testMeasurementATo.getUnitId());
-
- // Then
- assertNotNull("Looked like stored measurments have not been flushed.", lastRecordedTime);
- assertEquals("Did not retrieved the latest measurement date", LocalDateTime.now().getYear(), lastRecordedTime.getYear());
- }
-
-
- @Test
- @Transactional
- public void testRemoveMeasurement() throws Exception {
- // Given a stored measurement for a tank and user
- TestDataFactory testDataFactory = TestDataFactory.getInstance();
- testDataFactory.withUserService(userService);
- testDataFactory.withTankService(tankService);
- String newTestUserMail = "junit@sabi.de";
- UserTo persistedTestUserTo = testDataFactory.getRegisterNewTestUser(newTestUserMail);
- AquariumTo testAquariumTo = testDataFactory.getTestAquariumFor(persistedTestUserTo);
- testAquariumTo.setId(57654L);
- ResultTo newTankResultTo = tankService.registerNewTank(testAquariumTo, newTestUserMail);
-
- MeasurementTo testMeasurementTo = testDataFactory.getTestMeasurementTo(newTankResultTo.getValue().getId());
- testMeasurementTo.setId(889911L); // will be ignored ID will be overwritten, i.e. provided by addMeasurement
- ResultTo measurementToResultTo1 = measurementService.addMeasurement(testMeasurementTo, newTestUserMail);
-
- // When
- ResultTo measurementToResultTo = measurementService.removeMeasurement(measurementToResultTo1.getValue().getId(), newTestUserMail);
+ ResultTo measurementToResultTo = measurementService.removeMeasurement(42L, userEntity.getEmail());
// Then
assertNotNull("Should not happen!",measurementToResultTo);
- assertNotNull("Should not happen!",measurementToResultTo.getValue());
- CATEGORY messageType = measurementToResultTo.getMessage().getType();
+ assertNotNull("Should not happen! ResultTo contains no Value!",measurementToResultTo.getValue());
+ Message.CATEGORY messageType = measurementToResultTo.getMessage().getType();
assertTrue("Removal of measurement failed?", messageType.equals(Message.CATEGORY.INFO));
}
@Test
- @Transactional
+ @Rollback
public void testUpdateMeasurement() throws Exception {
- // Given already stored measurements
- List measurementToList = measurementService.listMeasurements(P_USER1_EMAIL, 0);
- MeasurementTo prestoresMeasurementTo = measurementToList.get(0);
- float oldValue = prestoresMeasurementTo.getMeasuredValue();
+ // Given
+ float oldValue = 1.0f;
+ float newValue = 2.0f;
+
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity userEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+
+ AquariumTo testAquariumTo = testDataFactory.getTestAquariumFor(testUserTo);
+
+ MeasurementEntity measurementEntity = testDataFactory.getTestMeasurementEntityWithDefaults();
+ measurementEntity.setMeasuredValue(oldValue);
+ measurementEntity.setId(42L);
+
+ MeasurementEntity updatedMeasurementEntity = testDataFactory.getTestMeasurementEntityWithDefaults();
+ updatedMeasurementEntity.setMeasuredValue(newValue);
+ updatedMeasurementEntity.setId(42L);
+
+ given(userRepository.getByEmail(TestDataFactory.TESTUSER_EMAIL1)).willReturn(userEntity);
+ given(measurementRepository.getByIdAndUser(42L, userEntity)).willReturn(measurementEntity);
+ given(measurementRepository.save(any())).willReturn(updatedMeasurementEntity);
// When we update the measurement
- float newValue = oldValue + 1.5f;
- prestoresMeasurementTo.setMeasuredValue(newValue);
- ResultTo measurementToResultTo = measurementService.updateMeasurement(prestoresMeasurementTo, P_USER1_EMAIL);
+ MeasurementTo testMeasurementTo = testDataFactory.getTestMeasurementTo(testAquariumTo);
+ testMeasurementTo.setMeasuredValue(newValue);
+ testMeasurementTo.setId(measurementEntity.getId());
+
+ ResultTo measurementToResultTo = measurementService.updateMeasurement(testMeasurementTo, TestDataFactory.TESTUSER_EMAIL1);
// Then
assertNotNull("Should not happen!",measurementToResultTo);
assertEquals("Update failed?",newValue, measurementToResultTo.getValue().getMeasuredValue());
- CATEGORY messageType = measurementToResultTo.getMessage().getType();
+ Message.CATEGORY messageType = measurementToResultTo.getMessage().getType();
assertTrue("Update of measurement failed?", messageType.equals(Message.CATEGORY.INFO));
}
@Test
- @Transactional
+ @Rollback
public void testAddIotAuthorizedMeasurement() {
- // Given already store test data
- AquariumTo aquariumTo = tankService.listTanks(P_USER1_EMAIL).get(0);
- assertNotNull("Prepersisted Testdata is missing", aquariumTo);
- Long tankID = aquariumTo.getId();
+ // Given
+
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(testDataFactory.TESTUSER_EMAIL1);
+ UserEntity userEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+
+ AquariumTo testAquariumTo = testDataFactory.getTestAquariumFor(testUserTo);
+ AquariumEntity testAquariumEntity = testDataFactory.getTestAquariumEntity(testAquariumTo, userEntity);
+
+ MeasurementTo testMeasurementTo = testDataFactory.getTestMeasurementTo(testAquariumTo);
+ MeasurementEntity createdMeasurementEntity = testDataFactory.getTestMeasurementEntity(testMeasurementTo, testAquariumEntity);
+
+ given(aquariumRepository.findById(TEST_TANK_ID)).willReturn(Optional.of(testAquariumEntity));
+ given(measurementRepository.saveAndFlush(any())).willReturn(createdMeasurementEntity);
// When
- TestDataFactory testDataFactory = TestDataFactory.getInstance();
- MeasurementTo testMeasurementTo = testDataFactory.getTestMeasurementTo(tankID);
ResultTo measurementToResultTo = measurementService.addIotAuthorizedMeasurement(testMeasurementTo);
// Then
assertNotNull("Should not happen!",measurementToResultTo);
- assertNotNull("Should not happen!",measurementToResultTo.getValue());
- CATEGORY messageType = measurementToResultTo.getMessage().getType();
+ assertNotNull("Should not happen! ResultTO contains no value",measurementToResultTo.getValue());
+ Message.CATEGORY messageType = measurementToResultTo.getMessage().getType();
assertTrue("Creating measurement failed?", messageType.equals(Message.CATEGORY.INFO));
}
+
}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/NotificationServiceTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/NotificationServiceTest.java
index 16a5f5a2..171c22cc 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/NotificationServiceTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/services/NotificationServiceTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
@@ -8,12 +8,19 @@
import de.bluewhale.sabi.configs.AppConfig;
import jakarta.mail.MessagingException;
import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.springframework.test.util.AssertionErrors.fail;
@@ -23,13 +30,31 @@
* @author Stefan Schubert
*/
@SpringBootTest
+@Testcontainers
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ContextConfiguration(classes = AppConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
+@Tag("ServiceTest")
public class NotificationServiceTest {
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
@Autowired
NotificationService notificationService;
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+
@Test
@Disabled
// To avoid mail spam, this test with real mail server settings was just to see
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/PlagueCenterServiceTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/PlagueCenterServiceTest.java
index 7149af75..eb46e811 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/PlagueCenterServiceTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/services/PlagueCenterServiceTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
@@ -10,19 +10,27 @@
import de.bluewhale.sabi.persistence.model.LocalizedPlagueStatusEntity;
import de.bluewhale.sabi.persistence.model.PlagueStatusEntity;
import de.bluewhale.sabi.persistence.repositories.PlagueStatusRepository;
+import de.bluewhale.sabi.util.TestDataFactory;
import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.ArrayList;
import java.util.List;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.util.AssertionErrors.assertEquals;
@@ -33,12 +41,24 @@
* Business-Layer tests for PlagueCenterService.
*/
@SpringBootTest
+@Testcontainers
@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
+@Tag("ServiceTest")
public class PlagueCenterServiceTest {
// ------------------------------ FIELDS ------------------------------
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
@Autowired
private PlagueCenterService plagueCenterService;
@@ -48,6 +68,14 @@ public class PlagueCenterServiceTest {
@Autowired
private UserService userService;
+ static TestDataFactory testDataFactory = TestDataFactory.getInstance();
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
+
// -------------------------- OTHER METHODS --------------------------
@@ -55,26 +83,8 @@ public class PlagueCenterServiceTest {
@Transactional
public void testListTranslatedPlagueStatus() throws Exception {
// Given
- PlagueStatusEntity plagueStatusEntity = new PlagueStatusEntity();
- plagueStatusEntity.setId(1l);
-
- ArrayList localizedPlagueStatusEntities = new ArrayList<>();
-
- LocalizedPlagueStatusEntity localizedPlagueStatus1 = new LocalizedPlagueStatusEntity();
- localizedPlagueStatus1.setPlague_status_id(1);
- localizedPlagueStatus1.setId(99l);
- localizedPlagueStatus1.setDescription("Spreading");
- localizedPlagueStatus1.setLanguage("en");
-
- LocalizedPlagueStatusEntity localizedPlagueStatus2 = new LocalizedPlagueStatusEntity();
- localizedPlagueStatus2.setPlague_status_id(1);
- localizedPlagueStatus2.setId(88l);
- localizedPlagueStatus2.setDescription("Ausweitend");
- localizedPlagueStatus2.setLanguage("de");
-
- localizedPlagueStatusEntities.add(localizedPlagueStatus1);
- localizedPlagueStatusEntities.add(localizedPlagueStatus2);
-
+ PlagueStatusEntity plagueStatusEntity = testDataFactory.getTestPlagueStatusEntity();
+ List localizedPlagueStatusEntities = testDataFactory.getTestLocalizedPlagueStatusEntities();
plagueStatusEntity.setLocalizedPlagueStatusEntities(localizedPlagueStatusEntities);
ArrayList plagueStatusEntities = new ArrayList();
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/TankServiceTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/TankServiceTest.java
index 842552d1..8fe175ee 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/TankServiceTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/services/TankServiceTest.java
@@ -1,27 +1,45 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
package de.bluewhale.sabi.services;
-import de.bluewhale.sabi.TestDataFactory;
import de.bluewhale.sabi.configs.AppConfig;
-import de.bluewhale.sabi.exception.Message.CATEGORY;
-import de.bluewhale.sabi.model.*;
+import de.bluewhale.sabi.exception.Message;
+import de.bluewhale.sabi.model.AquariumTo;
+import de.bluewhale.sabi.model.ResultTo;
+import de.bluewhale.sabi.model.UserTo;
+import de.bluewhale.sabi.persistence.model.AquariumEntity;
+import de.bluewhale.sabi.persistence.model.UserEntity;
+import de.bluewhale.sabi.persistence.repositories.AquariumRepository;
+import de.bluewhale.sabi.persistence.repositories.UserRepository;
+import de.bluewhale.sabi.util.TestDataFactory;
import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
-import org.springframework.transaction.annotation.Transactional;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.List;
-import static de.bluewhale.sabi.TestDataFactory.TESTUSER_EMAIL1;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
+import static de.bluewhale.sabi.util.TestDataFactory.TESTUSER_EMAIL1;
+import static de.bluewhale.sabi.util.TestDataFactory.TEST_TANK_ID;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.doNothing;
import static org.springframework.test.util.AssertionErrors.*;
@@ -31,161 +49,188 @@
* Date: 30.08.15
*/
@SpringBootTest
+@Testcontainers
@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+@Tag("ServiceTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class TankServiceTest {
- private static final String JABA_DABA_DOOOOO = "JabaDabaDooooo";
- // ------------------------------ FIELDS ------------------------------
- @Autowired
- private TankService tankService;
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+ private static final String JABA_DABA_DOOOOO = "JabaDabaDooooo";
- @Autowired
- private UserService userService;
-// -------------------------- OTHER METHODS --------------------------
-
- /**
- * Tank properties are something like name, description, size.
- * Excluded are inhabitants etc... they are linked to a tank
- *
- * @throws Exception
- */
- @Test
- @Transactional
- public void testAlterTankProperties() throws Exception {
- // Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
- final UserTo registeredUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
-
- AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
- ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, registeredUser.getEmail());
-
- // When
- aquariumTo = aquariumToResultTo.getValue();
- aquariumTo.setDescription(JABA_DABA_DOOOOO);
-
- // Then
- aquariumToResultTo = tankService.updateTank(aquariumTo, TESTUSER_EMAIL1);
-
- aquariumTo = tankService.getTank(aquariumToResultTo.getValue().getId(), TESTUSER_EMAIL1);
-
- // Then
- assertEquals("Tank was not updated", aquariumTo.getDescription(), JABA_DABA_DOOOOO);
- }
-
- @Test
- @Transactional
- public void testListUsersTanks() throws Exception {
- // Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withTankService(tankService).withUserService(userService);
- UserTo registeredUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
-
- AquariumTo aquariumTo1 = new AquariumTo();
- aquariumTo1.setDescription("Small Test Tank");
- aquariumTo1.setSize(40);
- aquariumTo1.setSizeUnit(SizeUnit.LITER);
- aquariumTo1.setWaterType(WaterType.SEA_WATER);
-
- AquariumTo aquariumTo2 = new AquariumTo();
- aquariumTo2.setDescription("Big Test Tank");
- aquariumTo2.setSize(120);
- aquariumTo2.setSizeUnit(SizeUnit.LITER);
- aquariumTo2.setWaterType(WaterType.SEA_WATER);
-
- ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo1, registeredUser.getEmail());
- ResultTo aquariumToResultTo1 = tankService.registerNewTank(aquariumTo2, registeredUser.getEmail());
-
- // When
- List usersAquariums = tankService.listTanks(TESTUSER_EMAIL1);
-
- // Then
- assertNotNull("Oops",usersAquariums);
- assertEquals("Persisted Testdata?",2, usersAquariums.size());
- assertTrue(usersAquariums.get(0).getDescription() == aquariumTo1.getDescription());
- assertTrue(usersAquariums.get(1).getDescription() == aquariumTo2.getDescription());
- }
-
-/*
- @BeforeClass
- public static void init() throws NamingException {
- }
-*/
-
-/* @AfterClass
- public static void tearDownClass() throws Exception {
- }
-*/
-
-
- @Test
- @Transactional
- public void testRegisterNewTank() throws Exception {
- // Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
- final UserTo registeredUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
-
- final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
-
- // When
- final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, TESTUSER_EMAIL1);
+ static TestDataFactory testDataFactory = TestDataFactory.getInstance();
- // Then
- assertNotNull("ResultObject must not be empty",aquariumToResultTo);
- final AquariumTo aquarium = aquariumToResultTo.getValue();
- assertNotNull("ResultObject had no Aquarium inside!",aquarium);
- assertNotNull("Tank ID was not provided!",aquarium.getId());
- assertEquals("User Assignment missing.",registeredUser.getId(), aquarium.getUserId());
- assertEquals("Wrong message type.", CATEGORY.INFO, aquariumToResultTo.getMessage().getType());
- }
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
- @Test
- @Transactional
- public void testCreateTemperatureAPIKeyForTankAndRetrieveTankByAPIKey() throws Exception {
- // Given - prepersisted Testdata
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
- final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
- UserTo testUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
- final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, TESTUSER_EMAIL1);
+ // ------------------------------ FIELDS ------------------------------
- // When
- ResultTo resultOfAPIKeyGeneration = tankService.generateAndAssignNewTemperatureApiKey(aquariumToResultTo.getValue().getId(), TESTUSER_EMAIL1);
- AquariumTo retrievedTankByAPIKey = tankService.getTankForTemperatureApiKey(resultOfAPIKeyGeneration.getValue().getTemperatueApiKey());
+ @Autowired
+ private TankService tankService;
- // Then
+ @Autowired
+ private UserService userService;
- assertNotNull("ResultObject for generate an API Key must not be empty",resultOfAPIKeyGeneration);
+ @MockBean
+ private UserRepository userRepository;
+ @MockBean
+ private AquariumRepository aquariumRepository;
- final AquariumTo aquariumWithJustAddedAPIKey = resultOfAPIKeyGeneration.getValue();
- assertNotNull("ResultObject for generate an API Key had no Aquarium inside!",aquariumWithJustAddedAPIKey);
- assertNotNull("API Key has not been stored!",aquariumWithJustAddedAPIKey.getTemperatueApiKey());
- assertEquals("Wrong message type.", CATEGORY.INFO, resultOfAPIKeyGeneration.getMessage().getType());
-
- assertNotNull("Did not retrieved a tank by API Key",retrievedTankByAPIKey);
- assertEquals("Ouch - returned different Tank! This is a MAJOR Bug", aquariumWithJustAddedAPIKey.getId(), retrievedTankByAPIKey.getId());
- }
-
-
- @Test
- @Transactional
- public void testRemoveTank() throws Exception {
-
- // Given
- TestDataFactory testDataFactory = TestDataFactory.getInstance().withUserService(userService);
- final UserTo registeredUser = testDataFactory.getRegisterNewTestUser(TESTUSER_EMAIL1);
- final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
- final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, registeredUser.getEmail());
-
- Long persistedTankId = aquariumToResultTo.getValue().getId();
- tankService.removeTank(persistedTankId, TESTUSER_EMAIL1);
-
- // When
- AquariumTo tankAfterDeletion = tankService.getTank(persistedTankId, TESTUSER_EMAIL1);
-
- // Then
- assertNull("Users tank was supposed to be removed!", tankAfterDeletion);
-
- }
+// -------------------------- OTHER METHODS --------------------------
+ /**
+ * Tank properties are something like name, description, size.
+ * Excluded are inhabitants etc... they are linked to a tank
+ *
+ * @throws Exception
+ */
+ @Test
+ @Rollback
+ public void testAlterTankProperties() throws Exception {
+ // Given
+ final UserTo testUser = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity userEntity = testDataFactory.getNewTestUserEntity(testUser);
+
+ AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
+ aquariumTo.setDescription(JABA_DABA_DOOOOO); // Change the description
+ AquariumEntity aquariumEntity = testDataFactory.getTestAquariumEntity(aquariumTo, userEntity);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(userEntity);
+ given(aquariumRepository.getAquariumEntityByIdAndUser_IdIs(TEST_TANK_ID, userEntity.getId())).willReturn(aquariumEntity);
+ given(aquariumRepository.saveAndFlush(any())).willReturn(aquariumEntity);
+
+ // When
+ ResultTo aquariumToResultTo = tankService.updateTank(aquariumTo, TESTUSER_EMAIL1);
+
+ // Then
+ aquariumTo = tankService.getTank(aquariumToResultTo.getValue().getId(), TESTUSER_EMAIL1);
+
+ // Then
+ assertEquals("Tank was not updated", aquariumTo.getDescription(), JABA_DABA_DOOOOO);
+ }
+
+
+ @Test
+ public void testListUsersTanks() throws Exception {
+ // Given
+ UserTo testUser = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity userEntity = testDataFactory.getNewTestUserEntity(testUser);
+
+ AquariumEntity aquarium1 = testDataFactory.getTestAquariumEntity(testDataFactory.getTestAquariumTo(), userEntity);
+ AquariumEntity aquarium2 = testDataFactory.getTestAquariumEntity(testDataFactory.getTestAquariumTo(), userEntity);
+ aquarium2.setDescription("Second Tank");
+
+ // Mocking
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(userEntity);
+ given(aquariumRepository.findAllByUser_IdIs(userEntity.getId())).willReturn(List.of(aquarium1, aquarium2));
+
+ // When
+ List usersAquariums = tankService.listTanks(TESTUSER_EMAIL1);
+
+ // Then
+ assertNotNull("Oops", usersAquariums);
+ assertEquals("Lost Testdata?", 2, usersAquariums.size());
+ assertTrue(usersAquariums.get(0).getDescription() == aquarium1.getDescription());
+ assertTrue(usersAquariums.get(1).getDescription() == aquarium2.getDescription());
+ }
+
+
+ @Test
+ @Rollback
+ public void testRegisterNewTank() throws Exception {
+ // Given
+ final UserTo testUser = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity userEntity = testDataFactory.getNewTestUserEntity(testUser);
+
+ final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
+ AquariumEntity aquarium1 = testDataFactory.getTestAquariumEntity(aquariumTo, userEntity);
+
+ // Mocking
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(userEntity);
+ given(aquariumRepository.existsById(TEST_TANK_ID)).willReturn(false);
+ given(aquariumRepository.saveAndFlush(any())).willReturn(aquarium1);
+
+ // When
+ final ResultTo aquariumToResultTo = tankService.registerNewTank(aquariumTo, TESTUSER_EMAIL1);
+
+ // Then
+ assertNotNull("ResultObject must not be empty", aquariumToResultTo);
+ final AquariumTo aquarium = aquariumToResultTo.getValue();
+ assertNotNull("ResultObject had no Aquarium inside!", aquarium);
+ assertNotNull("Tank ID was not provided!", aquarium.getId());
+ assertEquals("User Assignment missing.", testUser.getId(), aquarium.getUserId());
+ assertEquals("Wrong message type.", Message.CATEGORY.INFO, aquariumToResultTo.getMessage().getType());
+ }
+
+ @Test
+ @Rollback
+ public void testCreateTemperatureAPIKeyForTankAndRetrieveTankByAPIKey() throws Exception {
+ // Given
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity testUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+ AquariumTo aquariumTo = testDataFactory.getTestAquariumFor(testUserTo);
+ AquariumEntity testAquariumEntity = testDataFactory.getTestAquariumEntity(aquariumTo, testUserEntity);
+ AquariumEntity testAquariumEntityWithAPIKey = testDataFactory.getTestAquariumEntity(aquariumTo, testUserEntity);
+ testAquariumEntityWithAPIKey.setTemperatureApiKey("1234567890");
+
+ // Mocking
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(testUserEntity);
+ given(aquariumRepository.getAquariumEntityByIdAndUser_IdIs(TEST_TANK_ID, testUserTo.getId())).willReturn(testAquariumEntity);
+ given(aquariumRepository.getAquariumEntityByTemperatureApiKeyEquals(any()))
+ .willReturn(null)
+ .willReturn(testAquariumEntityWithAPIKey);
+
+ // When
+ ResultTo resultOfAPIKeyGeneration = tankService.generateAndAssignNewTemperatureApiKey(aquariumTo.getId(), TESTUSER_EMAIL1);
+ AquariumTo retrievedTankByAPIKey = tankService.getTankForTemperatureApiKey(resultOfAPIKeyGeneration.getValue().getTemperatureApiKey());
+
+ // Then
+
+ assertNotNull("ResultObject for generate an API Key must not be empty", resultOfAPIKeyGeneration);
+
+ final AquariumTo aquariumWithJustAddedAPIKey = resultOfAPIKeyGeneration.getValue();
+ assertNotNull("ResultObject for generate an API Key had no Aquarium inside!", aquariumWithJustAddedAPIKey);
+ assertNotNull("API Key has not been stored!", aquariumWithJustAddedAPIKey.getTemperatureApiKey());
+ assertEquals("Wrong message type.", Message.CATEGORY.INFO, resultOfAPIKeyGeneration.getMessage().getType());
+
+ assertNotNull("Did not retrieved a tank by API Key", retrievedTankByAPIKey);
+ assertEquals("Ouch - returned different Tank! This is a MAJOR Bug", aquariumWithJustAddedAPIKey.getId(), retrievedTankByAPIKey.getId());
+ }
+
+ @Test
+ @Rollback
+ public void testRemoveTank() throws Exception {
+
+ // Given
+ final UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity testUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+ final AquariumTo aquariumTo = testDataFactory.getTestAquariumTo();
+ AquariumEntity testAquariumEntity = testDataFactory.getTestAquariumEntity(aquariumTo, testUserEntity);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(testUserEntity);
+ given(aquariumRepository.getAquariumEntityByIdAndUser_IdIs(aquariumTo.getId(), testUserTo.getId()))
+ .willReturn(testAquariumEntity)
+ .willReturn(null);
+ doNothing().when(aquariumRepository).delete(testAquariumEntity);
+
+ // When
+ tankService.removeTank(aquariumTo.getId(), TESTUSER_EMAIL1);
+
+ // Then
+ AquariumTo tankAfterDeletion = tankService.getTank(aquariumTo.getId(), TESTUSER_EMAIL1);
+ assertNull("Users tank was supposed to be removed!", tankAfterDeletion);
+
+ }
}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/UserServiceTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/UserServiceTest.java
index 74feec66..deb0be89 100644
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/UserServiceTest.java
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/services/UserServiceTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
@@ -13,18 +13,29 @@
import de.bluewhale.sabi.model.UserTo;
import de.bluewhale.sabi.persistence.model.UserEntity;
import de.bluewhale.sabi.persistence.repositories.UserRepository;
+import de.bluewhale.sabi.util.TestDataFactory;
import jakarta.validation.constraints.NotNull;
+import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
+import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
-import org.springframework.transaction.annotation.Transactional;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
-import java.util.Locale;
-
-import static de.bluewhale.sabi.TestDataFactory.*;
+import static de.bluewhale.sabi.util.TestContainerVersions.MARIADB_11_3_2;
+import static de.bluewhale.sabi.util.TestDataFactory.TESTUSER_EMAIL1;
import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.when;
/**
@@ -33,319 +44,332 @@
* Date: 30.08.15
*/
@SpringBootTest
+@Testcontainers
@ContextConfiguration(classes = AppConfig.class)
+@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
+@Tag("ServiceTest")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class UserServiceTest {
+
+ /*
+ NOTICE This Testclass initializes a Testcontainer to satisfy the
+ Spring Boot Context Initialization of JPAConfig. In fact we don't rely here on the
+ Database level, as for test layer isolation we completely mock the repositories here.
+ The Testcontainer is just needed to satisfy the Spring Boot Context Initialization.
+ In future this Testclass might be refactored to be able to run without spring context,
+ but for now we keep it as it is.
+ */
+
+ static TestDataFactory testDataFactory = TestDataFactory.getInstance();
+
+ @Container
+ @ServiceConnection
+ // This does the trick. Spring Autoconfigures itself to connect against this container data requests-
+ static MariaDBContainer> mariaDBContainer = new MariaDBContainer<>(MARIADB_11_3_2);
+
// ------------------------------ FIELDS ------------------------------
- @Autowired
- UserService userService;
+ @Autowired
+ UserService userService;
+
+ @Autowired
+ PasswordEncoder passwordEncoder;
- @Autowired
- UserRepository userRepository;
+ @MockBean
+ UserRepository userRepository;
// -------------------------- OTHER METHODS --------------------------
-/*
- @BeforeClass
- public static void init() throws NamingException {
- }
-*/
-
-/* @AfterClass
- public static void tearDownClass() throws Exception {
- }
-*/
-
-
- /**
- * This test was written to demonstrate that @NotNull Annotations won't do anything if confronted with null values.
- * The reason for this is, that this is only half of the part of correct bean validation.
- * As soon as we start to introduce constraint checking through a JSR-303 Validator class, this test might fail.
- *
- * @throws Exception
- */
- @Test
- public void testNullValidation() throws Exception {
- // Given
- // Nothing
- // When
- userService.validateUser(null, null);
- // Then
- // No Exception occurs
- }
-
-
- @Test
- @Transactional
- public void testRegisterExistingUserWithSameEmailAdress() throws Exception {
- // Given
- NewRegistrationTO userTo1 = new NewRegistrationTO(TESTUSER_EMAIL1, "User1", VALID_PASSWORD);
- final ResultTo firstUserResultTo = userService.registerNewUser(userTo1);
-
- // When
- NewRegistrationTO userTo2 = new NewRegistrationTO(TESTUSER_EMAIL1, "User2", VALID_PASSWORD);
- final ResultTo userResultTo = userService.registerNewUser(userTo2);
-
- // Then
- assertNotNull(userResultTo);
- assertNull(userResultTo.getValue());
- final Message message = userResultTo.getMessage();
- assertNotNull(message);
- assertNotNull(message.getCode());
- assertEquals(message.getCode().getExceptionCode(),
- AuthExceptionCodes.USER_REGISTRATION_FAILED);
- assertEquals(message.getCode(), AuthMessageCodes.USER_ALREADY_EXISTS_WITH_THIS_EMAIL);
- }
-
- @Test
- @Transactional
- public void testUserProfileUpdate() throws Exception {
- // Given
- NewRegistrationTO userTo1 = new NewRegistrationTO(TESTUSER_EMAIL1, "User1", VALID_PASSWORD);
- userTo1.setLanguage(Locale.GERMAN.getLanguage());
- userTo1.setCountry(Locale.GERMAN.getCountry());
- final ResultTo firstUserResultTo = userService.registerNewUser(userTo1);
-
- // When
- UserProfileTo userProfileTo = new UserProfileTo(
- Locale.ENGLISH.getLanguage(),
- Locale.ENGLISH.getCountry());
-
- ResultTo userProfileResultTo = userService.updateProfile(userProfileTo, TESTUSER_EMAIL1);
- UserEntity userEntity = userRepository.getByEmail(TESTUSER_EMAIL1);
-
- // Then
- final Message message = userProfileResultTo.getMessage();
- assertNotNull(message);
- assertEquals(message.getCode(), CommonMessageCodes.UPDATE_SUCCEEDED);
-
- assertEquals(userEntity.getLanguage(), Locale.ENGLISH.getLanguage());
- assertEquals(userEntity.getCountry(), Locale.ENGLISH.getCountry());
- }
-
- @Test
- @Transactional
- public void testGetUserProfile() throws Exception {
- // Given
- NewRegistrationTO userTo1 = new NewRegistrationTO(TESTUSER_EMAIL1, "User1", VALID_PASSWORD);
- userTo1.setLanguage(Locale.GERMAN.getLanguage());
- userTo1.setCountry(Locale.GERMAN.getCountry());
- final ResultTo firstUserResultTo = userService.registerNewUser(userTo1);
-
- // When
- ResultTo userProfileToResultTo = userService.getUserProfile(TESTUSER_EMAIL1);
-
- // Then
- final Message message = userProfileToResultTo.getMessage();
- assertNotNull(userProfileToResultTo);
- assertNotNull(message);
- assertEquals(message.getCode(), CommonMessageCodes.OK);
-
- UserProfileTo userProfileTo = userProfileToResultTo.getValue();
- assertNotNull(userProfileTo);
- assertEquals(userProfileTo.getLanguage(), userTo1.getLanguage());
- assertEquals(userProfileTo.getCountry(), userTo1.getCountry());
-
- }
-
- @Test
- @Transactional
- public void testFraudUserProfileUpdate() throws Exception {
- assertThrows(BusinessException.class, () -> {
-
- // Given
- NewRegistrationTO userTo1 = new NewRegistrationTO(TESTUSER_EMAIL1, "User1", VALID_PASSWORD);
- userTo1.setLanguage(Locale.GERMAN.getLanguage());
- userTo1.setCountry(Locale.GERMAN.getCountry());
- final ResultTo firstUserResultTo = userService.registerNewUser(userTo1);
-
- // When
- UserProfileTo userProfileTo = new UserProfileTo(
- Locale.ENGLISH.getLanguage(),
- Locale.ENGLISH.getCountry());
-
- ResultTo userProfileResultTo = userService.updateProfile(userProfileTo, "DOES@NOT.MATCH");
-
- });
- }
-
- @Test
- @Transactional
- public void testRegisterExistingUserWithSameUsername() throws Exception {
- // Given
- NewRegistrationTO userTo1 = new NewRegistrationTO(TESTUSER_EMAIL1, "User1", VALID_PASSWORD);
- final ResultTo firstUserResultTo = userService.registerNewUser(userTo1);
-
- // When
- NewRegistrationTO userTo2 = new NewRegistrationTO(TESTUSER_EMAIL2, "User1", VALID_PASSWORD);
- final ResultTo userResultTo = userService.registerNewUser(userTo2);
-
- // Then
- assertNotNull(userResultTo);
- assertNull(userResultTo.getValue());
- final Message message = userResultTo.getMessage();
- assertNotNull(message);
- assertNotNull(message.getCode());
- assertEquals(message.getCode().getExceptionCode(),
- AuthExceptionCodes.USER_REGISTRATION_FAILED);
- assertEquals(message.getCode(), AuthMessageCodes.USER_ALREADY_EXISTS_WITH_THIS_USERNAME);
- }
-
- @Test
- @Transactional
- public void testRegisterUser() throws Exception {
- // Given
- NewRegistrationTO userTo = new NewRegistrationTO(TESTUSER_EMAIL1, "Tester", VALID_PASSWORD);
-
- // When
- final ResultTo userToResultTo = userService.registerNewUser(userTo);
-
- // Then
- assertNotNull(userToResultTo);
- assertNotNull(userToResultTo.getValue());
- assertNotNull(userToResultTo.getValue().getId());
- assertNotNull(userToResultTo.getValue().getValidationToken());
- }
-
-
- /**
- * During register process the user gets an email with an token. This test the service to validate the token for the user.
- *
- * @throws Exception
- */
- @Test
- @Transactional
- public void testUserValidation() throws Exception {
- // Given
- NewRegistrationTO userTo = new NewRegistrationTO(TESTUSER_EMAIL1, "Tester", VALID_PASSWORD);
- final ResultTo userToResultTo = userService.registerNewUser(userTo);
- final String token = userToResultTo.getValue().getValidationToken();
-
- // When
- boolean isValidated = userService.validateUser(userTo.getEmail(), token);
- boolean brokenValidation = userService.validateUser(userTo.getEmail(), "otherToken");
-
- // Then
- assertTrue(isValidated);
- assertFalse(brokenValidation);
- }
-
-
- /**
- * For users safety we are using a one way password encryption. So that the DB admin is not ensnared to misuse the data.
- *
- * @throws Exception
- */
- @Test
- @Transactional
- public void testStoreEncryptedPasswords() throws Exception {
- // Given
-
- final String clearTextPassword = VALID_PASSWORD;
- NewRegistrationTO userTo = new NewRegistrationTO(TESTUSER_EMAIL1, "Tester", clearTextPassword);
-
- // When
- final ResultTo userToResultTo = userService.registerNewUser(userTo);
-
- // Then
- final UserTo storedUser = userToResultTo.getValue();
- final String encryptedPassword = storedUser.getPassword();
- assertNotEquals("Stored passwords must be encrypted!", clearTextPassword, encryptedPassword);
- }
-
-
- @Test
- @Transactional
- public void testSignIn() throws Exception {
- // Given
- final String clearTextPassword = VALID_PASSWORD;
- NewRegistrationTO userTo = new NewRegistrationTO(TESTUSER_EMAIL1, "Tester", clearTextPassword);
- ResultTo resultTo = userService.registerNewUser(userTo);
- userService.validateUser(TESTUSER_EMAIL1, resultTo.getValue().getValidationToken());
-
- // When
- @NotNull ResultTo signInResult = userService.signIn(TESTUSER_EMAIL1, clearTextPassword);
-
- // Then
- assertNotNull("Contract Broken? Expected Session Token", signInResult.getValue());
-
- final Message message = signInResult.getMessage();
- assertNotNull(message);
- assertNotNull(message.getCode());
- assertEquals(AuthMessageCodes.SIGNIN_SUCCEEDED, message.getCode());
- }
-
-
- @Test
- @Transactional
- public void testSignInWithUnknownUserName() throws Exception {
- // Given
- // Nothing
-
- // When
- @NotNull ResultTo signInResult = userService.signIn("I Don't care@anything", "abc");
-
- // Then
- assertNotNull(signInResult);
- final Message message = signInResult.getMessage();
- assertNotNull(message);
- assertNotNull(message.getCode());
- assertEquals(message.getCode().getExceptionCode(),
- AuthExceptionCodes.AUTHENTICATION_FAILED);
- assertEquals(message.getCode(), AuthMessageCodes.UNKNOWN_USERNAME);
- }
-
-
- @Test
- @Transactional
- public void testSignInWithUnknownOrWrongPassword() throws Exception {
- // Given a user which registered and validated his email address.
- final String clearTextPassword = VALID_PASSWORD;
- NewRegistrationTO userTo = new NewRegistrationTO(TESTUSER_EMAIL1, "Tester", clearTextPassword);
- ResultTo userToResultTo = userService.registerNewUser(userTo);
- UserTo createdUserTo = userToResultTo.getValue();
- userService.validateUser(TESTUSER_EMAIL1, createdUserTo.getValidationToken());
-
-
- // When
- @NotNull ResultTo signInResult = userService.signIn(TESTUSER_EMAIL1, "abc");
-
- // Then
- assertNotNull(signInResult);
- final Message message = signInResult.getMessage();
- assertNotNull(message);
- assertNotNull(message.getCode());
- assertEquals(message.getCode().getExceptionCode(),
- AuthExceptionCodes.AUTHENTICATION_FAILED);
- assertEquals(message.getCode(), AuthMessageCodes.WRONG_PASSWORD);
- }
-
-
- @Test
- @Transactional
- public void testSignInWithUnvalidatedEmailFails() throws Exception {
-
- // For being able to login we need a clearly validated user account,
- // for which we use the verified email address
-
- // Given
- final String clearTextPassword = VALID_PASSWORD;
- NewRegistrationTO userTo = new NewRegistrationTO(TESTUSER_EMAIL1, "Tester", clearTextPassword);
- userService.registerNewUser(userTo);
-
- // When
- @NotNull ResultTo signInResult = userService.signIn(TESTUSER_EMAIL1, "abc");
-
- // Then
- assertNotNull(signInResult);
- final Message message = signInResult.getMessage();
- assertNotNull(message);
- assertNotNull(message.getCode());
- assertEquals(message.getCode().getExceptionCode(),
- AuthExceptionCodes.AUTHENTICATION_FAILED);
- assertEquals(message.getCode(), AuthMessageCodes.INCOMPLETE_REGISTRATION_PROCESS);
- }
+
+ /**
+ * This test was written to demonstrate that @NotNull Annotations won't do anything if confronted with null values.
+ * The reason for this is, that this is only half of the part of correct bean validation.
+ * As soon as we start to introduce constraint checking through a JSR-303 Validator class, this test might fail.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testNullValidation() throws Exception {
+ // Given
+ // Nothing
+ // When
+ userService.validateUser(null, null);
+ // Then
+ // No Exception occurs
+ }
+
+
+ @Test
+ @Rollback
+ public void testRegisterExistingUserWithSameEmailAddress() throws Exception {
+ // Given
+
+ NewRegistrationTO newRegistrationTO = testDataFactory.getNewRegistrationTO(TESTUSER_EMAIL1);
+
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity allreadyExistingUser = testDataFactory.getNewTestUserEntity(testUserTo);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(allreadyExistingUser);
+ given(userRepository.getByUsername(testUserTo.getUsername())).willReturn(allreadyExistingUser);
+
+ // When
+ final ResultTo userResultTo = userService.registerNewUser(newRegistrationTO);
+
+ // Then
+ assertNotNull(userResultTo);
+ assertNull(userResultTo.getValue());
+ final Message message = userResultTo.getMessage();
+ assertNotNull(message);
+ assertNotNull(message.getCode());
+ assertEquals(message.getCode().getExceptionCode(),
+ AuthExceptionCodes.USER_REGISTRATION_FAILED);
+ assertEquals(message.getCode(), AuthMessageCodes.USER_ALREADY_EXISTS_WITH_THIS_EMAIL);
+ }
+
+ @Test
+ @Rollback
+ public void testUserProfileUpdate() throws Exception {
+ // Given
+ UserProfileTo userProfileTo = testDataFactory.getBasicUserProfileTo();
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity testUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+
+ // Mocking the UserRepository
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(testUserEntity);
+
+ // When
+ ResultTo userProfileResultTo = userService.updateProfile(userProfileTo, TESTUSER_EMAIL1);
+
+ // Then
+ final Message message = userProfileResultTo.getMessage();
+ assertNotNull(message);
+ assertEquals(message.getCode(), CommonMessageCodes.UPDATE_SUCCEEDED);
+ }
+
+ @Test
+ public void testGetUserProfile() throws Exception {
+ // Given
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(newTestUserEntity);
+
+ // When
+ ResultTo userProfileToResultTo = userService.getUserProfile(TESTUSER_EMAIL1);
+
+ // Then
+ final Message message = userProfileToResultTo.getMessage();
+ assertNotNull(userProfileToResultTo);
+ assertNotNull(message);
+ assertEquals(message.getCode(), CommonMessageCodes.OK);
+
+ UserProfileTo userProfileTo = userProfileToResultTo.getValue();
+ assertNotNull(userProfileTo);
+ }
+
+ @Test
+ @Rollback
+ public void testFraudUserProfileUpdate() throws Exception {
+ assertThrows(BusinessException.class, () -> {
+
+ // Given
+ UserProfileTo basicUserProfileTo = testDataFactory.getBasicUserProfileTo();
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(null);
+
+ // When (result in an exception)
+ ResultTo userProfileResultTo = userService.updateProfile(basicUserProfileTo, "DOES@NOT.MATCH");
+
+ });
+ }
+
+ @Test
+ @Rollback
+ public void testRegisterExistingUserWithSameUsername() throws Exception {
+ // Given
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+ NewRegistrationTO newRegistrationTO = testDataFactory.getNewRegistrationTO(TESTUSER_EMAIL1);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(null);
+ given(userRepository.getByUsername(any())).willReturn(newTestUserEntity);
+
+
+ // When
+ final ResultTo userResultTo = userService.registerNewUser(newRegistrationTO);
+
+ // Then
+ assertNotNull(userResultTo);
+ assertNull(userResultTo.getValue());
+ final Message message = userResultTo.getMessage();
+ assertNotNull(message);
+ assertNotNull(message.getCode());
+ assertEquals(message.getCode().getExceptionCode(),
+ AuthExceptionCodes.USER_REGISTRATION_FAILED);
+ assertEquals(message.getCode(), AuthMessageCodes.USER_ALREADY_EXISTS_WITH_THIS_USERNAME);
+ }
+
+ @Test
+ @Rollback
+ public void testRegisterUser() throws Exception {
+ // Given
+ NewRegistrationTO newRegistrationTO = testDataFactory.getNewRegistrationTO(TESTUSER_EMAIL1);
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+ newTestUserEntity.setValidateToken("123456");
+
+
+ // Mocking the UserRepository
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(null);
+ given(userRepository.getByUsername(any())).willReturn(null);
+ given(userRepository.saveAndFlush(any())).willReturn(newTestUserEntity);
+
+ // When
+ final ResultTo userToResultTo = userService.registerNewUser(newRegistrationTO);
+
+ // Then
+ assertNotNull(userToResultTo);
+ assertNotNull(userToResultTo.getValue());
+ assertNotNull(userToResultTo.getValue().getId());
+ assertNotNull(userToResultTo.getValue().getValidationToken());
+ }
+
+
+ /**
+ * During register process the user gets an email with an token. This test the service to validate the token for the user.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testUserValidation() throws Exception {
+ // Given
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(newTestUserEntity);
+
+ // When
+ boolean isValidated = userService.validateUser(testUserTo.getEmail(), newTestUserEntity.getValidateToken());
+ boolean brokenValidation = userService.validateUser(testUserTo.getEmail(), "otherToken");
+
+ // Then
+ assertTrue(isValidated);
+ assertFalse(brokenValidation);
+ }
+
+
+ /**
+ * For users safety we are using a one way password encryption. So that the DB admin is not ensnared to misuse the data.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testStoreEncryptedPasswords() throws Exception {
+ // Given
+ NewRegistrationTO newRegistrationTO = testDataFactory.getNewRegistrationTO(TESTUSER_EMAIL1);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(null);
+ given(userRepository.getByUsername(any())).willReturn(null);
+ when(userRepository.saveAndFlush(any(UserEntity.class))).thenAnswer(invocation -> invocation.getArgument(0));
+
+ // When
+ final ResultTo userToResultTo = userService.registerNewUser(newRegistrationTO);
+
+ // Then
+ final UserTo storedUser = userToResultTo.getValue();
+ final String encryptedPassword = storedUser.getPassword();
+ assertNotEquals("Stored passwords must be encrypted!", newRegistrationTO.getPassword(), encryptedPassword);
+ }
+
+
+ @Test
+ public void testSignIn() throws Exception {
+ // Given
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+ newTestUserEntity.setPassword(passwordEncoder.encode(testUserTo.getPassword()));
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(newTestUserEntity);
+
+ // When
+ @NotNull ResultTo signInResult = userService.signIn(TESTUSER_EMAIL1, testUserTo.getPassword());
+
+ // Then
+ assertNotNull("Contract Broken? Expected Session Token", signInResult.getValue());
+
+ final Message message = signInResult.getMessage();
+ assertNotNull(message);
+ assertNotNull(message.getCode());
+ assertEquals(AuthMessageCodes.SIGNIN_SUCCEEDED, message.getCode());
+ }
+
+
+ @Test
+ public void testSignInWithUnknownUserName() throws Exception {
+ // Given
+ given(userRepository.getByEmail(any())).willReturn(null);
+ given(userRepository.getByUsername(any())).willReturn(null);
+
+ // When
+ @NotNull ResultTo signInResult = userService.signIn("I Don't care@anything", "abc");
+
+ // Then
+ assertNotNull(signInResult);
+ final Message message = signInResult.getMessage();
+ assertNotNull(message);
+ assertNotNull(message.getCode());
+ assertEquals(message.getCode().getExceptionCode(),
+ AuthExceptionCodes.AUTHENTICATION_FAILED);
+ assertEquals(message.getCode(), AuthMessageCodes.UNKNOWN_USERNAME);
+ }
+
+
+ @Test
+ public void testSignInWithUnknownOrWrongPassword() throws Exception {
+ // Given a user which registered and validated his email address.
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(newTestUserEntity);
+
+ // When
+ @NotNull ResultTo signInResult = userService.signIn(TESTUSER_EMAIL1, "abc");
+
+ // Then
+ assertNotNull(signInResult);
+ final Message message = signInResult.getMessage();
+ assertNotNull(message);
+ assertNotNull(message.getCode());
+ assertEquals(message.getCode().getExceptionCode(),
+ AuthExceptionCodes.AUTHENTICATION_FAILED);
+ assertEquals(message.getCode(), AuthMessageCodes.WRONG_PASSWORD);
+ }
+
+
+ @Test
+ public void testSignInWithUnvalidatedEmailFails() throws Exception {
+
+ // For being able to login we need a clearly validated user account,
+ // for which we use the verified email address
+
+ // Given
+ UserTo testUserTo = testDataFactory.getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = testDataFactory.getNewTestUserEntity(testUserTo);
+ newTestUserEntity.setValidated(false);
+
+ given(userRepository.getByEmail(TESTUSER_EMAIL1)).willReturn(newTestUserEntity);
+
+ // When
+ @NotNull ResultTo signInResult = userService.signIn(TESTUSER_EMAIL1, "abc");
+
+ // Then
+ assertNotNull(signInResult);
+ final Message message = signInResult.getMessage();
+ assertNotNull(message);
+ assertNotNull(message.getCode());
+ assertEquals(message.getCode().getExceptionCode(),
+ AuthExceptionCodes.AUTHENTICATION_FAILED);
+ assertEquals(message.getCode(), AuthMessageCodes.INCOMPLETE_REGISTRATION_PROCESS);
+ }
}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/MotdControllerTest.java b/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/MotdControllerTest.java
deleted file mode 100644
index 8715f7af..00000000
--- a/sabi-server/src/test/java/de/bluewhale/sabi/services/rest/MotdControllerTest.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright (c) 2023 by Stefan Schubert under the MIT License (MIT).
- * See project LICENSE file for the detailed terms and conditions.
- */
-
-package de.bluewhale.sabi.services.rest;
-
-import de.bluewhale.sabi.model.MotdTo;
-import de.bluewhale.sabi.services.AppService;
-import de.bluewhale.sabi.util.RestHelper;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.test.mock.mockito.MockBean;
-import org.springframework.boot.test.web.client.TestRestTemplate;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.test.annotation.DirtiesContext;
-
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.BDDMockito.given;
-import static org.mockito.Mockito.reset;
-
-
-/**
- * Checks Motd Service
- */
-@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
-@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
-public class MotdControllerTest {
-
- @MockBean
- AppService appService;
-
- @Autowired
- private TestRestTemplate restTemplate;
-
-
- @AfterEach
- public void cleanUpMocks() {
- reset(appService);
- }
-
- /**
- * Tests MOTD Rest API in case we have no content.
- *
- * @throws Exception
- */
- @Test // REST-API
- public void testModtRetrievalWithNoNews() throws Exception {
-
- // given a motd
- MotdTo motdTo = new MotdTo("junit modt");
-
- HttpHeaders headers = RestHelper.buildHttpHeader();
- ResponseEntity responseEntity = restTemplate.exchange("/api/app/motd/xx", HttpMethod.GET, null, String.class);
-
- // then we should get a 204 as result.
- assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.NO_CONTENT));
- }
-
- /**
- * Tests MOTD Rest API in case we news.
- *
- * @throws Exception
- */
- @Test // REST-API
- public void testModtRetrieval() throws Exception {
-
- // given a motd
- String motd = "Junit Modt";
- given(this.appService.fetchMotdFor("en")).willReturn(motd);
-
- // when
- HttpHeaders headers = RestHelper.buildHttpHeader();
- ResponseEntity responseEntity = restTemplate.exchange("/api/app/motd/en", HttpMethod.GET, null, String.class);
-
- // then we should get a 200 as result.
- assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
- }
-}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/util/TestContainerVersions.java b/sabi-server/src/test/java/de/bluewhale/sabi/util/TestContainerVersions.java
new file mode 100644
index 00000000..0ba95a43
--- /dev/null
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/util/TestContainerVersions.java
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
+ * See project LICENSE file for the detailed terms and conditions.
+ */
+
+package de.bluewhale.sabi.util;
+
+public interface TestContainerVersions {
+ String MARIADB_11_3_2 = "mariadb:11.3.2";
+}
diff --git a/sabi-server/src/test/java/de/bluewhale/sabi/util/TestDataFactory.java b/sabi-server/src/test/java/de/bluewhale/sabi/util/TestDataFactory.java
new file mode 100644
index 00000000..d96771cf
--- /dev/null
+++ b/sabi-server/src/test/java/de/bluewhale/sabi/util/TestDataFactory.java
@@ -0,0 +1,234 @@
+/*
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
+ * See project LICENSE file for the detailed terms and conditions.
+ */
+
+package de.bluewhale.sabi.util;
+
+import de.bluewhale.sabi.model.*;
+import de.bluewhale.sabi.persistence.model.*;
+
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Small Util class which provide common test DTOs.
+ *
+ * @author Stefan Schubert
+ */
+public class TestDataFactory {
+// ------------------------------ FIELDS ------------------------------
+
+ public static final String TESTUSER_EMAIL1 = "testservice1@bluewhale.de";
+ public static final String TESTUSER_EMAIL2 = "testservice2@bluewhale.de";
+
+ public static final String INVALID_PASSWORD = "quertz!1";
+ public static final String VALID_PASSWORD = "All!Rules8-)Applied";
+
+ public static final Long TEST_TANK_ID = 1L; // do not change - integration tests rely on this
+ public static final Long TEST_USER_ID = 1L; // do not change - integration tests rely on this
+ public static final Long TEST_FISH_ID = 1L; // do not change - integration tests rely on this
+ public static final Long TEST_PLAGUE_ID = 1L; // do not change - integration tests rely on this
+
+ private static TestDataFactory instance;
+
+
+// -------------------------- STATIC METHODS --------------------------
+
+ protected TestDataFactory() {
+ // defeat instantiation from outside
+ }
+
+// --------------------------- CONSTRUCTORS ---------------------------
+
+ public static TestDataFactory getInstance() {
+ if (instance == null) {
+ instance = new TestDataFactory();
+ }
+ return instance;
+ }
+
+// -------------------------- OTHER METHODS --------------------------
+
+ public AquariumTo getTestAquariumTo() {
+ final AquariumTo aquariumTo = new AquariumTo();
+ aquariumTo.setDescription("Test Tank");
+ aquariumTo.setActive(Boolean.TRUE);
+ aquariumTo.setSize(40);
+ aquariumTo.setSizeUnit(SizeUnit.LITER);
+ aquariumTo.setWaterType(WaterType.SEA_WATER);
+ aquariumTo.setId(TEST_TANK_ID);
+ return aquariumTo;
+ }
+
+ public AquariumTo getTestAquariumFor(UserTo userTo) {
+ AquariumTo aquariumTo = getTestAquariumTo();
+ aquariumTo.setUserId(userTo.getId());
+ return aquariumTo;
+ }
+
+ public AquariumEntity getTestAquariumEntity(AquariumTo aquariumTo, UserEntity userEntity) {
+ AquariumEntity aquariumEntity = new AquariumEntity();
+ aquariumEntity.setId(aquariumTo.getId());
+ aquariumEntity.setDescription(aquariumTo.getDescription());
+ aquariumEntity.setSize(aquariumTo.getSize());
+ aquariumEntity.setSizeUnit(aquariumTo.getSizeUnit());
+ aquariumEntity.setWaterType(aquariumTo.getWaterType());
+ aquariumEntity.setUser(userEntity);
+ return aquariumEntity;
+ }
+
+ public FishTo getTestFishTo(AquariumTo aquariumTo) {
+ FishTo fishTo = new FishTo();
+ fishTo.setAquariumId(aquariumTo.getId());
+ fishTo.setFishCatalogueId(1L);
+ fishTo.setNickname("Green Latern");
+ return fishTo;
+ }
+
+ public FishEntity getTestFishEntity(FishTo fishTo, Long aquariumId) {
+ FishEntity fishEntity = new FishEntity();
+ fishEntity.setAquariumId(aquariumId);
+ fishEntity.setFishCatalogueId(fishTo.getFishCatalogueId());
+ fishEntity.setNickname(fishTo.getNickname());
+ fishEntity.setId(TEST_FISH_ID);
+ return fishEntity;
+ }
+
+
+ public NewRegistrationTO getNewRegistrationTO(String eMail) {
+ NewRegistrationTO newRegistrationTO = new NewRegistrationTO();
+ newRegistrationTO.setEmail(eMail);
+ newRegistrationTO.setPassword(VALID_PASSWORD);
+ newRegistrationTO.setLanguage(Locale.ENGLISH.getLanguage());
+ newRegistrationTO.setCountry(Locale.UK.getCountry());
+ return newRegistrationTO;
+ }
+
+ public UserTo getNewTestUserTo(String eMail) {
+ UserTo userTo1 = new UserTo(eMail, eMail, VALID_PASSWORD);
+ userTo1.setUsername("TestUser");
+ userTo1.setLanguage(Locale.ENGLISH.getLanguage());
+ userTo1.setCountry(Locale.UK.getCountry());
+ userTo1.setValidated(true);
+ userTo1.setId(TEST_USER_ID);
+ return userTo1;
+ }
+
+ public UserEntity getNewTestUserEntity(UserTo userTo) {
+ UserEntity userEntity = new UserEntity();
+ userEntity.setEmail(userTo.getEmail());
+ userEntity.setId(userTo.getId());
+ userEntity.setLanguage(userTo.getLanguage());
+ userEntity.setCountry(userTo.getCountry());
+ userEntity.setValidateToken("1234567890");
+ userEntity.setValidated(userTo.isValidated());
+ return userEntity;
+ }
+
+ public UserProfileTo getBasicUserProfileTo() {
+ return new UserProfileTo(Locale.ENGLISH.getLanguage(), Locale.UK.getCountry());
+ }
+
+ public UserProfileTo getUserProfileToWithMeasurementReminderFor(UserTo userTo) {
+ UserProfileTo userProfileTo = getBasicUserProfileTo();
+ MeasurementReminderTo reminderTo = new MeasurementReminderTo();
+ reminderTo.setUserId(userTo.getId().intValue());
+ reminderTo.setPastDays(12);
+ reminderTo.setUnitId(1);
+ userProfileTo.getMeasurementReminderTos().add(reminderTo);
+
+ return userProfileTo;
+ }
+
+
+ /**
+ * Links preexisting testdata for aquarium (user) and unit.
+ *
+ * @param aquariumTo, can be null in which case we assign a default tank id
+ * @return
+ */
+ public MeasurementTo getTestMeasurementTo(AquariumTo aquariumTo) {
+ final MeasurementTo measurementTo = new MeasurementTo();
+ measurementTo.setAquariumId(aquariumTo == null ? TEST_TANK_ID:aquariumTo.getId());
+ measurementTo.setUnitId(1);
+ measurementTo.setId(4711l);
+ measurementTo.setMeasuredValue(1.15f);
+ measurementTo.setMeasuredOn(LocalDateTime.now());
+ return measurementTo;
+ }
+
+ public MeasurementEntity getTestMeasurementEntity(MeasurementTo measurementTo, AquariumEntity aquariumEntity) {
+ MeasurementEntity measurementEntity = new MeasurementEntity();
+ measurementEntity.setId(measurementTo.getId());
+ measurementEntity.setUnitId(measurementTo.getUnitId());
+ measurementEntity.setAquarium(aquariumEntity);
+ measurementEntity.setMeasuredOn(measurementTo.getMeasuredOn());
+ measurementEntity.setMeasuredValue(measurementTo.getMeasuredValue());
+ return measurementEntity;
+ }
+
+ /**
+ * Will assign Default testuser and test-aquarium &
+ * @return a valid MeasurementEntity
+ */
+ public MeasurementEntity getTestMeasurementEntityWithDefaults() {
+ UserTo testUserTo = getNewTestUserTo(TESTUSER_EMAIL1);
+ UserEntity newTestUserEntity = getNewTestUserEntity(testUserTo);
+ AquariumTo testAquariumTo = getTestAquariumTo();
+ AquariumEntity testAquariumEntity = getTestAquariumEntity(testAquariumTo, newTestUserEntity);
+ MeasurementTo testMeasurementTo = getTestMeasurementTo(testAquariumTo);
+ return getTestMeasurementEntity(testMeasurementTo, testAquariumEntity);
+ }
+
+
+ public ParameterTo getTestParameterTo() {
+ ParameterTo parameterTo = new ParameterTo();
+ parameterTo.setBelongingUnitId(4711);
+ parameterTo.setMaxThreshold(10f);
+ parameterTo.setMinThreshold(20f);
+ parameterTo.setId(101);
+ parameterTo.setDescription("Junit");
+ return parameterTo;
+ }
+
+ public UnitTo getTestUnitTo() {
+ UnitTo unitTo = new UnitTo();
+ unitTo.setId(4711);
+ unitTo.setUnitSign("SP");
+ unitTo.setDescription("Scrum Story Points");
+ return unitTo;
+ }
+
+ public PlagueStatusEntity getTestPlagueStatusEntity() {
+ PlagueStatusEntity plagueStatusEntity = new PlagueStatusEntity();
+ plagueStatusEntity.setId(TEST_PLAGUE_ID);
+ plagueStatusEntity.setLocalizedPlagueStatusEntities(null);
+ return plagueStatusEntity;
+ }
+
+ public List getTestLocalizedPlagueStatusEntities() {
+ ArrayList localizedPlagueStatusEntities = new ArrayList<>();
+
+ LocalizedPlagueStatusEntity localizedPlagueStatus1 = new LocalizedPlagueStatusEntity();
+ localizedPlagueStatus1.setPlague_status_id(1);
+ localizedPlagueStatus1.setId(99l);
+ localizedPlagueStatus1.setDescription("Spreading");
+ localizedPlagueStatus1.setLanguage("en");
+
+ LocalizedPlagueStatusEntity localizedPlagueStatus2 = new LocalizedPlagueStatusEntity();
+ localizedPlagueStatus2.setPlague_status_id(1);
+ localizedPlagueStatus2.setId(88l);
+ localizedPlagueStatus2.setDescription("Ausweitend");
+ localizedPlagueStatus2.setLanguage("de");
+
+ localizedPlagueStatusEntities.add(localizedPlagueStatus1);
+ localizedPlagueStatusEntities.add(localizedPlagueStatus2);
+
+ return localizedPlagueStatusEntities;
+
+ }
+
+}
diff --git a/sabi-server/src/test/resources/application.yml b/sabi-server/src/test/resources/application.yml
index 2c967bef..1818d794 100644
--- a/sabi-server/src/test/resources/application.yml
+++ b/sabi-server/src/test/resources/application.yml
@@ -9,9 +9,6 @@ eclipselink:
logging:
level: INFO
target-database: Auto
-h2:
- test:
- mode: true
logging:
level:
org:
@@ -48,10 +45,10 @@ spring:
datasource:
hikari:
connection-timeout: 3000
- maximum-pool-size: 5
+ maximum-pool-size: 20
minimum-idle: 2
- type: com.zaxxer.hikari.HikariDataSource
- url: jdbc:h2:mem:test;MODE=LEGACY;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DB_CLOSE_ON_EXIT=true;DB_CLOSE_DELAY=-1
+ # Hikari will be configured through Testcontainer Setup in integration tests.
+ # url: jdbc:h2:mem:test;MODE=LEGACY;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DB_CLOSE_ON_EXIT=true;DB_CLOSE_DELAY=-1
mail:
host: localhost
password: YouWillNeverKnow
@@ -64,4 +61,4 @@ spring:
enable: true
username: sabi@bluewhale.de
main:
- allow-bean-definition-overriding: true
+ allow-bean-definition-overriding: true
\ No newline at end of file
diff --git a/sabi-server/src/test/resources/banner.txt b/sabi-server/src/test/resources/banner.txt
new file mode 100644
index 00000000..acb5d360
--- /dev/null
+++ b/sabi-server/src/test/resources/banner.txt
@@ -0,0 +1,8 @@
+ ____ _ _ _____ _ _
+/ ___| __ _| |__ (_) |_ _|__ ___| |_(_)_ __ __ _
+\___ \ / _` | '_ \| | | |/ _ \/ __| __| | '_ \ / _` |
+ ___) | (_| | |_) | | | | __/\__ \ |_| | | | | (_| |_ _ _
+|____/ \__,_|_.__/|_| |_|\___||___/\__|_|_| |_|\__, (_|_|_)
+ |___/
+using Spring-Boot: ${spring-boot.formatted-version} 🐋
+
diff --git a/sabi-webclient/pom.xml b/sabi-webclient/pom.xml
index 801eecd1..7ebefe61 100644
--- a/sabi-webclient/pom.xml
+++ b/sabi-webclient/pom.xml
@@ -10,7 +10,7 @@
de.bluewhale
sabi-webclient
- 1.2.7
+ 1.2.9
jar
sabi-webclient
A JSF based webclient for sabi.
@@ -47,23 +47,23 @@
org.springframework.boot
spring-boot-starter-parent
- 3.2.2
+ 3.3.3
UTF-8
UTF-8
- 21
+ 22
2.20.0
- 4.0.1
- 1.12.3
+ 4.1.0
+ 1.13.5
5.2.2
- 1.2.6
- 1.6.4
- 9.0.9
- 1.18.30
- 2.16.2
+ 1.2.9
+ 1.6.5
+ 10.0.3
+ 1.18.34
+ 2.17.1
@@ -255,28 +255,6 @@
-
-
- org.owasp
- dependency-check-maven
- ${owasp.plugin.version}
-
-
- ${nvd.api.key}
-
-
-
- verify
-
- check
-
-
-
-
@@ -297,4 +275,44 @@
+
+
+
+ owasp-check
+
+
+
+
+ nvd.api.key
+ !@{nvd.api.key}
+
+
+
+
+
+ org.owasp
+ dependency-check-maven
+ ${owasp.plugin.version}
+
+
+ ${nvd.api.key}
+
+
+
+ verify
+
+ check
+
+
+
+
+
+
+
+
+
diff --git a/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/apigateway/TankServiceImpl.java b/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/apigateway/TankServiceImpl.java
index 40d9bf1f..34836617 100644
--- a/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/apigateway/TankServiceImpl.java
+++ b/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/apigateway/TankServiceImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2022 by Stefan Schubert under the MIT License (MIT).
+ * Copyright (c) 2024 by Stefan Schubert under the MIT License (MIT).
* See project LICENSE file for the detailed terms and conditions.
*/
@@ -100,7 +100,7 @@ public String reCreateTemperatureAPIKey(Long tankID, String pJWTBackendAuthtoken
throw new BusinessException(CommonExceptionCodes.INTERNAL_ERROR);
}
- return myTankWithAPIKey.getTemperatueApiKey();
+ return myTankWithAPIKey.getTemperatureApiKey();
}
@Override
diff --git a/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/controller/TankListView.java b/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/controller/TankListView.java
index 93d777b4..d62cc598 100644
--- a/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/controller/TankListView.java
+++ b/sabi-webclient/src/main/java/de/bluewhale/sabi/webclient/controller/TankListView.java
@@ -101,7 +101,7 @@ public void generateTemperatureApiKey(AquariumTo tank) {
selectedTank = tank;
try {
String apiKey = tankService.reCreateTemperatureAPIKey(tank.getId(), userSession.getSabiBackendToken());
- tank.setTemperatueApiKey(apiKey);
+ tank.setTemperatureApiKey(apiKey);
} catch (BusinessException e) {
log.error(e.getLocalizedMessage());
MessageUtil.warn("messages","common.error.internal_server_problem.t",userSession.getLocale());
diff --git a/sabi-webclient/src/main/resources/META-INF/resources/secured/tankView.xhtml b/sabi-webclient/src/main/resources/META-INF/resources/secured/tankView.xhtml
index f8a38932..5c8d8285 100644
--- a/sabi-webclient/src/main/resources/META-INF/resources/secured/tankView.xhtml
+++ b/sabi-webclient/src/main/resources/META-INF/resources/secured/tankView.xhtml
@@ -1,6 +1,6 @@
-
-
+