Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dricazenck committed Oct 3, 2024
1 parent a6dceb6 commit 1b83d0b
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .run/PlatformApplication.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<module name="wcc-backend.main" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.wcc.platform.*" />
<option name="PATTERN" value="com.wcc.platform.integrationtests.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
Expand Down
11 changes: 11 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.time.Duration

plugins {
java
pmd
Expand Down Expand Up @@ -26,6 +28,8 @@ repositories {
mavenCentral()
}

val testContainer = "1.19.0"

dependencies {

compileOnly("org.projectlombok:lombok")
Expand All @@ -38,13 +42,20 @@ 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")
}

tasks.withType<Test> {
useJUnitPlatform()
jvmArgs = listOf("-Xmx2048m")
timeout.set(Duration.ofMinutes(2))
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/com/wcc/platform/service/CmsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -65,8 +58,6 @@ public FooterPage getFooter() {
* @return Landing page of the community.
*/
public LandingPage getLandingPage() {
Optional<Object> page = repository.findById("page:⟨landing_page⟩");

try {
return objectMapper.readValue(
FileUtil.readFileAsString(ApiResourcesFile.LANDING_PAGE.getFileName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.http.HttpStatus;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class DefaultControllerIntegrationTest {
class DefaultControllerIntegrationTest extends SurrealDbIntegrationTest {

@Autowired private DefaultController controller;

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,7 @@ void testSaveMember() {

@Test
void testGetAll() {

var total = service.getAll().size();

var member = createMemberTest(MemberType.MEMBER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.springframework.http.HttpStatus;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class ProgramControllerIntegrationTest {
class ProgramControllerIntegrationTest extends SurrealDbIntegrationTest {

@Autowired private ProgrammeController controller;

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
4 changes: 1 addition & 3 deletions src/test/java/com/wcc/platform/service/CmsServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ spring:

file:
storage:
directory: data-test
directory: data-test

surrealdb:
host: localhost
namespace: test_namespace
database: test_db

0 comments on commit 1b83d0b

Please sign in to comment.