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