Skip to content

Commit

Permalink
[#84] Replace application.properties with application.yml in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Dec 9, 2024
1 parent b0c28f2 commit d0c537a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {TestPersistenceConfig.class})
@ContextConfiguration(classes = {TestPersistenceConfig.class}, initializers = {ConfigDataApplicationContextInitializer.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
abstract public class BaseDaoTestRunner extends TransactionalTestRunner {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cz.cvut.kbss.study.persistence;

import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.DefaultPropertiesPropertySource;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.env.RandomValuePropertySource;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class ConfigDataApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public ConfigDataApplicationContextInitializer() {
}

public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
RandomValuePropertySource.addToEnvironment(environment);
DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
ConfigDataEnvironmentPostProcessor.applyTo(environment, applicationContext, bootstrapContext, new String[0]);
bootstrapContext.close(applicationContext);
DefaultPropertiesPropertySource.moveToEnd(environment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:application.properties")
@EnableConfigurationProperties(cz.cvut.kbss.study.util.Configuration.class)
@Profile("test")
public class TestFormGenPersistenceFactory {

private static final String URL_PROPERTY = "test." + ConfigParam.FORM_GEN_REPOSITORY_URL;
private static final String DRIVER_PROPERTY = "test." + ConfigParam.PERSISTENCE_DRIVER;

@Autowired
private Environment environment;
private cz.cvut.kbss.study.util.Configuration config;

private EntityManagerFactory emf;

Expand All @@ -36,8 +34,8 @@ public EntityManagerFactory getEntityManagerFactory() {
@PostConstruct
private void init() {
final Map<String, String> properties = TestPersistenceFactory.getDefaultProperties();
properties.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, environment.getProperty(URL_PROPERTY));
properties.put(JOPAPersistenceProperties.DATA_SOURCE_CLASS, environment.getProperty(DRIVER_PROPERTY));
properties.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, config.getFormGenRepositoryUrl());
properties.put(JOPAPersistenceProperties.DATA_SOURCE_CLASS, config.getPersistenceDriver());
this.emf = Persistence.createEntityManagerFactory("formGenTestPU", properties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
Expand All @@ -24,14 +25,15 @@
import static cz.cvut.kbss.ontodriver.config.OntoDriverProperties.DATA_SOURCE_USERNAME;

@Configuration
@PropertySource("classpath:application.properties")
@EnableConfigurationProperties(cz.cvut.kbss.study.util.Configuration.class)
@Profile("test")
public class TestPersistenceFactory {

private static final String URL_PROPERTY = "test." + ConfigParam.REPOSITORY_URL;
private static final String DRIVER_PROPERTY = "test." + ConfigParam.PERSISTENCE_DRIVER;
private static final String USERNAME_PROPERTY = "test.username";
private static final String PASSWORD_PROPERTY = "test.password";
private static final String USERNAME_PROPERTY = "username";
private static final String PASSWORD_PROPERTY = "password";

@Autowired
private cz.cvut.kbss.study.util.Configuration config;

@Autowired
private Environment environment;
Expand All @@ -47,8 +49,8 @@ public EntityManagerFactory getEntityManagerFactory() {
@PostConstruct
private void init() {
final Map<String, String> properties = getDefaultProperties();
properties.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, environment.getProperty(URL_PROPERTY));
properties.put(JOPAPersistenceProperties.DATA_SOURCE_CLASS, environment.getProperty(DRIVER_PROPERTY));
properties.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, config.getRepositoryUrl());
properties.put(JOPAPersistenceProperties.DATA_SOURCE_CLASS,config.getPersistenceDriver());
if (environment.getProperty(USERNAME_PROPERTY) != null) {
properties.put(DATA_SOURCE_USERNAME, environment.getProperty(USERNAME_PROPERTY));
properties.put(DATA_SOURCE_PASSWORD, environment.getProperty(PASSWORD_PROPERTY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cz.cvut.kbss.study.environment.generator.Generator;
import cz.cvut.kbss.study.model.Institution;
import cz.cvut.kbss.study.model.User;
import cz.cvut.kbss.study.persistence.ConfigDataApplicationContextInitializer;
import cz.cvut.kbss.study.persistence.dao.InstitutionDao;
import cz.cvut.kbss.study.persistence.dao.UserDao;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -19,7 +20,7 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {TestServiceConfig.class, TestPersistenceConfig.class})
@ContextConfiguration(classes = {TestServiceConfig.class, TestPersistenceConfig.class}, initializers = {ConfigDataApplicationContextInitializer.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
public abstract class BaseServiceTestRunner extends TransactionalTestRunner {
Expand Down
6 changes: 0 additions & 6 deletions src/test/resources/application.properties

This file was deleted.

4 changes: 4 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
persistenceDriver: cz.cvut.kbss.ontodriver.rdf4j.Rdf4jDataSource
repositoryUrl: study-test-repository
formGenRepositoryUrl: test-formGen-repository
formGenServiceUrl: http://localhost:8081/formGenerator

0 comments on commit d0c537a

Please sign in to comment.