Search in sources :

Example 31 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project dhis2-core by dhis2.

the class GenericSmsGatewayTest method testSendSms_Json.

@Test
void testSendSms_Json() {
    strSubstitutor = new StringSubstitutor(valueStore);
    body = strSubstitutor.replace(CONFIG_TEMPLATE_JSON);
    gatewayConfig.getParameters().clear();
    gatewayConfig.setParameters(Arrays.asList(username, password));
    gatewayConfig.setContentType(ContentType.APPLICATION_JSON);
    gatewayConfig.setConfigurationTemplate(CONFIG_TEMPLATE_JSON);
    ResponseEntity<String> responseEntity = new ResponseEntity<>("success", HttpStatus.OK);
    when(restTemplate.exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity);
    assertThat(subject.send(SUBJECT, TEXT, RECIPIENTS, gatewayConfig).isOk(), is(true));
    verify(restTemplate).exchange(any(URI.class), httpMethodArgumentCaptor.capture(), httpEntityArgumentCaptor.capture(), eq(String.class));
    assertNotNull(httpEntityArgumentCaptor.getValue());
    assertNotNull(httpMethodArgumentCaptor.getValue());
    HttpMethod httpMethod = httpMethodArgumentCaptor.getValue();
    assertEquals(HttpMethod.POST, httpMethod);
    HttpEntity<String> requestEntity = httpEntityArgumentCaptor.getValue();
    assertEquals(body, requestEntity.getBody());
    HttpHeaders httpHeaders = requestEntity.getHeaders();
    assertTrue(httpHeaders.get("Content-type").contains(gatewayConfig.getContentType().getValue()));
    List<GenericGatewayParameter> parameters = gatewayConfig.getParameters();
    parameters.stream().filter(p -> p.isEncode() && p.isConfidential() && p.isHeader()).forEach(p -> {
        assertTrue(httpHeaders.containsKey(p.getKey()));
        assertEquals(" Basic ZGVjcnlwdGVkVGV4dA==", httpHeaders.get(p.getKey()).get(0));
    });
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SmsGateway(org.hisp.dhis.sms.config.SmsGateway) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) HashMap(java.util.HashMap) GenericGatewayParameter(org.hisp.dhis.sms.config.GenericGatewayParameter) StringUtils(org.apache.commons.lang3.StringUtils) Captor(org.mockito.Captor) StringSubstitutor(org.apache.commons.text.StringSubstitutor) GenericHttpGatewayConfig(org.hisp.dhis.sms.config.GenericHttpGatewayConfig) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) PBEStringEncryptor(org.jasypt.encryption.pbe.PBEStringEncryptor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) URI(java.net.URI) SmsUtils(org.hisp.dhis.system.util.SmsUtils) RestTemplate(org.springframework.web.client.RestTemplate) SimplisticHttpGetGateWay(org.hisp.dhis.sms.config.SimplisticHttpGetGateWay) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) Sets(com.google.common.collect.Sets) Mockito.verify(org.mockito.Mockito.verify) ContentType(org.hisp.dhis.sms.config.ContentType) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ResponseEntity(org.springframework.http.ResponseEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) GenericGatewayParameter(org.hisp.dhis.sms.config.GenericGatewayParameter) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 32 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project dhis2-core by dhis2.

the class AttributeOptionComboLoaderTest method replace.

private String replace(String sql, String... keyVal) {
    Map<String, String> vals = new HashMap<>();
    for (int i = 0; i < keyVal.length - 1; i++) {
        vals.put(keyVal[i], keyVal[i + 1]);
    }
    StrSubstitutor sub = new StrSubstitutor(vals);
    return sub.replace(sql);
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 33 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project eclipse.jdt.ls by eclipse.

the class ResourceUtils method expandPath.

/**
 * Expand paths starting with ~/ if necessary; replaces all the occurrences of
 * variables as ${variabeName} in the given string with their matching values
 * from the environment variables and system properties.
 *
 * @param path
 * @return expanded or original path
 */
public static String expandPath(String path) {
    if (path != null) {
        if (path.startsWith("~" + File.separator)) {
            path = System.getProperty("user.home") + path.substring(1);
        }
        StrLookup<String> variableResolver = new StrLookup<>() {

            @Override
            public String lookup(String key) {
                if (key.length() > 0) {
                    try {
                        String prop = System.getProperty(key);
                        if (prop != null) {
                            return prop;
                        }
                        return System.getenv(key);
                    } catch (final SecurityException scex) {
                        return null;
                    }
                }
                return null;
            }
        };
        StrSubstitutor strSubstitutor = new StrSubstitutor(variableResolver);
        return strSubstitutor.replace(path);
    }
    return path;
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) StrLookup(org.apache.commons.lang3.text.StrLookup)

Example 34 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project thingsboard by thingsboard.

the class BasicMapperUtils method getOAuth2User.

public static OAuth2User getOAuth2User(String email, Map<String, Object> attributes, OAuth2MapperConfig config) {
    OAuth2User oauth2User = new OAuth2User();
    oauth2User.setEmail(email);
    oauth2User.setTenantName(getTenantName(email, attributes, config));
    if (!StringUtils.isEmpty(config.getBasic().getLastNameAttributeKey())) {
        String lastName = getStringAttributeByKey(attributes, config.getBasic().getLastNameAttributeKey());
        oauth2User.setLastName(lastName);
    }
    if (!StringUtils.isEmpty(config.getBasic().getFirstNameAttributeKey())) {
        String firstName = getStringAttributeByKey(attributes, config.getBasic().getFirstNameAttributeKey());
        oauth2User.setFirstName(firstName);
    }
    if (!StringUtils.isEmpty(config.getBasic().getCustomerNamePattern())) {
        StrSubstitutor sub = new StrSubstitutor(attributes, START_PLACEHOLDER_PREFIX, END_PLACEHOLDER_PREFIX);
        String customerName = sub.replace(config.getBasic().getCustomerNamePattern());
        oauth2User.setCustomerName(customerName);
    }
    oauth2User.setAlwaysFullScreen(config.getBasic().isAlwaysFullScreen());
    if (!StringUtils.isEmpty(config.getBasic().getDefaultDashboardName())) {
        oauth2User.setDefaultDashboardName(config.getBasic().getDefaultDashboardName());
    }
    return oauth2User;
}
Also used : OAuth2User(org.thingsboard.server.dao.oauth2.OAuth2User) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor)

Example 35 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project dcos-commons by mesosphere.

the class YAMLConfigurationLoader method loadConfigFromEnv.

public static <T> T loadConfigFromEnv(Class<T> configurationClass, final String path) throws IOException {
    LOGGER.info("Parsing configuration file from {} ", path);
    logProcessEnv();
    final Path configPath = Paths.get(path);
    final File file = configPath.toAbsolutePath().toFile();
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    final StrSubstitutor sub = new StrSubstitutor(new StrLookup<Object>() {

        @Override
        public String lookup(String key) {
            return System.getenv(key);
        }
    });
    sub.setEnableSubstitutionInVariables(true);
    final String conf = sub.replace(FileUtils.readFileToString(file));
    return mapper.readValue(conf, configurationClass);
}
Also used : Path(java.nio.file.Path) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)32 HashMap (java.util.HashMap)16 File (java.io.File)5 Map (java.util.Map)5 IOException (java.io.IOException)4 StrLookup (org.apache.commons.lang3.text.StrLookup)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)2 MetricConfigDTO (com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO)2 Field (de.tblsoft.solr.pipeline.bean.Field)2 FileWriter (java.io.FileWriter)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 Properties (java.util.Properties)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DomainVO (com.cloud.domain.DomainVO)1 AccountVO (com.cloud.user.AccountVO)1 UserVO (com.cloud.user.UserVO)1