// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping;

import com.yahoo.vespa.config.search.AttributesConfig;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.search.Query;
import com.yahoo.search.config.ClusterConfig;
import com.yahoo.search.grouping.request.GroupingOperation;
import com.yahoo.search.searchchain.Execution;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author Simon Thoresen Hult
 */
public class GroupingValidatorTestCase {

    @Test
    public void requireThatAvailableAttributesDoNotThrow() {
        Query query = new Query();
        GroupingRequest req = GroupingRequest.newInstance(query);
        req.setRootOperation(GroupingOperation.fromString("all(group(foo) each(output(max(bar))))"));
        validateGrouping("myCluster", Arrays.asList("foo", "bar"), query);
    }

    @Test
    public void requireThatUnavailableAttributesThrow() {
        Query query = new Query();
        GroupingRequest req = GroupingRequest.newInstance(query);
        req.setRootOperation(GroupingOperation.fromString("all(group(foo) each(output(max(bar))))"));
        try {
            validateGrouping("myCluster", Arrays.asList("foo"), query);
            fail("Validator should throw exception because attribute 'bar' is unavailable.");
        } catch (UnavailableAttributeException e) {
            assertEquals("myCluster", e.getClusterName());
            assertEquals("bar", e.getAttributeName());
        }
    }

    @Test
    public void requireThatEnableFlagPreventsThrow() {
        Query query = new Query();
        GroupingRequest req = GroupingRequest.newInstance(query);
        req.setRootOperation(GroupingOperation.fromString("all(group(foo) each(output(max(bar))))"));
        query.properties().set(GroupingValidator.PARAM_ENABLED, "false");
        validateGrouping("myCluster", Arrays.asList("foo"), query);
    }

    private static void validateGrouping(String clusterName, Collection<String> attributeNames, Query query) {
        QrSearchersConfig.Builder qrsConfig = new QrSearchersConfig.Builder().searchcluster(
                new QrSearchersConfig.Searchcluster.Builder()
                        .indexingmode(QrSearchersConfig.Searchcluster.Indexingmode.Enum.REALTIME)
                        .name(clusterName));
        ClusterConfig.Builder clusterConfig = new ClusterConfig.Builder().
                clusterId(0).
                clusterName("test");
        AttributesConfig.Builder attributesConfig = new AttributesConfig.Builder();
        for (String attributeName : attributeNames) {
            attributesConfig.attribute(new AttributesConfig.Attribute.Builder()
                                            .name(attributeName));
        }
        new Execution(
                new GroupingValidator(new QrSearchersConfig(qrsConfig),
                                      new ClusterConfig(clusterConfig),
                                      new AttributesConfig(attributesConfig)),
                Execution.Context.createContextStub()).search(query);
    }
}

@@ -759,1260 +759,1311 @@
     
-Query query = new Query();%0A        GroupingRequest req = GroupingRequest.newInstance(query);%0A        req.setRootOperation(GroupingOperation.fromString(%22all(group(foo) each(output(max(bar))))%22));%0A        validateGrouping(%22myCluster%22, Arrays.asList(%22foo%22, %22bar%22), query);%0A    %7D%0A%0A    @Test%0A    public void requireThatUnavailableAttributesThrow() %7B%0A        Query query = new Query();%0A        GroupingRequest req = GroupingRequest.newInstance(query);%0A        req.setRootOperation(GroupingOperation.fromString(%22all(group(foo) each(output(max(bar))))%22));%0A        try %7B%0A            validateGrouping(%22myCluster%22, Arrays.asList(%22foo%22), query);%0A            fail(%22Validator should throw exception because attribute 'bar' is unavailable.%22);%0A        %7D catch (UnavailableAttributeException e) %7B%0A            assertEquals(%22myCluster%22, e.getClusterName());%0A            assertEquals(%22bar%22, e.getAttributeName());%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void requireThatEnableFlagPreventsThrow() %7B%0A        Query query = new Query();%0A        GroupingRequest req = GroupingRequest.newInstance(query);%0A        req.setRootOperation(GroupingOperation.fromString(%22all(group(foo) each(output(max(bar))))%22));%0A        query.properties().set(GroupingValidator.PARAM_ENABLED, %22false%22);
+validateGrouping(%22myCluster%22, Arrays.asList(%22foo%22, %22bar%22),%0A                %22all(group(foo) each(output(max(bar))))%22);;%0A    %7D%0A%0A    @Test%0A    public void requireThatUnavailableAttributesThrow() %7B%0A        try %7B%0A            validateGrouping(%22myCluster%22, Arrays.asList(%22foo%22),%0A                    %22all(group(foo) each(output(max(bar))))%22);%0A            fail(%22Validator should throw exception because attribute 'bar' is unavailable.%22);%0A        %7D catch (UnavailableAttributeException e) %7B%0A            assertEquals(%22myCluster%22, e.getClusterName());%0A            assertEquals(%22bar%22, e.getAttributeName());%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void requireThatEnableFlagPreventsThrow() %7B%0A        Query query = createQuery(%22all(group(foo) each(output(max(bar))))%22);%0A        query.properties().set(GroupingValidator.PARAM_ENABLED, %22false%22);%0A        validateGrouping(%22myCluster%22, Arrays.asList(%22foo%22), query);%0A    %7D%0A%0A    private static Query createQuery(String groupingExpression) %7B%0A        Query query = new Query();%0A        GroupingRequest req = GroupingRequest.newInstance(query);%0A        req.setRootOperation(GroupingOperation.fromString(groupingExpression));%0A        return query;%0A    %7D%0A%0A    private static void validateGrouping(String clusterName, Collection%3CString%3E attributeNames, String groupingExpression) %7B
 %0A   
@@ -2088,48 +2088,68 @@
 ing(
-%22myC
+c
 luster
-%22, Arrays.asList(%22foo%22), query
+Name, attributeNames, createQuery(groupingExpression)
 );%0A 

Simplify setup of tests.