Format java POLICY-SDK-APP
[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-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
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.Matchers.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.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.springframework.web.servlet.ModelAndView;
48
49 @RunWith(PowerMockRunner.class)
50 public class PolicyNotificationControllerTest {
51     @PrepareForTest({UserUtils.class})
52     @Test
53     public void testWatch() throws IOException {
54         // Mock user utilities
55         PowerMockito.mockStatic(UserUtils.class);
56         User user = new User();
57         user.setOrgUserId("testID");
58         when(UserUtils.getUserSession(any())).thenReturn(user);
59
60         // Mock database
61         CommonClassDao dao = Mockito.mock(CommonClassDao.class);
62         Mockito.when(dao.getDataByQuery(any(), any())).thenReturn(Collections.emptyList());
63
64         // Test watch
65         PolicyNotificationController controller = new PolicyNotificationController();
66         controller.commonClassDao = dao;
67         MockHttpServletRequest request = new MockHttpServletRequest();
68         request.setBodyContent("{\n\"watchData\": {\"name\": \"testVal\",\"path\": \"testPath\"\n}}\n");
69         MockHttpServletResponse response = new MockHttpServletResponse();
70         ModelAndView model = controller.watchPolicy(request, response);
71         assertNull(model);
72         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
73
74         // Negative test watch
75         request.setBodyContent("{\n\"watchData\": {\"name\": \"testVal\",\"nopath\": \"testPath\"\n}}\n");
76         response = new MockHttpServletResponse();
77         model = controller.watchPolicy(request, response);
78         assertNull(model);
79         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
80     }
81 }