View Javadoc
1   package org.wikimedia.search.extra.analysis.textify;
2   
3   import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
4   import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
5   
6   import java.io.IOException;
7   import java.util.Arrays;
8   import java.util.Collection;
9   import java.util.Collections;
10  import java.util.concurrent.ExecutionException;
11  
12  import org.elasticsearch.action.search.SearchRequestBuilder;
13  import org.elasticsearch.common.settings.SettingsException;
14  import org.elasticsearch.common.xcontent.XContentBuilder;
15  import org.elasticsearch.plugins.Plugin;
16  import org.elasticsearch.test.ESIntegTestCase;
17  import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
18  import org.junit.Test;
19  
20  @ClusterScope(scope = ESIntegTestCase.Scope.SUITE, transportClientRatio = 0.0)
21  public class ICUTokenRepairFailingIntegrationTest extends ESIntegTestCase {
22      SearchRequestBuilder srchReqBldr;
23  
24      @Test(expected = IllegalArgumentException.class)
25      public void testBadConfigMaxLenTooLow() throws IOException, InterruptedException,
26              ExecutionException {
27          XContentBuilder settings = jsonBuilder()
28                  .startObject()
29                      .field("number_of_shards", 1)
30                      .startObject("analysis")
31                          .startObject("filter")
32                              .startObject("broken_icutokrep")
33                                  .field("type", "icu_token_repair")
34                                  .field(ICUTokenRepairFilterFactory.MAX_TOK_LEN_KEY,
35                                      ICUTokenRepairFilterConfig.MIN_MAX_TOK_LEN - 1)
36                              .endObject()
37                          .endObject()
38                      .endObject()
39                  .endObject();
40  
41          assertAcked(prepareCreate("my_index").setSettings(settings));
42      }
43  
44      @Test(expected = IllegalArgumentException.class)
45      public void testBadConfigMaxLenTooBig() throws IOException, InterruptedException,
46              ExecutionException {
47          XContentBuilder settings = jsonBuilder()
48                  .startObject()
49                      .field("number_of_shards", 1)
50                      .startObject("analysis")
51                          .startObject("filter")
52                              .startObject("broken_icutokrep")
53                                  .field("type", "icu_token_repair")
54                                  .field(ICUTokenRepairFilterFactory.MAX_TOK_LEN_KEY,
55                                      ICUTokenRepairFilterConfig.MAX_MAX_TOK_LEN + 1)
56                              .endObject()
57                          .endObject()
58                      .endObject()
59                  .endObject();
60  
61          assertAcked(prepareCreate("my_index").setSettings(settings));
62      }
63  
64      @Test(expected = IllegalArgumentException.class)
65      public void testBadConfigCamelNumIncompatible() throws IOException, InterruptedException,
66              ExecutionException {
67          XContentBuilder settings = jsonBuilder()
68                  .startObject()
69                      .field("number_of_shards", 1)
70                      .startObject("analysis")
71                          .startObject("filter")
72                              .startObject("broken_icutokrep")
73                                  .field("type", "icu_token_repair")
74                                  .field(ICUTokenRepairFilterFactory.KEEP_CAMEL_KEY, false)
75                                  .field(ICUTokenRepairFilterFactory.NUM_ONLY_KEY, true)
76                                  // only incompatible combo
77                              .endObject()
78                          .endObject()
79                          // check for incompatible config is at instantiation time, so we
80                          // need to specify an analyzer to triger this error
81                          .startObject("analyzer")
82                              .startObject("broken_analyzer")
83                                  .field("tokenizer", "textify_icu_tokenizer")
84                                  .array("filter", "broken_icutokrep")
85                              .endObject()
86                          .endObject()
87                      .endObject()
88                  .endObject();
89  
90          assertAcked(prepareCreate("my_index").setSettings(settings));
91      }
92  
93      @Test(expected = SettingsException.class)
94      public void testBadConfigUnkownTokenType() throws IOException, InterruptedException,
95              ExecutionException {
96          XContentBuilder settings = jsonBuilder()
97                  .startObject()
98                      .field("number_of_shards", 1)
99                      .startObject("analysis")
100                         .startObject("filter")
101                             .startObject("broken_icutokrep")
102                                 .field("type", "icu_token_repair")
103                                 .array(ICUTokenRepairFilterFactory.DENY_TYPES_KEY, "<NUMBER>")
104                                 // should be <NUM>, not <NUMBER>
105                             .endObject()
106                         .endObject()
107                     .endObject()
108                 .endObject();
109 
110         assertAcked(prepareCreate("my_index").setSettings(settings));
111     }
112 
113     @Test(expected = SettingsException.class)
114     public void testBadConfigUnknownPreset() throws IOException, InterruptedException,
115             ExecutionException {
116         XContentBuilder settings = jsonBuilder()
117                 .startObject()
118                     .field("number_of_shards", 1)
119                     .startObject("analysis")
120                         .startObject("filter")
121                             .startObject("broken_icutokrep")
122                                 .field("type", "icu_token_repair")
123                                 .field(ICUTokenRepairFilterFactory.TYPE_PRESET_KEY, "unkwownwn")
124                             .endObject()
125                         .endObject()
126                     .endObject()
127                 .endObject();
128 
129         assertAcked(prepareCreate("my_index").setSettings(settings));
130     }
131 
132     @Test(expected = SettingsException.class)
133     public void testBadConfigMultipleTypeSettings1() throws IOException, InterruptedException,
134             ExecutionException {
135         XContentBuilder settings = jsonBuilder()
136                 .startObject()
137                     .field("number_of_shards", 1)
138                     .startObject("analysis")
139                         .startObject("filter")
140                             .startObject("broken_icutokrep")
141                                 .field("type", "icu_token_repair")
142                                 .array(ICUTokenRepairFilterFactory.ALLOW_TYPES_KEY, "<NUM>")
143                                 .array(ICUTokenRepairFilterFactory.DENY_TYPES_KEY, "<IDEOGRAM>")
144                             .endObject()
145                         .endObject()
146                     .endObject()
147                 .endObject();
148 
149         assertAcked(prepareCreate("my_index").setSettings(settings));
150     }
151 
152     @Test(expected = SettingsException.class)
153     public void testBadConfigMultipleTypeSettings2() throws IOException, InterruptedException,
154             ExecutionException {
155         XContentBuilder settings = jsonBuilder()
156                 .startObject()
157                     .field("number_of_shards", 1)
158                     .startObject("analysis")
159                         .startObject("filter")
160                             .startObject("broken_icutokrep")
161                                 .field("type", "icu_token_repair")
162                                 .array(ICUTokenRepairFilterFactory.ALLOW_TYPES_KEY, "<NUM>")
163                                 .field(ICUTokenRepairFilterFactory.TYPE_PRESET_KEY, "default")
164                             .endObject()
165                         .endObject()
166                     .endObject()
167                 .endObject();
168 
169         assertAcked(prepareCreate("my_index").setSettings(settings));
170     }
171 
172     @Test(expected = IllegalArgumentException.class)
173     public void testBadConfigUnkownScriptType() throws IOException, InterruptedException,
174             ExecutionException {
175         XContentBuilder settings = jsonBuilder()
176                 .startObject()
177                     .field("number_of_shards", 1)
178                     .startObject("analysis")
179                         .startObject("filter")
180                             .startObject("broken_icutokrep")
181                                 .field("type", "icu_token_repair")
182                                 .array(ICUTokenRepairFilterFactory.ALLOW_SCRIPTS_KEY, "Latin+Unkownian")
183                             .endObject()
184                         .endObject()
185                     .endObject()
186                 .endObject();
187 
188         assertAcked(prepareCreate("my_index").setSettings(settings));
189     }
190 
191     @Test(expected = SettingsException.class)
192     public void testBadConfigMultipleScriptTypes() throws IOException, InterruptedException,
193             ExecutionException {
194         XContentBuilder settings = jsonBuilder()
195                 .startObject()
196                     .field("number_of_shards", 1)
197                     .startObject("analysis")
198                         .startObject("filter")
199                             .startObject("broken_icutokrep")
200                                 .field("type", "icu_token_repair")
201                                 .array(ICUTokenRepairFilterFactory.ALLOW_SCRIPTS_KEY, "Latin+Greek")
202                                 .array(ICUTokenRepairFilterFactory.SCRIPT_PRESET_KEY, "all")
203                             .endObject()
204                         .endObject()
205                     .endObject()
206                 .endObject();
207 
208         assertAcked(prepareCreate("my_index").setSettings(settings));
209     }
210 
211     @Test(expected = SettingsException.class)
212     public void testBadConfigUnknownScriptPreset() throws IOException, InterruptedException,
213             ExecutionException {
214         XContentBuilder settings = jsonBuilder()
215                 .startObject()
216                     .field("number_of_shards", 1)
217                     .startObject("analysis")
218                         .startObject("filter")
219                             .startObject("broken_icutokrep")
220                                 .field("type", "icu_token_repair")
221                                 .array(ICUTokenRepairFilterFactory.SCRIPT_PRESET_KEY, "unknown")
222                             .endObject()
223                         .endObject()
224                     .endObject()
225                 .endObject();
226 
227         assertAcked(prepareCreate("my_index").setSettings(settings));
228     }
229 
230     @Override
231     protected Collection<Class<? extends Plugin>> nodePlugins() {
232         return Collections.<Class<? extends Plugin>>unmodifiableList(Arrays.asList(ExtraAnalysisTextifyPlugin.class));
233     }
234 
235 }