2 * ============LICENSE_START=======================================================
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
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=========================================================
23 package org.onap.policy.controller;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
28 import com.mockrunner.mock.web.MockHttpServletRequest;
29 import com.mockrunner.mock.web.MockHttpServletResponse;
31 import java.io.IOException;
33 import javax.servlet.http.HttpServletResponse;
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;
46 @RunWith(PowerMockRunner.class)
47 public class AutoPushControllerTest {
48 private PolicyController controller = new PolicyController();
49 private AutoPushController apController = new AutoPushController();
52 public ExpectedException thrown = ExpectedException.none();
55 public void testAutoPushSetGet() throws IOException {
57 apController.setPolicyController(controller);
58 assertEquals(apController.getPolicyController(), controller);
62 public void testNegativeCase1() {
64 apController.getPolicyGroupContainerData(null, null);
65 } catch (Exception ex) {
66 fail("No exceptions expected, received: " + ex);
71 public void testNegativeCase2() throws IOException {
72 thrown.expect(NullPointerException.class);
73 apController.pushPolicyToPDPGroup(null, null);
77 public void testNegativeCase3() throws IOException {
78 thrown.expect(NullPointerException.class);
79 apController.removePDPGroup(null, null);
82 @Test(expected = NullPointerException.class)
83 public void testRefresh() throws IOException {
84 apController.refreshGroups();
87 @PrepareForTest({UserUtils.class})
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);
95 // Mock policy controller
96 PolicyController policyController = Mockito.mock(PolicyController.class);
97 PowerMockito.whenNew(PolicyController.class).withNoArguments().thenReturn(policyController);
98 Mockito.when(policyController.getRoles(Mockito.any())).thenReturn(null);
100 // Test group container
101 MockHttpServletRequest request = new MockHttpServletRequest();
102 MockHttpServletResponse response = new MockHttpServletResponse();
103 apController.getPolicyGroupContainerData(request, response);
104 assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());
107 apController.pushPolicyToPDPGroup(request, response);
108 assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());
111 apController.removePDPGroup(request, response);
112 assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());