Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / PushPolicyControllerTest.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.controller;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.when;
25
26 import com.att.research.xacml.api.pap.PAPException;
27
28 import java.io.BufferedReader;
29 import java.io.ByteArrayInputStream;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.StringReader;
33 import java.nio.charset.StandardCharsets;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37
38 import javax.servlet.ReadListener;
39 import javax.servlet.ServletConfig;
40 import javax.servlet.ServletException;
41 import javax.servlet.ServletInputStream;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.hibernate.SessionFactory;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.mockito.Mockito;
51 import org.onap.policy.common.logging.flexlogger.FlexLogger;
52 import org.onap.policy.common.logging.flexlogger.Logger;
53 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
54 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTest;
55 import org.onap.policy.rest.dao.CommonClassDao;
56 import org.onap.policy.rest.jpa.PolicyVersion;
57 import org.springframework.mock.web.MockServletConfig;
58
59 public class PushPolicyControllerTest {
60
61     private static Logger logger = FlexLogger.getLogger(PushPolicyControllerTest.class);
62     private static CommonClassDao commonClassDao;
63     private static SessionFactory sessionFactory;
64     private String jsonString = null;
65     private HttpServletRequest request = null;
66     private PushPolicyController controller = null;
67     private HttpServletResponse response = null;
68     private List<String> headers = new ArrayList<>();
69     private ServletConfig servletConfig;
70     private XACMLPapServlet pap;
71
72     @BeforeClass
73     public static void beforeClassSetup() throws ServletException {
74         sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("pushcontrollertest");
75     }
76
77     @Before
78     public void setUp() throws Exception {
79         logger.info("setUp: Entering");
80         request = Mockito.mock(HttpServletRequest.class);
81         response = Mockito.mock(HttpServletResponse.class);
82         Mockito.when(request.getHeaderNames()).thenReturn(Collections.enumeration(headers));
83         Mockito.when(request.getAttributeNames()).thenReturn(Collections.enumeration(headers));
84
85         servletConfig = Mockito.mock(MockServletConfig.class);
86         System.setProperty("com.sun.management.jmxremote.port", "9993");
87         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
88         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
89                 .thenReturn("src/test/resources/xacml.pap.properties");
90
91         commonClassDao = Mockito.mock(CommonClassDao.class);
92         controller = new PushPolicyController();
93         controller.setCommonClassDao(commonClassDao);
94         logger.info("setUp: exit");
95     }
96
97     @Test
98     public void testPushPolicy() throws ServletException, PAPException {
99         PolicyVersion versionData = new PolicyVersion();
100         versionData.setPolicyName("com" + File.separator + "Config_Test");
101         versionData.setActiveVersion(1);
102         versionData.setHigherVersion(1);
103         List<Object> data = new ArrayList<>();
104         data.add(versionData);
105         when(commonClassDao.getDataById(PolicyVersion.class, "policyName", "com" + File.separator + "Config_Test"))
106                 .thenReturn(data);
107         pap = new XACMLPapServlet();
108         pap.init(servletConfig);
109         callPushPolicy();
110         when(commonClassDao.getDataById(PolicyVersion.class, "policyName", "com" + File.separator + "Config_Test"))
111                 .thenReturn(null);
112         callPushPolicy();
113     }
114
115     public void callPushPolicy() {
116         jsonString =
117                 "{\"policyScope\":\"com\",\"filePrefix\":\"Config_\",\"policyName\":\"Test\",\"pdpGroup\":\"default\"}";
118         try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
119             char[] charBuffer = new char[8 * 1024];
120             StringBuilder builder = new StringBuilder();
121             int numCharsRead;
122             while ((numCharsRead = br.read(charBuffer, 0, charBuffer.length)) != -1) {
123                 builder.append(charBuffer, 0, numCharsRead);
124             }
125             when(request.getInputStream())
126                     .thenReturn(getInputStream(builder.toString().getBytes(StandardCharsets.UTF_8)));
127             controller.pushPolicy(request, response);
128             assertTrue(response != null);
129         } catch (Exception e) {
130             logger.error("Exception" + e);
131         }
132     }
133
134     public ServletInputStream getInputStream(byte[] body) throws IOException {
135         final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
136         ServletInputStream servletInputStream = new ServletInputStream() {
137             @Override
138             public int read() throws IOException {
139                 return byteArrayInputStream.read();
140             }
141
142             @Override
143             public boolean isFinished() {
144                 return false;
145             }
146
147             @Override
148             public boolean isReady() {
149                 return false;
150             }
151
152             @Override
153             public void setReadListener(ReadListener readListener) {
154             }
155         };
156         return servletInputStream;
157     }
158
159     @After
160     public void destroy() {
161         if (pap != null)
162             pap.destroy();
163     }
164 }