-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #106: Introduced an HTTP status validator.
This class is meant to be used within instantiation of CommandResponses. Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
- Loading branch information
Juergen Fickel
committed
Dec 8, 2021
1 parent
e39f42e
commit 43f4e6b
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...ava/org/eclipse/ditto/base/model/signals/commands/CommandResponseHttpStatusValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2021 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.signals.commands; | ||
|
||
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Collection; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.eclipse.ditto.base.model.common.ConditionChecker; | ||
import org.eclipse.ditto.base.model.common.HttpStatus; | ||
|
||
/** | ||
* Validates if a particular {@link HttpStatus} is valid for a certain {@link CommandResponse}. | ||
* | ||
* @since 2.3.0 | ||
*/ | ||
@Immutable | ||
public final class CommandResponseHttpStatusValidator { | ||
|
||
private CommandResponseHttpStatusValidator() { | ||
throw new AssertionError(); | ||
} | ||
|
||
/** | ||
* Checks if the specified {@code HttpStatus} argument is contained in the also specified allowed HTTP statuses | ||
* argument. | ||
* | ||
* @param httpStatus the HTTP status to be validated. | ||
* @param allowedHttpStatuses the HTTP statuses that are allowed. | ||
* @param commandResponseType is required to build a descriptive error message in case validation failed. | ||
* @return {@code httpStatus} if it is valid. | ||
* @throws NullPointerException if any argument is {@code null}. | ||
* @throws IllegalArgumentException if {@code allowedHttpStatuses} is empty or if {@code httpStatus} is not in | ||
* {@code allowedHttpStatuses}. | ||
*/ | ||
public static HttpStatus validateHttpStatus(final HttpStatus httpStatus, | ||
final Collection<HttpStatus> allowedHttpStatuses, | ||
final Class<? extends CommandResponse> commandResponseType) { | ||
|
||
ConditionChecker.argumentNotEmpty(allowedHttpStatuses, "allowedHttpStatuses"); | ||
checkNotNull(commandResponseType, "commandResponseType"); | ||
return ConditionChecker.checkArgument(checkNotNull(httpStatus, "httpStatus"), | ||
allowedHttpStatuses::contains, | ||
() -> MessageFormat.format("{0} is invalid. {1} only allows {2}.", | ||
httpStatus, | ||
commandResponseType.getSimpleName(), | ||
allowedHttpStatuses)); | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
...org/eclipse/ditto/base/model/signals/commands/CommandResponseHttpStatusValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2021 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.signals.commands; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf; | ||
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.eclipse.ditto.base.model.common.HttpStatus; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Unit test for {@link CommandResponseHttpStatusValidator}. | ||
*/ | ||
public final class CommandResponseHttpStatusValidatorTest { | ||
|
||
@Test | ||
public void assertImmutability() { | ||
assertInstancesOf(CommandResponseHttpStatusValidator.class, areImmutable()); | ||
} | ||
|
||
@Test | ||
public void validateHttpStatusWithNullHttpStatusThrowsNullPointerException() { | ||
Assertions.assertThatNullPointerException() | ||
.isThrownBy(() -> CommandResponseHttpStatusValidator.validateHttpStatus(null, | ||
Collections.singleton(HttpStatus.OK), | ||
AbstractCommandResponse.class)) | ||
.withMessage("The httpStatus must not be null!") | ||
.withNoCause(); | ||
} | ||
|
||
@Test | ||
public void validateHttpStatusWithNullAllowedHttpStatusesThrowsNullPointerException() { | ||
Assertions.assertThatNullPointerException() | ||
.isThrownBy(() -> CommandResponseHttpStatusValidator.validateHttpStatus(HttpStatus.OK, | ||
null, | ||
AbstractCommandResponse.class)) | ||
.withMessage("The allowedHttpStatuses must not be null!") | ||
.withNoCause(); | ||
} | ||
|
||
@Test | ||
public void validateHttpStatusWithEmptyAllowedHttpStatusesThrowsIllegalArgumentException() { | ||
Assertions.assertThatIllegalArgumentException() | ||
.isThrownBy(() -> CommandResponseHttpStatusValidator.validateHttpStatus(HttpStatus.OK, | ||
Collections.emptySet(), | ||
AbstractCommandResponse.class)) | ||
.withMessage("The argument 'allowedHttpStatuses' must not be empty!") | ||
.withNoCause(); | ||
} | ||
|
||
@Test | ||
public void validateHttpStatusWithNullCommandResponseTypeThrowsNullPointerException() { | ||
Assertions.assertThatNullPointerException() | ||
.isThrownBy(() -> CommandResponseHttpStatusValidator.validateHttpStatus(HttpStatus.OK, | ||
Collections.singleton(HttpStatus.OK), | ||
null)) | ||
.withMessage("The commandResponseType must not be null!") | ||
.withNoCause(); | ||
} | ||
|
||
@Test | ||
public void validateHttpStatusWithAllowedHttpStatusReturnsExpected() { | ||
final HttpStatus httpStatus = HttpStatus.NO_CONTENT; | ||
|
||
final HttpStatus validHttpStatus = CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus, | ||
Arrays.asList(HttpStatus.OK, HttpStatus.CREATED, HttpStatus.NO_CONTENT), | ||
AbstractCommandResponse.class); | ||
|
||
assertThat(validHttpStatus).isEqualTo(httpStatus); | ||
} | ||
|
||
@Test | ||
public void validateHttpStatusWithDisallowedHttpStatusThrowsIllegalArgumentException() { | ||
final HttpStatus httpStatus = HttpStatus.OK; | ||
final List<HttpStatus> allowedHttpStatuses = Arrays.asList(HttpStatus.CREATED, HttpStatus.NO_CONTENT); | ||
final Class<AbstractCommandResponse> commandResponseType = AbstractCommandResponse.class; | ||
|
||
Assertions.assertThatIllegalArgumentException() | ||
.isThrownBy(() -> CommandResponseHttpStatusValidator.validateHttpStatus(httpStatus, | ||
allowedHttpStatuses, | ||
commandResponseType)) | ||
.withMessage("%s is invalid. %s only allows %s.", | ||
httpStatus, | ||
commandResponseType.getSimpleName(), | ||
allowedHttpStatuses) | ||
.withNoCause(); | ||
} | ||
|
||
} |