Modify ONAP PAP REST classes basic checkstyle
[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 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.Mockito.when;
28 import static org.mockito.Matchers.any;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.onap.policy.common.logging.ONAPLoggingContext;
33 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
34 import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController;
35 import org.onap.policy.rest.jpa.PolicyEntity;
36 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
37 import org.onap.policy.xacml.std.pap.StdEngine;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import com.mockrunner.mock.web.MockHttpServletRequest;
42 import com.mockrunner.mock.web.MockHttpServletResponse;
43 import java.sql.Connection;
44 import java.util.Collections;
45 import java.util.List;
46 import javax.persistence.EntityManager;
47
48 @RunWith(PowerMockRunner.class)
49 public class DeleteHandlerTest {
50     @Test
51     public void testGets() {
52         DeleteHandler handler = new DeleteHandler();
53         assertNotNull(handler);
54         assertEquals(handler.preSafetyCheck(null), true);
55         assertNull(handler.getDeletedGroup());
56     }
57
58     @Test
59     public void testGetInstance() {
60         DeleteHandler handler = DeleteHandler.getInstance();
61         assertNotNull(handler);
62     }
63
64     @PrepareForTest({DeleteHandler.class, XACMLPapServlet.class})
65     @Test
66     public void testDeletes() throws Exception {
67         // Mock request
68         DeleteHandler handler = new DeleteHandler();
69         MockHttpServletRequest request = new MockHttpServletRequest();
70         request.setBodyContent("{\n\"PAPPolicyType\": \"StdPAPPolicy\"\n}\n");
71
72         // Mock servlet
73         PAPPolicyEngine engine = Mockito.mock(StdEngine.class);
74         PowerMockito.mockStatic(XACMLPapServlet.class);
75         when(XACMLPapServlet.getPAPEngine()).thenReturn(engine);
76         when(engine.getGroup(any())).thenReturn(null);
77
78         // Mock elastic search
79         PolicyElasticSearchController controller = Mockito.mock(PolicyElasticSearchController.class);
80         PowerMockito.whenNew(PolicyElasticSearchController.class).withNoArguments().thenReturn(controller);
81
82         // Mock entity manager
83         EntityManager em = Mockito.mock(EntityManager.class);
84
85         // Test deletion from PAP
86         MockHttpServletResponse response = new MockHttpServletResponse();
87         try {
88             handler.doAPIDeleteFromPAP(request, response);
89         }
90         catch (Exception ex) {
91             fail("Not expecting an exception: " + ex);
92         }
93
94         // Test deletion from PDP
95         ONAPLoggingContext loggingContext = Mockito.mock(ONAPLoggingContext.class);
96         try {
97             handler.doAPIDeleteFromPDP(request, response, loggingContext);
98         }
99         catch (Exception ex) {
100             fail("Not expecting an exception: " + ex);
101         }
102
103         // Test delete entity
104         PolicyEntity policyEntity = new PolicyEntity();
105         policyEntity.setPolicyName("testVal");
106         String result = DeleteHandler.deletePolicyEntityData(em, policyEntity);
107         assertEquals(result, "success");
108
109         // Test check entity
110         Connection con = null;
111         List<?> peResult = Collections.emptyList();
112         assertEquals(DeleteHandler.checkPolicyGroupEntity(con, peResult), false);
113     }
114 }