How to use CompositeKey class of org.testingisdocumenting.webtau.data.table.header package

Best Webtau code snippet using org.testingisdocumenting.webtau.data.table.header.CompositeKey

Source:TableData.java Github

copy

Full Screen

...17package org.testingisdocumenting.webtau.data.table;18import org.testingisdocumenting.webtau.console.ConsoleOutput;19import org.testingisdocumenting.webtau.data.render.PrettyPrintable;20import org.testingisdocumenting.webtau.data.render.TableDataRenderer;21import org.testingisdocumenting.webtau.data.table.header.CompositeKey;22import org.testingisdocumenting.webtau.data.table.header.TableDataHeader;23import org.testingisdocumenting.webtau.utils.JsonUtils;24import java.util.*;25import java.util.function.Function;26import java.util.stream.Stream;27import static java.util.stream.Collectors.joining;28import static java.util.stream.Collectors.toList;29/**30 * Represents a set of rows with named columns to be used as part of test input preparation and/or test output validation31 */32public class TableData implements Iterable<Record>, PrettyPrintable {33 private final List<Record> rows;34 private final Map<CompositeKey, Record> rowsByKey;35 private final Map<CompositeKey, Integer> rowIdxByKey;36 private final TableDataHeader header;37 public TableData(List<?> columnNamesAndOptionalValues) {38 this(new TableDataHeader(extractColumnNames(columnNamesAndOptionalValues.stream()).stream()));39 populateValues(columnNamesAndOptionalValues.stream());40 }41 public TableData(Stream<?> columnNamesAndOptionalValues) {42 this(columnNamesAndOptionalValues.collect(toList()));43 }44 public TableData(TableDataHeader header) {45 this.header = header;46 this.rows = new ArrayList<>();47 this.rowsByKey = new HashMap<>();48 this.rowIdxByKey = new HashMap<>();49 }50 public TableDataHeader getHeader() {51 return header;52 }53 public boolean isEmpty() {54 return rows.isEmpty();55 }56 public Set<CompositeKey> keySet() {57 return rowsByKey.keySet();58 }59 public Integer findRowIdxByKey(CompositeKey key) {60 return rowIdxByKey.get(key);61 }62 /**63 * create new table data with the data of a current one but with new key columns.64 * can be used to validate new key columns uniqueness65 * @param keyColumns new key columns66 * @return new table data with updated key columns67 */68 public TableData withNewKeyColumns(String... keyColumns) {69 TableDataHeader newHeader = new TableDataHeader(header.getNamesStream(), Arrays.stream(keyColumns));70 TableData withNewHeader = new TableData(newHeader);71 for (Record originalRow : rows) {72 withNewHeader.addRow(newHeader.createRecord(originalRow.valuesStream()));73 }74 return withNewHeader;75 }76 /**77 * @param values row values combined in one vararg78 * @return populate table data instance79 */80 public TableData values(Object... values) {81 int numberOfRows = header.size() == 0 ? 0 : values.length / header.size();82 int numberOfExtraValues = header.size() == 0 ? 0 : values.length % header.size();83 if (numberOfExtraValues != 0) {84 int startIdxOfExtraValues = numberOfRows * header.size();85 throw new IllegalArgumentException("unfinished row idx " + numberOfRows + ", header: " + header + "\nvalues so far: " +86 Arrays.stream(values).skip(startIdxOfExtraValues).map(Object::toString).87 collect(joining(", ")));88 }89 int total = numberOfRows * header.size();90 for (int i = 0; i < total; i += header.size()) {91 addRow(Arrays.stream(values).skip(i).limit(header.size()));92 }93 return this;94 }95 public Record row(int rowIdx) {96 validateRowIdx(rowIdx);97 return rows.get(rowIdx);98 }99 public Record find(CompositeKey key) {100 return rowsByKey.get(key);101 }102 public void addRow(List<Object> values) {103 addRow(values.stream());104 }105 public void addRow(Stream<Object> values) {106 Record record = new Record(header, values);107 if (record.hasMultiValues()) {108 record.unwrapMultiValues().forEach(this::addRow);109 } else {110 addRow(record);111 }112 }113 public void addRow(Record record) {114 if (header != record.getHeader()) {115 throw new RuntimeException("incompatible headers. current getHeader: " + header + ", new record one: " + record.getHeader());116 }117 int rowIdx = rows.size();118 CompositeKey key = getOrBuildKey(rowIdx, record);119 Record existing = rowsByKey.put(key, record);120 if (existing != null) {121 throw new IllegalArgumentException("duplicate entry found with key: " + key +122 "\n" + existing +123 "\n" + record);124 }125 Record previous = rows.isEmpty() ? null : rows.get(rows.size() - 1);126 Record withEvaluatedGenerators = record.evaluateValueGenerators(previous, rows.size());127 rowIdxByKey.put(key, rowIdx);128 rows.add(withEvaluatedGenerators);129 }130 public <T, R> TableData map(TableDataCellMapFunction<T, R> mapper) {131 TableData mapped = new TableData(header);132 int rowIdx = 0;133 for (Record originalRow : rows) {134 mapped.addRow(mapRow(rowIdx, originalRow, mapper));135 rowIdx++;136 }137 return mapped;138 }139 public TableData replace(Object before, Object after) {140 return map(((rowIdx, colIdx, columnName, v) -> v.equals(before) ? after : v));141 }142 public <T, R> Stream<R> mapColumn(String columnName, Function<T, R> mapper) {143 int idx = header.columnIdxByName(columnName);144 return rows.stream().map(r -> mapper.apply(r.get(idx)));145 }146 private <T, R> Stream<Object> mapRow(int rowIdx, Record originalRow, TableDataCellMapFunction<T, R> mapper) {147 return header.getColumnIdxStream()148 .mapToObj(idx -> mapper.apply(rowIdx, idx, header.columnNameByIdx(idx), originalRow.get(idx)));149 }150 public Stream<Record> rowsStream() {151 return rows.stream();152 }153 public List<Map<String, ?>> toListOfMaps() {154 return rows.stream().map(Record::toMap).collect(toList());155 }156 public String toJson() {157 return JsonUtils.serializePrettyPrint(toListOfMaps());158 }159 @Override160 public Iterator<Record> iterator() {161 return rows.iterator();162 }163 public int numberOfRows() {164 return rows.size();165 }166 private void validateRowIdx(int rowIdx) {167 if (rowIdx < 0 || rowIdx >= numberOfRows())168 throw new IllegalArgumentException("rowIdx is out of range: [0, " + (numberOfRows() - 1) + "]");169 }170 private CompositeKey getOrBuildKey(int rowIdx, Record row) {171 if (header.hasKeyColumns()) {172 return row.getKey();173 }174 return new CompositeKey(Stream.of(rowIdx));175 }176 private void populateValues(Stream<?> columnNameAndValues) {177 values(columnNameAndValues.skip(header.size() + 1).toArray());178 }179 @Override180 public String toString() {181 return TableDataRenderer.renderTable(this);182 }183 private static List<String> extractColumnNames(Stream<?> columnNameAndValues) {184 List<String> result = new ArrayList<>();185 Iterator<?> iterator = columnNameAndValues.iterator();186 while (iterator.hasNext()) {187 Object nameOrValue = iterator.next();188 if (nameOrValue instanceof TableDataUnderscore) {...

Full Screen

Full Screen

Source:Record.java Github

copy

Full Screen

...16 */17package org.testingisdocumenting.webtau.data.table;18import org.testingisdocumenting.webtau.data.MultiValue;19import org.testingisdocumenting.webtau.data.table.autogen.TableDataCellValueGenerator;20import org.testingisdocumenting.webtau.data.table.header.CompositeKey;21import org.testingisdocumenting.webtau.data.table.header.TableDataHeader;22import java.util.*;23import java.util.function.Function;24import java.util.stream.Stream;25public class Record {26 private final TableDataHeader header;27 private final List<Object> values;28 private final CompositeKey key;29 private final boolean hasMultiValues;30 private final boolean hasValueGenerators;31 public Record(TableDataHeader header, Stream<Object> values) {32 this.header = header;33 RecordFromStream recordFromStream = new RecordFromStream(values);34 hasMultiValues = recordFromStream.hasMultiValues;35 hasValueGenerators = recordFromStream.hasValueGenerators;36 this.values = recordFromStream.values;37 this.key = header.hasKeyColumns() ?38 new CompositeKey(header.getKeyIdxStream().map(this::get)) : null;39 }40 public TableDataHeader getHeader() {41 return header;42 }43 public CompositeKey getKey() {44 return key;45 }46 @SuppressWarnings("unchecked")47 public <E> E get(String name) {48 return (E) values.get(header.columnIdxByName(name));49 }50 @SuppressWarnings("unchecked")51 public <E> E get(String name, E defaultValue) {52 int idx = header.findColumnIdxByName(name);53 return idx ==-1 ? defaultValue : (E) values.get(idx);54 }55 @SuppressWarnings("unchecked")56 public <E> E get(int idx) {57 header.validateIdx(idx);...

Full Screen

Full Screen

Source:TableDataComparison.java Github

copy

Full Screen

...13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.testingisdocumenting.webtau.data.table.comparison;17import org.testingisdocumenting.webtau.data.table.header.CompositeKey;18import org.testingisdocumenting.webtau.data.table.Record;19import org.testingisdocumenting.webtau.data.table.TableData;20import org.testingisdocumenting.webtau.expectation.equality.CompareToComparator;21import java.util.HashMap;22import java.util.HashSet;23import java.util.Map;24import java.util.Set;25import java.util.stream.Stream;26import static org.testingisdocumenting.webtau.WebTauCore.createActualPath;27import static java.util.stream.Collectors.toSet;28public class TableDataComparison {29 private TableData actual;30 private TableData expected;31 private final Map<CompositeKey, Record> actualRowsByKey;32 private final Map<CompositeKey, Integer> actualRowIdxByKey;33 private TableDataComparisonResult comparisonResult;34 private Set<String> columnsToCompare;35 public static TableDataComparisonResult compare(TableData actual, TableData expected) {36 TableDataComparison comparison = new TableDataComparison(actual, expected);37 comparison.compare();38 return comparison.comparisonResult;39 }40 private TableDataComparison(TableData actual, TableData expected) {41 this.actual = actual;42 this.expected = expected;43 this.actualRowIdxByKey = new HashMap<>();44 this.actualRowsByKey = new HashMap<>();45 mapActualRowsByKeyDefinedInExpected();46 this.comparisonResult = new TableDataComparisonResult(actual, expected);47 }48 private void compare() {49 compareColumns();50 compareRows();51 }52 private void compareColumns() {53 Set<String> actualColumns = actual.getHeader().getNamesStream().collect(toSet());54 Set<String> expectedColumns = expected.getHeader().getNamesStream().collect(toSet());55 columnsToCompare = expectedColumns.stream().filter(actualColumns::contains).collect(toSet());56 expectedColumns.stream().filter(c -> ! actualColumns.contains(c)).forEach(comparisonResult::addMissingColumn);57 }58 private void compareRows() {59 reportExtraRows();60 reportMissingRows();61 compareCommonRows();62 }63 private void reportExtraRows() {64 HashSet<CompositeKey> actualKeys = new HashSet<>(actualRowIdxByKey.keySet());65 actualKeys.removeAll(expected.keySet());66 for (CompositeKey actualKey : actualKeys) {67 comparisonResult.addExtraRow(actualRowsByKey.get(actualKey));68 }69 }70 private void reportMissingRows() {71 HashSet<CompositeKey> expectedKeys = new HashSet<>(expected.keySet());72 expectedKeys.removeAll(actualRowIdxByKey.keySet());73 for (CompositeKey expectedKey : expectedKeys) {74 comparisonResult.addMissingRow(expected.find(expectedKey));75 }76 }77 private void compareCommonRows() {78 HashSet<CompositeKey> actualKeys = new HashSet<>(actualRowsByKey.keySet());79 actualKeys.retainAll(expected.keySet());80 for (CompositeKey actualKey : actualKeys) {81 Integer actualRowIdx = actualRowIdxByKey.get(actualKey);82 Integer expectedRowIdx = expected.findRowIdxByKey(actualKey);83 compare(actualRowIdx, expectedRowIdx,84 actual.row(actualRowIdx), expected.row(expectedRowIdx));85 }86 }87 private void compare(Integer actualRowIdx, Integer expectedRowIdx, Record actual, Record expected) {88 columnsToCompare.forEach(columnName -> compare(actualRowIdx, expectedRowIdx, columnName,89 actual.get(columnName), expected.get(columnName)));90 }91 private void compare(Integer actualRowIdx, Integer expectedRowIdx, String columnName, Object actual, Object expected) {92 CompareToComparator comparator = CompareToComparator.comparator();93 boolean isEqual = comparator.compareIsEqual(createActualPath(columnName), actual, expected);94 if (isEqual) {95 return;96 }97 comparisonResult.addMismatch(actualRowIdx, expectedRowIdx, columnName, comparator.generateEqualMismatchReport());98 }99 private void mapActualRowsByKeyDefinedInExpected() {100 for (int rowIdx = 0; rowIdx < actual.numberOfRows(); rowIdx++) {101 Record row = actual.row(rowIdx);102 CompositeKey key = expected.getHeader().hasKeyColumns() ?103 expected.getHeader().createKey(row) :104 new CompositeKey(Stream.of(rowIdx));105 Record previous = actualRowsByKey.put(key, row);106 if (previous != null) {107 throw new IllegalArgumentException("duplicate entry found in actual table with key: " + key +108 "\n" + previous +109 "\n" + row);110 }111 actualRowIdxByKey.put(key, rowIdx);112 }113 }114}...

Full Screen

Full Screen

CompositeKey

Using AI Code Generation

copy

Full Screen

1package org.testingisdocumenting.examples;2import org.testingisdocumenting.webtau.data.table.header.CompositeKey;3import org.testingisdocumenting.webtau.data.table.header.TableHeader;4import java.util.HashMap;5import java.util.Map;6public class CompositeKeyExample {7 public static void main(String[] args) {8 Map<CompositeKey, String> map = new HashMap<>();9 map.put(TableHeader.compositeKey("a", "b"), "value");10 map.put(TableHeader.compositeKey("c", "d"), "value2");11 System.out.println(map.get(TableHeader.compositeKey("a", "b")));12 System.out.println(map.get(TableHeader.compositeKey("c", "d")));13 }14}15package org.testingisdocumenting.examples;16import org.testingisdocumenting.webtau.data.table.header.CompositeKey;17import org.testingisdocumenting.webtau.data.table.header.TableHeader;18import java.util.HashMap;19import java.util.Map;20public class CompositeKeyExample {21 public static void main(String[] args) {22 Map<CompositeKey, String> map = new HashMap<>();23 map.put(new CompositeKey("a", "b"), "value");24 map.put(new CompositeKey("c", "d"), "value2");25 System.out.println(map.get(new CompositeKey("a", "b")));26 System.out.println(map.get(new CompositeKey("c", "d")));27 }28}29package org.testingisdocumenting.examples;30import org.testingisdocumenting.webtau.data.table.header.CompositeKey;31import org.testingisdocumenting.webtau.data.table.header.TableHeader;32import java.util.HashMap;33import java.util.Map;34public class CompositeKeyExample {35 public static void main(String[] args) {36 Map<CompositeKey, String> map = new HashMap<>();37 map.put(new CompositeKey("a", "b"), "value");38 map.put(new CompositeKey("c", "d"), "value2");39 System.out.println(map.get(TableHeader.compositeKey("a", "b")));40 System.out.println(map.get(TableHeader.compositeKey("c", "d")));41 }42}

Full Screen

Full Screen

CompositeKey

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.data.table.header.CompositeKey;2import org.testingisdocumenting.webtau.data.table.header.TableHeader;3import org.testingisdocumenting.webtau.data.table.TableData;4import static org.testingisdocumenting.webtau.WebTauDsl.*;5public class CompositeKeyDemo {6 public static void main(String[] args) {7 TableData table = table(8 header("first", "second", "third"),9 row("a", "b", "c"),10 row("d", "e", "f"),11 row("g", "h", "i"));12 CompositeKey compositeKey = CompositeKey.of("first", "second");13 table.header().setCompositeKey(compositeKey);14 table.should(equal(15 header("first", "second", "third"),16 row("a", "b", "c"),17 row("d", "e", "f"),18 row("g", "h", "i")));19 }20}21import org.testingisdocumenting.webtau.data.table.header.CompositeKey;22import org.testingisdocumenting.webtau.data.table.header.TableHeader;23import org.testingisdocumenting.webtau.data.table.TableData;24import static org.testingisdocumenting.webtau.WebTauDsl.*;25public class CompositeKeyDemo {26 public static void main(String[] args) {27 TableData table = table(28 header("first", "second", "third"),29 row("a", "b", "c"),30 row("d", "e", "f"),31 row("g", "h", "i"));32 table.header().setCompositeKey("first", "second");33 table.should(equal(34 header("first", "second", "third"),35 row("a", "b", "c"),36 row("d", "e", "f"),37 row("g", "h", "i")));38 }39}40import org.testingisdocumenting.webtau.data.table.header.CompositeKey;41import org.testingisdocumenting.webtau.data.table.header.TableHeader;42import org

Full Screen

Full Screen

CompositeKey

Using AI Code Generation

copy

Full Screen

1import org.testingisdocumenting.webtau.data.table.header.CompositeKey;2import org.testingisdocumenting.webtau.data.table.header.TableHeader;3import java.util.Arrays;4import static org.testingisdocumenting.webtau.Ddjt.*;5public class CompositeKeyTable {6 public static void main(String[] args) {7 TableHeader header = new TableHeader(Arrays.asList("col1", "col2", "col3"));8 CompositeKey key = new CompositeKey(Arrays.asList("col1", "col2"));9 table(header, key,10 row("a", "b", "c"),11 row("a", "b", "d"),12 row("a", "c", "d"),13 row("a", "c", "e"),14 row("a", "c", "f"),15 row("b", "c", "d")16 );17 at("a", "b").should(equal("c"));18 at("a", "c").should(equal("d"));19 at("b", "c").should(equal("d"));20 }21}22import org.testingisdocumenting.webtau.data.table.header.CompositeKey;23import org.testingisdocumenting.webtau.data.table.header.TableHeader;24import java.util.Arrays;25import static org.testingisdocumenting.webtau.Ddjt.*;26public class CompositeKeyTable {27 public static void main(String[] args) {28 TableHeader header = new TableHeader(Arrays.asList("col1", "col2", "col3"));29 CompositeKey key = new CompositeKey(Arrays.asList("col1", "col2"));30 table(header, key,31 row("a", "b", "c"),32 row("a", "b", "d"),33 row("a", "c", "d"),34 row("a", "c", "e"),35 row("a", "c", "f"),36 row("b", "c", "d")37 );38 at("a", "b").should(equal("c"));39 at("a", "c").should(equal("d"));40 at("b", "c").should(equal("d"));41 }42}

Full Screen

Full Screen

CompositeKey

Using AI Code Generation

copy

Full Screen

1Header header = header("id", "name", "age");2TableData tableData = table(header, 3 row("1", "john", "20"),4 row("2", "jane", "21"));5Header header = header("id", "name", "age");6TableData tableData = table(header, 7 row("1", "john", "20"),8 row("2", "jane", "21"));9Header header = header("id", "name", "age");10TableData tableData = table(header, 11 row("1", "john", "20"),12 row("2", "jane", "21"));13Header header = header("id", "name", "age");14TableData tableData = table(header, 15 row("1", "john", "20"),16 row("2", "jane", "21"));17Header header = header("id", "name", "age");18TableData tableData = table(header, 19 row("1", "john", "20"),20 row("2", "jane", "21"));21Header header = header("id", "name", "age");22TableData tableData = table(header, 23 row("1", "john", "20"),24 row("2", "jane", "21"));25Header header = header("id", "name", "age");26TableData tableData = table(header, 27 row("1", "john", "20"),28 row("2", "jane", "21"));29Header header = header("id", "name", "age");30TableData tableData = table(header, 31 row("1", "john", "20"),32 row("2", "jane", "

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Webtau automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in CompositeKey

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful