More JUnit additions for PAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / elk / PolicyElasticSearchControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2018-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.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.mockito.Mockito.when;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.StringReader;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType;
42 import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController;
43 import org.onap.policy.rest.adapter.PolicyRestAdapter;
44 import org.onap.policy.rest.dao.CommonClassDao;
45 import org.springframework.mock.web.MockHttpServletResponse;
46
47 public class PolicyElasticSearchControllerTest {
48
49     private PolicyElasticSearchController controller;
50     private HttpServletRequest request = null;
51     private HttpServletResponse response = null;
52
53     @Before
54     public void setup() {
55         controller = new PolicyElasticSearchController();
56         request = Mockito.mock(HttpServletRequest.class);
57         response = new MockHttpServletResponse();
58     }
59
60     @Test
61     public void testSearchDictionary() throws IOException {
62         List<String> jsonString = new ArrayList<>();
63         jsonString.add("{\"type\":\"attribute\",\"data\":{\"xacmlId\":\"Test\"}}");
64         jsonString.add("{\"type\":\"onapName\",\"data\":{\"onapName\":\"Test\"}}");
65         jsonString.add("{\"type\":\"actionPolicy\",\"data\":{\"attributeName\":\"Test\"}}");
66         jsonString.add("{\"type\":\"brmsParam\",\"data\":{\"ruleName\":\"Test\"}}");
67         jsonString.add("{\"type\":\"pepOptions\",\"data\":{\"pepName\":\"Test\"}}");
68         jsonString.add("{\"type\":\"clSite\",\"data\":{\"siteName\":\"Test\"}}");
69         jsonString.add("{\"type\":\"clService\",\"data\":{\"serviceName\":\"Test\"}}");
70         jsonString.add("{\"type\":\"clVarbind\",\"data\":{\"varbindName\":\"Test\"}}");
71         jsonString.add("{\"type\":\"clVnf\",\"data\":{\"vnftype\":\"Test\"}}");
72         jsonString.add("{\"type\":\"clVSCL\",\"data\":{\"vsclaction\":\"Test\"}}");
73         jsonString.add("{\"type\":\"decision\",\"data\":{\"xacmlId\":\"Test\"}}");
74         jsonString.add("{\"type\":\"fwTerm\",\"data\":{\"termName\":\"Test\"}}");
75         jsonString.add("{\"type\":\"msDCAEUUID\",\"data\":{\"name\":\"Test\"}}");
76         jsonString.add("{\"type\":\"msLocation\",\"data\":{\"name\":\"Test\"}}");
77         jsonString.add("{\"type\":\"msModels\",\"data\":{\"modelName\":\"Test\"}}");
78         jsonString.add("{\"type\":\"psGroupPolicy\",\"data\":{\"name\":\"Test\"}}");
79         jsonString.add("{\"type\":\"safeRisk\",\"data\":{\"name\":\"Test\"}}");
80         jsonString.add("{\"type\":\"safePolicyWarning\",\"data\":{\"name\":\"Test\"}}");
81         for (int i = 0; i < jsonString.size(); i++) {
82             BufferedReader br = new BufferedReader(new StringReader(jsonString.get(i)));
83             when(request.getReader()).thenReturn(br);
84             assertThatCode(() -> controller.searchDictionary(request, response)).doesNotThrowAnyException();
85         }
86     }
87
88     @Test
89     public void testController() throws IOException {
90         CommonClassDao dao = Mockito.mock(CommonClassDao.class);
91         PolicyElasticSearchController controller = new PolicyElasticSearchController(dao);
92         assertEquals(PolicyIndexType.all, controller.toPolicyIndexType(null));
93         assertEquals(PolicyIndexType.config, controller.toPolicyIndexType("config"));
94
95         Map<String, String> searchKeys = new HashMap<String, String>();
96         searchKeys.put("key", "value");
97         assertThatThrownBy(() -> controller.search(PolicyIndexType.config, "text", searchKeys))
98             .isInstanceOf(Exception.class);
99
100         when(request.getParameter("policyName")).thenReturn("policyName");
101         when(request.getParameter("action")).thenReturn("search");
102         when(request.getReader())
103             .thenReturn(new BufferedReader(new StringReader("{\"searchdata\": { \"query\": \"value space\", "
104                 + "\"policyType\": \"all\", " + "\"closedLooppolicyType\": \"type\", " + "\"onapName\": \"pef\", "
105                 + "\"vnfType\": \"vnf\", " + "\"policyStatus\": \"active\", " + "\"vproAction\": \"reboot\", "
106                 + "\"serviceType\": \"type\", " + "\"bindTextSearch\": \"pef\", " + "\"d2Service\": \"vDNS\"} }")));
107         controller.searchPolicy(request, response);
108         assertEquals(HttpServletResponse.SC_OK, response.getStatus());
109
110         PolicyRestAdapter policyData = new PolicyRestAdapter();
111         assertFalse(controller.deleteElk(policyData));
112     }
113 }