summaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/ApplicationBuilderTest.java
blob: 503f11ca0d70e4ba525187ac2496275730d38b8a (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
70
71
72
73
74
75
76
77
78
79
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application;

import com.yahoo.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.nio.file.Files;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class ApplicationBuilderTest {
    @Test
    void query_profile_types_can_be_added() throws Exception {
        withApplicationBuilder(builder -> {
            builder.queryProfileType("MyProfileType", "<query-profile-type id=\"MyProfileType\">" + //
                    "<field name=\"age\" type=\"integer\" />" + //
                    "<field name=\"profession\" type=\"string\" />" + //
                    "<field name=\"user\" type=\"query-profile:MyUserProfile\" />" + //
                    "</query-profile-type>");

            assertTrue(Files.exists(builder.getPath().resolve("search/query-profiles/types/MyProfileType.xml")));
        });
    }

    @Test
    void query_profile_can_be_added() throws Exception {
        withApplicationBuilder(builder -> {
            builder.queryProfile("MyProfile",
                    "<query-profile id=\"MyProfile\">" +
                            "<field name=\"message\">Hello world!</field>" +
                            "</query-profile>");

            assertTrue(Files.exists(builder.getPath().resolve("search/query-profiles/MyProfile.xml")));
        });
    }

    @Test
    void rank_expression_can_be_added() throws Exception {
        withApplicationBuilder(builder -> {
            builder.rankExpression("myExpression", "content");
            assertTrue(Files.exists(builder.getPath().resolve("schemas/myExpression.expression")));
        });
    }

    // application unreferenced inside try
    @Test
    @SuppressWarnings("try")
    void builder_cannot_be_reused() throws Exception {
        Throwable exception = assertThrows(RuntimeException.class, () -> {

            ApplicationBuilder builder = new ApplicationBuilder();
            builder.servicesXml("<container version=\"1.0\" />");
            try (Application application = builder.build()) {
                // do nothing
            }

            builder.servicesXml(""); // should fail
        });
        assertTrue(exception.getMessage().contains("build method")); // should fail
    }

    private interface TestCase {
        void accept(ApplicationBuilder ab) throws Exception;
    }

    private static void withApplicationBuilder(TestCase f) throws Exception {
        ApplicationBuilder builder = new ApplicationBuilder();
        try {
            f.accept(builder);
        } finally {
            IOUtils.recursiveDeleteDir(builder.getPath().toFile());
        }
    }
}