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