1f8cb6f90b710ca3ba4cedaa3cdd58456d50522a
[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 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 package org.onap.policy.pap.xacml.rest.controller;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.when;
24
25 import java.io.BufferedReader;
26 import java.io.ByteArrayInputStream;
27 import java.io.File;
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;
34
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
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;
52
53 import com.att.research.xacml.api.pap.PAPException;
54
55
56 public class PushPolicyControllerTest {
57         
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;
67
68         @Before
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));
75
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");
80        
81                 commonClassDao = Mockito.mock(CommonClassDao.class);
82                 controller = new PushPolicyController();
83                 controller.setCommonClassDao(commonClassDao);
84                 logger.info("setUp: exit");
85         }
86         
87         @Test
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);
98                 callPushPolicy();
99                 when(commonClassDao.getDataById(PolicyVersion.class, "policyName", "com"+File.separator+"Config_Test")).thenReturn(null);
100                 callPushPolicy();
101         }
102         
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();
108                         int numCharsRead;
109                         while ((numCharsRead = br.read(charBuffer, 0, charBuffer.length)) != -1) {
110                                 builder.append(charBuffer, 0, numCharsRead);
111                         }
112                         when(request.getInputStream()).thenReturn(getInputStream(builder.toString().getBytes(StandardCharsets.UTF_8)));
113                         controller.pushPolicy(request, response);
114                         assertTrue(response != null);
115                 }catch(Exception e){
116                         logger.error("Exception"+ e);
117                 } 
118         }
119         
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(); 
125             }
126
127                         @Override
128                         public boolean isFinished() {
129                                 return false;
130                         }
131
132                         @Override
133                         public boolean isReady() {
134                                 return false;
135                         }
136
137                         @Override
138                         public void setReadListener(ReadListener readListener) {
139                         } 
140         }; 
141         return servletInputStream; 
142     } 
143         
144          @After
145          public void destroy(){
146                  if(pap!=null)
147                          pap.destroy();
148          }
149 }