/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.presto.tests;

import com.google.common.collect.ImmutableList;

import java.util.List;

import static java.util.Objects.requireNonNull;

public class QueryTemplate
{
    private final String queryTemplate;
    private final List<Parameter> defaultParameters;

    public QueryTemplate(String queryTemplate, Parameter... parameters)
    {
        this.queryTemplate = queryTemplate;
        this.defaultParameters = ImmutableList.copyOf(parameters);
    }

    public String resolve(Parameter ... parameters)
    {
        String query = queryTemplate;
        for (Parameter parameter : parameters) {
            query = resolve(query, parameter.getKey(), parameter.getValue());
        }
        for (Parameter parameter : defaultParameters) {
            query = resolve(query, parameter.getKey(), parameter.getValue());
        }

        return query;
    }

    private String resolve(String query, String key, String value)
    {
        return query.replaceAll("%" + key + "%", value);
    }

    public static final class Parameter
    {
        private final String key;
        private final String value;

        public Parameter(String key, String value)
        {
            this.key = requireNonNull(key, "key is null");
            this.value = requireNonNull(value, "defaultValue is null");
        }

        public String getValue()
        {
            return value;
        }

        public String getKey()
        {
            return key;
        }

        public Parameter of(String value)
        {
            return new Parameter(key, value);
        }
    }
}

@@ -661,24 +661,90 @@
 util.List;%0A%0A
+import static com.google.common.base.Preconditions.checkArgument;%0A
 import stati
@@ -779,16 +779,16 @@
 onNull;%0A
-
 %0Apublic 
@@ -973,32 +973,341 @@
 rameters)%0A    %7B%0A
+        for (Parameter parameter : parameters) %7B%0A            String queryParameterKey = asQueryParameterKey(parameter.getKey());%0A            checkArgument(%0A                    queryTemplate.contains(queryParameterKey),%0A                    %22Query template does not contain: %25s%22, queryParameterKey);%0A        %7D%0A%0A
         this.que
@@ -1443,17 +1443,16 @@
 arameter
- 
 ... para
@@ -1919,16 +1919,129 @@
 laceAll(
+asQueryParameterKey(key), value);%0A    %7D%0A%0A    private String asQueryParameterKey(String key)%0A    %7B%0A        return 
 %22%25%22 + ke
@@ -2047,24 +2047,16 @@
 ey + %22%25%22
-, value)
 ;%0A    %7D%0A

Check if QueryTemplate.Parameter is used in template