Merge "JUnit additions for ONAP-PAP-REST"
[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-2018 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 import io.searchbox.client.JestResult;
29
30 import java.io.IOException;
31 import java.lang.reflect.Method;
32
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
37 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnectorImpl;
38 import org.onap.policy.rest.adapter.PolicyRestAdapter;
39
40 public class ElkConnectorImplTest {
41
42         @Test
43         public void isAlphaNumericTest() {
44                 try {
45                         Method method = ElkConnectorImpl.class.getDeclaredMethod("isAlphaNumeric", String.class);
46                         method.setAccessible(true);
47                         assertTrue((boolean) method.invoke(new ElkConnectorImpl(), "abc123"));
48                         assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123*"));
49                         assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123{}"));
50                         assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123\n"));
51                         assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123<"));
52                         assertFalse((boolean) method.invoke(new ElkConnectorImpl(), "abc123:"));
53                 } catch (Exception e) {
54                         fail();
55                 }
56         }
57
58         @Test
59         public void searchTest(){
60                 JestResult r1=null, r2=null, r3=null, r4=null;
61
62                 // Should always work if the above test passes and ELK server is up
63                 try{
64                         r1 = new ElkConnectorImpl().search(PolicyIndexType.decision, "abc123");
65                 } catch (Exception e) {
66                         // ELK server is down. Don't continue the test
67                         if(e instanceof IllegalStateException){
68                                 return;
69                         }
70                         fail();
71                 }
72
73                 // Should always work
74                 try{
75                         r2 = new ElkConnectorImpl().search(PolicyIndexType.decision, "The_quick_brown_fox_jumps_over_the_lazy_dog");
76                 } catch (Exception e) {
77                         fail();
78                 }
79
80                 // Should throw exception
81                 try{
82                         r3 = new ElkConnectorImpl().search(PolicyIndexType.decision, "abc123{}");
83                 } catch (Exception e) {
84                         if(! (e instanceof IllegalArgumentException)){
85                                 fail();
86                         }
87                 }
88                 
89                 // Should throw exception
90                 try{
91                         r4 = new ElkConnectorImpl().search(PolicyIndexType.decision, "The quick brown fox jumps over the lazy dog");
92                 } catch (Exception e) {
93                         if(! (e instanceof IllegalArgumentException)){
94                                 fail();
95                         }
96                 }
97
98                 assertNotNull(r1);
99                 assertNotNull(r2);
100                 assertNull(r3);
101                 assertNull(r4);
102         }
103         
104         @Rule
105         public ExpectedException thrown = ExpectedException.none();
106         
107         @Test
108         public void testDelete() {
109                 thrown.expect(NullPointerException.class);
110
111                 ElkConnectorImpl impl = new ElkConnectorImpl();
112                 PolicyRestAdapter adapter = new PolicyRestAdapter();
113                 impl.delete(adapter);
114                 fail("Expected exception to be thrown");
115         }
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 }