Skip to content

Commit

Permalink
Merge pull request #368 from TikhomirovSergey/iterable_new_options
Browse files Browse the repository at this point in the history
Ability to select sub-lists/sub-arrays and certain items
  • Loading branch information
TikhomirovSergey authored Sep 8, 2022
2 parents 57de635 + be3873d commit e1de281
Show file tree
Hide file tree
Showing 81 changed files with 8,036 additions and 3,792 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

ext {
globalVersion = '0.22.9-ALPHA'
globalVersion = '0.23.0-ALPHA'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
@Description("Resulted array")
public class ArrayCaptor extends CollectionCaptor {

public ArrayCaptor() {
super();
}

public ArrayCaptor(String description) {
super(description);
}

@Override
public List<?> getCaptured(Object toBeCaptured) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@

import ru.tinkoff.qa.neptune.core.api.steps.annotations.Description;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static ru.tinkoff.qa.neptune.core.api.utils.IsLoggableUtil.isLoggable;

@Description("Resulted collection")
public class CollectionCaptor extends IterableCaptor<List<?>> {

public CollectionCaptor() {
super();
}

public CollectionCaptor(String description) {
super(description);
}

@Override
public List<?> getCaptured(Object toBeCaptured) {
return ofNullable(toBeCaptured)
.map(capture -> {
if (!Collection.class.isAssignableFrom(capture.getClass())) {
return null;
}

var result = ((Collection<?>) capture)
.stream()
.filter(o -> {
var clazz = ofNullable(o)
.map(Object::getClass)
.orElse(null);

return isLoggable(o)
|| ofNullable(clazz).map(aClass -> aClass.isArray()
|| Iterable.class.isAssignableFrom(aClass)
|| Map.class.isAssignableFrom(aClass)).orElse(false);
}).collect(toList());
.map(capture -> {
if (!Collection.class.isAssignableFrom(capture.getClass())) {
return null;
}

var result = new ArrayList<>(((Collection<?>) capture));
if (result.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,55 @@
import static java.lang.String.format;
import static java.util.Optional.ofNullable;

public abstract class IterableCaptor<T extends Iterable<?>> extends StringCaptor<T> {
@SuppressWarnings("unchecked")
public class IterableCaptor<T extends Iterable<?>> extends StringCaptor<T> {

private static final String LINE_SEPARATOR = "\r\n";

public IterableCaptor() {
super();
}

public IterableCaptor(String description) {
super(description);
}

@Override
public StringBuilder getData(T caught) {
var captured = new StringBuilder();
caught.forEach(o -> ofNullable(o).ifPresentOrElse(o1 -> {
var clazz = o1.getClass();
var clazz = o1.getClass();

if (Iterable.class.isAssignableFrom(clazz)) {
captured.append(format("%s%s", Iterables.toString((Iterable<?>) o1), LINE_SEPARATOR));
return;
}
if (Iterable.class.isAssignableFrom(clazz)) {
captured.append(format("%s%s", Iterables.toString((Iterable<?>) o1), LINE_SEPARATOR));
return;
}

if (clazz.isArray()) {
captured.append(format("%s%s", Arrays.toString((Object[]) o1), LINE_SEPARATOR));
return;
}
if (clazz.isArray()) {
captured.append(format("%s%s", Arrays.toString((Object[]) o1), LINE_SEPARATOR));
return;
}

captured.append(format("%s%s", o1, LINE_SEPARATOR));
},
() -> captured.append(format("%s%s", o, LINE_SEPARATOR))));
captured.append(format("%s%s", o1, LINE_SEPARATOR));
},
() -> captured.append(format("%s%s", o, LINE_SEPARATOR))));
return captured;
}

@Override
public T getCaptured(Object toBeCaptured) {
if (toBeCaptured instanceof Iterable<?>) {
try {
var iterable = (T) toBeCaptured;
if (Iterables.isEmpty(iterable)) {
return null;
}

return iterable;
} catch (ClassCastException e) {
return null;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import ru.tinkoff.qa.neptune.core.api.steps.annotations.Description;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;

import static java.lang.String.format;
import static java.lang.String.valueOf;
import static java.util.Optional.ofNullable;
import static ru.tinkoff.qa.neptune.core.api.utils.IsLoggableUtil.isLoggable;
import static ru.tinkoff.qa.neptune.core.api.utils.ToArrayUtil.toArray;

@Description("Resulted map")
Expand All @@ -23,19 +19,19 @@ public class MapCaptor extends StringCaptor<Map<?, ?>> {

private String stringValueOf(Object o) {
return ofNullable(o)
.map(o1 -> {
var clazz = o1.getClass();
.map(o1 -> {
var clazz = o1.getClass();

if (Iterable.class.isAssignableFrom(clazz)) {
return Iterables.toString((Iterable<?>) o1);
}
if (Iterable.class.isAssignableFrom(clazz)) {
return Iterables.toString((Iterable<?>) o1);
}

if (clazz.isArray()) {
return Arrays.toString(toArray(o1));
}
if (clazz.isArray()) {
return Arrays.toString(toArray(o1));
}

return o1.toString();
}).orElseGet(() -> valueOf(o));
return o1.toString();
}).orElseGet(() -> valueOf(o));
}

@Override
Expand All @@ -52,35 +48,17 @@ public StringBuilder getData(Map<?, ?> caught) {
@Override
public Map<?, ?> getCaptured(Object toBeCaptured) {
return ofNullable(toBeCaptured)
.map(capture -> {
if (!Map.class.isAssignableFrom(capture.getClass())) {
return null;
}
.map(capture -> {
if (!Map.class.isAssignableFrom(capture.getClass())) {
return null;
}

var list = ((Map<?, ?>) capture).entrySet().stream().filter(entry -> {
var key = entry.getKey();
var value = entry.getValue();
Class<?> keyClazz;
Class<?> valueClazz;
return (isLoggable(key)
|| (isLoggable(value))
|| (keyClazz = key.getClass()).isArray()
|| Iterable.class.isAssignableFrom(keyClazz)
|| Map.class.isAssignableFrom(keyClazz)
|| (valueClazz = value.getClass()).isArray()
|| Iterable.class.isAssignableFrom(valueClazz)
|| Map.class.isAssignableFrom(valueClazz));
}).collect(Collectors.toCollection(LinkedList::new));
if (((Map<?, ?>) toBeCaptured).isEmpty()) {
return null;
}

Map<Object, Object> result = new LinkedHashMap<>();
list.forEach(e -> result.put(e.getKey(), e.getValue()));

if (result.isEmpty()) {
return null;
}

return result;
})
.orElse(null);
return (Map<?, ?>) toBeCaptured;
})
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.google.common.collect.Iterables;
import ru.tinkoff.qa.neptune.core.api.event.firing.Captor;
import ru.tinkoff.qa.neptune.core.api.steps.annotations.MaxDepthOfReporting;
import ru.tinkoff.qa.neptune.core.api.steps.annotations.Description;
import ru.tinkoff.qa.neptune.core.api.steps.annotations.DescriptionFragment;
import ru.tinkoff.qa.neptune.core.api.steps.annotations.MaxDepthOfReporting;
import ru.tinkoff.qa.neptune.core.api.steps.annotations.ThrowWhenNoData;
import ru.tinkoff.qa.neptune.core.api.steps.context.Context;

Expand All @@ -14,13 +14,15 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;
import static ru.tinkoff.qa.neptune.core.api.event.firing.StaticEventFiring.catchValue;
import static ru.tinkoff.qa.neptune.core.api.event.firing.annotations.CaptureOnSuccess.CaptureOnSuccessReader.readCaptorsOnSuccess;
import static ru.tinkoff.qa.neptune.core.api.steps.conditions.ToGetSingleCheckedObject.getSingle;
import static ru.tinkoff.qa.neptune.core.api.steps.conditions.ToGetConditionalHelper.AS_IS;
import static ru.tinkoff.qa.neptune.core.api.steps.conditions.ToGetConditionalHelper.getSingle;

@SequentialGetStepSupplier.DefineTimeOutParameterName("Time of the waiting for absence")
@SequentialGetStepSupplier.DefineResultDescriptionParameterName("Is absent")
Expand All @@ -46,11 +48,11 @@ private Absence() {

private Absence(SequentialGetStepSupplier<?, ?, ?, ?, ?> toBeAbsent) {
this();
from(turnReportingOff(toBeAbsent
.clone()
.clearTimeout()
.addIgnored(Throwable.class)));

var copy = turnReportingOff(
eraseTimeOut(makeACopy(toBeAbsent)).addIgnored(Throwable.class)
);
copy.ignoreSelection();
from(copy);
readCaptorsOnSuccess(toBeAbsent.getClass(), successCaptors);
}

Expand Down Expand Up @@ -80,6 +82,7 @@ public Map<String, String> getParameters() {
return result;
}

@SuppressWarnings("unchecked")
protected Function<T, Object> preparePreFunction() {
var preFunction = super.preparePreFunction();

Expand Down Expand Up @@ -119,7 +122,7 @@ protected Function<T, Object> preparePreFunction() {

lastCaught = result;
return null;
}, timeToGet);
}, (Predicate<Object>) AS_IS, timeToGet, null, null, new HashSet<>());
return ((Function<Object, Object>) o -> ofNullable(o).orElse(false)).compose(resulted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* This class is designed to create a {@link Predicate} used by {@link SequentialGetStepSupplier}
*
* @param <T> is a type of a value to be checked/filtered
* @param <T> is a type of value to be checked/filtered
*/
@SuppressWarnings("unchecked")
public final class Criteria<T> implements Supplier<Predicate<T>> {
Expand Down
Loading

0 comments on commit e1de281

Please sign in to comment.