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.fail;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Mockito.when;
29 import com.mockrunner.mock.web.MockHttpServletRequest;
30 import com.mockrunner.mock.web.MockHttpServletResponse;
31 import java.util.Collections;
32 import java.util.List;
33 import org.hibernate.Session;
34 import org.hibernate.SessionFactory;
35 import org.hibernate.Transaction;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.onap.policy.common.logging.ONAPLoggingContext;
41 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
42 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
43 import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController;
44 import org.onap.policy.rest.jpa.PolicyEntity;
45 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
46 import org.onap.policy.xacml.std.pap.StdEngine;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
51 @RunWith(PowerMockRunner.class)
52 public class DeleteHandlerTest {
55 SessionFactory mockedSessionFactory = Mockito.mock(SessionFactory.class);
56 Session mockedSession = Mockito.mock(Session.class);
57 Transaction mockedTransaction = Mockito.mock(Transaction.class);
58 Mockito.when(mockedSessionFactory.openSession()).thenReturn(mockedSession);
59 Mockito.when(mockedSession.beginTransaction()).thenReturn(mockedTransaction);
60 CommonClassDaoImpl.setSessionfactory(mockedSessionFactory);
61 new DeleteHandler(new CommonClassDaoImpl());
65 public void testGets() {
66 DeleteHandler handler = new DeleteHandler();
67 assertNotNull(handler);
68 assertEquals(handler.preSafetyCheck(null), true);
69 assertNull(handler.getDeletedGroup());
73 public void testGetInstance() {
74 DeleteHandler handler = DeleteHandler.getInstance();
75 assertNotNull(handler);
78 @PrepareForTest({DeleteHandler.class, XACMLPapServlet.class})
80 public void testDeletes() throws Exception {
82 DeleteHandler handler = new DeleteHandler();
83 MockHttpServletRequest request = new MockHttpServletRequest();
84 request.setBodyContent("{\n\"PAPPolicyType\": \"StdPAPPolicy\"\n}\n");
87 PAPPolicyEngine engine = Mockito.mock(StdEngine.class);
88 PowerMockito.mockStatic(XACMLPapServlet.class);
89 when(XACMLPapServlet.getPAPEngine()).thenReturn(engine);
90 when(engine.getGroup(any())).thenReturn(null);
92 // Mock elastic search
93 PolicyElasticSearchController controller = Mockito.mock(PolicyElasticSearchController.class);
94 PowerMockito.whenNew(PolicyElasticSearchController.class).withNoArguments().thenReturn(controller);
96 // Test deletion from PAP
97 MockHttpServletResponse response = new MockHttpServletResponse();
99 handler.doAPIDeleteFromPAP(request, response);
100 } catch (Exception ex) {
101 fail("Not expecting an exception: " + ex);
104 // Test deletion from PDP
105 ONAPLoggingContext loggingContext = Mockito.mock(ONAPLoggingContext.class);
107 handler.doAPIDeleteFromPDP(request, response, loggingContext);
109 catch (Exception ex) {
110 fail("Not expecting an exception: " + ex);
113 // Test delete entity
114 PolicyEntity policyEntity = new PolicyEntity();
115 policyEntity.setPolicyName("testVal");
116 String result = DeleteHandler.deletePolicyEntityData(policyEntity);
117 assertEquals(result, "success");
120 List<?> peResult = Collections.emptyList();
121 assertEquals(DeleteHandler.checkPolicyGroupEntity(peResult), false);