Merge "Reformat POLICY-SDK-APP test cases"
[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 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 package org.onap.policy.controller;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26 import java.io.IOException;
27 import javax.servlet.http.HttpServletResponse;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.onap.portalsdk.core.domain.User;
34 import org.onap.portalsdk.core.web.support.UserUtils;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import com.mockrunner.mock.web.MockHttpServletRequest;
39 import com.mockrunner.mock.web.MockHttpServletResponse;
40
41 @RunWith(PowerMockRunner.class)
42 public class AutoPushControllerTest {
43     private PolicyController controller = new PolicyController();;
44     private AutoPushController apController = new AutoPushController();
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     @Test
50     public void testAutoPushSetGet() throws IOException {
51         // Get and set tests
52         apController.setPolicyController(controller);
53         assertEquals(apController.getPolicyController(), controller);
54     }
55
56     @Test
57     public void testNegativeCase1() {
58         try {
59             apController.getPolicyGroupContainerData(null, null);
60         } catch (Exception ex) {
61             fail("No exceptions expected, received: " + ex);
62         }
63     }
64
65     @Test
66     public void testNegativeCase2() throws IOException {
67         thrown.expect(NullPointerException.class);
68         apController.pushPolicyToPDPGroup(null, null);
69     }
70
71     @Test
72     public void testNegativeCase3() throws IOException {
73         thrown.expect(NullPointerException.class);
74         apController.removePDPGroup(null, null);
75     }
76
77     @Test(expected = NullPointerException.class)
78     public void testRefresh() throws IOException {
79         apController.refreshGroups();
80     }
81
82     @PrepareForTest({UserUtils.class})
83     @Test
84     public void testRequests() throws Exception {
85         // Mock user utilities
86         PowerMockito.mockStatic(UserUtils.class);
87         User user = new User();
88         Mockito.when(UserUtils.getUserSession(Mockito.any())).thenReturn(user);
89
90         // Mock policy controller
91         PolicyController pController = Mockito.mock(PolicyController.class);
92         PowerMockito.whenNew(PolicyController.class).withNoArguments().thenReturn(pController);
93         Mockito.when(pController.getRoles(Mockito.any())).thenReturn(null);
94
95         // Test group container
96         MockHttpServletRequest request = new MockHttpServletRequest();
97         MockHttpServletResponse response = new MockHttpServletResponse();
98         apController.getPolicyGroupContainerData(request, response);
99         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
100
101         // Test push
102         apController.pushPolicyToPDPGroup(request, response);
103         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
104
105         // Test remove
106         apController.removePDPGroup(request, response);
107         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
108     }
109 }