81fe8a7abb3bcd951f6e90627d8ba266e2bd62a5
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / handler / PushPolicyHandlerTest.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 org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
29 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
30 import org.onap.policy.xacml.std.pap.StdPDPGroup;
31 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
32 import org.powermock.api.mockito.PowerMockito;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35 import com.mockrunner.mock.web.MockHttpServletRequest;
36 import com.mockrunner.mock.web.MockHttpServletResponse;
37 import javax.persistence.EntityManager;
38 import javax.persistence.EntityManagerFactory;
39 import javax.persistence.EntityTransaction;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.persistence.Query;
42
43 @RunWith(PowerMockRunner.class)
44 public class PushPolicyHandlerTest {
45         @PrepareForTest({XACMLPapServlet.class})
46         @Test
47         public void testGetsAndChecks() {
48                 // Mock servlet, entity mgr, transaction, and query
49                 EntityManager em = Mockito.mock(EntityManager.class);
50                 EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
51                 PowerMockito.mockStatic(XACMLPapServlet.class);
52                 PowerMockito.when(XACMLPapServlet.getEmf()).thenReturn(emf);
53                 Mockito.when(emf.createEntityManager()).thenReturn(em);
54                 EntityTransaction transaction = Mockito.mock(EntityTransaction.class);
55                 Mockito.when(em.getTransaction()).thenReturn(transaction);
56                 Query query = Mockito.mock(Query.class);
57                 Mockito.when(em.createQuery(Mockito.anyString())).thenReturn(query);
58                 
59                 // Test constructor
60                 PushPolicyHandler handler = new PushPolicyHandler();
61                 assertNotNull(handler);
62
63                 // Test gets
64                 MockHttpServletRequest request = new MockHttpServletRequest();
65                 MockHttpServletResponse response = new MockHttpServletResponse();
66                 handler.getActiveVersion(request, response);
67                 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
68                 request.setupAddParameter("gitPath", "testPath");
69                 handler.getSelectedURI(request, response);
70                 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
71                 
72                 // Test check
73                 StdPDPPolicy policy = new StdPDPPolicy();
74                 OnapPDPGroup onapPolicy = new StdPDPGroup();
75                 String configHome = "testVal";
76                 assertEquals(handler.preSafetyCheck(policy, configHome), true);
77                 assertEquals(handler.preSafetyCheck(onapPolicy, configHome), true);
78         }
79         
80         @Test
81         public void testGetInstance() {
82                 PushPolicyHandler handler = PushPolicyHandler.getInstance();
83                 assertNotNull(handler);
84         }
85 }