7d2d5f8c2854b169ef5f87d7eb6ef2ed1df981c1
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / AutoPushControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.controller;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27
28 import com.mockrunner.mock.web.MockHttpServletRequest;
29 import com.mockrunner.mock.web.MockHttpServletResponse;
30
31 import java.io.IOException;
32
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.onap.portalsdk.core.domain.User;
41 import org.onap.portalsdk.core.web.support.UserUtils;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
45
46 @RunWith(PowerMockRunner.class)
47 public class AutoPushControllerTest {
48     private PolicyController controller = new PolicyController();;
49     private AutoPushController apController = new AutoPushController();
50
51     @Rule
52     public ExpectedException thrown = ExpectedException.none();
53
54     @Test
55     public void testAutoPushSetGet() throws IOException {
56         // Get and set tests
57         apController.setPolicyController(controller);
58         assertEquals(apController.getPolicyController(), controller);
59     }
60
61     @Test
62     public void testNegativeCase1() {
63         try {
64             apController.getPolicyGroupContainerData(null, null);
65         } catch (Exception ex) {
66             fail("No exceptions expected, received: " + ex);
67         }
68     }
69
70     @Test
71     public void testNegativeCase2() throws IOException {
72         thrown.expect(NullPointerException.class);
73         apController.pushPolicyToPDPGroup(null, null);
74     }
75
76     @Test
77     public void testNegativeCase3() throws IOException {
78         thrown.expect(NullPointerException.class);
79         apController.removePDPGroup(null, null);
80     }
81
82     @Test(expected = NullPointerException.class)
83     public void testRefresh() throws IOException {
84         apController.refreshGroups();
85     }
86
87     @PrepareForTest({UserUtils.class})
88     @Test
89     public void testRequests() throws Exception {
90         // Mock user utilities
91         PowerMockito.mockStatic(UserUtils.class);
92         User user = new User();
93         Mockito.when(UserUtils.getUserSession(Mockito.any())).thenReturn(user);
94
95         // Mock policy controller
96         PolicyController pController = Mockito.mock(PolicyController.class);
97         PowerMockito.whenNew(PolicyController.class).withNoArguments().thenReturn(pController);
98         Mockito.when(pController.getRoles(Mockito.any())).thenReturn(null);
99
100         // Test group container
101         MockHttpServletRequest request = new MockHttpServletRequest();
102         MockHttpServletResponse response = new MockHttpServletResponse();
103         apController.getPolicyGroupContainerData(request, response);
104         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
105
106         // Test push
107         apController.pushPolicyToPDPGroup(request, response);
108         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
109
110         // Test remove
111         apController.removePDPGroup(request, response);
112         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
113     }
114 }