-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
731 additions
and
495 deletions.
There are no files selected for viewing
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
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
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
81 changes: 81 additions & 0 deletions
81
vividus-engine/src/main/java/org/vividus/variable/DynamicVariableCalculationResult.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,81 @@ | ||
/* | ||
* Copyright 2019-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.variable; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
public final class DynamicVariableCalculationResult | ||
{ | ||
private final Optional<String> value; | ||
private final Optional<String> error; | ||
|
||
private DynamicVariableCalculationResult(Optional<String> value, Optional<String> error) | ||
{ | ||
this.value = value; | ||
this.error = error; | ||
} | ||
|
||
public static DynamicVariableCalculationResult withValue(String value) | ||
{ | ||
return new DynamicVariableCalculationResult(Optional.of(value), Optional.empty()); | ||
} | ||
|
||
public static DynamicVariableCalculationResult withError(String error) | ||
{ | ||
return new DynamicVariableCalculationResult(Optional.empty(), Optional.of(error)); | ||
} | ||
|
||
public static DynamicVariableCalculationResult withValueAndError(Optional<String> value, | ||
Supplier<String> errorSupplier) | ||
{ | ||
return new DynamicVariableCalculationResult(value, | ||
value.isEmpty() ? Optional.of(errorSupplier.get()) : Optional.empty()); | ||
} | ||
|
||
public Optional<String> getValueOrHandleError(Consumer<String> errorHandler) | ||
{ | ||
if (!value.isPresent()) | ||
{ | ||
errorHandler.accept(error.get()); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (this == o) | ||
{ | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) | ||
{ | ||
return false; | ||
} | ||
DynamicVariableCalculationResult that = (DynamicVariableCalculationResult) o; | ||
return value.equals(that.value) && error.equals(that.error); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return Objects.hash(value, error); | ||
} | ||
} |
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
98 changes: 98 additions & 0 deletions
98
vividus-engine/src/test/java/org/vividus/variable/DynamicVariableCalculationResultTests.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,98 @@ | ||
/* | ||
* Copyright 2019-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.variable; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import nl.jqno.equalsverifier.Warning; | ||
|
||
class DynamicVariableCalculationResultTests | ||
{ | ||
private static final String VALUE = "value"; | ||
private static final String ERROR = "error"; | ||
|
||
@Test | ||
void shouldCreateResultWithValueAndWithoutError() | ||
{ | ||
var result = DynamicVariableCalculationResult.withValue(VALUE); | ||
assertResultWithValue(result, VALUE); | ||
} | ||
|
||
@Test | ||
void shouldCreateResultWithoutValueAndWithError() | ||
{ | ||
var result = DynamicVariableCalculationResult.withError(ERROR); | ||
assertResultWithError(result, ERROR); | ||
} | ||
|
||
@Test | ||
void shouldCreateResultWithOptionalValueAndWithoutError() | ||
{ | ||
var result = DynamicVariableCalculationResult.withValueAndError(Optional.of(VALUE), () -> ERROR); | ||
assertResultWithValue(result, VALUE); | ||
} | ||
|
||
@Test | ||
void shouldCreateResultWithMissingValueAndWithError() | ||
{ | ||
var result = DynamicVariableCalculationResult.withValueAndError(Optional.empty(), () -> ERROR); | ||
assertResultWithError(result, ERROR); | ||
} | ||
|
||
@Test | ||
void validateHashCodeAndEquals() | ||
{ | ||
EqualsVerifier.forClass(DynamicVariableCalculationResult.class) | ||
.suppress(Warning.NULL_FIELDS) | ||
.verify(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void assertResultWithValue(DynamicVariableCalculationResult result, String expectedValue) | ||
{ | ||
assertAll( | ||
() -> { | ||
var errorHandler = mock(Consumer.class); | ||
assertEquals(Optional.of(expectedValue), result.getValueOrHandleError(errorHandler)); | ||
verifyNoInteractions(errorHandler); | ||
}, | ||
() -> assertEquals(result, DynamicVariableCalculationResult.withValue(expectedValue)) | ||
); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void assertResultWithError(DynamicVariableCalculationResult result, String expectedError) | ||
{ | ||
assertAll( | ||
() -> { | ||
var errorHandler = mock(Consumer.class); | ||
assertEquals(Optional.empty(), result.getValueOrHandleError(errorHandler)); | ||
verify(errorHandler).accept(expectedError); | ||
}, | ||
() -> assertEquals(result, DynamicVariableCalculationResult.withError(expectedError))); | ||
} | ||
} |
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
Oops, something went wrong.