Cenralized handling of USER_ID header
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / test / java / org / onap / sdc / workflow / server / resolvers / UserIdResolverTest.java
1 /*
2  * Copyright © 2018 European Support Limited
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.onap.sdc.workflow.server.resolvers;
18
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;
25
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;
32
33 /**
34  * Tests injection of user ID from HTTP headers.
35  *
36  * @author evitaliy
37  * @since 21 Aug 2018
38  */
39 public class UserIdResolverTest {
40
41     @Test
42     public void oneHeaderSelectedWhenMultipleUserIdHeadersSent() throws ServletRequestBindingException {
43
44         final String headerValue = "UserIdValueFromHeader";
45
46         HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
47         when(servletRequestMock.getHeader(USER_ID_HEADER)).thenReturn(headerValue);
48
49         NativeWebRequest webRequestMock = mock(NativeWebRequest.class);
50         when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(servletRequestMock);
51
52         Object resolved = new UserIdResolver().resolveArgument(null, null, webRequestMock, null);
53         assertEquals(headerValue, resolved);
54     }
55
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);
63     }
64
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);
70     }
71
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);
76     }
77
78     @Test
79     public void parameterNotSupportedWhenNotAnnotatedWithUserIdAnnotation() {
80         MethodParameter methodParameterMock = mock(MethodParameter.class);
81         assertFalse(new UserIdResolver().supportsParameter(methodParameterMock));
82     }
83
84     @Test
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));
91     }
92 }