Skip to content

Commit

Permalink
Add utility to resolve placeholders.
Browse files Browse the repository at this point in the history
  • Loading branch information
msqr committed Oct 16, 2024
1 parent 0ee8b25 commit aa06bd3
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@

package net.solarnetwork.central.c2c.domain;

import java.util.Map;
import net.solarnetwork.central.dao.UserRelatedStdEntity;
import net.solarnetwork.central.domain.UserRelatedCompositeKey;
import net.solarnetwork.service.ServiceConfiguration;
import net.solarnetwork.util.StringUtils;

/**
* API for cloud integration configuration entities.
Expand All @@ -33,11 +36,20 @@
* @param <K>
* the key type
* @author matt
* @version 1.0
* @version 1.1
*/
public interface CloudIntegrationsConfigurationEntity<C extends CloudIntegrationsConfigurationEntity<C, K>, K extends UserRelatedCompositeKey<K>>
extends UserRelatedStdEntity<C, K> {

/**
* A service property key for a map of named placeholder values, that can be
* used to resolve placeholder names, for example in source IDs or value
* references.
*
* @since 1.1
*/
String PLACEHOLDERS_SERVICE_PROPERTY = "placeholders";

/**
* Test if this configuration is "fully" configured.
*
Expand All @@ -51,4 +63,32 @@ public interface CloudIntegrationsConfigurationEntity<C extends CloudIntegration
*/
boolean isFullyConfigured();

/**
* Resolve placeholder values on a template string.
*
* <p>
* The template uses placeholder value in the form {@code {X}} where
* {@code X} is a placeholder name.
* </p>
*
* @param template
* the template string to resolve placeholder values on
* @param configuration
* the service configuration to obtain placeholder values from, on a
* {@link #PLACEHOLDERS_SERVICE_PROPERTY} service property
* @return the {@code template} with all placeholder values resolved, or
* {@literal null} if {@code template} is {@code null}
* @see StringUtils#expandTemplateString(String, Map)
* @since 1.1
*/
static String resolvePlaceholders(String template, ServiceConfiguration configuration) {
if ( template == null || template.isEmpty() ) {
return template;
}
@SuppressWarnings("unchecked")
Map<String, ?> placeholders = configuration.serviceProperty(PLACEHOLDERS_SERVICE_PROPERTY,
Map.class);
return StringUtils.expandTemplateString(template, placeholders);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* ==================================================================
* CloudIntegrationsConfigurationEntityTests.java - 17/10/2024 8:52:51 am
*
* Copyright 2024 SolarNetwork.net Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ==================================================================
*/

package net.solarnetwork.central.c2c.domain.test;

import static net.solarnetwork.central.c2c.domain.CloudIntegrationsConfigurationEntity.PLACEHOLDERS_SERVICE_PROPERTY;
import static org.assertj.core.api.BDDAssertions.then;
import java.util.Map;
import org.junit.jupiter.api.Test;
import net.solarnetwork.central.c2c.domain.CloudIntegrationsConfigurationEntity;
import net.solarnetwork.domain.BasicIdentifiableConfiguration;

/**
* Test cases for the {@link CloudIntegrationsConfigurationEntity} class.
*
* @author matt
* @version 1.0
*/
public class CloudIntegrationsConfigurationEntityTests {

@Test
public void resolvePlaceholders() {
// GIVEN
// @formatter:off
var conf = new BasicIdentifiableConfiguration(null, null, Map.of(PLACEHOLDERS_SERVICE_PROPERTY, Map.of(
"a", "eh",
"b", "bee",
"c", 3
)));
// @formatter:on

// WHEN
String result = CloudIntegrationsConfigurationEntity
.resolvePlaceholders("/{a}/{b}/{c}/{d:egads}", conf);

// THEN
// @formatter:off
then(result)
.as("Placeholders resolved")
.isEqualTo("/eh/bee/3/egads")
;
// @formatter:on
}

@Test
public void resolvePlaceholders_missing() {
// GIVEN
var conf = new BasicIdentifiableConfiguration(null, null, Map.of("no", "placeholders"));

// WHEN
String result = CloudIntegrationsConfigurationEntity.resolvePlaceholders("/{a:sad}", conf);

// THEN
// @formatter:off
then(result)
.as("Placeholders resolved even when the placeholders map is missing")
.isEqualTo("/sad")
;
// @formatter:on
}

@Test
public void resolvePlaceholders_notAMap() {
// GIVEN
var conf = new BasicIdentifiableConfiguration(null, null,
Map.of(PLACEHOLDERS_SERVICE_PROPERTY, "oops"));

// WHEN
String result = CloudIntegrationsConfigurationEntity.resolvePlaceholders("/{a:sad}", conf);

// THEN
// @formatter:off
then(result)
.as("Placeholders resolved even when the placeholders service property is not a map")
.isEqualTo("/sad")
;
// @formatter:on
}

}

0 comments on commit aa06bd3

Please sign in to comment.