summaryrefslogtreecommitdiffstats
path: root/application/src/test/java/com/yahoo/application/ApplicationBuilderTest.java
blob: 94c3e74292347f03874af02f030f38e85eb1de8c (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.nio.file.Files;

import org.junit.Test;

import com.yahoo.io.IOUtils;

/**
 * @author tonytv
 */
public class ApplicationBuilderTest {
	@Test
	public 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
	public 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
	public void rank_expression_can_be_added() throws Exception {
		withApplicationBuilder(builder -> {
			builder.rankExpression("myExpression", "content");
			assertTrue(Files.exists(builder.getPath().resolve("searchdefinitions/myExpression.expression")));
		});
	}

	@Test
	public void builder_cannot_be_reused() throws Exception {
		try {
			ApplicationBuilder builder = new ApplicationBuilder();
			builder.servicesXml("<jdisc version=\"1.0\" />");
			builder.build();

			builder.servicesXml("");
			fail("Expected exception.");
		} catch (RuntimeException e) {
			assertThat(e.getMessage(), containsString("build method"));
		}

	}

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

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