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.handler;
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 import com.mockrunner.mock.web.MockHttpServletResponse;
30 import java.util.ArrayList;
31 import java.util.List;
32 import javax.script.SimpleBindings;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.PolicyVersion;
39 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
40 import org.onap.policy.xacml.std.pap.StdPDPGroup;
41 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
44 public class PushPolicyHandlerTest {
46 public void testGetsAndChecks() {
47 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
49 PushPolicyHandler handler = new PushPolicyHandler(commonClassDao);
50 assertNotNull(handler);
53 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
54 HttpServletResponse response = Mockito.mock(MockHttpServletResponse.class);
55 Mockito.when(request.getParameter("policyScope")).thenReturn("com");
56 Mockito.when(request.getParameter("filePrefix")).thenReturn("Config_");
57 Mockito.when(request.getParameter("policyName")).thenReturn("testPush");
58 PolicyVersion version = new PolicyVersion();
59 version.setActiveVersion(1);
60 version.setCreatedBy("API");
61 version.setHigherVersion(1);
63 version.setModifiedBy("API");
64 version.setPolicyName("com" + File.separator + "Config_testPush");
65 List<PolicyVersion> list = new ArrayList<>();
68 doNothing().when(commonClassDao).save(any(PolicyVersion.class));
69 doReturn(list).when(commonClassDao).getDataByQuery(any(String.class), any(SimpleBindings.class));
71 handler.getActiveVersion(request, response);
73 Mockito.verify(response).setStatus(HttpServletResponse.SC_OK);
74 Mockito.verify(response).addHeader("version", "1");
76 request = Mockito.mock(HttpServletRequest.class);
77 response = Mockito.mock(MockHttpServletResponse.class);
78 Mockito.when(request.getParameter("gitPath")).thenReturn("testPath");
79 handler.getSelectedURI(request, response);
80 Mockito.verify(response).setStatus(HttpServletResponse.SC_OK);
82 StdPDPPolicy policy = new StdPDPPolicy();
83 OnapPDPGroup onapPolicy = new StdPDPGroup();
84 String configHome = "testVal";
85 assertEquals(handler.preSafetyCheck(policy, configHome), true);
86 assertEquals(handler.preSafetyCheck(onapPolicy, configHome), true);
90 public void testGetInstance() {
91 PushPolicyHandler handler = PushPolicyHandler.getInstance();
92 assertNotNull(handler);