2 * Copyright © 2018 European Support Limited
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.onap.sdc.workflow.server.resolvers;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24 import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;
26 import javax.servlet.http.HttpServletRequest;
27 import org.junit.Test;
28 import org.onap.sdc.workflow.services.annotations.UserId;
29 import org.springframework.core.MethodParameter;
30 import org.springframework.web.bind.ServletRequestBindingException;
31 import org.springframework.web.context.request.NativeWebRequest;
34 * Tests injection of user ID from HTTP headers.
39 public class UserIdResolverTest {
42 public void oneHeaderSelectedWhenMultipleUserIdHeadersSent() throws ServletRequestBindingException {
44 final String headerValue = "UserIdValueFromHeader";
46 HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
47 when(servletRequestMock.getHeader(USER_ID_HEADER)).thenReturn(headerValue);
49 NativeWebRequest webRequestMock = mock(NativeWebRequest.class);
50 when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(servletRequestMock);
52 Object resolved = new UserIdResolver().resolveArgument(null, null, webRequestMock, null);
53 assertEquals(headerValue, resolved);
56 @Test(expected = IllegalStateException.class)
57 public void illegalTypeErrorThrownWhenAnnotatedParameterIsNotOfTypeString() {
58 MethodParameter methodParameterMock = mock(MethodParameter.class);
59 when(methodParameterMock.hasParameterAnnotation(UserId.class)).thenReturn(true);
60 //noinspection unchecked
61 when(methodParameterMock.getParameterType()).thenReturn((Class)String[].class);
62 new UserIdResolver().supportsParameter(methodParameterMock);
65 @Test(expected = ServletRequestBindingException.class)
66 public void missingHeaderErrorThrownWhenUserIdHeaderNotPopulated() throws ServletRequestBindingException {
67 NativeWebRequest webRequestMock = mock(NativeWebRequest.class);
68 when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(mock(HttpServletRequest.class));
69 new UserIdResolver().resolveArgument(null, null, webRequestMock, null);
72 @Test(expected = NullPointerException.class)
73 public void exceptionThrownWhenRequestTypeIsNotHttpRequest() throws ServletRequestBindingException {
74 NativeWebRequest webRequestMock = mock(NativeWebRequest.class);
75 new UserIdResolver().resolveArgument(null, null, webRequestMock, null);
79 public void parameterNotSupportedWhenNotAnnotatedWithUserIdAnnotation() {
80 MethodParameter methodParameterMock = mock(MethodParameter.class);
81 assertFalse(new UserIdResolver().supportsParameter(methodParameterMock));
85 public void parameterSupportedWhenAnnotatedWithUserIdAnnotationAndOfTypeString() {
86 MethodParameter methodParameterMock = mock(MethodParameter.class);
87 when(methodParameterMock.hasParameterAnnotation(UserId.class)).thenReturn(true);
88 //noinspection unchecked
89 when(methodParameterMock.getParameterType()).thenReturn((Class) String.class);
90 assertTrue(new UserIdResolver().supportsParameter(methodParameterMock));