Merge "Cleanup drl files in policy/engine test modules"
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / elk / 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;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import io.searchbox.client.JestResult;
30
31 import java.io.IOException;
32 import java.lang.reflect.Method;
33
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
38 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnectorImpl;
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 }