policy/engine jdk11 upgrades
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / PolicyNotificationControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.controller;
24
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;
29
30 import com.mockrunner.mock.web.MockHttpServletRequest;
31 import com.mockrunner.mock.web.MockHttpServletResponse;
32
33 import java.io.IOException;
34 import java.util.Collections;
35
36 import javax.servlet.http.HttpServletResponse;
37
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;
49
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 {
54     @Test
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);
61
62         // Mock database
63         CommonClassDao dao = Mockito.mock(CommonClassDao.class);
64         Mockito.when(dao.getDataByQuery(any(), any())).thenReturn(Collections.emptyList());
65
66         // Test watch
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);
73         assertNull(model);
74         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
75
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);
80         assertNull(model);
81         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
82     }
83 }