2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.holmes.common.utils;
19 import static org.hamcrest.core.IsEqual.equalTo;
20 import static org.junit.Assert.assertThat;
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 {
30 private HttpServletRequest request;
33 public void before() throws Exception {
34 request = PowerMock.createMock(HttpServletRequest.class);
38 public void after() throws Exception {
42 public void getUserName_header_name_empty() throws Exception {
43 EasyMock.expect(request.getHeader("username")).andReturn(null);
45 PowerMock.replayAll();
47 String userName = UserUtil.getUserName(request);
49 PowerMock.verifyAll();
51 assertThat("admin", equalTo(userName));
55 public void getUserName_normal() throws Exception {
56 EasyMock.expect(request.getHeader("username")).andReturn("name1");
58 PowerMock.replayAll();
60 String userName = UserUtil.getUserName(request);
62 PowerMock.verifyAll();
64 assertThat("name1", equalTo(userName));