METRON-1503 Alerts are not getting populated in alerts UI when search engine is Solr...
[metron.git] / metron-platform / metron-solr / src / test / java / org / apache / metron / solr / dao / SolrColumnMetadataTest.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.metron.solr.dao;
19
20 import org.apache.metron.indexing.dao.search.FieldType;
21 import org.apache.solr.client.solrj.SolrServerException;
22 import org.apache.solr.common.SolrException;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertNotNull;
38 import static org.mockito.Mockito.doReturn;
39 import static org.mockito.Mockito.doThrow;
40 import static org.mockito.Mockito.spy;
41
42 public class SolrColumnMetadataTest {
43
44 @Rule
45 public final ExpectedException exception = ExpectedException.none();
46
47 private static final String zkHost = "zookeeper:2181";
48
49 private SolrColumnMetadataDao solrColumnMetadataDao;
50
51 @SuppressWarnings("unchecked")
52 @Before
53 public void setUp() throws Exception {
54 solrColumnMetadataDao = new SolrColumnMetadataDao(zkHost);
55 }
56
57 @Test
58 public void getColumnMetadataShouldProperlyReturnColumnMetadata() throws Exception {
59 List<Map<String, Object>> broFields = new ArrayList<>();
60 broFields.add(new HashMap<String, Object>(){{
61 put("name", "string");
62 put("type", "string");
63 }});
64 broFields.add(new HashMap<String, Object>(){{
65 put("name", "int");
66 put("type", "pint");
67 }});
68 broFields.add(new HashMap<String, Object>(){{
69 put("name", "float");
70 put("type", "pfloat");
71 }});
72 broFields.add(new HashMap<String, Object>(){{
73 put("name", "double");
74 put("type", "pdouble");
75 }});
76 broFields.add(new HashMap<String, Object>(){{
77 put("name", "boolean");
78 put("type", "boolean");
79 }});
80 broFields.add(new HashMap<String, Object>(){{
81 put("name", "broField");
82 put("type", "string");
83 }});
84 broFields.add(new HashMap<String, Object>(){{
85 put("name", "conflict");
86 put("type", "string");
87 }});
88
89
90 List<Map<String, Object>> snortFields = new ArrayList<>();
91 snortFields.add(new HashMap<String, Object>(){{
92 put("name", "long");
93 put("type", "plong");
94 }});
95 snortFields.add(new HashMap<String, Object>(){{
96 put("name", "snortField");
97 put("type", "plong");
98 }});
99 snortFields.add(new HashMap<String, Object>(){{
100 put("name", "unknown");
101 put("type", "unknown");
102 }});
103 broFields.add(new HashMap<String, Object>(){{
104 put("name", "conflict");
105 put("type", "plong");
106 }});
107
108 solrColumnMetadataDao = spy(new SolrColumnMetadataDao(zkHost));
109 doReturn(broFields).when(solrColumnMetadataDao).getIndexFields("bro");
110 doReturn(snortFields).when(solrColumnMetadataDao).getIndexFields("snort");
111
112 Map<String, FieldType> columnMetadata = solrColumnMetadataDao.getColumnMetadata(Arrays.asList("bro", "snort"));
113
114 assertEquals(FieldType.BOOLEAN, columnMetadata.get("boolean"));
115 assertEquals(FieldType.TEXT, columnMetadata.get("string"));
116 assertEquals(FieldType.TEXT, columnMetadata.get("broField"));
117 assertEquals(FieldType.DOUBLE, columnMetadata.get("double"));
118 assertEquals(FieldType.LONG, columnMetadata.get("long"));
119 assertEquals(FieldType.FLOAT, columnMetadata.get("float"));
120 assertEquals(FieldType.INTEGER, columnMetadata.get("int"));
121 assertEquals(FieldType.LONG, columnMetadata.get("snortField"));
122 assertEquals(FieldType.OTHER, columnMetadata.get("conflict"));
123 assertEquals(FieldType.OTHER, columnMetadata.get("unknown"));
124
125 }
126
127 @Test
128 public void getColumnMetadataShouldThrowSolrException() throws Exception {
129 exception.expect(IOException.class);
130 exception.expectMessage("solr exception");
131
132 solrColumnMetadataDao = spy(new SolrColumnMetadataDao(zkHost));
133 doThrow(new SolrServerException("solr exception")).when(solrColumnMetadataDao).getIndexFields("bro");
134
135 solrColumnMetadataDao.getColumnMetadata(Arrays.asList("bro", "snort"));
136 }
137
138 @Test
139 public void getColumnMetadataShouldHandle400Exception() throws Exception {
140 solrColumnMetadataDao = spy(new SolrColumnMetadataDao(zkHost));
141 SolrException solrException = new SolrException(SolrException.ErrorCode.BAD_REQUEST, "solr exception");
142
143 doThrow(solrException).when(solrColumnMetadataDao).getIndexFields("bro");
144
145 Map<String, FieldType> columnMetadata = solrColumnMetadataDao.getColumnMetadata(Collections.singletonList("bro"));
146
147 assertNotNull(columnMetadata);
148 }
149
150 }