diff --git a/.run/PlatformApplication.run.xml b/.run/PlatformApplication.run.xml index 288a7e33..7210756e 100644 --- a/.run/PlatformApplication.run.xml +++ b/.run/PlatformApplication.run.xml @@ -6,7 +6,7 @@ - diff --git a/build.gradle.kts b/build.gradle.kts index 9ff558a4..3f780167 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,5 @@ +import java.time.Duration + plugins { java pmd @@ -26,6 +28,8 @@ repositories { mavenCentral() } +val testContainer = "1.19.0" + dependencies { compileOnly("org.projectlombok:lombok") @@ -38,6 +42,11 @@ dependencies { testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.skyscreamer:jsonassert:1.5.3") + + testImplementation("com.surrealdb:surrealdb-driver:0.1.0") + testImplementation("org.testcontainers:testcontainers:${testContainer}") + testImplementation("org.testcontainers:junit-jupiter:$testContainer") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") testCompileOnly("org.projectlombok:lombok") testAnnotationProcessor("org.projectlombok:lombok") @@ -45,6 +54,8 @@ dependencies { tasks.withType { useJUnitPlatform() + jvmArgs = listOf("-Xmx2048m") + timeout.set(Duration.ofMinutes(2)) } tasks { diff --git a/src/main/java/com/wcc/platform/configuration/DatabaseConfig.java b/src/main/java/com/wcc/platform/configuration/DatabaseConfig.java index 39222dff..e8848457 100644 --- a/src/main/java/com/wcc/platform/configuration/DatabaseConfig.java +++ b/src/main/java/com/wcc/platform/configuration/DatabaseConfig.java @@ -16,8 +16,13 @@ public class DatabaseConfig { public SyncSurrealDriver getDriver(final SurrealDbConfig config) { final var conn = new SurrealWebSocketConnection(config.getHost(), config.getPort(), config.isTls()); + conn.connect(config.getConnections()); + final var driver = new SyncSurrealDriver(conn); + + driver.signIn(config.getUsername(), config.getPassword()); + driver.use(config.getNamespace(), config.getDatabase()); return driver; diff --git a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java index b589066a..86c1b391 100644 --- a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java +++ b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java @@ -12,7 +12,8 @@ @Repository public class SurrealDbPageRepository implements PageRepository { - static final String TABLE = "page"; + /* default */ static final String TABLE = "page"; + private final SyncSurrealDriver driver; @Autowired diff --git a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbResourceRepository.java b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbResourceRepository.java index e092a900..4a7da398 100644 --- a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbResourceRepository.java +++ b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbResourceRepository.java @@ -13,7 +13,7 @@ @Repository public class SurrealDbResourceRepository implements ResourceContentRepository { - static final String TABLE = "resource_content"; + /* default */ static final String TABLE = "resource_content"; private final SyncSurrealDriver driver; @Autowired diff --git a/src/main/java/com/wcc/platform/service/CmsService.java b/src/main/java/com/wcc/platform/service/CmsService.java index 324aac96..05e75a57 100644 --- a/src/main/java/com/wcc/platform/service/CmsService.java +++ b/src/main/java/com/wcc/platform/service/CmsService.java @@ -10,25 +10,18 @@ import com.wcc.platform.domain.cms.pages.LandingPage; import com.wcc.platform.domain.cms.pages.TeamPage; import com.wcc.platform.domain.exceptions.PlatformInternalException; -import com.wcc.platform.repository.PageRepository; import com.wcc.platform.utils.FileUtil; -import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** CMS service responsible for simple pages. */ @Service public class CmsService { private final ObjectMapper objectMapper; - private final PageRepository repository; @Autowired - public CmsService( - final ObjectMapper objectMapper, - @Qualifier("getPageRepository") final PageRepository repository) { + public CmsService(final ObjectMapper objectMapper) { this.objectMapper = objectMapper; - this.repository = repository; } /** @@ -65,8 +58,6 @@ public FooterPage getFooter() { * @return Landing page of the community. */ public LandingPage getLandingPage() { - Optional page = repository.findById("page:⟨landing_page⟩"); - try { return objectMapper.readValue( FileUtil.readFileAsString(ApiResourcesFile.LANDING_PAGE.getFileName()), diff --git a/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java b/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java index dd03b64c..6ab90ffd 100644 --- a/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java +++ b/src/test/java/com/wcc/platform/integrationtests/CmsServiceIntegrationTest.java @@ -26,7 +26,7 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -class CmsServiceIntegrationTest { +class CmsServiceIntegrationTest extends SurrealDbIntegrationTest { @Autowired private CmsService service; diff --git a/src/test/java/com/wcc/platform/integrationtests/DefaultControllerIntegrationTest.java b/src/test/java/com/wcc/platform/integrationtests/DefaultControllerIntegrationTest.java index 0e910ab6..e4427eee 100644 --- a/src/test/java/com/wcc/platform/integrationtests/DefaultControllerIntegrationTest.java +++ b/src/test/java/com/wcc/platform/integrationtests/DefaultControllerIntegrationTest.java @@ -15,7 +15,7 @@ import org.springframework.http.HttpStatus; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -class DefaultControllerIntegrationTest { +class DefaultControllerIntegrationTest extends SurrealDbIntegrationTest { @Autowired private DefaultController controller; diff --git a/src/test/java/com/wcc/platform/PlatformApplicationTests.java b/src/test/java/com/wcc/platform/integrationtests/PlatformApplicationTests.java similarity index 62% rename from src/test/java/com/wcc/platform/PlatformApplicationTests.java rename to src/test/java/com/wcc/platform/integrationtests/PlatformApplicationTests.java index 96ceef36..3f87cebc 100644 --- a/src/test/java/com/wcc/platform/PlatformApplicationTests.java +++ b/src/test/java/com/wcc/platform/integrationtests/PlatformApplicationTests.java @@ -1,19 +1,20 @@ -package com.wcc.platform; +package com.wcc.platform.integrationtests; import static org.junit.jupiter.api.Assertions.assertNotNull; 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.context.SpringBootTest.WebEnvironment; import org.springframework.context.ApplicationContext; -@SpringBootTest -class PlatformApplicationTests { +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +class PlatformApplicationTests extends SurrealDbIntegrationTest { @Autowired private ApplicationContext context; @Test void contextLoads() { - assertNotNull(context, "The application context should have loaded."); + assertNotNull(context, "The application context should have loaded"); } } diff --git a/src/test/java/com/wcc/platform/integrationtests/PlatformServiceIntegrationTest.java b/src/test/java/com/wcc/platform/integrationtests/PlatformServiceIntegrationTest.java index 4b07c5c3..a4172749 100644 --- a/src/test/java/com/wcc/platform/integrationtests/PlatformServiceIntegrationTest.java +++ b/src/test/java/com/wcc/platform/integrationtests/PlatformServiceIntegrationTest.java @@ -11,9 +11,10 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.test.context.ActiveProfiles; -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") -class PlatformServiceIntegrationTest { +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +class PlatformServiceIntegrationTest extends SurrealDbIntegrationTest { + @Autowired private PlatformService service; @Test @@ -26,6 +27,7 @@ void testSaveMember() { @Test void testGetAll() { + var total = service.getAll().size(); var member = createMemberTest(MemberType.MEMBER); diff --git a/src/test/java/com/wcc/platform/integrationtests/ProgramControllerIntegrationTest.java b/src/test/java/com/wcc/platform/integrationtests/ProgramControllerIntegrationTest.java index 6178d5fa..50112f4a 100644 --- a/src/test/java/com/wcc/platform/integrationtests/ProgramControllerIntegrationTest.java +++ b/src/test/java/com/wcc/platform/integrationtests/ProgramControllerIntegrationTest.java @@ -17,7 +17,7 @@ import org.springframework.http.HttpStatus; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -class ProgramControllerIntegrationTest { +class ProgramControllerIntegrationTest extends SurrealDbIntegrationTest { @Autowired private ProgrammeController controller; diff --git a/src/test/java/com/wcc/platform/integrationtests/SurrealDbIntegrationTest.java b/src/test/java/com/wcc/platform/integrationtests/SurrealDbIntegrationTest.java new file mode 100644 index 00000000..5e28fd80 --- /dev/null +++ b/src/test/java/com/wcc/platform/integrationtests/SurrealDbIntegrationTest.java @@ -0,0 +1,54 @@ +package com.wcc.platform.integrationtests; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +@Testcontainers +class SurrealDbIntegrationTest { + + private static final GenericContainer surrealDbContainer = + new GenericContainer<>(DockerImageName.parse("surrealdb/surrealdb:latest")) + .withExposedPorts(8000) + .withCommand("start --user root --pass root"); + + @DynamicPropertySource + static void registerSurrealDbProperties(DynamicPropertyRegistry registry) { + surrealDbContainer.start(); + String host = surrealDbContainer.getHost(); + Integer port = surrealDbContainer.getMappedPort(8000); + registry.add("surrealdb.host", () -> host); + registry.add("surrealdb.port", port::toString); + registry.add("surrealdb.username", () -> "root"); + registry.add("surrealdb.password", () -> "root"); + registry.add("surrealdb.namespace", () -> "test_namespace"); + registry.add("surrealdb.database", () -> "test_db"); + } + + @BeforeAll + static void setUp() { + surrealDbContainer.start(); + } + + @AfterAll + static void tearDown() { + surrealDbContainer.stop(); + } + + @Test + @DisplayName("Should create and retrieve a ResourceContent entity") + void testSurrealDbConnection() { + assertTrue(surrealDbContainer.isCreated()); + assertTrue(surrealDbContainer.isRunning()); + } +} diff --git a/src/test/java/com/wcc/platform/service/CmsServiceTest.java b/src/test/java/com/wcc/platform/service/CmsServiceTest.java index a4e6c3df..9ff1ccca 100644 --- a/src/test/java/com/wcc/platform/service/CmsServiceTest.java +++ b/src/test/java/com/wcc/platform/service/CmsServiceTest.java @@ -18,7 +18,6 @@ import com.wcc.platform.domain.cms.pages.TeamPage; import com.wcc.platform.domain.exceptions.PlatformInternalException; import com.wcc.platform.factories.SetupFactories; -import com.wcc.platform.repository.PageRepository; import java.io.IOException; import java.util.List; import org.junit.jupiter.api.BeforeEach; @@ -33,8 +32,7 @@ class CmsServiceTest { @BeforeEach void setUp() { objectMapper = Mockito.mock(ObjectMapper.class); - var repository = Mockito.mock(PageRepository.class); - service = new CmsService(objectMapper, repository); + service = new CmsService(objectMapper); } @Test diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index eb520028..1f7f96e2 100644 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -5,4 +5,9 @@ spring: file: storage: - directory: data-test \ No newline at end of file + directory: data-test + +surrealdb: + host: localhost + namespace: test_namespace + database: test_db \ No newline at end of file