DefaultParametersRetriever implements ParametersRetriever { @Override public Map<String, Object> getParameters(final NativeWebRequest webRequest) { Map<String, Object> resultMap = new HashMap<String, Object>(); Map<String, String> pathVariables = (Map<String, String>) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); Map<String, Object> requestParams = getRequestParameterMap(webRequest); for (Map.Entry<String, Object> entry : requestParams.entrySet()) { resultMap.put(entry.getKey(), entry.getValue()); } resultMap.putAll(pathVariables); return resultMap; }  @Override Map<String, Object> getParameters(final NativeWebRequest webRequest); MultipleParameterRetriever getMultipleParameterRetriever(); void setMultipleParameterRetriever(final MultipleParameterRetriever multipleParameterRetriever);  }
@Test public void testGetParameters() throws Exception { String field1 = "field1"; String value1 = "value1"; Map<String, String> pathVariables = new HashMap<>(); pathVariables.put(field1, value1); Map<String,String[]> requestParams = new HashMap<>(); String value2 = "value2"; String[] values2 = {value2}; String field2 = "field2"; requestParams.put(field2, values2); Map<String, Object> expected = new HashMap<>(); expected.put(field1, value1); expected.put(field2, value2); when(webRequest.getAttribute(anyString(), anyInt())).thenReturn(pathVariables); when(webRequest.getParameterMap()).thenReturn(requestParams); Map<String, Object> actual = unit.getParameters(webRequest); assertEquals(expected, actual); }
@Test public void testGetParametersWithEmptyMap() throws Exception { Map<String, String> pathVariables = new HashMap<>(); Map<String,String[]> requestParams = new HashMap<>(); Map<String, Object> expected = new HashMap<>(); when(webRequest.getAttribute(anyString(), anyInt())).thenReturn(pathVariables); when(webRequest.getParameterMap()).thenReturn(requestParams); Map<String, Object> actual = unit.getParameters(webRequest); assertEquals(expected, actual); }
@Test public void testGetParametersWithMultipleParameter() throws Exception { String field1 = "field1"; String value1 = "value1"; Map<String, String> pathVariables = new HashMap<>(); pathVariables.put(field1, value1); Long id1 = 1L; Long id2 = 2L; Long id3 = 3L; List<Object> idList = Arrays.asList(String.valueOf(id1), String.valueOf(id2), String.valueOf(id3)); String value2 = String.valueOf(id1); String[] values2 = {value2}; String field2 = "field2"; Map<String,String[]> requestParams = new HashMap<>(); requestParams.put(field2, values2); Map<String, Object> expected = new HashMap<>(); expected.put(field1, value1); expected.put(field2, value2); expected.put(field2, value2); when(webRequest.getAttribute(anyString(), anyInt())).thenReturn(pathVariables); when(webRequest.getParameterMap()).thenReturn(requestParams); doReturn(idList).when(multipleParameterRetriever).getMultiParameter(values2); Map<String, Object> actual = unit.getParameters(webRequest); verifyZeroInteractions(multipleParameterRetriever); assertEquals(expected, actual); }