From 00f2f289f36dcdef8b71ad33abeea03a1e622497 Mon Sep 17 00:00:00 2001 From: vempo Date: Tue, 21 Aug 2018 16:42:26 +0300 Subject: Cenralized handling of USER_ID header Minor code style and spelling fixes. Change-Id: I07495a08d3c02276806c9043ed7dbf81ed1a9394 Issue-ID: SDC-1670 Signed-off-by: vempo --- .../sdc/workflow/api/WorkflowControllerTest.java | 22 +++--- .../server/resolvers/UserIdResolverTest.java | 92 ++++++++++++++++++++++ 2 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 workflow-designer-be/src/test/java/org/onap/sdc/workflow/server/resolvers/UserIdResolverTest.java (limited to 'workflow-designer-be/src/test') diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java index 258e9c51..597e27fc 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java @@ -33,6 +33,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.sdc.workflow.RestPath; +import org.onap.sdc.workflow.server.resolvers.UserIdResolver; import org.onap.sdc.workflow.services.WorkflowManager; import org.onap.sdc.workflow.services.types.Page; import org.onap.sdc.workflow.services.types.PagingRequest; @@ -49,8 +50,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; public class WorkflowControllerTest { private static final String USER_ID = "userId"; - private static final String MISSING_USER_HEADER_ERROR = - "Missing request header 'USER_ID' for method parameter of type String"; + private static final String MISSING_USER_HEADER_ERROR = "Missing mandatory request header 'USER_ID'"; private static final String DEFAULT_SORT_VALUE = "name:asc"; private MockMvc mockMvc; @@ -66,6 +66,7 @@ public class WorkflowControllerTest { public void setUp() { mockMvc = MockMvcBuilders.standaloneSetup(workflowController) .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver()) + .setCustomArgumentResolvers(new UserIdResolver()) .setControllerAdvice(new ExceptionsHandler()).build(); } @@ -183,7 +184,7 @@ public class WorkflowControllerTest { } @Test - public void shouldCreateWorkflowWhenCallingPostRESTRequest() throws Exception { + public void shouldCreateWorkflowWhenCallingPostRestRequest() throws Exception { Item item = new Item(); item.setId(new Id("abc")); Workflow reqWorkflow = createWorkflow(1, false); @@ -235,9 +236,11 @@ public class WorkflowControllerTest { Workflow reqWorkflow = new Workflow(); reqWorkflow.setName("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON) - .content(JsonUtil.object2Json(reqWorkflow))).andDo(print()) - .andExpect(status().isBadRequest()).andExpect( - jsonPath("$.message", is("Workflow name must be at least " + MIN_LENGTH + " characters, and no more than " + MAX_LENGTH + " characters."))); + .content(JsonUtil.object2Json(reqWorkflow))).andDo(print()).andExpect( + status().isBadRequest()).andExpect(jsonPath("$.message", is( + "Workflow name must be at least " + MIN_LENGTH + + " characters, and no more than " + MAX_LENGTH + + " characters."))); } @Test @@ -245,9 +248,10 @@ public class WorkflowControllerTest { Workflow reqWorkflow = new Workflow(); reqWorkflow.setName("AAA"); mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON) - .content(JsonUtil.object2Json(reqWorkflow))).andDo(print()) - .andExpect(status().isBadRequest()).andExpect( - jsonPath("$.message", is("Workflow name must be at least " + MIN_LENGTH + " characters, and no more than " + MAX_LENGTH + " characters."))); + .content(JsonUtil.object2Json(reqWorkflow))).andDo(print()).andExpect( + status().isBadRequest()).andExpect(jsonPath("$.message", + is("Workflow name must be at least " + MIN_LENGTH + " characters, and no more than " + + MAX_LENGTH + " characters."))); } private void mockManagerList3() { diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/server/resolvers/UserIdResolverTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/server/resolvers/UserIdResolverTest.java new file mode 100644 index 00000000..464af465 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/server/resolvers/UserIdResolverTest.java @@ -0,0 +1,92 @@ +/* + * Copyright © 2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.workflow.server.resolvers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER; + +import javax.servlet.http.HttpServletRequest; +import org.junit.Test; +import org.onap.sdc.workflow.services.annotations.UserId; +import org.springframework.core.MethodParameter; +import org.springframework.web.bind.ServletRequestBindingException; +import org.springframework.web.context.request.NativeWebRequest; + +/** + * Tests injection of user ID from HTTP headers. + * + * @author evitaliy + * @since 21 Aug 2018 + */ +public class UserIdResolverTest { + + @Test + public void oneHeaderSelectedWhenMultipleUserIdHeadersSent() throws ServletRequestBindingException { + + final String headerValue = "UserIdValueFromHeader"; + + HttpServletRequest servletRequestMock = mock(HttpServletRequest.class); + when(servletRequestMock.getHeader(USER_ID_HEADER)).thenReturn(headerValue); + + NativeWebRequest webRequestMock = mock(NativeWebRequest.class); + when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(servletRequestMock); + + Object resolved = new UserIdResolver().resolveArgument(null, null, webRequestMock, null); + assertEquals(headerValue, resolved); + } + + @Test(expected = IllegalStateException.class) + public void illegalTypeErrorThrownWhenAnnotatedParameterIsNotOfTypeString() { + MethodParameter methodParameterMock = mock(MethodParameter.class); + when(methodParameterMock.hasParameterAnnotation(UserId.class)).thenReturn(true); + //noinspection unchecked + when(methodParameterMock.getParameterType()).thenReturn((Class)String[].class); + new UserIdResolver().supportsParameter(methodParameterMock); + } + + @Test(expected = ServletRequestBindingException.class) + public void missingHeaderErrorThrownWhenUserIdHeaderNotPopulated() throws ServletRequestBindingException { + NativeWebRequest webRequestMock = mock(NativeWebRequest.class); + when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(mock(HttpServletRequest.class)); + new UserIdResolver().resolveArgument(null, null, webRequestMock, null); + } + + @Test(expected = NullPointerException.class) + public void exceptionThrownWhenRequestTypeIsNotHttpRequest() throws ServletRequestBindingException { + NativeWebRequest webRequestMock = mock(NativeWebRequest.class); + new UserIdResolver().resolveArgument(null, null, webRequestMock, null); + } + + @Test + public void parameterNotSupportedWhenNotAnnotatedWithUserIdAnnotation() { + MethodParameter methodParameterMock = mock(MethodParameter.class); + assertFalse(new UserIdResolver().supportsParameter(methodParameterMock)); + } + + @Test + public void parameterSupportedWhenAnnotatedWithUserIdAnnotationAndOfTypeString() { + MethodParameter methodParameterMock = mock(MethodParameter.class); + when(methodParameterMock.hasParameterAnnotation(UserId.class)).thenReturn(true); + //noinspection unchecked + when(methodParameterMock.getParameterType()).thenReturn((Class) String.class); + assertTrue(new UserIdResolver().supportsParameter(methodParameterMock)); + } +} \ No newline at end of file -- cgit 1.2.3-korg