d81c919aefbcca5a163084ecf53dbf85a0e6256e
[holmes/common.git] / holmes-actions / src / test / java / org / openo / holmes / common / utils / UserUtilTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openo.holmes.common.utils;
18
19 import static org.hamcrest.core.IsEqual.equalTo;
20 import static org.junit.Assert.assertThat;
21
22 import javax.servlet.http.HttpServletRequest;
23 import org.easymock.EasyMock;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.powermock.api.easymock.PowerMock;
28 public class UserUtilTest {
29
30     private HttpServletRequest request;
31
32     @Before
33     public void before() throws Exception {
34         request = PowerMock.createMock(HttpServletRequest.class);
35     }
36
37     @After
38     public void after() throws Exception {
39     }
40
41     @Test
42     public void getUserName_header_name_empty() throws Exception {
43         EasyMock.expect(request.getHeader("username")).andReturn(null);
44
45         PowerMock.replayAll();
46
47         String userName = UserUtil.getUserName(request);
48
49         PowerMock.verifyAll();
50
51         assertThat("admin", equalTo(userName));
52     }
53
54     @Test
55     public void getUserName_normal() throws Exception {
56         EasyMock.expect(request.getHeader("username")).andReturn("name1");
57
58         PowerMock.replayAll();
59
60         String userName = UserUtil.getUserName(request);
61
62         PowerMock.verifyAll();
63
64         assertThat("name1", equalTo(userName));
65     }
66
67