Base16BigIntegerConverterProvider implements ParamConverterProvider { @Override public <T> ParamConverter<T> getConverter(final Class<T> clazz, final Type type, final Annotation[] annotations) { if (!clazz.getName().equals(BigInteger.class.getName())) { return null; } for (Annotation a : Arrays.asList(annotations)) { if (a instanceof PathParam) { return new BigIntConverter<>(NotFoundException::new); } if (a instanceof QueryParam) { return new BigIntConverter<>(MalformedIdException::new); } if (a instanceof FormParam) { return new BigIntConverter<>(MalformedIdException::new); } } return null; }  @Override ParamConverter<T> getConverter(final Class<T> clazz,
                                              final Type type,
                                              final Annotation[] annotations);  }
@Test public void testBigIntegerOnly() { Annotation[] annotations = new Annotation[]{ pathAnnotation, queryAnnotation}; assertNotNull(converterProvider .getConverter(BigInteger.class, null, annotations)); assertNull(converterProvider .getConverter(Integer.class, null, annotations)); assertNull(converterProvider .getConverter(String.class, null, annotations)); assertNull(converterProvider .getConverter(Byte.class, null, annotations)); assertNull(converterProvider .getConverter(Double.class, null, annotations)); assertNull(converterProvider .getConverter(Number.class, null, annotations)); }
@Test public void testNoAnnotation() { Annotation[] annotations = new Annotation[]{}; assertNull(converterProvider .getConverter(BigInteger.class, null, annotations)); }
@Test public void testOnlyQueryParam() { Annotation[] annotations = new Annotation[]{queryAnnotation}; assertNotNull(converterProvider .getConverter(BigInteger.class, null, annotations)); }
@Test public void testOnlyFormParam() { Annotation[] annotations = new Annotation[]{formAnnotation}; assertNotNull(converterProvider .getConverter(BigInteger.class, null, annotations)); }
@Test public void testOnlyPathParam() { Annotation[] annotations = new Annotation[]{pathAnnotation}; assertNotNull(converterProvider .getConverter(BigInteger.class, null, annotations)); }
@Test public void testNonSupportedAnnotation() { Annotation[] annotations = new Annotation[]{otherAnnotation}; assertNull(converterProvider .getConverter(BigInteger.class, null, annotations)); }
@Test public void testConvertBigInteger() { Annotation[] annotations = new Annotation[]{pathAnnotation}; ParamConverter converter = converterProvider .getConverter(BigInteger.class, null, annotations); BigInteger id = IdUtil.next(); String idString = converter.toString(id); assertEquals(idString, IdUtil.toString(id)); }
@Test(expected = NotFoundException.class) public void testFailConvertPath() { Annotation[] annotations = new Annotation[]{pathAnnotation}; ParamConverter converter = converterProvider .getConverter(BigInteger.class, null, annotations); converter.fromString("not_a_valid_string"); }
@Test(expected = MalformedIdException.class) public void testFailConvertForm() { Annotation[] annotations = new Annotation[]{formAnnotation}; ParamConverter converter = converterProvider .getConverter(BigInteger.class, null, annotations); converter.fromString("not_a_valid_string"); }
@Test(expected = MalformedIdException.class) public void testFailConvertQuery() { Annotation[] annotations = new Annotation[]{queryAnnotation}; ParamConverter converter = converterProvider .getConverter(BigInteger.class, null, annotations); converter.fromString("not_a_valid_string"); }
@Test public void testValidConversion() { Annotation[] annotations = new Annotation[]{pathAnnotation}; ParamConverter converter = converterProvider .getConverter(BigInteger.class, null, annotations); BigInteger id = IdUtil.next(); BigInteger converted = (BigInteger) converter.fromString(IdUtil.toString(id)); assertEquals(id, converted); }