diff --git a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithBloatCheckOnHostTest.java b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithBloatCheckOnHostTest.java index 6f416d26..410c45c5 100644 --- a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithBloatCheckOnHostTest.java +++ b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithBloatCheckOnHostTest.java @@ -51,8 +51,8 @@ void onDatabaseWithThem(final String schemaName) { .executing(ctx) .hasSize(2) .containsExactlyInAnyOrder( - TableWithBloat.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0), - TableWithBloat.of(ctx.enrichWithSchema("clients"), 0L, 0L, 0)) + TableWithBloat.of(ctx, "accounts", 0L, 0L, 0), + TableWithBloat.of(ctx, "clients", 0L, 0L, 0)) .allMatch(t -> t.getTableSizeInBytes() > 0L) // real size doesn't matter .allMatch(t -> t.getBloatPercentage() == 0 && t.getBloatSizeInBytes() == 0L); diff --git a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithMissingIndexesCheckOnHostTest.java b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithMissingIndexesCheckOnHostTest.java index e0457eda..4eb92463 100644 --- a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithMissingIndexesCheckOnHostTest.java +++ b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/TablesWithMissingIndexesCheckOnHostTest.java @@ -45,7 +45,7 @@ void onDatabaseWithThem(final String schemaName) { .executing(ctx) .hasSize(1) .containsExactly( - TableWithMissingIndex.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0L)) + TableWithMissingIndex.of(ctx, "accounts", 0L, 0L, 0L)) .allMatch(t -> t.getSeqScans() >= AMOUNT_OF_TRIES) .allMatch(t -> t.getIndexScans() == 0) .allMatch(t -> t.getTableSizeInBytes() > 1L); diff --git a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithBloat.java b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithBloat.java index 9b89177b..fd3f7ced 100644 --- a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithBloat.java +++ b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithBloat.java @@ -11,6 +11,7 @@ package io.github.mfvanek.pg.model.table; import io.github.mfvanek.pg.model.bloat.BloatAware; +import io.github.mfvanek.pg.model.context.PgContext; import io.github.mfvanek.pg.model.validation.Validators; import java.util.Objects; @@ -107,6 +108,7 @@ public int compareTo(@Nonnull final TableWithBloat other) { * @param bloatPercentage bloat percentage in the range from 0 to 100 inclusive. * @return {@code TableWithBloat} */ + @Nonnull public static TableWithBloat of(@Nonnull final String tableName, final long tableSizeInBytes, final long bloatSizeInBytes, @@ -115,6 +117,27 @@ public static TableWithBloat of(@Nonnull final String tableName, return of(table, bloatSizeInBytes, bloatPercentage); } + /** + * Constructs a {@code TableWithBloat} object with given context. + * + * @param pgContext the schema context to enrich table name; must be non-null. + * @param tableName table name; should be non-blank. + * @param tableSizeInBytes table size in bytes; should be positive or zero. + * @param bloatSizeInBytes bloat amount in bytes; should be positive or zero. + * @param bloatPercentage bloat percentage in the range from 0 to 100 inclusive. + * @return {@code TableWithBloat} + * @since 0.14.3 + */ + @Nonnull + public static TableWithBloat of(@Nonnull final PgContext pgContext, + @Nonnull final String tableName, + final long tableSizeInBytes, + final long bloatSizeInBytes, + final double bloatPercentage) { + final Table table = Table.of(pgContext, tableName, tableSizeInBytes); + return of(table, bloatSizeInBytes, bloatPercentage); + } + /** * Constructs a {@code TableWithBloat} object. * @@ -124,6 +147,7 @@ public static TableWithBloat of(@Nonnull final String tableName, * @return {@code TableWithBloat} * @since 0.7.0 */ + @Nonnull public static TableWithBloat of(@Nonnull final Table table, final long bloatSizeInBytes, final double bloatPercentage) { diff --git a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithMissingIndex.java b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithMissingIndex.java index 7ca2dad1..cd01720f 100644 --- a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithMissingIndex.java +++ b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/table/TableWithMissingIndex.java @@ -10,6 +10,7 @@ package io.github.mfvanek.pg.model.table; +import io.github.mfvanek.pg.model.context.PgContext; import io.github.mfvanek.pg.model.validation.Validators; import java.util.Objects; @@ -118,6 +119,7 @@ public int compareTo(@Nonnull final TableWithMissingIndex other) { * @param indexScans number of index scans initiated on this table; should be non-negative. * @return {@code TableWithMissingIndex} */ + @Nonnull public static TableWithMissingIndex of(@Nonnull final String tableName, final long tableSizeInBytes, final long seqScans, @@ -126,6 +128,27 @@ public static TableWithMissingIndex of(@Nonnull final String tableName, return of(table, seqScans, indexScans); } + /** + * Constructs a {@code TableWithMissingIndex} object with given context. + * + * @param pgContext the schema context to enrich table name; must be non-null. + * @param tableName table name; should be non-blank. + * @param tableSizeInBytes table size in bytes; should be positive or zero. + * @param seqScans number of sequential scans initiated on this table; should be non-negative. + * @param indexScans number of index scans initiated on this table; should be non-negative. + * @return {@code TableWithMissingIndex} + * @since 0.14.3 + */ + @Nonnull + public static TableWithMissingIndex of(@Nonnull final PgContext pgContext, + @Nonnull final String tableName, + final long tableSizeInBytes, + final long seqScans, + final long indexScans) { + final Table table = Table.of(pgContext, tableName, tableSizeInBytes); + return of(table, seqScans, indexScans); + } + /** * Constructs a {@code TableWithMissingIndex} object. * @@ -135,6 +158,7 @@ public static TableWithMissingIndex of(@Nonnull final String tableName, * @return {@code TableWithMissingIndex} * @since 0.7.0 */ + @Nonnull public static TableWithMissingIndex of(@Nonnull final Table table, final long seqScans, final long indexScans) { diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableTest.java index 90e2a0ac..2b045fc3 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableTest.java @@ -61,8 +61,8 @@ void testToString() { .hasToString("Table{tableName='t', tableSizeInBytes=2}"); assertThat(Table.of(PgContext.ofPublic(), "t", 2L)) .hasToString("Table{tableName='t', tableSizeInBytes=2}"); - assertThat(Table.of(PgContext.of("test_schema"), "t")) - .hasToString("Table{tableName='test_schema.t', tableSizeInBytes=0}"); + assertThat(Table.of(PgContext.of("tst"), "t")) + .hasToString("Table{tableName='tst.t', tableSizeInBytes=0}"); } @SuppressWarnings("ConstantConditions") diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithBloatTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithBloatTest.java index cb1c3c9b..e610a3cf 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithBloatTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithBloatTest.java @@ -10,6 +10,7 @@ package io.github.mfvanek.pg.model.table; +import io.github.mfvanek.pg.model.context.PgContext; import io.github.mfvanek.pg.model.dbobject.PgObjectType; import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.Test; @@ -39,6 +40,8 @@ void gettersShouldWork() { void testToString() { assertThat(TableWithBloat.of("t", 2L, 1L, 50)) .hasToString("TableWithBloat{tableName='t', tableSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}"); + assertThat(TableWithBloat.of(PgContext.of("tst"), "t", 2L, 1L, 50)) + .hasToString("TableWithBloat{tableName='tst.t', tableSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}"); } @SuppressWarnings("ConstantConditions") @@ -59,6 +62,9 @@ void withInvalidArguments() { assertThatThrownBy(() -> TableWithBloat.of("t", -1L, 0L, 0)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("tableSizeInBytes cannot be less than zero"); + assertThatThrownBy(() -> TableWithBloat.of(null, "t", 0L, 0L, 0)) + .isInstanceOf(NullPointerException.class) + .hasMessage("pgContext cannot be null"); assertThat(TableWithBloat.of("t", 0L, 0L, 0)) .isNotNull(); diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithMissingIndexTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithMissingIndexTest.java index 659e0c65..feb96fcd 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithMissingIndexTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/table/TableWithMissingIndexTest.java @@ -10,6 +10,7 @@ package io.github.mfvanek.pg.model.table; +import io.github.mfvanek.pg.model.context.PgContext; import io.github.mfvanek.pg.model.dbobject.PgObjectType; import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.Test; @@ -59,12 +60,17 @@ void invalidArguments() { assertThatThrownBy(() -> TableWithMissingIndex.of(null, 0L, 0L)) .isInstanceOf(NullPointerException.class) .hasMessage("table cannot be null"); + assertThatThrownBy(() -> TableWithMissingIndex.of(null, "t", 0L, 0L, 0L)) + .isInstanceOf(NullPointerException.class) + .hasMessage("pgContext cannot be null"); } @Test void testToString() { assertThat(TableWithMissingIndex.of("t", 11L, 33L, 22L)) .hasToString("TableWithMissingIndex{tableName='t', tableSizeInBytes=11, seqScans=33, indexScans=22}"); + assertThat(TableWithMissingIndex.of(PgContext.of("tst"), "t", 11L, 33L, 22L)) + .hasToString("TableWithMissingIndex{tableName='tst.t', tableSizeInBytes=11, seqScans=33, indexScans=22}"); } @SuppressWarnings("ConstantConditions") diff --git a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithBloatCheckOnClusterTest.java b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithBloatCheckOnClusterTest.java index 965e9916..b3536b32 100644 --- a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithBloatCheckOnClusterTest.java +++ b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithBloatCheckOnClusterTest.java @@ -50,8 +50,8 @@ void onDatabaseWithThem(final String schemaName) { .executing(ctx) .hasSize(2) .containsExactlyInAnyOrder( - TableWithBloat.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0), - TableWithBloat.of(ctx.enrichWithSchema("clients"), 0L, 0L, 0)) + TableWithBloat.of(ctx, "accounts", 0L, 0L, 0), + TableWithBloat.of(ctx, "clients", 0L, 0L, 0)) .allMatch(t -> t.getTableSizeInBytes() > 0L) // real size doesn't matter .allMatch(t -> t.getBloatPercentage() == 0 && t.getBloatSizeInBytes() == 0L); diff --git a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithMissingIndexesCheckOnClusterTest.java b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithMissingIndexesCheckOnClusterTest.java index 6a3f551a..85af10d3 100644 --- a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithMissingIndexesCheckOnClusterTest.java +++ b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/TablesWithMissingIndexesCheckOnClusterTest.java @@ -47,7 +47,7 @@ void onDatabaseWithThem(final String schemaName) { .executing(ctx) .hasSize(1) .containsExactly( - TableWithMissingIndex.of(ctx.enrichWithSchema("accounts"), 0L, 0L, 0L)) + TableWithMissingIndex.of(ctx, "accounts", 0L, 0L, 0L)) .allMatch(t -> t.getSeqScans() >= AMOUNT_OF_TRIES) .allMatch(t -> t.getIndexScans() == 0) .allMatch(t -> t.getTableSizeInBytes() > 1L);