Add and Modify jUnits for code coverage (model, admin)
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / admin / PolicyUserInfoControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017, 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.admin;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.atLeast;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.io.IOException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import javax.servlet.http.HttpSession;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.portalsdk.core.domain.User;
40 import org.onap.portalsdk.core.util.SystemProperties;
41 import org.springframework.mock.web.MockHttpServletResponse;
42
43 public class PolicyUserInfoControllerTest {
44
45     private HttpServletRequest request;
46     private MockHttpServletResponse response;
47
48     @Before
49     public void setUp() throws Exception {
50         HttpSession mockSession = mock(HttpSession.class);
51         request = mock(HttpServletRequest.class);
52         response = new MockHttpServletResponse();
53         User user = new User();
54         user.setOrgUserId("Test");
55         Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
56         Mockito.when(request.getSession(false)).thenReturn(mockSession);
57     }
58
59     @Test
60     public final void testGetPolicyUserInfo() {
61         PolicyUserInfoController controller = new PolicyUserInfoController();
62         controller.getPolicyUserInfo(request, response);
63         try {
64             assertTrue(response.getStatus() == 200);
65         } catch (Exception e) {
66             fail();
67         }
68     }
69
70     @SuppressWarnings("unchecked")
71     @Test
72     public void testGetPolicyUserInfoException() throws IOException {
73         HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
74         when(mockResponse.getWriter()).thenThrow(IOException.class);
75         PolicyUserInfoController controller = new PolicyUserInfoController();
76         controller.getPolicyUserInfo(request, mockResponse);
77         verify(mockResponse, atLeast(1)).getWriter();
78     }
79 }