summaryrefslogtreecommitdiffstats
path: root/vespa-documentgen-plugin/src/test/java/com/yahoo/vespa/DocumentGenTest.java
blob: 72a3209a8fdf0bb7c6a93d6e31ea44ff3497b630 (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
69
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa;

import com.yahoo.document.DataType;
import com.yahoo.document.StructDataType;
import com.yahoo.document.WeightedSetDataType;
import com.yahoo.searchdefinition.Search;
import org.junit.Test;

import java.io.File;
import java.util.Map;

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

public class DocumentGenTest {
    private static final File schemasDir = new File("src/test/resources/schemas/");
    private static final File musicSchemaDir = new File(schemasDir, "music/");
    private static final File complexSchemaDir = new File(schemasDir, "complex/");
    private static final File localappSchemaDir = new File(schemasDir, "localapp/");

    @Test
    public void testMusic() {
        DocumentGenMojo mojo = new DocumentGenMojo();
        mojo.execute(musicSchemaDir, new File("target/generated-test-sources/vespa-documentgen-plugin/"), "com.yahoo.vespa.document");
        Map<String, Search> searches = mojo.getSearches();
        assertEquals(searches.size(), 1);
        assertEquals(searches.get("music").getDocument("music").getField("title").getDataType(), DataType.STRING);
        assertEquals(searches.get("music").getDocument("music").getField("eitheror").getDataType(), DataType.BOOL);
    }

    @Test
    public void testComplex() {
        DocumentGenMojo mojo = new DocumentGenMojo();
        mojo.execute(complexSchemaDir, new File("target/generated-test-sources/vespa-documentgen-plugin/"), "com.yahoo.vespa.document");
        Map<String, Search> searches = mojo.getSearches();
        assertEquals(searches.get("video").getDocument("video").getField("weight").getDataType(), DataType.FLOAT);
        assertEquals(searches.get("book").getDocument("book").getField("sw1").getDataType(), DataType.FLOAT);
        assertTrue(searches.get("music3").getDocument("music3").getField("pos").getDataType() instanceof StructDataType);
        assertEquals(searches.get("music3").getDocument("music3").getField("pos").getDataType().getName(), "position");
        assertTrue(searches.get("book").getDocument("book").getField("mystruct").getDataType() instanceof StructDataType);
        assertTrue(searches.get("book").getDocument("book").getField("mywsfloat").getDataType() instanceof WeightedSetDataType);
        assertTrue(((WeightedSetDataType)(searches.get("book").getDocument("book").getField("mywsfloat").getDataType())).getNestedType() == DataType.FLOAT);
    }

    @Test
    public void testLocalApp() {
        DocumentGenMojo mojo = new DocumentGenMojo();
        mojo.execute(localappSchemaDir, new File("target/generated-test-sources/vespa-documentgen-plugin/"), "com.yahoo.vespa.document");
        Map<String, Search> searches = mojo.getSearches();
        assertEquals(searches.get("video").getDocument("video").getField("weight").getDataType(), DataType.FLOAT);
        assertTrue(searches.get("book").getDocument("book").getField("mystruct").getDataType() instanceof StructDataType);
        assertTrue(searches.get("book").getDocument("book").getField("mywsfloat").getDataType() instanceof WeightedSetDataType);
        assertTrue(((WeightedSetDataType)(searches.get("book").getDocument("book").getField("mywsfloat").getDataType())).getNestedType() == DataType.FLOAT);
    }

    @Test
    public void testEmptyPkgNameForbidden() {
        DocumentGenMojo mojo = new DocumentGenMojo();
        try {
            mojo.execute(new File("etc/localapp/"), new File("target/generated-test-sources/vespa-documentgen-plugin/"), "");
            fail("Didn't throw in empty pkg");
        } catch (IllegalArgumentException e) {

        }
    }

}