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;
30 import com.mockrunner.mock.web.MockHttpServletRequest;
31 import com.mockrunner.mock.web.MockHttpServletResponse;
33 import java.util.Collections;
34 import java.util.List;
36 import org.hibernate.Session;
37 import org.hibernate.SessionFactory;
38 import org.hibernate.Transaction;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mockito;
43 import org.onap.policy.common.logging.OnapLoggingContext;
44 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
45 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
46 import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController;
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();
102 handler.doApiDeleteFromPap(request, response);
103 } catch (Exception ex) {
104 fail("Not expecting an exception: " + ex);
107 // Test deletion from PDP
108 OnapLoggingContext loggingContext = Mockito.mock(OnapLoggingContext.class);
110 handler.doApiDeleteFromPdp(request, response, loggingContext);
111 } catch (Exception ex) {
112 fail("Not expecting an exception: " + ex);
115 // Test delete entity
116 PolicyEntity policyEntity = new PolicyEntity();
117 policyEntity.setPolicyName("testVal");
118 String result = DeleteHandler.deletePolicyEntityData(policyEntity);
119 assertEquals(result, "success");
122 List<?> peResult = Collections.emptyList();
123 assertEquals(DeleteHandler.checkPolicyGroupEntity(peResult), false);