2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.controller;
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;
39 @RunWith(PowerMockRunner.class)
40 public class AutoPushControllerTest {
41 private PolicyController controller = new PolicyController();;
42 private AutoPushController apController = new AutoPushController();
45 public ExpectedException thrown = ExpectedException.none();
48 public void testAutoPushSetGet() throws IOException {
50 apController.setPolicyController(controller);
51 assertEquals(apController.getPolicyController(), controller);
55 public void testNegativeCase1() {
57 apController.getPolicyGroupContainerData(null, null);
58 } catch (Exception ex) {
59 fail("No exceptions expected, received: " + ex);
64 public void testNegativeCase2() throws IOException {
65 thrown.expect(NullPointerException.class);
66 apController.pushPolicyToPDPGroup(null, null);
70 public void testNegativeCase3() throws IOException {
71 thrown.expect(NullPointerException.class);
72 apController.removePDPGroup(null, null);
75 @Test(expected = NullPointerException.class)
76 public void testRefresh() throws IOException {
77 apController.refreshGroups();
80 @PrepareForTest({UserUtils.class})
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);
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);
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);
100 apController.pushPolicyToPDPGroup(request, response);
101 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
104 apController.removePDPGroup(request, response);
105 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);