Merge "Change default log location per logging guidelines"
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.when;
26 import java.io.IOException;
27 import java.util.Collections;
28 import javax.servlet.http.HttpServletResponse;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.onap.policy.rest.dao.CommonClassDao;
33 import org.onap.portalsdk.core.domain.User;
34 import org.onap.portalsdk.core.web.support.UserUtils;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import org.springframework.web.servlet.ModelAndView;
39 import com.mockrunner.mock.web.MockHttpServletRequest;
40 import com.mockrunner.mock.web.MockHttpServletResponse;
41
42 @RunWith(PowerMockRunner.class)
43 public class PolicyNotificationControllerTest {
44         @PrepareForTest({UserUtils.class})
45         @Test
46         public void testWatch() throws IOException{
47                 // Mock user utilities
48                 PowerMockito.mockStatic(UserUtils.class);
49                 User user = new User();
50                 user.setOrgUserId("testID");
51                 when(UserUtils.getUserSession(any())).thenReturn(user);
52                 
53                 // Mock database
54                 CommonClassDao dao = Mockito.mock(CommonClassDao.class);
55                 Mockito.when(dao.getDataByQuery(any(), any())).thenReturn(Collections.emptyList());
56
57                 // Test watch
58                 PolicyNotificationController controller = new PolicyNotificationController();
59                 controller.commonClassDao = dao;
60                 MockHttpServletRequest request = new MockHttpServletRequest();
61                 request.setBodyContent("{\n\"watchData\": {\"name\": \"testVal\",\"path\": \"testPath\"\n}}\n");
62                 MockHttpServletResponse response = new MockHttpServletResponse();
63                 ModelAndView model = controller.watchPolicy(request, response);
64                 assertNull(model);
65                 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
66
67                 // Negative test watch
68                 request.setBodyContent("{\n\"watchData\": {\"name\": \"testVal\",\"nopath\": \"testPath\"\n}}\n");
69                 response = new MockHttpServletResponse();
70                 model = controller.watchPolicy(request, response);
71                 assertNull(model);
72                 assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
73         }
74 }