HeaderValueValidator implements BiConsumer<HeaderDefinition, CharSequence> { @Override public void accept(@Nonnull final HeaderDefinition definition, @Nullable final CharSequence charSequence) { final ValueValidator validatorChain = HeaderValueValidators.getIntValidator() .andThen(HeaderValueValidators.getLongValidator()) .andThen(HeaderValueValidators.getBooleanValidator()) .andThen(HeaderValueValidators.getJsonArrayValidator()) .andThen(HeaderValueValidators.getEntityTagValidator()) .andThen(HeaderValueValidators.getEntityTagMatchersValidator()); validatorChain.accept(definition, charSequence); } private  HeaderValueValidator(); static HeaderValueValidator getInstance(); @Override void accept(@Nonnull final HeaderDefinition definition, @Nullable final CharSequence charSequence);  }
@Test public void validBooleanStringValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(boolean.class); final String value = String.valueOf(Boolean.TRUE); try { underTest.accept(headerDefinition, value); } catch (final Throwable t) { fail(value + " is a valid boolean representation!"); } }
@Test public void invalidBooleanStringValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(Boolean.class); final String value = "foo"; assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> underTest.accept(headerDefinition, value)) .withMessage("The value '%s' of the header '%s' is not a valid boolean.", value, HEADER_KEY) .withNoCause(); }
@Test public void validIntStringValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(int.class); final String value = String.valueOf(42); try { underTest.accept(headerDefinition, value); } catch (final Throwable t) { fail(value + " is a valid int representation!"); } }
@Test public void invalidIntStringValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(Integer.class); final String value = "foo"; assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> underTest.accept(headerDefinition, value)) .withMessage("The value '%s' of the header '%s' is not a valid int.", value, HEADER_KEY) .withNoCause(); }
@Test public void validJsonArrayStringValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(JsonArray.class); final JsonArray jsonArray = JsonFactory.newArrayBuilder() .add("foo") .add("bar") .add("baz") .build(); final String value = jsonArray.toString(); try { underTest.accept(headerDefinition, value); } catch (final Throwable t) { fail(value + " is a valid JsonArray representation!"); } }
@Test public void invalidJsonArrayStringValueNoJsonArray() { Mockito.when(headerDefinition.getJavaType()).thenReturn(JsonArray.class); final String value = "foo"; assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> underTest.accept(headerDefinition, value)) .withMessage("The value '%s' of the header '%s' is not a valid JSON array.", value, HEADER_KEY) .withNoCause(); }
@Test public void invalidJsonArrayStringValueNoStringItem() { Mockito.when(headerDefinition.getJavaType()).thenReturn(JsonArray.class); final JsonArray jsonArray = JsonFactory.newArrayBuilder() .add("foo") .add(2) .add("baz") .build(); final String value = jsonArray.toString(); assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> underTest.accept(headerDefinition, value)) .withMessage("JSON array for <%s> contained non-string values!", HEADER_KEY) .withNoCause(); }
@Test public void invalidEntityTagValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(EntityTag.class); final String value = "invalid\"foo\""; assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> underTest.accept(headerDefinition, value)) .withMessage("The value '%s' of the header '%s' is not a valid entity-tag.", value, HEADER_KEY) .withNoCause(); }
@Test public void validEntityTagValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(EntityTag.class); final String value = "\"foo\""; try { underTest.accept(headerDefinition, value); } catch (final Throwable t) { fail(value + " is a valid entity-tag!"); } }
@Test public void invalidEntityTagsValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(EntityTagMatchers.class); final String value = "\"foo\",invalid\"bar\""; assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> underTest.accept(headerDefinition, value)) .withMessage("The value '%s' of the header '%s' is not a valid entity-tag.", "invalid\"bar\"", HEADER_KEY) .withNoCause(); }
@Test public void validEntityTagsValue() { Mockito.when(headerDefinition.getJavaType()).thenReturn(EntityTagMatchers.class); final String value = "\"foo\",\"bar\""; try { underTest.accept(headerDefinition, value); } catch (final Throwable t) { fail(value + " is a valid entity-tag!"); } }
@Test public void validEntityTagsValueWitSpaces() { Mockito.when(headerDefinition.getJavaType()).thenReturn(EntityTagMatchers.class); final String value = "\"foo\", \"bar\""; try { underTest.accept(headerDefinition, value); } catch (final Throwable t) { fail(value + " is a valid entity-tag!"); } }