More JUnit additions for PAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / elk / client / ElkConnectorImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.elk.client;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import io.searchbox.client.JestResult;
31 import java.io.IOException;
32 import java.lang.reflect.Method;
33 import java.util.HashMap;
34 import java.util.Map;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
39 import org.onap.policy.rest.adapter.PolicyRestAdapter;
40
41 public class ElkConnectorImplTest {
42
43     @Test
44     public void isAlphaNumericTest() {
45         try {
46             Method method = ElkConnectorImpl.class.getDeclaredMethod("isAlphaNumeric", String.class);
47             method.setAccessible(true);
48             assertTrue((boolean) method.invoke(new ElkConnectorImpl(), "abc123"));
49             assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123*"));
50             assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123{}"));
51             assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123\n"));
52             assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123<"));
53             assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123:"));
54         } catch (Exception e) {
55             fail();
56         }
57     }
58
59     @Test
60     public void searchTest() {
61         JestResult r1 = null, r2 = null, r3 = null, r4 = null;
62
63         // Should always work if the above test passes and ELK server is up
64         try {
65             r1 = new ElkConnectorImpl().search(PolicyIndexType.decision, "abc123");
66         } catch (Exception e) {
67             // ELK server is down. Don't continue the test
68             if (e instanceof IllegalStateException) {
69                 return;
70             }
71             fail();
72         }
73
74         // Should always work
75         try {
76             r2 = new ElkConnectorImpl().search(PolicyIndexType.decision, "The_quick_brown_fox_jumps_over_the_lazy_dog");
77         } catch (Exception e) {
78             fail();
79         }
80
81         // Should throw exception
82         try {
83             r3 = new ElkConnectorImpl().search(PolicyIndexType.decision, "abc123{}");
84         } catch (Exception e) {
85             if (!(e instanceof IllegalArgumentException)) {
86                 fail();
87             }
88         }
89
90         // Should throw exception
91         try {
92             r4 = new ElkConnectorImpl().search(PolicyIndexType.decision, "The quick brown fox jumps over the lazy dog");
93         } catch (Exception e) {
94             if (!(e instanceof IllegalArgumentException)) {
95                 fail();
96             }
97         }
98
99         assertNotNull(r1);
100         assertNotNull(r2);
101         assertNull(r3);
102         assertNull(r4);
103     }
104
105     @Rule
106     public ExpectedException thrown = ExpectedException.none();
107
108     @Test
109     public void testDelete() {
110         thrown.expect(NullPointerException.class);
111
112         ElkConnectorImpl impl = new ElkConnectorImpl();
113         PolicyRestAdapter adapter = new PolicyRestAdapter();
114         impl.delete(adapter);
115         fail("Expected exception to be thrown");
116     }
117
118     @Test
119     public void testPut() throws IOException {
120         thrown.expect(NullPointerException.class);
121
122         ElkConnectorImpl impl = new ElkConnectorImpl();
123         PolicyRestAdapter adapter = new PolicyRestAdapter();
124         impl.put(adapter);
125         fail("Expected exception to be thrown");
126     }
127
128     @Test
129     public void testUpdate() {
130         thrown.expect(IllegalStateException.class);
131
132         ElkConnectorImpl impl = new ElkConnectorImpl();
133         PolicyRestAdapter adapter = new PolicyRestAdapter();
134         impl.update(adapter);
135         fail("Expected exception to be thrown");
136     }
137
138     @Test
139     public void testSearchWithFilter() {
140         thrown.expect(IllegalStateException.class);
141
142         ElkConnectorImpl impl = new ElkConnectorImpl();
143         impl.search(PolicyIndexType.config, "search", null);
144         fail("Expected exception to be thrown");
145     }
146
147     @Test
148     public void testImplNegCases() throws IOException {
149         ElkConnectorImpl impl = new ElkConnectorImpl();
150         Map<String, String> filter = new HashMap<String, String>();
151         assertThatThrownBy(() -> impl.isType(PolicyIndexType.config)).isInstanceOf(IOException.class);
152         assertThatThrownBy(() -> impl.isIndex()).isInstanceOf(IOException.class);
153         assertThatThrownBy(() -> impl.search(null, null)).isInstanceOf(IllegalArgumentException.class);
154         assertThatThrownBy(() -> impl.search(null, "")).isInstanceOf(IllegalArgumentException.class);
155         assertThatThrownBy(() -> impl.search(null, ";;;")).isInstanceOf(IllegalArgumentException.class);
156         assertThatThrownBy(() -> impl.search(null, "foo")).isInstanceOf(IllegalStateException.class);
157         assertThatThrownBy(() -> impl.search(PolicyIndexType.all, "foo")).isInstanceOf(IllegalStateException.class);
158
159         assertThatThrownBy(() -> impl.search(null, null, null)).isInstanceOf(IllegalArgumentException.class);
160         assertThatThrownBy(() -> impl.search(null, null, filter)).isInstanceOf(IllegalArgumentException.class);
161         filter.put("key", "value");
162         assertThatThrownBy(() -> impl.search(null, ";;;", filter)).isInstanceOf(IllegalArgumentException.class);
163         assertThatThrownBy(() -> impl.search(null, "foo", filter)).isInstanceOf(IllegalStateException.class);
164         assertThatThrownBy(() -> impl.search(PolicyIndexType.config, "foo", filter))
165             .isInstanceOf(IllegalStateException.class);
166
167         PolicyRestAdapter adapter = new PolicyRestAdapter();
168         adapter.setNewFileName("scope.Decision_newFile");
169         adapter.setConfigPolicyType("Config");
170         assertThatThrownBy(() -> impl.put(adapter)).isInstanceOf(IOException.class);
171         assertThatThrownBy(() -> impl.delete(adapter)).isInstanceOf(IllegalStateException.class);
172     }
173 }