package com.yahoo.searchdefinition;

import com.yahoo.tensor.TensorType;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

/**
 * @author gjoranv
 */
public class RankingConstantTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void tensor_constant_properties_are_set() throws Exception {
        final String TENSOR_NAME = "my_global_tensor";
        final String TENSOR_FILE = "path/my-tensor-file.json";
        final String TENSOR_TYPE = "tensor(x{})";
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder searchBuilder = new SearchBuilder(rankProfileRegistry);
        searchBuilder.importString(joinLines(
                "search test {",
                "  document test { }",
                "  rank-profile my_rank_profile {",
                "    first-phase {",
                "      expression: sum(constant(my_global_tensor))",
                "    }",
                "  }",
                "  constant " + TENSOR_NAME + " {",
                "    file: " + TENSOR_FILE,
                "    type: " + TENSOR_TYPE,
                "  }",
                "}"
        ));
        searchBuilder.build();
        Search s = searchBuilder.getSearch();
        RankingConstant constant = s.getRankingConstants().iterator().next();
        assertEquals(TENSOR_NAME, constant.getName());
        assertEquals(TENSOR_FILE, constant.getFileName());
        assertEquals(TENSOR_TYPE, constant.getType());
    }

    @Test
    public void tensor_constant_must_have_a_type() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder searchBuilder = new SearchBuilder(rankProfileRegistry);
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("must have a type");
        searchBuilder.importString(joinLines(
                "search test {",
                "  document test { }",
                "  constant foo {",
                "    file: bar.baz",
                "  }",
                "}"
        ));
    }

    @Test
    public void tensor_constant_must_have_a_file() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder searchBuilder = new SearchBuilder(rankProfileRegistry);
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("must have a file");
        searchBuilder.importString(joinLines(
                "search test {",
                "  document test { }",
                "  constant foo {",
                "    type: tensor(x[])",
                "  }",
                "}"
        ));
    }

    @Test
    public void constant_file_does_not_need_path_or_ending() throws Exception {
        RankProfileRegistry rankProfileRegistry = new RankProfileRegistry();
        SearchBuilder searchBuilder = new SearchBuilder(rankProfileRegistry);
        searchBuilder.importString(joinLines(
                "search test {",
                "  document test { }",
                "  constant foo {",
                "    type: tensor(x{})",
                "    file: simplename",
                "  }",
                "}"
        ));
        searchBuilder.build();
        Search s = searchBuilder.getSearch();
        RankingConstant constant = s.getRankingConstants().iterator().next();
        assertEquals("simplename", constant.getFileName());
    }

    private static String joinLines(String... lines) {
        return String.join("\n", lines);
    }
}

@@ -34,44 +34,8 @@
 n;%0A%0A
-import com.yahoo.tensor.TensorType;%0A
 impo
@@ -53,16 +53,16 @@
 t.Rule;%0A
+
 import o
@@ -115,24 +115,52 @@
 Exception;%0A%0A
+import java.util.Iterator;%0A%0A
 import stati
@@ -191,16 +191,60 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertFalse;
 %0A%0A/**%0A *
@@ -1372,32 +1372,33 @@
 etSearch();%0A
+%0A
         
 RankingConst
@@ -1377,32 +1377,41 @@
 rch();%0A%0A        
+Iterator%3C
 RankingConstant 
@@ -1401,32 +1401,33 @@
 %3CRankingConstant
+%3E
  constant = s.ge
@@ -1411,32 +1411,40 @@
 nstant%3E constant
+Iterator
  = s.getRankingC
@@ -1456,32 +1456,85 @@
 nts().iterator()
+;%0A        RankingConstant constant = constantIterator
 .next();%0A       
@@ -1632,32 +1632,32 @@
 getFileName());%0A
-
         assertEq
@@ -1690,24 +1690,74 @@
 .getType());
+%0A%0A        assertFalse(constantIterator.hasNext());
 %0A    %7D%0A%0A    

Check that there is only one constant.