2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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.assertNull;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
30 import com.mockrunner.mock.web.MockHttpServletRequest;
31 import com.mockrunner.mock.web.MockHttpServletResponse;
33 import java.io.IOException;
34 import java.util.Collections;
36 import javax.servlet.http.HttpServletResponse;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.portalsdk.core.domain.User;
43 import org.onap.portalsdk.core.web.support.UserUtils;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PowerMockIgnore;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import org.springframework.web.servlet.ModelAndView;
50 @RunWith(PowerMockRunner.class)
51 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
52 @PrepareForTest({UserUtils.class})
53 public class PolicyNotificationControllerTest {
55 public void testWatch() throws IOException {
56 // Mock user utilities
57 PowerMockito.mockStatic(UserUtils.class);
58 User user = new User();
59 user.setOrgUserId("testID");
60 when(UserUtils.getUserSession(any())).thenReturn(user);
63 CommonClassDao dao = Mockito.mock(CommonClassDao.class);
64 Mockito.when(dao.getDataByQuery(any(), any())).thenReturn(Collections.emptyList());
67 PolicyNotificationController controller = new PolicyNotificationController();
68 controller.commonClassDao = dao;
69 MockHttpServletRequest request = new MockHttpServletRequest();
70 request.setBodyContent("{\n\"watchData\": {\"name\": \"testVal\",\"path\": \"testPath\"\n}}\n");
71 MockHttpServletResponse response = new MockHttpServletResponse();
72 ModelAndView model = controller.watchPolicy(request, response);
74 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
76 // Negative test watch
77 request.setBodyContent("{\n\"watchData\": {\"name\": \"testVal\",\"nopath\": \"testPath\"\n}}\n");
78 response = new MockHttpServletResponse();
79 model = controller.watchPolicy(request, response);
81 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);