PageRenderTime 560ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/namespace-lookup/src/test/java/io/druid/query/extraction/namespace/URIExtractionNamespaceTest.java

https://gitlab.com/lcp0578/druid
Java | 338 lines | 294 code | 23 blank | 21 comment | 2 complexity | 5695287be12d4b07dd72dbe8b385f1c1 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to Metamarkets Group Inc. (Metamarkets) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. Metamarkets licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package io.druid.query.extraction.namespace;
  20. import com.fasterxml.jackson.databind.ObjectMapper;
  21. import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
  22. import com.fasterxml.jackson.databind.introspect.GuiceAnnotationIntrospector;
  23. import com.fasterxml.jackson.databind.introspect.GuiceInjectableValues;
  24. import com.google.common.collect.ImmutableList;
  25. import com.google.common.collect.ImmutableMap;
  26. import com.google.inject.Binder;
  27. import com.google.inject.Guice;
  28. import com.google.inject.Module;
  29. import io.druid.guice.annotations.Json;
  30. import io.druid.jackson.DefaultObjectMapper;
  31. import org.junit.Assert;
  32. import org.junit.Test;
  33. import java.io.IOException;
  34. import java.util.Map;
  35. /**
  36. *
  37. */
  38. public class URIExtractionNamespaceTest
  39. {
  40. public static ObjectMapper registerTypes(
  41. final ObjectMapper mapper
  42. )
  43. {
  44. mapper.setInjectableValues(
  45. new GuiceInjectableValues(
  46. Guice.createInjector(
  47. ImmutableList.of(
  48. new Module()
  49. {
  50. @Override
  51. public void configure(Binder binder)
  52. {
  53. binder.bind(ObjectMapper.class).annotatedWith(Json.class).toInstance(mapper);
  54. binder.bind(ObjectMapper.class).toInstance(mapper);
  55. }
  56. }
  57. )
  58. )
  59. )
  60. ).registerSubtypes(URIExtractionNamespace.class, URIExtractionNamespace.FlatDataParser.class);
  61. final GuiceAnnotationIntrospector guiceIntrospector = new GuiceAnnotationIntrospector();
  62. mapper.setAnnotationIntrospectors(
  63. new AnnotationIntrospectorPair(
  64. guiceIntrospector, mapper.getSerializationConfig().getAnnotationIntrospector()
  65. ),
  66. new AnnotationIntrospectorPair(
  67. guiceIntrospector, mapper.getDeserializationConfig().getAnnotationIntrospector()
  68. )
  69. );
  70. return mapper;
  71. }
  72. @Test
  73. public void testCSV()
  74. {
  75. URIExtractionNamespace.CSVFlatDataParser parser = new URIExtractionNamespace.CSVFlatDataParser(
  76. ImmutableList.of(
  77. "col1",
  78. "col2",
  79. "col3"
  80. ), "col2", "col3"
  81. );
  82. Assert.assertEquals(ImmutableMap.of("B", "C"), parser.getParser().parse("A,B,C"));
  83. }
  84. @Test(expected = IllegalArgumentException.class)
  85. public void testBadCSV()
  86. {
  87. URIExtractionNamespace.CSVFlatDataParser parser = new URIExtractionNamespace.CSVFlatDataParser(
  88. ImmutableList.of(
  89. "col1",
  90. "col2",
  91. "col3"
  92. ), "col2", "col3ADFSDF"
  93. );
  94. Assert.assertEquals(ImmutableMap.of("B", "C"), parser.getParser().parse("A,B,C"));
  95. }
  96. @Test(expected = NullPointerException.class)
  97. public void testBadCSV2()
  98. {
  99. URIExtractionNamespace.CSVFlatDataParser parser = new URIExtractionNamespace.CSVFlatDataParser(
  100. ImmutableList.of(
  101. "col1",
  102. "col2",
  103. "col3"
  104. ), "col2", "col3"
  105. );
  106. Map<String, String> map = parser.getParser().parse("A");
  107. }
  108. @Test
  109. public void testTSV()
  110. {
  111. URIExtractionNamespace.TSVFlatDataParser parser = new URIExtractionNamespace.TSVFlatDataParser(
  112. ImmutableList.of("col1", "col2", "col3"),
  113. "|",
  114. "col2",
  115. "col3"
  116. );
  117. Assert.assertEquals(ImmutableMap.of("B", "C"), parser.getParser().parse("A|B|C"));
  118. }
  119. @Test(expected = IllegalArgumentException.class)
  120. public void testBadTSV()
  121. {
  122. URIExtractionNamespace.TSVFlatDataParser parser = new URIExtractionNamespace.TSVFlatDataParser(
  123. ImmutableList.of("col1", "col2", "col3fdsfds"),
  124. ",",
  125. "col2",
  126. "col3"
  127. );
  128. Map<String, String> map = parser.getParser().parse("A,B,C");
  129. Assert.assertEquals(ImmutableMap.of("B", "C"), parser.getParser().parse("A,B,C"));
  130. }
  131. @Test(expected = NullPointerException.class)
  132. public void testBadTSV2()
  133. {
  134. URIExtractionNamespace.TSVFlatDataParser parser = new URIExtractionNamespace.TSVFlatDataParser(
  135. ImmutableList.of("col1", "col2", "col3"),
  136. ",",
  137. "col2",
  138. "col3"
  139. );
  140. Map<String, String> map = parser.getParser().parse("A");
  141. Assert.assertEquals(ImmutableMap.of("B", "C"), parser.getParser().parse("A,B,C"));
  142. }
  143. @Test
  144. public void testJSONFlatDataParser()
  145. {
  146. final String keyField = "keyField";
  147. final String valueField = "valueField";
  148. URIExtractionNamespace.JSONFlatDataParser parser = new URIExtractionNamespace.JSONFlatDataParser(
  149. new ObjectMapper(),
  150. keyField,
  151. valueField
  152. );
  153. Assert.assertEquals(
  154. ImmutableMap.of("B", "C"),
  155. parser.getParser()
  156. .parse(
  157. String.format(
  158. "{\"%s\":\"B\", \"%s\":\"C\", \"FOO\":\"BAR\"}",
  159. keyField,
  160. valueField
  161. )
  162. )
  163. );
  164. }
  165. @Test(expected = NullPointerException.class)
  166. public void testJSONFlatDataParserBad()
  167. {
  168. final String keyField = "keyField";
  169. final String valueField = "valueField";
  170. URIExtractionNamespace.JSONFlatDataParser parser = new URIExtractionNamespace.JSONFlatDataParser(
  171. new ObjectMapper(),
  172. keyField,
  173. valueField
  174. );
  175. Assert.assertEquals(
  176. ImmutableMap.of("B", "C"),
  177. parser.getParser()
  178. .parse(
  179. String.format(
  180. "{\"%sDFSDFDS\":\"B\", \"%s\":\"C\", \"FOO\":\"BAR\"}",
  181. keyField,
  182. valueField
  183. )
  184. )
  185. );
  186. }
  187. @Test(expected = IllegalArgumentException.class)
  188. public void testJSONFlatDataParserBad2()
  189. {
  190. final String keyField = "keyField";
  191. final String valueField = "valueField";
  192. URIExtractionNamespace.JSONFlatDataParser parser = new URIExtractionNamespace.JSONFlatDataParser(
  193. registerTypes(new ObjectMapper()),
  194. null,
  195. valueField
  196. );
  197. Assert.assertEquals(
  198. ImmutableMap.of("B", "C"),
  199. parser.getParser()
  200. .parse(
  201. String.format(
  202. "{\"%sDFSDFDS\":\"B\", \"%s\":\"C\", \"FOO\":\"BAR\"}",
  203. keyField,
  204. valueField
  205. )
  206. )
  207. );
  208. }
  209. @Test(expected = IllegalArgumentException.class)
  210. public void testJSONFlatDataParserBad3()
  211. {
  212. final String keyField = "keyField";
  213. final String valueField = "valueField";
  214. URIExtractionNamespace.JSONFlatDataParser parser = new URIExtractionNamespace.JSONFlatDataParser(
  215. registerTypes(new ObjectMapper()),
  216. keyField,
  217. null
  218. );
  219. Assert.assertEquals(
  220. ImmutableMap.of("B", "C"),
  221. parser.getParser()
  222. .parse(
  223. String.format(
  224. "{\"%sDFSDFDS\":\"B\", \"%s\":\"C\", \"FOO\":\"BAR\"}",
  225. keyField,
  226. valueField
  227. )
  228. )
  229. );
  230. }
  231. @Test(expected = IllegalArgumentException.class)
  232. public void testJSONFlatDataParserBad4()
  233. {
  234. final String keyField = "keyField";
  235. final String valueField = "valueField";
  236. URIExtractionNamespace.JSONFlatDataParser parser = new URIExtractionNamespace.JSONFlatDataParser(
  237. registerTypes(new ObjectMapper()),
  238. "",
  239. ""
  240. );
  241. Assert.assertEquals(
  242. ImmutableMap.of("B", "C"),
  243. parser.getParser()
  244. .parse(
  245. String.format(
  246. "{\"%sDFSDFDS\":\"B\", \"%s\":\"C\", \"FOO\":\"BAR\"}",
  247. keyField,
  248. valueField
  249. )
  250. )
  251. );
  252. }
  253. @Test
  254. public void testObjectMapperFlatDataParser()
  255. {
  256. URIExtractionNamespace.ObjectMapperFlatDataParser parser = new URIExtractionNamespace.ObjectMapperFlatDataParser(
  257. registerTypes(new ObjectMapper())
  258. );
  259. Assert.assertEquals(ImmutableMap.of("B", "C"), parser.getParser().parse("{\"B\":\"C\"}"));
  260. }
  261. @Test
  262. public void testSimpleJSONSerDe() throws IOException
  263. {
  264. final ObjectMapper mapper = registerTypes(new DefaultObjectMapper());
  265. for (URIExtractionNamespace.FlatDataParser parser : ImmutableList.of(
  266. new URIExtractionNamespace.CSVFlatDataParser(
  267. ImmutableList.of(
  268. "col1",
  269. "col2",
  270. "col3"
  271. ), "col2", "col3"
  272. ),
  273. new URIExtractionNamespace.ObjectMapperFlatDataParser(mapper),
  274. new URIExtractionNamespace.JSONFlatDataParser(mapper, "keyField", "valueField"),
  275. new URIExtractionNamespace.TSVFlatDataParser(ImmutableList.of("A", "B"), ",", "A", "B")
  276. )) {
  277. final String str = mapper.writeValueAsString(parser);
  278. final URIExtractionNamespace.FlatDataParser parser2 = mapper.readValue(
  279. str,
  280. URIExtractionNamespace.FlatDataParser.class
  281. );
  282. Assert.assertEquals(str, mapper.writeValueAsString(parser2));
  283. }
  284. }
  285. @Test
  286. public void testSimpleToString() throws IOException
  287. {
  288. final ObjectMapper mapper = registerTypes(new DefaultObjectMapper());
  289. for (URIExtractionNamespace.FlatDataParser parser : ImmutableList.of(
  290. new URIExtractionNamespace.CSVFlatDataParser(
  291. ImmutableList.of(
  292. "col1",
  293. "col2",
  294. "col3"
  295. ), "col2", "col3"
  296. ),
  297. new URIExtractionNamespace.ObjectMapperFlatDataParser(mapper),
  298. new URIExtractionNamespace.JSONFlatDataParser(mapper, "keyField", "valueField"),
  299. new URIExtractionNamespace.TSVFlatDataParser(ImmutableList.of("A", "B"), ",", "A", "B")
  300. )) {
  301. Assert.assertFalse(parser.toString().contains("@"));
  302. }
  303. }
  304. @Test
  305. public void testExplicitJson() throws IOException
  306. {
  307. final ObjectMapper mapper = registerTypes(new DefaultObjectMapper());
  308. URIExtractionNamespace namespace = mapper.readValue("{\"type\":\"uri\", \"uri\":\"file:/foo\", \"namespaceParseSpec\":{\"format\":\"simpleJson\"}, \"pollPeriod\":\"PT5M\", \"versionRegex\":\"a.b.c\", \"namespace\":\"testNamespace\"}", URIExtractionNamespace.class);
  309. Assert.assertEquals(URIExtractionNamespace.ObjectMapperFlatDataParser.class.getCanonicalName(), namespace.getNamespaceParseSpec().getClass().getCanonicalName());
  310. Assert.assertEquals("file:/foo", namespace.getUri().toString());
  311. Assert.assertEquals("testNamespace", namespace.getNamespace());
  312. Assert.assertEquals("a.b.c", namespace.getVersionRegex());
  313. Assert.assertEquals(5L * 60_000L, namespace.getPollMs());
  314. }
  315. }