summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/pagetemplates/engine/test/ChoiceOfSubsectionsTestCase.java
blob: c03322d63cfa11a1a854007b31de29a466c7c659 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.pagetemplates.engine.test;

import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.pagetemplates.engine.Organizer;
import com.yahoo.search.pagetemplates.engine.Resolution;
import com.yahoo.search.pagetemplates.engine.resolvers.DeterministicResolver;
import com.yahoo.search.pagetemplates.model.Choice;
import com.yahoo.search.result.HitGroup;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class ChoiceOfSubsectionsTestCase extends ExecutionAbstractTestCase {

    @Test
    public void testExecution() {
        // Create the page template
        Choice page=Choice.createSingleton(importPage("ChoiceOfSubsections.xml"));

        // Create a federated result
        Query query=new Query();
        Result result=new Result(query);
        result.hits().add(createHits("source1",3));
        result.hits().add(createHits("source2",3));
        result.hits().add(createHits("source3",3));
        result.hits().add(createHits("source4",3));

        new Organizer().organize(page,new DeterministicResolverAssertingMethod().resolve(page,query,result),result);

        // Check execution:
        // Two subsections with one source each
        assertEquals(2,result.hits().size());
        HitGroup section1=(HitGroup)result.hits().get(0);
        HitGroup section2=(HitGroup)result.hits().get(1);
        assertEqualHitGroups(createHits("source2",3),section1);
        assertEqualHitGroups(createHits("source4",3),section2);

        // Check rendering
        assertRendered(result,"ChoiceOfSubsectionsResult.xml");
    }

    /** Same as deterministic resolver, but asserts that it received the correct method names for each choice */
    private static class DeterministicResolverAssertingMethod extends DeterministicResolver {

        private int invocationNumber=0;

        /** Chooses the last alternative of any choice */
        @Override
        public void resolve(Choice choice, Query query, Result result, Resolution resolution) {
            invocationNumber++;
            if (invocationNumber==2)
                assertEquals("method1",choice.getMethod());
            else if (invocationNumber==3)
                assertEquals("method2",choice.getMethod());
            else if (invocationNumber>3)
                throw new IllegalStateException("Unexpected number of resolver invocations");

            super.resolve(choice,query,result,resolution);
        }

    }

}