Skip to content

Commit

Permalink
changed error code of DittoHeaderNotSupportedException;
Browse files Browse the repository at this point in the history
added DittoHeaderNotSupportedExceptionTest;
changed since annotation to 2.5.0;
extract metadata from thing in CreateThing command in constructor to be able to use the functionality also in the tests;

Signed-off-by: Stefan Maute <stefan.maute@bosch.io>
  • Loading branch information
Stefan Maute committed Jun 1, 2022
1 parent c4b24ea commit c28df08
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 231 deletions.
Expand Up @@ -45,11 +45,11 @@ public final class DittoHeaderNotSupportedException extends DittoRuntimeExceptio
/**
* Error code of this exception.
*/
public static final String ERROR_CODE = "header.not.supported";
public static final String ERROR_CODE = "header.notsupported";

private static final String DEFAULT_MESSAGE = "The header is not supported.";

private static final String MESSAGE_TEMPLATE = "The value ''{0}'' of the header ''{1}'' is not supported on this resource level {2}.";
private static final String MESSAGE_TEMPLATE = "The value ''{0}'' of the header ''{1}'' is not supported on this resource level.";

private static final String DEFAULT_DESCRIPTION = "Verify that the header has the correct syntax and is used on the correct resource level.";

Expand All @@ -58,7 +58,7 @@ public final class DittoHeaderNotSupportedException extends DittoRuntimeExceptio
/**
* Definition of an optional JSON field that contains the key of the not supported header.
*
* @since 3.0.0
* @since 2.5.0
*/
static final JsonFieldDefinition<String> JSON_FIELD_NOT_SUPPORTED_HEADER_KEY =
JsonFieldDefinition.ofString("notSupportedHeaderKey",
Expand All @@ -83,38 +83,36 @@ private DittoHeaderNotSupportedException(final DittoHeaders dittoHeaders,
*
* @param headerName the key of the header.
* @param headerValue the value of the header.
* @param headerType the expected type of the header. (int, String, entity-tag...)
* @return the builder.
* @throws NullPointerException if any argument is {@code null}.
*/
public static DittoHeaderNotSupportedException.Builder newInvalidTypeBuilder(final String headerName,
@Nullable final CharSequence headerValue, final String headerType) {
@Nullable final CharSequence headerValue) {

return new DittoHeaderNotSupportedException.Builder(headerName, headerValue, headerType);
return new DittoHeaderNotSupportedException.Builder(headerName, headerValue);
}

/**
* A mutable builder for a {@code DittoHeaderInvalidException} in case of an invalid type.
*
* @param headerDefinition the definition of the header.
* @param headerValue the value of the header.
* @param headerType the type of the header. (int, String, entity-tag...)
* @return the builder.
* @throws NullPointerException if any argument is {@code null}.
* @since 3.0.0
* @since 2.5.0
*/
public static DittoHeaderNotSupportedException.Builder newInvalidTypeBuilder(final HeaderDefinition headerDefinition,
@Nullable final CharSequence headerValue, final String headerType) {
@Nullable final CharSequence headerValue) {

return new DittoHeaderNotSupportedException.Builder(headerDefinition.getKey(), headerValue, headerType);
return new DittoHeaderNotSupportedException.Builder(headerDefinition.getKey(), headerValue);
}

/**
* Returns a new mutable builder with a fluent API for creating a {@code DittoHeaderInvalidException}.
* The returned builder is initialized with a default message and a default description.
*
* @return the builder.
* @since 3.0.0
* @since 2.5.0
*/
public static DittoHeaderNotSupportedException.Builder newBuilder() {
return new Builder();
Expand Down Expand Up @@ -143,7 +141,7 @@ public static DittoHeaderNotSupportedException fromJson(final JsonObject jsonObj
* Returns the key of the not supported header if known.
*
* @return an Optional that either contains the key of the not supported header or is empty if the key is unknown.
* @since 3.0.0
* @since 2.5.0
*/
public Optional<String> getNotSupportedHeaderKey() {
return Optional.ofNullable(notSupportedHeaderKey);
Expand Down Expand Up @@ -201,10 +199,9 @@ private Builder() {
description(DEFAULT_DESCRIPTION);
}

private Builder(final String headerName, @Nullable final CharSequence headerValue, final String headerType) {
private Builder(final String headerName, @Nullable final CharSequence headerValue) {
notSupportedHeaderKey = headerName;
message(MessageFormat.format(MESSAGE_TEMPLATE, String.valueOf(headerValue), requireNonNull(headerName),
requireNonNull(headerType)));
message(MessageFormat.format(MESSAGE_TEMPLATE, String.valueOf(headerValue), requireNonNull(headerName)));
description(DEFAULT_DESCRIPTION);
}

Expand All @@ -213,7 +210,7 @@ private Builder(final String headerName, @Nullable final CharSequence headerValu
*
* @param notSupportedHeaderKey the key of the not supported header.
* @return this builder instance for method chaining.
* @since 3.0.0
* @since 2.5.0
*/
public Builder withNotSupportedHeaderKey(@Nullable final CharSequence notSupportedHeaderKey) {
if (null != notSupportedHeaderKey) {
Expand Down
Expand Up @@ -327,7 +327,7 @@ public enum DittoHeaderDefinition implements HeaderDefinition {
* Key {@code "get-metadata"}, Java type: {@link String}.
* </p>
*
* @since 3.0.0
* @since 2.5.0
*/
GET_METADATA("get-metadata", String.class, true, false, HeaderValueValidators.getJsonFieldSelectorValidator()),

Expand All @@ -337,7 +337,7 @@ public enum DittoHeaderDefinition implements HeaderDefinition {
* Key {@code "delete-metadata"}, Java type: {@link String}.
* </p>
*
* @since 3.0.0
* @since 2.5.0
*/
DELETE_METADATA("delete-metadata", String.class, true, false, HeaderValueValidators.getJsonFieldSelectorValidator()),

Expand All @@ -347,7 +347,7 @@ public enum DittoHeaderDefinition implements HeaderDefinition {
* Key {@code "ditto-metadata"}, Java type: {@link JsonObject}.
* </p>
*
* @since 3.0.0
* @since 2.5.0
*/
DITTO_METADATA("ditto-metadata", JsonObject.class, false, true, HeaderValueValidators.getNoOpValidator()),

Expand Down
Expand Up @@ -352,7 +352,7 @@ static DittoHeadersBuilder newBuilder(final JsonObject jsonObject) {
*
* @return the MetadataHeaderKeys to get being a sorted set of {@code MetadataHeaderKey}s.
* Changes on the returned set are not reflected back to this DittoHeaders instance.
* @since 3.0.0
* @since 2.5.0
*/
Set<JsonPointer> getMetadataFieldsToGet();

Expand Down
Expand Up @@ -187,7 +187,7 @@ static ValueValidator getMetadataHeadersValidator() {
* Returns a validator for checking if a CharSequence represents a {@link org.eclipse.ditto.json.JsonFieldSelector}.
*
* @return the validator.
* @since 3.0.0
* @since 2.5.0
*/
static ValueValidator getJsonFieldSelectorValidator() {
return JsonFieldSelectorValidator.getInstance();
Expand Down
Expand Up @@ -24,7 +24,7 @@
* This validator parses a CharSequence to a {@link org.eclipse.ditto.json.JsonFieldSelector}.
* If parsing fails, a {@link org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException} with detailed description is thrown.
*
* @since 3.0.0
* @since 2.5.0
*/
@Immutable
final class JsonFieldSelectorValidator extends AbstractHeaderValueValidator {
Expand Down
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.base.model.exceptions;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;

import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.json.JsonObject;
import org.junit.Rule;
import org.junit.Test;

/**
* Unit test for {@link DittoHeaderNotSupportedException}.
*/
public class DittoHeaderNotSupportedExceptionTest {

@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();

@Test
public void assertImmutability() {
assertInstancesOf(DittoHeaderNotSupportedException.class, areImmutable());
}

@Test
public void buildFromInvalidHeaderKey() {
final String invalidHeaderKey = "plumbus";
final DittoHeaderNotSupportedException underTest = DittoHeaderNotSupportedException.newBuilder()
.withNotSupportedHeaderKey(invalidHeaderKey)
.build();

assertThat(underTest.getNotSupportedHeaderKey()).hasValue(invalidHeaderKey);
}

@Test
public void buildFromCustomMessage() {
final String customMessage = "theCustomMessage";

final DittoHeaderNotSupportedException headerInvalidException = DittoHeaderNotSupportedException.newBuilder()
.message(customMessage)
.build();

softly.assertThat(headerInvalidException.getNotSupportedHeaderKey()).as("invalid header key").isEmpty();
softly.assertThat(headerInvalidException.getMessage()).as("message").isEqualTo(customMessage);
softly.assertThat(headerInvalidException.getDescription())
.as("description")
.hasValue("Verify that the header has the correct syntax and is used on the correct resource level.");
}

@Test
public void buildForInvalidType() {
final String invalidHeaderKey = "theHeaderName";
final DittoHeaderNotSupportedException headerInvalidException =
DittoHeaderNotSupportedException.newInvalidTypeBuilder(invalidHeaderKey, "theValue")
.build();

softly.assertThat(headerInvalidException.getNotSupportedHeaderKey())
.as("header not supported")
.hasValue(invalidHeaderKey);
softly.assertThat(headerInvalidException.getMessage())
.as("message")
.isEqualTo("The value 'theValue' of the header 'theHeaderName' is not supported on this resource level.");
}

@Test
public void toJsonReturnsExpected() {
final String invalidHeaderKey = "void";
final String message = "The header key <" + invalidHeaderKey + "> is not supported.";
final String description = "YOLO!";
final String href = "https://www.example.com";

final JsonObject jsonObject = JsonObject.newBuilder()
.set(DittoRuntimeException.JsonFields.STATUS, HttpStatus.BAD_REQUEST.getCode())
.set(DittoRuntimeException.JsonFields.ERROR_CODE, DittoHeaderNotSupportedException.ERROR_CODE)
.set(DittoRuntimeException.JsonFields.MESSAGE, message)
.set(DittoRuntimeException.JsonFields.DESCRIPTION, description)
.set(DittoRuntimeException.JsonFields.HREF, href)
.set(DittoHeaderNotSupportedException.JSON_FIELD_NOT_SUPPORTED_HEADER_KEY, invalidHeaderKey)
.build();

final DittoHeaderNotSupportedException underTest = DittoHeaderNotSupportedException.newBuilder()
.withNotSupportedHeaderKey(invalidHeaderKey)
.message(message)
.description(description)
.href(href)
.build();

assertThat(underTest.toJson()).isEqualTo(jsonObject);
}

@Test
public void fromValidJsonReturnsExpected() {
final String invalidHeaderKey = "void";
final String message = "The header key <" + invalidHeaderKey + "> is not supported on this resource level theExpectedType.";
final String description = "YOLO!";
final String href = "https://www.example.com";

final DittoHeaderNotSupportedException underTest = DittoHeaderNotSupportedException.newBuilder()
.withNotSupportedHeaderKey(invalidHeaderKey)
.message(message)
.description(description)
.href(href)
.build();

assertThat(DittoHeaderNotSupportedException.fromJson(underTest.toJson(), DittoHeaders.empty())).isEqualTo(
underTest);
}

}

0 comments on commit c28df08

Please sign in to comment.