Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor stringassertion #393

Merged
merged 6 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class AbstractPropertyAssertion<T> implements ActualProperty<T>
protected final AbstractPropertyAssertion<T> parent;
protected PropertyAssertionConfig config;

public AbstractPropertyAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<T> provider) {
public AbstractPropertyAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
this.parent = parentAssertion;
this.provider = provider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/
public interface FileAssertion extends ActualProperty<File> {
QuantityAssertion<Long> bytes();
StringAssertion<String> name();
StringAssertion<String> extension();
StringAssertion<String> mimetype();
StringAssertion name();
StringAssertion extension();
StringAssertion mimetype();
BinaryAssertion<Boolean> exists();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Testerra
*
* (C) 2024, Sebastian Kiehne, Deutsche Telekom MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you 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
*
* http://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 eu.tsystems.mms.tic.testframework.internal.asserts;
martingrossmann marked this conversation as resolved.
Show resolved Hide resolved

public interface ObjectAssertion<T> extends BinaryAssertion<T> {
default boolean is(Object expected) {
return is(expected, null);
}

boolean is(Object expected, String subject);

default boolean isNot(Object expected) {
return isNot(expected, null);
}

boolean isNot(Object expected, String subject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@
* Allows numeric range based assertions
* @author Mike Reiche
*/
public interface QuantityAssertion<TYPE> extends BinaryAssertion<TYPE> {
default boolean is(Object expected) {
return is(expected, null);
}
boolean is(Object expected, String subject);

default boolean isNot(Object expected) {
return isNot(expected, null);
}
boolean isNot(Object expected, String subject);
public interface QuantityAssertion<TYPE> extends ObjectAssertion<TYPE> {

default boolean isGreaterThan(long expected) {
return isGreaterThan(new BigDecimal(expected));
Expand Down Expand Up @@ -124,7 +115,7 @@ default boolean isBetween(BigDecimal lower, BigDecimal higher) {
}
boolean isBetween(BigDecimal lower, BigDecimal higher, String subject);

<MAPPED_TYPE> StringAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction);
<MAPPED_TYPE> QuantityAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction);

QuantityAssertion<BigDecimal> absolute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Allows string based assertions
* @author Mike Reiche
*/
public interface StringAssertion<T> extends QuantityAssertion<T> {
public interface StringAssertion extends ObjectAssertion<String> {
BinaryAssertion <Boolean> contains(String expected);
BinaryAssertion <Boolean> startsWith(String expected);
BinaryAssertion <Boolean> endsWith(String expected);
Expand All @@ -54,4 +55,6 @@ default boolean isContaining(String text) {
default boolean isNotContaining(String text) {
return contains(text).is(false);
}

StringAssertion map(Function<String, String> mapFunction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class AbstractTestedPropertyAssertion<T> extends AbstractPropert
protected static final Assertion assertionImpl = Testerra.getInjector().getInstance(Assertion.class);
private static final TestController.Overrides overrides = Testerra.getInjector().getInstance(TestController.Overrides.class);

public AbstractTestedPropertyAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<T> provider) {
public AbstractTestedPropertyAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
super(parentAssertion, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @author Mike Reiche
*/
public class DefaultBinaryAssertion<T> extends AbstractTestedPropertyAssertion<T> implements BinaryAssertion<T> {
public DefaultBinaryAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<T> provider) {
public DefaultBinaryAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
super(parentAssertion, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class DefaultFileAssertion extends AbstractPropertyAssertion<File> implements FileAssertion {

public DefaultFileAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<File> provider) {
public DefaultFileAssertion(AbstractPropertyAssertion<File> parentAssertion, AssertionProvider<File> provider) {
super(parentAssertion, provider);
}

Expand All @@ -52,7 +52,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> name() {
public StringAssertion name() {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -67,7 +67,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> extension() {
public StringAssertion extension() {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand All @@ -82,7 +82,7 @@ public String createSubject() {
}

@Override
public StringAssertion<String> mimetype() {
public StringAssertion mimetype() {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<String>() {
@Override
public String getActual() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Testerra
*
* (C) 2024, Sebastian Kiehne, Deutsche Telekom MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you 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
*
* http://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 eu.tsystems.mms.tic.testframework.internal.asserts;
martingrossmann marked this conversation as resolved.
Show resolved Hide resolved

public class DefaultObjectAssertion<T> extends DefaultBinaryAssertion<T> implements ObjectAssertion<T> {

public DefaultObjectAssertion(AbstractPropertyAssertion<T> parentAssertion, AssertionProvider<T> provider) {
super(parentAssertion, provider);
}

@Override
public boolean is(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(expectedBoolean, failMessage);
}
return testSequence(
provider,
(actual) -> assertionImpl.equals(actual, expected),
(actual) -> assertionImpl.formatExpectEquals(null, expected, createFailMessage(failMessage))
);
}

@Override
public boolean isNot(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(!expectedBoolean, failMessage);
} else {
return testSequence(
provider,
(actual) -> assertionImpl.notEquals(actual, expected),
(actual) -> assertionImpl.formatExpectNotEquals(null, expected, createFailMessage(failMessage))
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class DefaultPatternAssertion extends AbstractTestedPropertyAssertion<Matcher> implements PatternAssertion {

public DefaultPatternAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<Matcher> provider) {
public DefaultPatternAssertion(AbstractPropertyAssertion<Matcher> parentAssertion, AssertionProvider<Matcher> provider) {
super(parentAssertion, provider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
*/
public class DefaultPropertyAssertionFactory implements PropertyAssertionFactory, Loggable {

public <ASSERTION extends AbstractPropertyAssertion, TYPE> ASSERTION createAssertion(
public <ASSERTION extends AbstractPropertyAssertion, T> ASSERTION createAssertion(
Class<ASSERTION> assertionClass,
AbstractPropertyAssertion parentAssertion,
AssertionProvider<TYPE> provider
AssertionProvider<T> provider
) {
ASSERTION assertion;
try {
Expand All @@ -48,10 +48,10 @@ public <ASSERTION extends AbstractPropertyAssertion, TYPE> ASSERTION createAsser
}

@Override
public <ASSERTION extends AbstractPropertyAssertion, TYPE> ASSERTION createWithParent(
public <ASSERTION extends AbstractPropertyAssertion, T> ASSERTION createWithParent(
Class<ASSERTION> assertionClass,
AbstractPropertyAssertion parentAssertion,
AssertionProvider<TYPE> provider
AssertionProvider<T> provider
) {
ASSERTION assertion = createAssertion(assertionClass, parentAssertion, provider);
assertion.config = parentAssertion.config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,12 @@
*
* @author Mike Reiche
*/
public class DefaultQuantityAssertion<TYPE> extends DefaultBinaryAssertion<TYPE> implements QuantityAssertion<TYPE> {
public class DefaultQuantityAssertion<TYPE> extends DefaultObjectAssertion<TYPE> implements QuantityAssertion<TYPE> {

public DefaultQuantityAssertion(AbstractPropertyAssertion parentAssertion, AssertionProvider<TYPE> provider) {
super(parentAssertion, provider);
}

@Override
public boolean is(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(expectedBoolean, failMessage);
}
return testSequence(
provider,
(actual) -> assertionImpl.equals(actual, expected),
(actual) -> assertionImpl.formatExpectEquals(null, expected, createFailMessage(failMessage))
);
}

@Override
public boolean isNot(Object expected, String failMessage) {
if (expected instanceof Boolean) {
boolean expectedBoolean = (Boolean) expected;
return this.is(!expectedBoolean, failMessage);
} else {
return testSequence(
provider,
(actual) -> assertionImpl.notEquals(actual, expected),
(actual) -> assertionImpl.formatExpectNotEquals(null, expected, createFailMessage(failMessage))
);
}
}

private BigDecimal toBigDecimal(Object given) {
if (given == null) {
return null;
Expand Down Expand Up @@ -116,8 +89,8 @@ public boolean isBetween(BigDecimal lower, BigDecimal higher, String failMessage
}

@Override
public <MAPPED_TYPE> StringAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction) {
return propertyAssertionFactory.createWithParent(DefaultStringAssertion.class, this, new AssertionProvider<MAPPED_TYPE>() {
public <MAPPED_TYPE> QuantityAssertion<MAPPED_TYPE> map(Function<? super TYPE, MAPPED_TYPE> mapFunction) {
return propertyAssertionFactory.createWithParent(DefaultQuantityAssertion.class, this, new AssertionProvider<MAPPED_TYPE>() {
@Override
public MAPPED_TYPE getActual() {
TYPE actual = provider.getActual();
Expand Down
Loading