CollectionMergingParamProcessor implements ParamProcessor { public List<EncodedPair> process(Param param, Charset charset, boolean encodeIfNeeded) throws Exception { StringBuilder sb = new StringBuilder(); Serializer serializer = param.getParamConfig().getSerializer(); boolean isEncoded = !encodeIfNeeded || param.getParamConfig().isEncoded(); boolean first = true; for(Object value : param.getValue()){ String serializedValue = serialize(serializer, value, charset); if(!first) { sb.append(listSeparator); } sb.append(serializedValue); first = false; } if(sb.length() == 0) { return emptyList(); }else{ return singletonList(toPair(param.getParamConfig().getName(), sb.toString(), charset, isEncoded)); } } CollectionMergingParamProcessor(String listSeparator); List<EncodedPair> process(Param param, Charset charset, boolean encodeIfNeeded);  }
@Test public void shouldProcessParamEncodingThem() throws Exception { Serializer serializer = new ToStringSerializer(); ParamConfig paramConfig = mock(ParamConfig.class); when(paramConfig.getSerializer()).thenReturn(serializer); when(paramConfig.isEncoded()).thenReturn(false); when(paramConfig.getName()).thenReturn("p1"); Param param = mock(Param.class); when(param.getParamConfig()).thenReturn(paramConfig); when(param.getValue()).thenReturn((Collection) asList("v 11", "v 12")); List<EncodedPair> actual = toTest.process(param, UTF8, true); assertEquals(1, actual.size()); assertEquals("p1", actual.get(0).getName()); assertEquals("v%2011--v%2012", actual.get(0).getValue()); }
@Test public void shouldProcessPreEncodedParams() throws Exception { Serializer serializer = new ToStringSerializer(); ParamConfig paramConfig = mock(ParamConfig.class); when(paramConfig.getSerializer()).thenReturn(serializer); when(paramConfig.isEncoded()).thenReturn(true); when(paramConfig.getName()).thenReturn("p1"); Param param = mock(Param.class); when(param.getParamConfig()).thenReturn(paramConfig); when(param.getValue()).thenReturn((Collection) asList("v%2011", "v%2012")); List<EncodedPair> actual = toTest.process(param, UTF8, true); assertEquals(1, actual.size()); assertEquals("p1", actual.get(0).getName()); assertEquals("v%2011--v%2012", actual.get(0).getValue()); }
@Test public void shouldProcessParamIgnoringEncodingFlag() throws Exception { Serializer serializer = new ToStringSerializer(); ParamConfig paramConfig = mock(ParamConfig.class); when(paramConfig.getSerializer()).thenReturn(serializer); when(paramConfig.isEncoded()).thenReturn(false); when(paramConfig.getName()).thenReturn("p1"); Param param = mock(Param.class); when(param.getParamConfig()).thenReturn(paramConfig); when(param.getValue()).thenReturn((Collection) asList("v 11", "v 12")); List<EncodedPair> actual = toTest.process(param, UTF8, false); assertEquals(1, actual.size()); assertEquals("p1", actual.get(0).getName()); assertEquals("v 11--v 12", actual.get(0).getValue()); }
@Test public void shouldProcessPreEncodedParamsIgnoringEncodingFlag() throws Exception { Serializer serializer = new ToStringSerializer(); ParamConfig paramConfig = mock(ParamConfig.class); when(paramConfig.getSerializer()).thenReturn(serializer); when(paramConfig.isEncoded()).thenReturn(true); when(paramConfig.getName()).thenReturn("p1"); Param param = mock(Param.class); when(param.getParamConfig()).thenReturn(paramConfig); when(param.getValue()).thenReturn((Collection) asList("v%2011", "v%2012")); List<EncodedPair> actual = toTest.process(param, UTF8, false); assertEquals(1, actual.size()); assertEquals("p1", actual.get(0).getName()); assertEquals("v%2011--v%2012", actual.get(0).getValue()); }
@Test public void shouldProcessEmptyParams() throws Exception { Serializer serializer = new ToStringSerializer(); ParamConfig paramConfig = mock(ParamConfig.class); when(paramConfig.getSerializer()).thenReturn(serializer); when(paramConfig.isEncoded()).thenReturn(true); when(paramConfig.getName()).thenReturn("p1"); Param param = mock(Param.class); when(param.getParamConfig()).thenReturn(paramConfig); when(param.getValue()).thenReturn(Collections.<Object>emptyList()); List<EncodedPair> actual = toTest.process(param, UTF8, true); assertEquals(0, actual.size()); }