2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.dbquery.internal.dbimpl;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.equalTo;
18 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.dbquery.internal.domain.QueryParameters;
28 * @author Joan Pujol - Initial contribution
31 public class StringSubstitutionParamsParserTest {
34 public void testMultipleParameters() {
35 String query = "from(bucket:\\\"my-bucket\\\") |> range(start: ${start}) |> fill( value: ${fillValue})";
36 var parser = new StringSubstitutionParamsParser(query);
37 QueryParameters parameters = new QueryParameters(Map.of("start", "0", "fillValue", "1"));
39 var result = parser.getQueryWithParametersReplaced(parameters);
41 assertThat(result, equalTo("from(bucket:\\\"my-bucket\\\") |> range(start: 0) |> fill( value: 1)"));
45 public void testRepeatedParameter() {
46 String query = "from(bucket:\\\"my-bucket\\\") |> range(start: ${start}) |> limit(n:${start})";
47 var parser = new StringSubstitutionParamsParser(query);
48 QueryParameters parameters = new QueryParameters(Map.of("start", "0"));
50 var result = parser.getQueryWithParametersReplaced(parameters);
52 assertThat(result, equalTo("from(bucket:\\\"my-bucket\\\") |> range(start: 0) |> limit(n:0)"));
56 public void testNullAndNotDefinedParametersAreSubstitutedByEmptyString() {
57 String query = "from(bucket:\\\"my-bucket\\\") |> range(start: ${start}) |> limit(n:${start})";
58 var parser = new StringSubstitutionParamsParser(query);
59 var paramMap = new HashMap<String, @Nullable Object>();
60 paramMap.put("start", null);
61 QueryParameters parameters = new QueryParameters(paramMap);
63 var result = parser.getQueryWithParametersReplaced(parameters);
65 assertThat(result, equalTo("from(bucket:\\\"my-bucket\\\") |> range(start: ) |> limit(n:)"));