string replace test

This commit is contained in:
Francesco Mangiacrapa 2024-03-21 15:54:09 +01:00
parent 01bad9d30f
commit 122fd7c882
1 changed files with 28 additions and 22 deletions

View File

@ -1,6 +1,9 @@
import java.io.BufferedInputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -8,6 +11,7 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.text.StringSubstitutor; import org.apache.commons.text.StringSubstitutor;
import org.gcube.application.cms.notifications.substitutor.SubstitutorMessagesMap;
import org.gcube.application.geoportal.common.utils.tests.GCubeTest; import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
import org.junit.Test; import org.junit.Test;
@ -24,39 +28,41 @@ public class StringReplacerTest {
"MSG_ITEM_REJECTED_REVIEW_REQUIRED", "MSG_ITEM_PUBLISHED"); "MSG_ITEM_REJECTED_REVIEW_REQUIRED", "MSG_ITEM_PUBLISHED");
public static String USERNAME = "francesco.mangiacrapa"; public static String USERNAME = "francesco.mangiacrapa";
public static String PROJECT_ID = "12345";
public static String PROJECT_NAME = "\"My Great Project\""; public static String PROJECT_NAME = "\"My Great Project\"";
public static String GIS_LINK = "https://my_gis_link"; public static String GIS_LINK = "https://my_gis_link";
public static String FILE_URL = "https://code-repo.d4science.org/gCubeSystem/gcube-cms-suite/raw/branch/event_manager/D4S_UCDs/DEV/devVRE/notifications/Notifications_Messages.properties";
@Test @Test
public void checkReplace() { public void checkReplace() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
if (GCubeTest.isTestInfrastructureEnabled()) { Map<String, String> valuesMap = new HashMap<>();
valuesMap.put(SubstitutorMessagesMap.USER, USERNAME);
valuesMap.put(SubstitutorMessagesMap.PROJECT_NAME, PROJECT_NAME);
valuesMap.put(SubstitutorMessagesMap.GIS_LINK, GIS_LINK);
valuesMap.put(SubstitutorMessagesMap.PROJECT_ID, PROJECT_ID);
// Build StringSubstitutor
StringSubstitutor sub = new StringSubstitutor(valuesMap);
Map<String, String> valuesMap = new HashMap<>(); try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream())) {
valuesMap.put("user", USERNAME);
valuesMap.put("project_name", PROJECT_NAME);
valuesMap.put("gis_link", GIS_LINK);
// Build StringSubstitutor
StringSubstitutor sub = new StringSubstitutor(valuesMap);
try (InputStream input = new FileInputStream(NOTIFICATION_MESSAGE_FILE_PATH)) { Properties prop = new Properties();
Properties prop = new Properties(); // load a properties file
// load a properties file prop.load(in);
prop.load(input);
for (String placeholder : PLACEHOLDERS) { for (String placeholder : PLACEHOLDERS) {
log.info("placeholder: {}", placeholder); log.info("placeholder: {}", placeholder);
String templateString = prop.getProperty(placeholder); String templateString = prop.getProperty(placeholder);
log.info("templateString: {}", templateString); log.info("templateString: {}", templateString);
// Replace // Replace
String resolvedString = sub.replace(templateString); String resolvedString = sub.replace(templateString);
log.info("resolvedString: {}", resolvedString); log.info("resolvedString: {}", resolvedString);
}
} catch (IOException ex) {
ex.printStackTrace();
} }
} catch (IOException e) {
e.printStackTrace();
} }
} }