Reformat POLICY-SDK-APP test cases
[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 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 package org.onap.policy.controller;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Mockito.when;
28 import java.io.IOException;
29 import java.util.Collections;
30 import javax.servlet.http.HttpServletResponse;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mockito;
34 import org.onap.policy.rest.dao.CommonClassDao;
35 import org.onap.portalsdk.core.domain.User;
36 import org.onap.portalsdk.core.web.support.UserUtils;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import org.springframework.web.servlet.ModelAndView;
41 import com.mockrunner.mock.web.MockHttpServletRequest;
42 import com.mockrunner.mock.web.MockHttpServletResponse;
43
44 @RunWith(PowerMockRunner.class)
45 public class PolicyNotificationControllerTest {
46     @PrepareForTest({UserUtils.class})
47     @Test
48     public void testWatch() throws IOException {
49         // Mock user utilities
50         PowerMockito.mockStatic(UserUtils.class);
51         User user = new User();
52         user.setOrgUserId("testID");
53         when(UserUtils.getUserSession(any())).thenReturn(user);
54
55         // Mock database
56         CommonClassDao dao = Mockito.mock(CommonClassDao.class);
57         Mockito.when(dao.getDataByQuery(any(), any())).thenReturn(Collections.emptyList());
58
59         // Test watch
60         PolicyNotificationController controller = new PolicyNotificationController();
61         controller.commonClassDao = dao;
62         MockHttpServletRequest request = new MockHttpServletRequest();
63         request.setBodyContent(
64                 "{\n\"watchData\": {\"name\": \"testVal\",\"path\": \"testPath\"\n}}\n");
65         MockHttpServletResponse response = new MockHttpServletResponse();
66         ModelAndView model = controller.watchPolicy(request, response);
67         assertNull(model);
68         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
69
70         // Negative test watch
71         request.setBodyContent(
72                 "{\n\"watchData\": {\"name\": \"testVal\",\"nopath\": \"testPath\"\n}}\n");
73         response = new MockHttpServletResponse();
74         model = controller.watchPolicy(request, response);
75         assertNull(model);
76         assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
77     }
78 }