Consolidate PolicyRestAdapter setup
[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
30 import com.mockrunner.mock.web.MockHttpServletRequest;
31 import com.mockrunner.mock.web.MockHttpServletResponse;
32
33 import java.util.Collections;
34 import java.util.List;
35
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;
53
54 @RunWith(PowerMockRunner.class)
55 public class DeleteHandlerTest {
56     @Before
57     public void setUp() {
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());
65     }
66
67     @Test
68     public void testGets() {
69         DeleteHandler handler = new DeleteHandler();
70         assertNotNull(handler);
71         assertEquals(handler.preSafetyCheck(null), true);
72         assertNull(handler.getDeletedGroup());
73     }
74
75     @Test
76     public void testGetInstance() {
77         DeleteHandler handler = DeleteHandler.getInstance();
78         assertNotNull(handler);
79     }
80
81     @PrepareForTest({DeleteHandler.class, XACMLPapServlet.class})
82     @Test
83     public void testDeletes() throws Exception {
84         // Mock request
85         DeleteHandler handler = new DeleteHandler();
86         MockHttpServletRequest request = new MockHttpServletRequest();
87         request.setBodyContent("{\n\"PAPPolicyType\": \"StdPAPPolicy\"\n}\n");
88
89         // Mock servlet
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);
94
95         // Mock elastic search
96         PolicyElasticSearchController controller = Mockito.mock(PolicyElasticSearchController.class);
97         PowerMockito.whenNew(PolicyElasticSearchController.class).withNoArguments().thenReturn(controller);
98
99         // Test deletion from PAP
100         MockHttpServletResponse response = new MockHttpServletResponse();
101         try {
102             handler.doApiDeleteFromPap(request, response);
103         } catch (Exception ex) {
104             fail("Not expecting an exception: " + ex);
105         }
106
107         // Test deletion from PDP
108         OnapLoggingContext loggingContext = Mockito.mock(OnapLoggingContext.class);
109         try {
110             handler.doApiDeleteFromPdp(request, response, loggingContext);
111         } catch (Exception ex) {
112             fail("Not expecting an exception: " + ex);
113         }
114
115         // Test delete entity
116         PolicyEntity policyEntity = new PolicyEntity();
117         policyEntity.setPolicyName("testVal");
118         String result = DeleteHandler.deletePolicyEntityData(policyEntity);
119         assertEquals(result, "success");
120
121         // Test check entity
122         List<?> peResult = Collections.emptyList();
123         assertEquals(DeleteHandler.checkPolicyGroupEntity(peResult), false);
124     }
125 }