HttpUtils { public static <T> WrappedWebTarget encodeCollectionFormatQueryParam( WrappedWebTarget target, String queryParamName, List<T> values, CollectionFormatType collectionFormatType) { if (StringUtils.isBlank(queryParamName)) { throw new IllegalArgumentException("A non-blank queryParamName must be provided"); } if (values != null && !values.isEmpty()) { final List<Object> valuesToUse = new ArrayList<>(); for (T v : values) { if (v == null) { continue; } if (v instanceof Enum) { final Object rawValue = ReflectionUtils.invokeGetter(v, "getValue"); if (rawValue != null) { valuesToUse.add((String) rawValue); } else { throw new IllegalArgumentException( String.format( "Could not get the correct value for enum %s", v.getClass().getCanonicalName())); } } else { valuesToUse.add(v); } } if (valuesToUse.isEmpty()) { return target; } if (collectionFormatType == CollectionFormatType.CommaSeparated) { target = target.queryParam( queryParamName, attemptEncodeQueryParam(StringUtils.join(valuesToUse, ','))); } else if (collectionFormatType == CollectionFormatType.PipeSeparated) { target = target.queryParam( queryParamName, attemptEncodeQueryParam(StringUtils.join(valuesToUse, '|'))); } else if (collectionFormatType == CollectionFormatType.SpaceSeparated) { target = target.queryParam( queryParamName, attemptEncodeQueryParam(StringUtils.join(valuesToUse, ' '))); } else if (collectionFormatType == CollectionFormatType.TabSeparated) { target = target.queryParam( queryParamName, attemptEncodeQueryParam(StringUtils.join(valuesToUse, '\t'))); } else if (collectionFormatType == CollectionFormatType.Multi) { final Object[] encodedValuesToUse = new Object[valuesToUse.size()]; for (int i = 0; i < valuesToUse.size(); i++) { encodedValuesToUse[i] = attemptEncodeQueryParam(valuesToUse.get(i)); } target = target.queryParam(queryParamName, encodedValuesToUse); } else { throw new IllegalArgumentException( String.format("Unknown collection format type: %s", collectionFormatType)); } } return target; }  static String encodePathSegment(@Nonnull String pathSegment); static String encodePathSegment(@Nonnull Number pathSegment); static String encodePathSegment(@Nonnull Boolean pathSegment); static String encodePathSegment(@Nonnull Character pathSegment); static String encodePathSegment(@Nonnull UUID pathSegment); static Object attemptEncodeQueryParam(Object queryParam); static WrappedWebTarget encodeMapQueryParam(
            WrappedWebTarget target, String prefix, Map<String, ?> queryParam); static WrappedWebTarget encodeCollectionFormatQueryParam(
            WrappedWebTarget target,
            String queryParamName,
            List<T> values,
            CollectionFormatType collectionFormatType);  }
@Test @Ignore("DEX-1801: Fix Mockito Problem in Release TeamCity Job") public void encodeCollectionFormatQueryParam_null() { WebTarget target = mock(WebTarget.class); WrappedWebTarget wrapped = new WrappedWebTarget(target); WrappedWebTarget result = HttpUtils.encodeCollectionFormatQueryParam( wrapped, "unitTest", null, CollectionFormatType.Multi); assertTrue(result == wrapped); Mockito.verifyNoMoreInteractions(target); }
@Test @Ignore("DEX-1801: Fix Mockito Problem in Release TeamCity Job") public void encodeCollectionFormatQueryParam_emptyList() { WebTarget target = mock(WebTarget.class); WrappedWebTarget wrapped = new WrappedWebTarget(target); WrappedWebTarget result = HttpUtils.encodeCollectionFormatQueryParam( wrapped, "unitTest", new ArrayList<String>(), CollectionFormatType.Multi); assertTrue(result == wrapped); Mockito.verifyNoMoreInteractions(target); }
@Test @Ignore("DEX-1801: Fix Mockito Problem in Release TeamCity Job") public void encodeCollectionFormatQueryParam_listOfNulls() { final List<String> values = new ArrayList<>(); values.add(null); values.add(null); WebTarget target = mock(WebTarget.class); WrappedWebTarget wrapped = new WrappedWebTarget(target); WrappedWebTarget result = HttpUtils.encodeCollectionFormatQueryParam( wrapped, "unitTest", values, CollectionFormatType.Multi); assertTrue(result == wrapped); Mockito.verifyNoMoreInteractions(target); }
@Test @Ignore("DEX-1801: Fix Mockito Problem in Release TeamCity Job") public void encodeCollectionFormatQueryParam_singleElementList() { final List<String> values = new ArrayList<>(); values.add("single"); for (CollectionFormatType cft : CollectionFormatType.values()) { WebTarget target = mock(WebTarget.class); WrappedWebTarget wrapped = new WrappedWebTarget(target); WrappedWebTarget result = HttpUtils.encodeCollectionFormatQueryParam(wrapped, "unitTest", values, cft); assertNotNull(result); verify(target).queryParam("unitTest", "single"); } }
@Test @Ignore("DEX-1801: Fix Mockito Problem in Release TeamCity Job") public void encodeCollectionFormatQueryParam_multipleElementList() { final List<String> values = new ArrayList<>(); values.add("number1"); values.add("number2"); for (CollectionFormatType cft : CollectionFormatType.values()) { WebTarget target = mock(WebTarget.class); WrappedWebTarget wrapped = new WrappedWebTarget(target); WrappedWebTarget result = HttpUtils.encodeCollectionFormatQueryParam(wrapped, "unitTest", values, cft); assertNotNull(result); if (cft == CollectionFormatType.CommaSeparated) { verify(target).queryParam("unitTest", "number1,number2"); } else if (cft == CollectionFormatType.PipeSeparated) { verify(target).queryParam("unitTest", "number1%7Cnumber2"); } else if (cft == CollectionFormatType.SpaceSeparated) { verify(target).queryParam("unitTest", "number1%20number2"); } else if (cft == CollectionFormatType.TabSeparated) { verify(target).queryParam("unitTest", "number1%09number2"); } else if (cft == CollectionFormatType.Multi) { verify(target).queryParam("unitTest", "number1", "number2"); } else { fail("Unrecognized CollectionFormatType: " + cft.toString()); } } }
@Test @Ignore("DEX-1801: Fix Mockito Problem in Release TeamCity Job") public void encodeCollectionFormatQueryParam_enumListMembers() { final List<LifecycleState> values = new ArrayList<>(); values.add(LifecycleState.Provisioning); values.add(LifecycleState.Stopping); for (CollectionFormatType cft : CollectionFormatType.values()) { WebTarget target = mock(WebTarget.class); WrappedWebTarget wrapped = new WrappedWebTarget(target); WrappedWebTarget result = HttpUtils.encodeCollectionFormatQueryParam(wrapped, "unitTest", values, cft); assertNotNull(result); if (cft == CollectionFormatType.CommaSeparated) { verify(target).queryParam("unitTest", "PROVISIONING,STOPPING"); } else if (cft == CollectionFormatType.PipeSeparated) { verify(target).queryParam("unitTest", "PROVISIONING%7CSTOPPING"); } else if (cft == CollectionFormatType.SpaceSeparated) { verify(target).queryParam("unitTest", "PROVISIONING%20STOPPING"); } else if (cft == CollectionFormatType.TabSeparated) { verify(target).queryParam("unitTest", "PROVISIONING%09STOPPING"); } else if (cft == CollectionFormatType.Multi) { verify(target).queryParam("unitTest", "PROVISIONING", "STOPPING"); } else { fail("Unrecognized CollectionFormatType: " + cft.toString()); } } }
@Test @Ignore("DEX-1801: Fix Mockito Problem in Release TeamCity Job") public void encodeCollectionFormatQueryParam_nullMembersIgnored() { final List<LifecycleState> values = new ArrayList<>(); values.add(LifecycleState.Running); values.add(null); for (CollectionFormatType cft : CollectionFormatType.values()) { WebTarget target = mock(WebTarget.class); WrappedWebTarget wrapped = new WrappedWebTarget(target); WrappedWebTarget result = HttpUtils.encodeCollectionFormatQueryParam(wrapped, "unitTest", values, cft); assertNotNull(result); if (cft == CollectionFormatType.CommaSeparated) { verify(target).queryParam("unitTest", "RUNNING"); } else if (cft == CollectionFormatType.PipeSeparated) { verify(target).queryParam("unitTest", "RUNNING"); } else if (cft == CollectionFormatType.SpaceSeparated) { verify(target).queryParam("unitTest", "RUNNING"); } else if (cft == CollectionFormatType.TabSeparated) { verify(target).queryParam("unitTest", "RUNNING"); } else if (cft == CollectionFormatType.Multi) { verify(target).queryParam("unitTest", "RUNNING"); } else { fail("Unrecognized CollectionFormatType: " + cft.toString()); } } }