2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controller;
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;
41 @RunWith(PowerMockRunner.class)
42 public class AutoPushControllerTest {
43 private PolicyController controller = new PolicyController();;
44 private AutoPushController apController = new AutoPushController();
47 public ExpectedException thrown = ExpectedException.none();
50 public void testAutoPushSetGet() throws IOException {
52 apController.setPolicyController(controller);
53 assertEquals(apController.getPolicyController(), controller);
57 public void testNegativeCase1() {
59 apController.getPolicyGroupContainerData(null, null);
60 } catch (Exception ex) {
61 fail("No exceptions expected, received: " + ex);
66 public void testNegativeCase2() throws IOException {
67 thrown.expect(NullPointerException.class);
68 apController.pushPolicyToPDPGroup(null, null);
72 public void testNegativeCase3() throws IOException {
73 thrown.expect(NullPointerException.class);
74 apController.removePDPGroup(null, null);
77 @Test(expected = NullPointerException.class)
78 public void testRefresh() throws IOException {
79 apController.refreshGroups();
82 @PrepareForTest({UserUtils.class})
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);
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);
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);
102 apController.pushPolicyToPDPGroup(request, response);
103 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
106 apController.removePDPGroup(request, response);
107 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);