2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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
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=========================================================
20 package org.onap.policy.pap.xacml.rest.controller;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.when;
25 import java.io.BufferedReader;
26 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.StringReader;
30 import java.nio.charset.StandardCharsets;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
35 import javax.servlet.ReadListener;
36 import javax.servlet.ServletConfig;
37 import javax.servlet.ServletException;
38 import javax.servlet.ServletInputStream;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mockito;
46 import org.onap.policy.common.logging.flexlogger.FlexLogger;
47 import org.onap.policy.common.logging.flexlogger.Logger;
48 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
49 import org.onap.policy.rest.dao.CommonClassDao;
50 import org.onap.policy.rest.jpa.PolicyVersion;
51 import org.springframework.mock.web.MockServletConfig;
53 import com.att.research.xacml.api.pap.PAPException;
56 public class PushPolicyControllerTest {
58 private static Logger logger = FlexLogger.getLogger(PushPolicyControllerTest.class);
59 private static CommonClassDao commonClassDao;
60 private String jsonString = null;
61 private HttpServletRequest request = null;
62 private PushPolicyController controller = null;
63 private HttpServletResponse response = null;
64 private List<String> headers = new ArrayList<>();
65 private ServletConfig servletConfig;
66 private XACMLPapServlet pap;
69 public void setUp() throws Exception {
70 logger.info("setUp: Entering");
71 request = Mockito.mock(HttpServletRequest.class);
72 response = Mockito.mock(HttpServletResponse.class);
73 Mockito.when(request.getHeaderNames()).thenReturn(Collections.enumeration(headers));
74 Mockito.when(request.getAttributeNames()).thenReturn(Collections.enumeration(headers));
76 servletConfig = Mockito.mock(MockServletConfig.class);
77 System.setProperty("com.sun.management.jmxremote.port", "9993");
78 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
79 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("src/test/resources/xacml.pap.properties");
81 commonClassDao = Mockito.mock(CommonClassDao.class);
82 controller = new PushPolicyController();
83 controller.setCommonClassDao(commonClassDao);
84 logger.info("setUp: exit");
88 public void testPushPolicy() throws ServletException, PAPException{
89 PolicyVersion versionData = new PolicyVersion();
90 versionData.setPolicyName("com"+File.separator+"Config_Test");
91 versionData.setActiveVersion(1);
92 versionData.setHigherVersion(1);
93 List<Object> data = new ArrayList<>();
94 data.add(versionData);
95 when(commonClassDao.getDataById(PolicyVersion.class, "policyName", "com"+File.separator+"Config_Test")).thenReturn(data);
96 pap = new XACMLPapServlet();
97 pap.init(servletConfig);
99 when(commonClassDao.getDataById(PolicyVersion.class, "policyName", "com"+File.separator+"Config_Test")).thenReturn(null);
103 public void callPushPolicy(){
104 jsonString = "{\"policyScope\":\"com\",\"filePrefix\":\"Config_\",\"policyName\":\"Test\",\"pdpGroup\":\"default\"}";
105 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
106 char[] charBuffer = new char[8 * 1024];
107 StringBuilder builder = new StringBuilder();
109 while ((numCharsRead = br.read(charBuffer, 0, charBuffer.length)) != -1) {
110 builder.append(charBuffer, 0, numCharsRead);
112 when(request.getInputStream()).thenReturn(getInputStream(builder.toString().getBytes(StandardCharsets.UTF_8)));
113 controller.pushPolicy(request, response);
114 assertTrue(response != null);
116 logger.error("Exception"+ e);
120 public ServletInputStream getInputStream(byte[] body) throws IOException {
121 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
122 ServletInputStream servletInputStream = new ServletInputStream() {
123 public int read() throws IOException {
124 return byteArrayInputStream.read();
128 public boolean isFinished() {
133 public boolean isReady() {
138 public void setReadListener(ReadListener readListener) {
141 return servletInputStream;
145 public void destroy(){