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