Fix policy/engine due to sonar changes in common
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / handler / DeleteHandlerTest.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.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;
50
51 @RunWith(PowerMockRunner.class)
52 public class DeleteHandlerTest {
53     @Before
54     public void setUp() {
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());
62     }
63
64     @Test
65     public void testGets() {
66         DeleteHandler handler = new DeleteHandler();
67         assertNotNull(handler);
68         assertEquals(handler.preSafetyCheck(null), true);
69         assertNull(handler.getDeletedGroup());
70     }
71
72     @Test
73     public void testGetInstance() {
74         DeleteHandler handler = DeleteHandler.getInstance();
75         assertNotNull(handler);
76     }
77
78     @PrepareForTest({DeleteHandler.class, XACMLPapServlet.class})
79     @Test
80     public void testDeletes() throws Exception {
81         // Mock request
82         DeleteHandler handler = new DeleteHandler();
83         MockHttpServletRequest request = new MockHttpServletRequest();
84         request.setBodyContent("{\n\"PAPPolicyType\": \"StdPAPPolicy\"\n}\n");
85
86         // Mock servlet
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);
91
92         // Mock elastic search
93         PolicyElasticSearchController controller = Mockito.mock(PolicyElasticSearchController.class);
94         PowerMockito.whenNew(PolicyElasticSearchController.class).withNoArguments().thenReturn(controller);
95
96                 // Test deletion from PAP
97                 MockHttpServletResponse response = new MockHttpServletResponse();
98                 try {
99                         handler.doApiDeleteFromPap(request, response);
100                 } catch (Exception ex) {
101                         fail("Not expecting an exception: " + ex);
102                 }
103
104         // Test deletion from PDP
105         OnapLoggingContext loggingContext = Mockito.mock(OnapLoggingContext.class);
106         try {
107             handler.doApiDeleteFromPdp(request, response, loggingContext);
108         }
109         catch (Exception ex) {
110             fail("Not expecting an exception: " + ex);
111         }
112
113                 // Test delete entity
114                 PolicyEntity policyEntity = new PolicyEntity();
115                 policyEntity.setPolicyName("testVal");
116                 String result = DeleteHandler.deletePolicyEntityData(policyEntity);
117                 assertEquals(result, "success");
118
119                 // Test check entity
120                 List<?> peResult = Collections.emptyList();
121                 assertEquals(DeleteHandler.checkPolicyGroupEntity(peResult), false);
122         }
123 }