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.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Mockito.when;
30 import com.mockrunner.mock.web.MockHttpServletRequest;
31 import com.mockrunner.mock.web.MockHttpServletResponse;
32 import java.io.IOException;
33 import java.util.Collections;
34 import java.util.List;
35 import org.hibernate.Session;
36 import org.hibernate.SessionFactory;
37 import org.hibernate.Transaction;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.logging.OnapLoggingContext;
43 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
44 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
45 import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController;
46 import org.onap.policy.rest.dao.CommonClassDao;
47 import org.onap.policy.rest.jpa.PolicyEntity;
48 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
49 import org.onap.policy.xacml.std.pap.StdEngine;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
54 @RunWith(PowerMockRunner.class)
55 public class DeleteHandlerTest {
58 SessionFactory mockedSessionFactory = Mockito.mock(SessionFactory.class);
59 Session mockedSession = Mockito.mock(Session.class);
60 Transaction mockedTransaction = Mockito.mock(Transaction.class);
61 Mockito.when(mockedSessionFactory.openSession()).thenReturn(mockedSession);
62 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
63 CommonClassDaoImpl.setSessionfactory(mockedSessionFactory);
64 new DeleteHandler(new CommonClassDaoImpl());
68 public void testGets() {
69 DeleteHandler handler = new DeleteHandler();
70 assertNotNull(handler);
71 assertEquals(handler.preSafetyCheck(null), true);
72 assertNull(handler.getDeletedGroup());
76 public void testGetInstance() {
77 DeleteHandler handler = DeleteHandler.getInstance();
78 assertNotNull(handler);
81 @PrepareForTest({DeleteHandler.class, XACMLPapServlet.class})
83 public void testDeletes() throws Exception {
85 DeleteHandler handler = new DeleteHandler();
86 MockHttpServletRequest request = new MockHttpServletRequest();
87 request.setBodyContent("{\n\"PAPPolicyType\": \"StdPAPPolicy\"\n}\n");
90 PAPPolicyEngine engine = Mockito.mock(StdEngine.class);
91 PowerMockito.mockStatic(XACMLPapServlet.class);
92 when(XACMLPapServlet.getPAPEngine()).thenReturn(engine);
93 when(engine.getGroup(any())).thenReturn(null);
95 // Mock elastic search
96 PolicyElasticSearchController controller = Mockito.mock(PolicyElasticSearchController.class);
97 PowerMockito.whenNew(PolicyElasticSearchController.class).withNoArguments().thenReturn(controller);
99 // Test deletion from PAP
100 MockHttpServletResponse response = new MockHttpServletResponse();
101 handler.doApiDeleteFromPap(request, response);
103 // Test deletion from PDP
104 OnapLoggingContext loggingContext = Mockito.mock(OnapLoggingContext.class);
105 handler.doApiDeleteFromPdp(request, response, loggingContext);
107 // Test delete entity
108 PolicyEntity policyEntity = new PolicyEntity();
109 policyEntity.setPolicyName("testVal");
110 String result = DeleteHandler.deletePolicyEntityData(policyEntity);
111 assertEquals(result, "success");
114 List<?> peResult = Collections.emptyList();
115 assertEquals(DeleteHandler.checkPolicyGroupEntity(peResult), false);
119 public void testDoDeletePap() throws IOException {
120 CommonClassDao dao = Mockito.mock(CommonClassDao.class);
121 DeleteHandler handler = new DeleteHandler(dao);
124 MockHttpServletRequest request = new MockHttpServletRequest();
125 request.setBodyContent(
126 "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n");
127 MockHttpServletResponse response = new MockHttpServletResponse();
129 handler.doApiDeleteFromPap(request, response);
130 assertTrue(response.containsHeader("error"));
133 @Test(expected = NullPointerException.class)
134 public void testDoDeletePdp() throws IOException {
135 CommonClassDao dao = Mockito.mock(CommonClassDao.class);
136 DeleteHandler handler = new DeleteHandler(dao);
137 OnapLoggingContext loggingContext = new OnapLoggingContext();
140 MockHttpServletRequest request = new MockHttpServletRequest();
141 request.setBodyContent(
142 "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n");
143 MockHttpServletResponse response = new MockHttpServletResponse();
145 handler.doApiDeleteFromPdp(request, response, loggingContext);