io.github.carlomicieli.footballdb.starter.pages.TableTests.java Source code

Java tutorial

Introduction

Here is the source code for io.github.carlomicieli.footballdb.starter.pages.TableTests.java

Source

/*
 * Copyright 2014 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
 *
 *    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 io.github.carlomicieli.footballdb.starter.pages;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.github.carlomicieli.footballdb.starter.pages.TableBuilder.Cell.cell;
import static io.github.carlomicieli.footballdb.starter.pages.TableBuilder.Cell.cells;
import static io.github.carlomicieli.footballdb.starter.pages.TableBuilder.Cell.multiple;
import static io.github.carlomicieli.footballdb.starter.test.MyProjectAssertions.assertThat;
import static java.util.Arrays.asList;

/**
 * @author Carlo Micieli
 */
public class TableTests {

    @Rule
    public ExpectedException expectedEx = ExpectedException.none();

    @Test
    public void shouldBuildNewTablesWithOneCell() {
        Table table1x1 = Table.builder().row("one").toTable();

        assertThat(table1x1.rowsCount()).isEqualTo(1);
        assertThat(table1x1.columnsCount()).isEqualTo(1);
        assertThat(table1x1.rowValues(1)).contains(asList("one"));
    }

    @Test
    public void shouldBuildNewTablesWithOneRowAndMoreColumns() {
        Table table1x3 = Table.builder().row("one", "two", "three").toTable();

        assertThat(table1x3.rowsCount()).isEqualTo(1);
        assertThat(table1x3.rowValues(1)).contains(asList("one", "two", "three"));
    }

    @Test
    public void shouldBuildNewTablesWithMoreRowsAndMoreColumns() {
        Table table2x3 = Table.builder().row("one", "two", "three")
                .row(cells(cell("four"), cell("five"), cell("six"))).toTable();

        assertThat(table2x3.rowsCount()).isEqualTo(2);
        assertThat(table2x3.columnsCount()).isEqualTo(3);
        assertThat(table2x3.rowValues(1)).contains(asList("one", "two", "three"));
        assertThat(table2x3.rowValues(2)).contains(asList("four", "five", "six"));
    }

    @Test
    public void shouldBuildNewTablesWithAnyNumberOfColumnsAndRows() {
        Table table3x3 = newTable3x3();

        assertThat(table3x3.rowsCount()).isEqualTo(3);
        assertThat(table3x3.rowValues(1)).contains(asList("one", "two", "three"));
        assertThat(table3x3.rowValues(2)).contains(asList("four", "five", "six"));
        assertThat(table3x3.rowValues(3)).contains(asList("seven", "eight", "nine"));
    }

    @Test
    public void shouldReturnsTheTableSize() {
        Table emptyTable = new TableBuilder().toTable();
        Table table3x3 = newTable3x3();

        assertThat(emptyTable.size()).isEqualTo(ImmutablePair.of(0, 0));
        assertThat(table3x3.size()).isEqualTo(ImmutablePair.of(3, 3));
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowsExceptionIfRowsHaveDifferentNumberOfColumns() {
        TableBuilder tableBuilder = new TableBuilder();
        tableBuilder.row("one", "two", "three");
        tableBuilder.row("four", "five");
    }

    @Test
    public void shouldReturnRowValuesFromTable() {
        Table table3x3 = newTable3x3();

        Optional<Table.Row> firstRow = table3x3.row(1);

        assertThat(firstRow).isPresent();
        assertThat(firstRow.orElse(null).toString()).isEqualTo("Row(one, two, three)");
        assertThat(table3x3.rowValues(4)).isNotPresent();
        assertThat(table3x3.rowValues(999)).isNotPresent();
    }

    @Test
    public void shouldGetValueFromRowByIndex() {
        Table table3x3 = newTable3x3();

        String cell_1_1 = table3x3.row(1).orElse(null).value(1);

        assertThat(cell_1_1).isEqualTo("one");
    }

    @Test
    public void shouldThrowExceptionIfGettingValueByColumnFromTablesWithoutHeader() {
        expectedEx.expect(UnsupportedOperationException.class);

        Table table3x3 = newTable3x3();
        table3x3.row(1).orElse(null).value("name");
    }

    @Test
    public void shouldReturnBlankIfGettingValueByColumnFromTableWhenColumnNotExists() {
        Table table3x3 = newTable3x3WithHeaders();

        String cell_1_1 = table3x3.row(1).orElse(null).value("NOT_FOUND");

        assertThat(cell_1_1).isEqualTo("");
    }

    @Test
    public void shouldGetValueFromRowByColumnName() {
        Table table3x3 = newTable3x3WithHeaders();

        String cell_1_1 = table3x3.row(1).orElse(null).value("COL2");

        assertThat(cell_1_1).isEqualTo("two");
    }

    @Test
    public void shouldReturnAStreamOfRows() {
        Table table3x3 = newTable3x3();
        Stream<Table.Row> rows = table3x3.rowsStream();
        assertThat(rows.count()).isEqualTo(3);
    }

    @Test
    public void shouldProduceStringRepresentationForTables() {
        Table table3x3 = newTable3x3();

        String expected = "Table(" + "Row(one, two, three), " + "Row(four, five, six), "
                + "Row(seven, eight, nine))";
        assertThat(table3x3.toString()).isEqualTo(expected);
    }

    @Test
    public void shouldProduceStringRepresentationForTablesWithHeader() {
        Table table2x2WithHeaders = Table.builder().headers("FIRST", "SECOND").row("one", "two")
                .row("three", "four").toTable();

        String expected = "Table(" + "Header(FIRST, SECOND), " + "Row(one, two), " + "Row(three, four))";
        assertThat(table2x2WithHeaders.toString()).isEqualTo(expected);
    }

    @Test
    public void shouldBuildNewTablesWithHeaderRow() {
        Table table2x2WithHeaders = Table.builder().headers("FIRST", "SECOND").row("one", "two")
                .row("three", "four").toTable();

        assertThat(table2x2WithHeaders.size()).isEqualTo(ImmutablePair.of(2, 2));
        assertThat(table2x2WithHeaders.header()).contains("FIRST", "SECOND");
    }

    @Test
    public void shouldBuildNewTablesWithHeaderRowWhenHeadAreSpanOverMultipleColumns() {
        Table table1x4WithHeaders = Table.builder().headers(cells(cell("FIRST"), multiple("SECOND", 3)))
                .row("one", "two", "three", "four").toTable();

        assertThat(table1x4WithHeaders.size()).isEqualTo(ImmutablePair.of(1, 4));
        assertThat(table1x4WithHeaders.header()).contains("FIRST", "SECOND", "SECOND", "SECOND");
    }

    @Test
    public void shouldBuildNewTablesWithMultipleHeaderRows() {
        Table table1x4WithHeaders = Table.builder().headers(cells(cell("FIRST"), multiple("SECOND", 3)))
                .headers("one", "two", "three", "four").row("one", "two", "three", "four").toTable();

        assertThat(table1x4WithHeaders.size()).isEqualTo(ImmutablePair.of(1, 4));
        assertThat(table1x4WithHeaders.header()).contains("FIRST.one", "SECOND.two", "SECOND.three", "SECOND.four");
    }

    @Test
    public void shouldReturnMapOfValuesForTableRows() {
        Table table1x4WithoutHeaders = Table.builder().row("one", "two", "three", "four").toTable();

        Table table1x4WithHeaders = Table.builder().headers("Col1", "Col2", "Col3", "Col4")
                .row("one", "two", "three", "four").toTable();

        Map<String, String> emptyValues = table1x4WithoutHeaders.valuesForRow(1);
        Map<String, String> values = table1x4WithHeaders.valuesForRow(1);

        assertThat(emptyValues).hasSize(0);
        assertThat(values).hasSize(4).containsEntry("Col1", "one").containsEntry("Col2", "two")
                .containsEntry("Col3", "three").containsEntry("Col4", "four");
    }

    @Test
    public void shouldFilterRowsInTables() {
        Table table3x3 = newTable3x3WithHeaders();

        long foundRows = table3x3.filter(r -> r.value("COL2").equals("five")).count();
        long notFoundRows = table3x3.filter(r -> r.value("COL2").equals("not_found")).count();

        assertThat(foundRows).isEqualTo(1);
        assertThat(notFoundRows).isEqualTo(0);
    }

    @Test
    public void shouldMapRowsInTables() {
        Table table3x3 = newTable3x3WithHeaders();

        List<Table.Row> rows = table3x3.map(r -> r.subset("COL1")).collect(Collectors.toList());

        assertThat(rows.get(0).toString()).isEqualTo("Row(one)");
        assertThat(rows.get(1).toString()).isEqualTo("Row(four)");
        assertThat(rows.get(2).toString()).isEqualTo("Row(seven)");
    }

    private static Table newTable3x3() {
        TableBuilder tableBuilder = new TableBuilder();
        return tableBuilder.row("one", "two", "three").row("four", "five", "six").row("seven", "eight", "nine")
                .toTable();
    }

    private static Table newTable3x3WithHeaders() {
        TableBuilder tableBuilder = new TableBuilder();
        return tableBuilder.headers("COL1", "COL2", "COL3").row("one", "two", "three").row("four", "five", "six")
                .row("seven", "eight", "nine").toTable();
    }
}