Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / handler / PushPolicyHandlerTest.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.handler;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.doReturn;
28
29 import com.mockrunner.mock.web.MockHttpServletResponse;
30
31 import java.io.File;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import javax.script.SimpleBindings;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.PolicyVersion;
43 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
44 import org.onap.policy.xacml.std.pap.StdPDPGroup;
45 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
46
47 public class PushPolicyHandlerTest {
48     @Test
49     public void testGetsAndChecks() {
50         CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
51         // Test constructor
52         PushPolicyHandler handler = new PushPolicyHandler(commonClassDao);
53         assertNotNull(handler);
54
55         // Test gets
56         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
57         HttpServletResponse response = Mockito.mock(MockHttpServletResponse.class);
58         Mockito.when(request.getParameter("policyScope")).thenReturn("com");
59         Mockito.when(request.getParameter("filePrefix")).thenReturn("Config_");
60         Mockito.when(request.getParameter("policyName")).thenReturn("testPush");
61         PolicyVersion version = new PolicyVersion();
62         version.setActiveVersion(1);
63         version.setCreatedBy("API");
64         version.setHigherVersion(1);
65         version.setId(1);
66         version.setModifiedBy("API");
67         version.setPolicyName("com" + File.separator + "Config_testPush");
68         List<PolicyVersion> list = new ArrayList<>();
69         list.add(version);
70
71         doNothing().when(commonClassDao).save(any(PolicyVersion.class));
72         doReturn(list).when(commonClassDao).getDataByQuery(any(String.class), any(SimpleBindings.class));
73
74         handler.getActiveVersion(request, response);
75
76         Mockito.verify(response).setStatus(HttpServletResponse.SC_OK);
77         Mockito.verify(response).addHeader("version", "1");
78
79         request = Mockito.mock(HttpServletRequest.class);
80         response = Mockito.mock(MockHttpServletResponse.class);
81         Mockito.when(request.getParameter("gitPath")).thenReturn("testPath");
82         handler.getSelectedURI(request, response);
83         Mockito.verify(response).setStatus(HttpServletResponse.SC_OK);
84
85         StdPDPPolicy policy = new StdPDPPolicy();
86         OnapPDPGroup onapPolicy = new StdPDPGroup();
87         String configHome = "testVal";
88         assertEquals(handler.preSafetyCheck(policy, configHome), true);
89         assertEquals(handler.preSafetyCheck(onapPolicy, configHome), true);
90     }
91
92     @Test
93     public void testGetInstance() {
94         PushPolicyHandler handler = PushPolicyHandler.getInstance();
95         assertNotNull(handler);
96     }
97 }