Skip to content

Commit c5a4f80

Browse files
author
Evgenii Grigorev
committed
Refactor tests as proof of concept
1 parent d64c8af commit c5a4f80

File tree

8 files changed

+131
-43
lines changed

8 files changed

+131
-43
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package og_spipes.config;
2+
3+
import jakarta.annotation.PreDestroy;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Profile;
8+
import org.springframework.util.FileSystemUtils;
9+
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
14+
@Configuration
15+
public class RepositoryPathConfig {
16+
17+
Path rootDir;
18+
19+
@Bean(name = "rdf4jRepositoryUrl")
20+
@Profile("test")
21+
public String testRepositoryUrl() throws IOException {
22+
rootDir = Files.createTempDirectory("og_spipes_sesame");
23+
24+
Path repoDir = rootDir.resolve("repo")
25+
.resolve("repositories")
26+
.resolve("s-pipes-hello-world");
27+
Files.createDirectories(repoDir);
28+
return repoDir.toUri().toString();
29+
}
30+
31+
@Bean(name = "rdf4jRepositoryUrl")
32+
@Profile("default")
33+
public String prodRepositoryUrl(@Value("${rdf4j.repositoryUrl}") String repositoryUrl) {
34+
return repositoryUrl;
35+
}
36+
37+
@Bean(name = "rdf4jRepositoryName")
38+
@Profile("test")
39+
public String testRepositoryName() {
40+
return "s-pipes-hello-world";
41+
}
42+
43+
@Bean(name = "rdf4jRepositoryName")
44+
@Profile("default")
45+
public String prodRepositoryName(@Value("${rdf4j.repositorName}") String repositoryName) {
46+
return repositoryName;
47+
}
48+
49+
@PreDestroy
50+
@Profile("test")
51+
private void clean() {
52+
FileSystemUtils.deleteRecursively(rootDir.toFile());
53+
}
54+
55+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package og_spipes.config;
2+
3+
import jakarta.annotation.PreDestroy;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Profile;
9+
import org.springframework.util.FileSystemUtils;
10+
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
15+
@Configuration
16+
public class ScriptPathConfig {
17+
18+
Path scriptDir;
19+
20+
@Bean
21+
@Profile("test")
22+
@Qualifier("scriptPath")
23+
public String TestScriptPath() throws IOException {
24+
scriptDir = Files.createTempDirectory("og_spipes_scripts");
25+
Files.createDirectories(scriptDir);
26+
27+
return scriptDir.toUri().toString();
28+
}
29+
30+
@Bean
31+
@Profile("default")
32+
@Qualifier("scriptPath")
33+
public String prodScriptPath(@Value("${scriptPaths}") String scriptPath) {
34+
return scriptPath;
35+
}
36+
37+
@PreDestroy
38+
@Profile("test")
39+
private void clean() {
40+
FileSystemUtils.deleteRecursively(scriptDir.toFile());
41+
}
42+
}
43+

src/main/java/og_spipes/persistence/BasePersistenceFactory.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
import org.springframework.context.annotation.Configuration;
1313
import jakarta.annotation.PostConstruct;
1414
import jakarta.annotation.PreDestroy;
15-
import org.springframework.util.FileSystemUtils;
1615

17-
import java.io.File;
18-
import java.net.URI;
19-
import java.net.URISyntaxException;
2016
import java.util.HashMap;
2117
import java.util.Map;
2218

@@ -30,8 +26,8 @@ public class BasePersistenceFactory {
3026
private EntityManagerFactory emf;
3127

3228
public BasePersistenceFactory(
33-
@Value("${rdf4j.repositoryUrl}") String repositoryUrl,
34-
@Value("${rdf4j.repositoryName}") String repositoryName,
29+
@Qualifier("rdf4jRepositoryUrl") String repositoryUrl,
30+
@Qualifier("rdf4jRepositoryName") String repositoryName,
3531
@Value("${rdf4j.driver}") String driver
3632
) {
3733
this.repositoryUrl = repositoryUrl;

src/main/java/og_spipes/persistence/PersistenceFactory.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.beans.factory.annotation.Qualifier;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
28-
import org.springframework.core.env.Environment;
2928

3029
import jakarta.annotation.PostConstruct;
3130
import jakarta.annotation.PreDestroy;
@@ -37,18 +36,17 @@ public class PersistenceFactory {
3736

3837
private static final Logger LOG = LoggerFactory.getLogger(PersistenceFactory.class);
3938

40-
private final Environment environment;
39+
private final String scriptPath;
4140

4241
private EntityManagerFactory emf;
4342

4443
@Autowired
45-
public PersistenceFactory(Environment environment) {
46-
this.environment = environment;
44+
public PersistenceFactory(@Qualifier("scriptPath") String scriptPath) {
45+
this.scriptPath = scriptPath;
4746
}
4847

4948
@PostConstruct
5049
private void init() {
51-
final String scriptPath = environment.getProperty("scriptPaths");
5250
LOG.info("Using repository path: {}.", scriptPath);
5351

5452
final Map<String, String> props = new HashMap<>();

src/main/java/og_spipes/persistence/Rdf4jPersistenceProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.slf4j.LoggerFactory;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.beans.factory.annotation.Qualifier;
14-
import org.springframework.beans.factory.annotation.Value;
1514
import org.springframework.context.annotation.Bean;
1615
import org.springframework.context.annotation.Configuration;
1716

@@ -32,7 +31,7 @@ public class Rdf4jPersistenceProvider {
3231
private Repository repository;
3332

3433
@Autowired
35-
public Rdf4jPersistenceProvider(@Value("${rdf4j.repositoryUrl}") String repositoryUrl, @Value("${rdf4j.repositoryName}") String repositoryName, @Qualifier("rdf4jEMF") EntityManagerFactory emf) {
34+
public Rdf4jPersistenceProvider(@Qualifier("rdf4jRepositoryUrl") String repositoryUrl, @Qualifier("rdf4jRepositoryName") String repositoryName, @Qualifier("rdf4jEMF") EntityManagerFactory emf) {
3635
this.repositoryUrl = repositoryUrl;
3736
this.repositoryName = repositoryName;
3837
this.emf = emf;

src/test/java/og_spipes/persistence/dao/TransformationDAOTest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
package og_spipes.persistence.dao;
22

33
import og_spipes.model.spipes.TransformationDTO;
4-
import og_spipes.testutil.AbstractSpringTest;
54
import org.eclipse.rdf4j.repository.Repository;
65
import org.eclipse.rdf4j.repository.RepositoryConnection;
7-
import org.eclipse.rdf4j.repository.sail.SailRepository;
86
import org.eclipse.rdf4j.rio.RDFFormat;
9-
import org.eclipse.rdf4j.sail.nativerdf.NativeStore;
107
import org.junit.jupiter.api.Assertions;
11-
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.BeforeEach;
129
import org.junit.jupiter.api.Test;
1310
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
1412

1513
import java.io.File;
1614
import java.io.IOException;
1715
import java.util.List;
1816

19-
class TransformationDAOTest extends AbstractSpringTest {
17+
@SpringBootTest
18+
class TransformationDAOTest {
2019

2120
@Autowired
2221
private TransformationDAO transformationDAO;
2322

24-
@BeforeAll
25-
static void beforeAll() throws IOException {
26-
File dataDir = tempDir.resolve("repositories").resolve("s-pipes-hello-world").toFile();
27-
Repository sesameRepo = new SailRepository(new NativeStore(dataDir));
28-
sesameRepo.init();
29-
RepositoryConnection conn = sesameRepo.getConnection();
30-
conn.clear();
31-
conn.add(new File("src/test/resources/rdf4j_source/repositories/rdf4j_export"), null, RDFFormat.TURTLE);
32-
conn.close();
33-
sesameRepo.shutDown();
23+
@Autowired
24+
private Repository repository;
25+
26+
@BeforeEach
27+
void beforeEach() throws IOException {
28+
try (RepositoryConnection conn = repository.getConnection()) {
29+
conn.clear();
30+
conn.add(new File("src/test/resources/rdf4j_source/repositories/rdf4j_export"),
31+
null, RDFFormat.TURTLE);
32+
}
3433
}
3534

3635
@Test

src/test/java/og_spipes/rest/ExecutionControllerTest.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package og_spipes.rest;
22

33
import com.google.common.collect.Sets;
4-
import og_spipes.config.Constants;
54
import og_spipes.service.ModuleExecutionInfo;
65
import og_spipes.service.ViewService;
7-
import og_spipes.testutil.AbstractControllerTest;
8-
import org.apache.commons.io.FileUtils;
9-
import org.junit.jupiter.api.BeforeEach;
106
import org.junit.jupiter.api.DisplayName;
117
import org.junit.jupiter.api.Test;
128
import org.mockito.Mock;
139
import org.mockito.Mockito;
14-
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
12+
import org.springframework.boot.test.context.SpringBootTest;
1513
import org.springframework.http.MediaType;
14+
import org.springframework.test.annotation.DirtiesContext;
15+
import org.springframework.test.web.servlet.MockMvc;
1616

17-
import java.io.File;
1817
import java.util.HashSet;
1918

2019
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -23,19 +22,16 @@
2322
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2423
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2524

26-
class ExecutionControllerTest extends AbstractControllerTest {
27-
28-
@Value(Constants.SCRIPTPATH_SPEL)
29-
private String scriptPath;
25+
@SpringBootTest
26+
@AutoConfigureMockMvc
27+
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
28+
class ExecutionControllerTest {
3029

3130
@Mock
3231
private ViewService viewService;
3332

34-
@BeforeEach
35-
void init() throws Exception {
36-
File scriptsHomeTmp = new File(scriptPath);
37-
FileUtils.copyDirectory(new File("src/test/resources/scripts_test/sample/hello-world"), scriptsHomeTmp);
38-
}
33+
@Autowired
34+
private MockMvc mockMvc;
3935

4036
@Test
4137
@DisplayName("List history of execution")

src/test/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ server.servlet.context-path=/og_spipes
22
spring.mvc.servlet.path=/rest/
33
server.port=18115
44

5+
spring.profiles.active=test
6+
57
scriptPaths=/tmp/og_spipes
68
engineurl=http://localhost:8080/s_pipes_web_war/
79
scriptRules=src/main/resources/rules/SHACL

0 commit comments

Comments
 (0)