Skip to content

Commit 7e6fa49

Browse files
committed
Providing implementation to handle new configs in DB. Required insertion script.
Something still seems off: why is full systemConfigurationValueDTO passed to update the value ? Seems a bit heavy. Had to break encapsulation within SystemConfigurationValueEjb to be handle to update config values at runtime. One optimization could be to not insert everytime BUT could break if something updates through the full-fledged update path. WARN: - Container tests do not start yet - COUNTRY_EPID_PREFIX is somewhat mandatory due to the way the tests are written.
1 parent c4f9f5a commit 7e6fa49

File tree

14 files changed

+1109
-114
lines changed

14 files changed

+1109
-114
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.symeda.sormas.backend.systemconfiguration;
2+
3+
import javax.persistence.AttributeConverter;
4+
import javax.persistence.Converter;
5+
6+
import org.apache.commons.lang3.StringUtils;
7+
8+
import de.symeda.sormas.api.systemconfiguration.Config;
9+
10+
/**
11+
* Converter was written to handle H2-database, it's fine.
12+
*/
13+
@Converter
14+
public class ConfigConverter implements AttributeConverter<Config, String> {
15+
16+
@Override
17+
public String convertToDatabaseColumn(Config attribute) {
18+
if (attribute == null) {
19+
return null;
20+
}
21+
return attribute.name(); // or attribute.toString()
22+
}
23+
24+
@Override
25+
public Config convertToEntityAttribute(String dbData) {
26+
if (StringUtils.isBlank(dbData)) {
27+
return null;
28+
}
29+
return Config.valueOf(dbData);
30+
}
31+
}

sormas-backend/src/main/java/de/symeda/sormas/backend/systemconfiguration/SystemConfigurationValue.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
package de.symeda.sormas.backend.systemconfiguration;
1717

1818
import javax.persistence.Column;
19+
import javax.persistence.Convert;
1920
import javax.persistence.Entity;
20-
import javax.persistence.EnumType;
21-
import javax.persistence.Enumerated;
2221
import javax.persistence.FetchType;
2322
import javax.persistence.ManyToOne;
2423

@@ -44,7 +43,6 @@ public class SystemConfigurationValue extends AbstractDomainObject {
4443
public static final String DESCRIPTION = "description";
4544

4645
private String value;
47-
@Enumerated(EnumType.STRING)
4846
private Config key;
4947
private String description;
5048
private SystemConfigurationCategory category;
@@ -55,7 +53,7 @@ public class SystemConfigurationValue extends AbstractDomainObject {
5553
private String validationMessage;
5654
private String defaultValue;
5755

58-
@Column(nullable = true, name = "config_value")
56+
@Column(nullable = true, name = "config_value", columnDefinition = "text")
5957
public String getValue() {
6058
return value;
6159
}
@@ -64,7 +62,8 @@ public void setValue(String value) {
6462
this.value = value;
6563
}
6664

67-
@Column(nullable = false, name = "config_key")
65+
@Column(nullable = false, name = "config_key", columnDefinition = "text")
66+
@Convert(converter = ConfigConverter.class)
6867
public Config getKey() {
6968
return key;
7069
}
@@ -73,7 +72,7 @@ public void setKey(Config key) {
7372
this.key = key;
7473
}
7574

76-
@Column(name = "value_description")
75+
@Column(name = "value_description", columnDefinition = "text")
7776
public String getDescription() {
7877
return description;
7978
}
@@ -101,7 +100,7 @@ public void setOptional(Boolean optional) {
101100
this.optional = optional;
102101
}
103102

104-
@Column(name = "value_pattern")
103+
@Column(name = "value_pattern", columnDefinition = "text")
105104
public String getPattern() {
106105
return pattern;
107106
}
@@ -120,7 +119,7 @@ public void setEncrypt(Boolean encrypt) {
120119
this.encrypt = encrypt;
121120
}
122121

123-
@Column(name = "data_provider")
122+
@Column(name = "data_provider", columnDefinition = "text")
124123
public String getDataProvider() {
125124
return dataProvider;
126125
}
@@ -129,7 +128,7 @@ public void setDataProvider(String dataProvider) {
129128
this.dataProvider = dataProvider;
130129
}
131130

132-
@Column(name = "validation_message")
131+
@Column(name = "validation_message", columnDefinition = "text")
133132
public String getValidationMessage() {
134133
return validationMessage;
135134
}
@@ -138,7 +137,7 @@ public void setValidationMessage(String validationMessage) {
138137
this.validationMessage = validationMessage;
139138
}
140139

141-
@Column(name = "default_value")
140+
@Column(name = "default_value", columnDefinition = "text")
142141
public String getDefaultValue() {
143142
return defaultValue;
144143
}

sormas-backend/src/main/java/de/symeda/sormas/backend/systemconfiguration/SystemConfigurationValueEjb.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public Optional<String> getValue(final Config key) {
141141
return systemConfigurationValueProjection.getActualValue();
142142
}
143143

144-
private void loadDataIfEmpty() {
144+
public void loadDataIfEmpty() {
145145
if (MapUtils.isEmpty(configurationValuesByKey)) {
146146
loadData();
147147
}
@@ -379,14 +379,18 @@ public void loadData() {
379379
configurationValuesByKey.clear();
380380

381381
// TODO: create a query that only fetches the mandatory fields and not everything
382-
service.getAll()
382+
getAll()
383383
.forEach(
384384
value -> configurationValuesByKey
385385
.put(value.getKey(), new SystemConfigurationValueProjection(value.getValue(), value.getDefaultValue())));
386386

387387
LOGGER.info("SystemConfiguration data loaded into cache successfully");
388388
}
389389

390+
public List<SystemConfigurationValue> getAll() {
391+
return service.getAll();
392+
}
393+
390394
/**
391395
* Converts a {@link SystemConfigurationValueDto} data transfer object to a {@link SystemConfigurationValue} entity.
392396
* If a target entity is provided, it will be filled with the data from the DTO; otherwise, a new entity will be created.
@@ -559,4 +563,7 @@ private SystemConfigurationValueIndexDto toIndexDto(final SystemConfigurationVal
559563
return dto;
560564
}
561565

566+
public ConcurrentHashMap<Config, SystemConfigurationValueProjection> getConfigurationValuesByKey() {
567+
return configurationValuesByKey;
568+
}
562569
}

sormas-backend/src/main/java/de/symeda/sormas/backend/systemconfiguration/SystemConfigurationValueProjection.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import java.util.Optional;
44

5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import net.minidev.json.annotate.JsonIgnore;
8+
59
public class SystemConfigurationValueProjection {
610

711
private String value;
@@ -15,7 +19,17 @@ public SystemConfigurationValueProjection(String value, String defaultValue) {
1519
this.defaultValue = defaultValue;
1620
}
1721

22+
public String getDefaultValue() {
23+
return defaultValue;
24+
}
25+
26+
@JsonIgnore
1827
public Optional<String> getActualValue() {
1928
return Optional.ofNullable(value).or(() -> Optional.ofNullable(defaultValue));
2029
}
30+
31+
@JsonProperty("actualValue")
32+
String getActualValueAsString() {
33+
return getActualValue().orElse(null);
34+
}
2135
}

sormas-backend/src/main/java/de/symeda/sormas/backend/user/KeycloakService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class KeycloakService {
9595
@PostConstruct
9696
public void init() {
9797

98+
// TODO: this is the call that fails within test
9899
if (!AuthProvider.KEYCLOAK.equalsIgnoreCase(configFacade.getAsStringOrThrow(Config.AUTHENTICATION_PROVIDER))) {
99100
logger.info("Keycloak Auth Provider not active");
100101
return;

sormas-backend/src/main/resources/sql/sormas_schema.sql

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15293,14 +15293,6 @@ $$
1529315293
'i18n/systemConfigurationValueValidation.NEGATIVE_COVID_TESTS_MAX_AGE_DAYS', now(), now(), nextval('entity_seq'),
1529415294
generate_base32_uuid(), 'i18n/systemConfigurationValueDescription.NEGATIVE_COVID_TESTS_MAX_AGE_DAYS', '90') ON CONFLICT DO NOTHING;
1529515295

15296-
INSERT INTO systemconfigurationvalue(config_key, config_value, category_id, value_optional, value_pattern,
15297-
value_encrypt, data_provider, validation_message, changedate, creationdate, id,
15298-
uuid, value_description, default_value)
15299-
VALUES ('CASE_CLASSIFICATION_CALCULATION_MODE_OVERRIDE_CHOLERA', 'DISABLED', general_configuration_id, true,
15300-
null, false, null,
15301-
'i18n/systemConfigurationValueValidation.CASE_CLASSIFICATION_CALCULATION_MODE_OVERRIDE_CHOLERA', now(), now(), nextval('entity_seq'),
15302-
generate_base32_uuid(), 'i18n/systemConfigurationValueDescription.CASE_CLASSIFICATION_CALCULATION_MODE_OVERRIDE_CHOLERA', 'DISABLED') ON CONFLICT DO NOTHING;
15303-
1530415296
INSERT INTO systemconfigurationvalue(config_key, config_value, category_id, value_optional, value_pattern,
1530515297
value_encrypt, data_provider, validation_message, changedate, creationdate, id,
1530615298
uuid, value_description, default_value)

sormas-backend/src/test/java/de/symeda/sormas/backend/AbstractBeanTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.mockito.ArgumentMatchers.any;
2020
import static org.mockito.Mockito.when;
2121

22+
import java.io.IOException;
23+
import java.io.StringWriter;
2224
import java.lang.annotation.Annotation;
2325
import java.util.Arrays;
2426
import java.util.HashSet;
@@ -34,6 +36,7 @@
3436
import javax.persistence.EntityManager;
3537
import javax.persistence.Query;
3638

39+
import org.apache.commons.io.IOUtils;
3740
import org.junit.jupiter.api.AfterEach;
3841
import org.junit.jupiter.api.BeforeAll;
3942
import org.junit.jupiter.api.BeforeEach;
@@ -265,6 +268,7 @@
265268
import de.symeda.sormas.backend.survey.SurveyTokenFacadeEjb.SurveyTokenFacadeEjbLocal;
266269
import de.symeda.sormas.backend.survey.SurveyTokenService;
267270
import de.symeda.sormas.backend.symptoms.SymptomsService;
271+
import de.symeda.sormas.backend.systemconfiguration.ConfigFacadeEjb;
268272
import de.symeda.sormas.backend.systemconfiguration.ExternalClientConfigurationEjb;
269273
import de.symeda.sormas.backend.systemconfiguration.SystemConfigurationCategoryEjb;
270274
import de.symeda.sormas.backend.systemconfiguration.SystemConfigurationCategoryService;
@@ -349,6 +353,21 @@ public void init() {
349353
I18nProperties.setUserLanguage(Language.EN);
350354

351355
createDiseaseConfigurations();
356+
357+
createSystemConfigurations();
358+
}
359+
360+
private void createSystemConfigurations() {
361+
StringWriter writer = new StringWriter();
362+
try {
363+
IOUtils.copy(getClass().getResourceAsStream("/sql/systemconfigurationvalue-inserts.sql"), writer, "UTF-8");
364+
} catch (IOException e) {
365+
throw new RuntimeException(e);
366+
}
367+
368+
executeInTransaction(em -> {
369+
getEntityManager().createNativeQuery(writer.toString()).executeUpdate();
370+
});
352371
}
353372

354373
protected void initH2Functions() {
@@ -468,6 +487,10 @@ protected void createFeatureConfiguration(FeatureType featureType, boolean enabl
468487
getFeatureConfigurationFacade().saveFeatureConfiguration(featureConfiguration, featureType);
469488
if (properties != null) {
470489
executeInTransaction(em -> {
490+
491+
System.out.println("NNNNNNNNNNNNNNNNNNNNNNNNN");
492+
System.out.println(em.createNativeQuery("select featuretype from featureconfiguration").getResultList());
493+
471494
Query query = em.createQuery("select f from featureconfiguration f where featureType = '" + featureType.name() + "'");
472495
FeatureConfiguration singleResult = (FeatureConfiguration) query.getSingleResult();
473496
singleResult.setProperties(properties);
@@ -477,7 +500,7 @@ protected void createFeatureConfiguration(FeatureType featureType, boolean enabl
477500
}
478501

479502
public ConfigFacade getConfigFacade() {
480-
return getBean(ConfigFacadeEjbLocalMock.class);
503+
return getBean(ConfigFacadeEjb.class);
481504
}
482505

483506
public StartupConfigurationValidationService getStartupConfigurationValidationService() {

sormas-backend/src/test/java/de/symeda/sormas/backend/ConfigFacadeEjbLocalMock.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

sormas-backend/src/test/java/de/symeda/sormas/backend/MockProducer.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class MockProducer implements InitialContextFactory {
8585

8686
private static SormasToSormasDiscoveryService sormasToSormasDiscoveryService = mock(SormasToSormasDiscoveryService.class);
8787

88-
static final TestHelperConfigImpl testHelperConfig = ConfigFacadeEjbLocalMock.testHelperConfig;
88+
static final TestHelperConfigImpl testHelperConfig = TestHelperConfigImpl.getInstance();
8989

9090
// Receiving e-mail server is mocked: org. jvnet. mock_javamail. mailbox
9191
private static Session mailSession;
@@ -129,27 +129,9 @@ public static void resetMocks() {
129129
s2sRestClient,
130130
managedScheduledExecutorService);
131131
wireMocks();
132-
resetConfigs();
133132
requestContextTO.setMobileSync(false);
134133
}
135134

136-
private static void resetConfigs() {
137-
testHelperConfig.clear();
138-
testHelperConfig.set(Config.COUNTRY_NAME, "nigeria");
139-
testHelperConfig.set(Config.COUNTRY_LOCALE, "en");
140-
testHelperConfig.set(Config.CSV_SEPARATOR, ";");
141-
testHelperConfig.set(Config.STEP_SIZE_FOR_CSV_EXPORT, "5000");
142-
143-
testHelperConfig.set(Config.TEMP_PATH, TMP_PATH);
144-
145-
testHelperConfig.set(Config.DOCUMENTS_PATH, TMP_PATH + "/documents");
146-
147-
testHelperConfig.set(Config.DEFAULT_CASE_CLASSIFICATION_CALCULATION_MODE, "AUTOMATIC");
148-
testHelperConfig.set(Config.CASE_CLASSIFICATION_CALCULATION_MODE_OVERRIDE, "{}");
149-
testHelperConfig.set(Config.NAME_SIMILARITY_THRESHOLD, "0.65D");
150-
151-
}
152-
153135
public static void wireMocks() {
154136

155137
when(sessionContext.getCallerPrincipal()).thenReturn(getPrincipal());

sormas-backend/src/test/java/de/symeda/sormas/backend/TestConfigFacade.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ default String getProperty(Config config) {
2222
}
2323

2424
void remove(Config config);
25+
2526
}

0 commit comments

Comments
 (0)