2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.when;
26 import com.att.research.xacml.api.pap.PAPException;
28 import java.io.BufferedReader;
29 import java.io.ByteArrayInputStream;
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;
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;
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;
59 public class PushPolicyControllerTest {
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;
73 public static void beforeClassSetup() throws ServletException {
74 sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("pushcontrollertest");
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));
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");
91 commonClassDao = Mockito.mock(CommonClassDao.class);
92 controller = new PushPolicyController();
93 controller.setCommonClassDao(commonClassDao);
94 logger.info("setUp: exit");
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"))
107 pap = new XACMLPapServlet();
108 pap.init(servletConfig);
110 when(commonClassDao.getDataById(PolicyVersion.class, "policyName", "com" + File.separator + "Config_Test"))
115 public void callPushPolicy() {
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();
122 while ((numCharsRead = br.read(charBuffer, 0, charBuffer.length)) != -1) {
123 builder.append(charBuffer, 0, numCharsRead);
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);
134 public ServletInputStream getInputStream(byte[] body) throws IOException {
135 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
136 ServletInputStream servletInputStream = new ServletInputStream() {
138 public int read() throws IOException {
139 return byteArrayInputStream.read();
143 public boolean isFinished() {
148 public boolean isReady() {
153 public void setReadListener(ReadListener readListener) {
156 return servletInputStream;
160 public void destroy() {