5fcb11e787af55a78883a27913d2ced98260b5d2
[sdc/sdc-workflow-designer.git] /
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.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;
26
27 import javax.servlet.http.HttpServletRequest;
28 import org.junit.jupiter.api.Test;
29 import org.onap.sdc.workflow.services.annotations.UserId;
30 import org.springframework.core.MethodParameter;
31 import org.springframework.web.bind.ServletRequestBindingException;
32 import org.springframework.web.context.request.NativeWebRequest;
33
34 /**
35  * Tests injection of user ID from HTTP headers.
36  *
37  * @author evitaliy
38  * @since 21 Aug 2018
39  */
40 public class UserIdResolverTest {
41
42     @Test
43     public void oneHeaderSelectedWhenMultipleUserIdHeadersSent() throws ServletRequestBindingException {
44
45         final String headerValue = "UserIdValueFromHeader";
46
47         HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
48         when(servletRequestMock.getHeader(USER_ID_HEADER)).thenReturn(headerValue);
49
50         NativeWebRequest webRequestMock = mock(NativeWebRequest.class);
51         when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(servletRequestMock);
52
53         Object resolved = new UserIdResolver().resolveArgument(null, null, webRequestMock, null);
54         assertEquals(headerValue, resolved);
55     }
56
57     @Test
58     public void illegalTypeErrorThrownWhenAnnotatedParameterIsNotOfTypeString() {
59         assertThrows(IllegalStateException.class, () -> {
60             MethodParameter methodParameterMock = mock(MethodParameter.class);
61             when(methodParameterMock.hasParameterAnnotation(UserId.class)).thenReturn(true);
62             //noinspection unchecked
63             when(methodParameterMock.getParameterType()).thenReturn((Class) String[].class);
64             new UserIdResolver().supportsParameter(methodParameterMock);
65         });
66     }
67
68     @Test
69     public void missingHeaderErrorThrownWhenUserIdHeaderNotPopulated() {
70         assertThrows(ServletRequestBindingException.class, () -> {
71             NativeWebRequest webRequestMock = mock(NativeWebRequest.class);
72             when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(mock(HttpServletRequest.class));
73             new UserIdResolver().resolveArgument(null, null, webRequestMock, null);
74         });
75     }
76
77     @Test
78     public void exceptionThrownWhenRequestTypeIsNotHttpRequest() {
79         assertThrows(NullPointerException.class, () -> {
80             NativeWebRequest webRequestMock = mock(NativeWebRequest.class);
81             new UserIdResolver().resolveArgument(null, null, webRequestMock, null);
82         });
83     }
84
85     @Test
86     public void parameterNotSupportedWhenNotAnnotatedWithUserIdAnnotation() {
87         MethodParameter methodParameterMock = mock(MethodParameter.class);
88         assertFalse(new UserIdResolver().supportsParameter(methodParameterMock));
89     }
90
91     @Test
92     public void parameterSupportedWhenAnnotatedWithUserIdAnnotationAndOfTypeString() {
93         MethodParameter methodParameterMock = mock(MethodParameter.class);
94         when(methodParameterMock.hasParameterAnnotation(UserId.class)).thenReturn(true);
95         //noinspection unchecked
96         when(methodParameterMock.getParameterType()).thenReturn((Class) String.class);
97         assertTrue(new UserIdResolver().supportsParameter(methodParameterMock));
98     }
99 }