diff --git a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/IndexesWithBloatCheckOnHostTest.java b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/IndexesWithBloatCheckOnHostTest.java index dbb714db..395420ac 100644 --- a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/IndexesWithBloatCheckOnHostTest.java +++ b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/IndexesWithBloatCheckOnHostTest.java @@ -55,10 +55,10 @@ void onDatabaseWithThem(final String schemaName) { .executing(ctx) .hasSize(4) .containsExactlyInAnyOrder( - IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_account_number_key"), 0L, 0L, 0), - IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_pkey"), 0L, 0L, 0), - IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("clients_pkey"), 0L, 0L, 0), - IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("i_clients_email_phone"), 0L, 0L, 0)) + IndexWithBloat.of(ctx, accountsTableName, "accounts_account_number_key", 0L, 0L, 0), + IndexWithBloat.of(ctx, accountsTableName, "accounts_pkey", 0L, 0L, 0), + IndexWithBloat.of(ctx, clientsTableName, "clients_pkey", 0L, 0L, 0), + IndexWithBloat.of(ctx, clientsTableName, "i_clients_email_phone", 0L, 0L, 0)) .allMatch(i -> i.getIndexSizeInBytes() > 1L) .allMatch(i -> i.getBloatSizeInBytes() > 1L && i.getBloatPercentage() >= 14); diff --git a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/InvalidIndexesCheckOnHostTest.java b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/InvalidIndexesCheckOnHostTest.java index 2bde20d8..0d874335 100644 --- a/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/InvalidIndexesCheckOnHostTest.java +++ b/pg-index-health-core/src/test/java/io/github/mfvanek/pg/core/checks/host/InvalidIndexesCheckOnHostTest.java @@ -43,8 +43,7 @@ void onDatabaseWithThem(final String schemaName) { assertThat(check) .executing(ctx) .hasSize(1) - .containsExactly( - Index.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name_first_name"))); + .containsExactly(Index.of(ctx, "clients", "i_clients_last_name_first_name")); assertThat(check) .executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "clients")) diff --git a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/Index.java b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/Index.java index 7bc6d032..a75877e1 100644 --- a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/Index.java +++ b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/Index.java @@ -10,6 +10,7 @@ package io.github.mfvanek.pg.model.index; +import io.github.mfvanek.pg.model.context.PgContext; import io.github.mfvanek.pg.model.dbobject.DbObject; import io.github.mfvanek.pg.model.dbobject.PgObjectType; import io.github.mfvanek.pg.model.table.TableNameAware; @@ -146,7 +147,25 @@ public int compareTo(@Nonnull final Index other) { * @param indexName index name; should be non-blank. * @return {@code Index} */ - public static Index of(@Nonnull final String tableName, @Nonnull final String indexName) { + @Nonnull + public static Index of(@Nonnull final String tableName, + @Nonnull final String indexName) { return new Index(tableName, indexName); } + + /** + * Constructs an {@code Index} object with given context. + * + * @param pgContext the schema context to enrich index name; must be non-null. + * @param tableName table name; should be non-blank. + * @param indexName index name; should be non-blank. + * @return {@code Index} + * @since 0.14.3 + */ + @Nonnull + public static Index of(@Nonnull final PgContext pgContext, + @Nonnull final String tableName, + @Nonnull final String indexName) { + return of(PgContext.enrichWith(tableName, pgContext), PgContext.enrichWith(indexName, pgContext)); + } } diff --git a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/IndexWithBloat.java b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/IndexWithBloat.java index a3760f6d..99519ab3 100644 --- a/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/IndexWithBloat.java +++ b/pg-index-health-model/src/main/java/io/github/mfvanek/pg/model/index/IndexWithBloat.java @@ -11,6 +11,7 @@ package io.github.mfvanek.pg.model.index; 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 javax.annotation.Nonnull; @@ -90,4 +91,27 @@ public static IndexWithBloat of(@Nonnull final String tableName, final double bloatPercentage) { return new IndexWithBloat(tableName, indexName, indexSizeInBytes, bloatSizeInBytes, bloatPercentage); } + + /** + * Constructs a {@code IndexWithBloat} object with given context. + * + * @param pgContext the schema context to enrich index name; must be non-null. + * @param tableName table name; should be non-blank. + * @param indexName index name; should be non-blank. + * @param indexSizeInBytes index 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 IndexWithBloat} + * @since 0.14.3 + */ + @Nonnull + public static IndexWithBloat of(@Nonnull final PgContext pgContext, + @Nonnull final String tableName, + @Nonnull final String indexName, + final long indexSizeInBytes, + final long bloatSizeInBytes, + final double bloatPercentage) { + return of(PgContext.enrichWith(tableName, pgContext), PgContext.enrichWith(indexName, pgContext), + indexSizeInBytes, bloatSizeInBytes, bloatPercentage); + } } diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexTest.java index 02c95979..4a64e2a2 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexTest.java @@ -10,6 +10,7 @@ package io.github.mfvanek.pg.model.index; +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; @@ -40,6 +41,9 @@ void validation() { assertThatThrownBy(() -> Index.of("t", " ")) .isInstanceOf(IllegalArgumentException.class) .hasMessage("indexName cannot be blank"); + assertThatThrownBy(() -> Index.of(null, null, null)) + .isInstanceOf(NullPointerException.class) + .hasMessage("pgContext cannot be null"); } @Test @@ -60,6 +64,8 @@ void getTableAndIndexName() { void testToString() { assertThat(Index.of("t", "i")) .hasToString("Index{tableName='t', indexName='i'}"); + assertThat(Index.of(PgContext.of("tst"), "t", "i")) + .hasToString("Index{tableName='tst.t', indexName='tst.i'}"); } @SuppressWarnings("ConstantConditions") diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexWithBloatTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexWithBloatTest.java index e8332fca..a5ca4de8 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexWithBloatTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/index/IndexWithBloatTest.java @@ -10,6 +10,7 @@ package io.github.mfvanek.pg.model.index; +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; @@ -41,6 +42,8 @@ void getBloatPercentage() { void testToString() { assertThat(IndexWithBloat.of("t", "i", 2L, 1L, 50)) .hasToString("IndexWithBloat{tableName='t', indexName='i', indexSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}"); + assertThat(IndexWithBloat.of(PgContext.of("tst"), "t", "i", 2L, 1L, 50)) + .hasToString("IndexWithBloat{tableName='tst.t', indexName='tst.i', indexSizeInBytes=2, bloatSizeInBytes=1, bloatPercentage=50.0}"); } @Test @@ -54,8 +57,8 @@ void withInvalidArguments() { assertThatThrownBy(() -> IndexWithBloat.of("t", "i", -1L, 0L, 0)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("indexSizeInBytes cannot be less than zero"); - final IndexWithBloat bloat = IndexWithBloat.of("t", "i", 0L, 0L, 0); - assertThat(bloat).isNotNull(); + assertThat(IndexWithBloat.of("t", "i", 0L, 0L, 0)) + .isNotNull(); } @SuppressWarnings("ConstantConditions") diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java index 0ab53a82..7402d135 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipFlywayTablesPredicateTest.java @@ -53,7 +53,7 @@ void shouldWorkWithCustomSchema(final String schemaName) { final PgContext ctx = PgContext.of(schemaName); assertThat(SkipFlywayTablesPredicate.of(ctx)) .accepts(Table.of(ctx, "t")) - .accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i"))) + .accepts(Index.of(ctx, "t", "i")) .accepts(SequenceState.of(ctx, "s", "int", 100.0)) .rejects(Table.of(ctx, "flyway_schema_history")) .rejects(Table.of(ctx, "FLYWAY_SCHEMA_HISTORY")); diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipIndexesByNamePredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipIndexesByNamePredicateTest.java index 199f3f74..b3fa31a7 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipIndexesByNamePredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipIndexesByNamePredicateTest.java @@ -131,7 +131,7 @@ void shouldWorkWithCustomSchema(final String schemaName) { final PgContext ctx = PgContext.of(schemaName); assertThat(SkipIndexesByNamePredicate.of(ctx, Set.of("i1", "i2"))) .accepts(Table.of(ctx, "t")) - .accepts(Index.of(ctx.enrichWithSchema("t1"), ctx.enrichWithSchema("i11"))) - .rejects(Index.of(ctx.enrichWithSchema("t2"), ctx.enrichWithSchema("i2"))); + .accepts(Index.of(ctx, "t1", "i11")) + .rejects(Index.of(ctx, "t2", "i2")); } } diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java index 8c4dacf8..909dc2dd 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipLiquibaseTablesPredicateTest.java @@ -53,7 +53,7 @@ void shouldWorkWithCustomSchema(final String schemaName) { final PgContext ctx = PgContext.of(schemaName); assertThat(SkipLiquibaseTablesPredicate.of(ctx)) .accepts(Table.of(ctx, "t")) - .accepts(Index.of(ctx.enrichWithSchema("t"), ctx.enrichWithSchema("i"))) + .accepts(Index.of(ctx, "t", "i")) .accepts(SequenceState.of(ctx, "s", "int", 100.0)) .rejects(Table.of(ctx, "databasechangelog")) .rejects(Table.of(ctx, "DATABASECHANGELOG")) diff --git a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java index bd13e928..7457133e 100644 --- a/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java +++ b/pg-index-health-model/src/test/java/io/github/mfvanek/pg/model/predicates/SkipTablesByNamePredicateTest.java @@ -95,10 +95,10 @@ void shouldWorkWithCustomSchema(final String schemaName) { final PgContext ctx = PgContext.of(schemaName); assertThat(SkipTablesByNamePredicate.of(ctx, Set.of("t2", "T1"))) .accepts(Table.of(ctx, "t")) - .accepts(Index.of(ctx.enrichWithSchema("T"), ctx.enrichWithSchema("I"))) + .accepts(Index.of(ctx, "T", "I")) .accepts(SequenceState.of(ctx, "s", "int", 100.0)) - .rejects(Index.of(ctx.enrichWithSchema("t1"), ctx.enrichWithSchema("i1"))) - .rejects(Index.of(ctx.enrichWithSchema("T2"), ctx.enrichWithSchema("i2"))) + .rejects(Index.of(ctx, "t1", "i1")) + .rejects(Index.of(ctx, "T2", "i2")) .accepts(Table.of(ctx, "t11")); } } diff --git a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/IndexesWithBloatCheckOnClusterTest.java b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/IndexesWithBloatCheckOnClusterTest.java index 1b3d9545..d8103dce 100644 --- a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/IndexesWithBloatCheckOnClusterTest.java +++ b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/IndexesWithBloatCheckOnClusterTest.java @@ -67,10 +67,10 @@ void onDatabaseWithThem(final String schemaName) { .executing(ctx) .hasSize(4) .containsExactlyInAnyOrder( - IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_account_number_key"), 0L, 0L, 0), - IndexWithBloat.of(accountsTableName, ctx.enrichWithSchema("accounts_pkey"), 0L, 0L, 0), - IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("clients_pkey"), 0L, 0L, 0), - IndexWithBloat.of(clientsTableName, ctx.enrichWithSchema("i_clients_email_phone"), 0L, 0L, 0)) + IndexWithBloat.of(ctx, accountsTableName, "accounts_account_number_key", 0L, 0L, 0), + IndexWithBloat.of(ctx, accountsTableName, "accounts_pkey", 0L, 0L, 0), + IndexWithBloat.of(ctx, clientsTableName, "clients_pkey", 0L, 0L, 0), + IndexWithBloat.of(ctx, clientsTableName, "i_clients_email_phone", 0L, 0L, 0)) .allMatch(i -> i.getIndexSizeInBytes() > 1L) .allMatch(i -> i.getBloatSizeInBytes() > 1L && i.getBloatPercentage() >= 14); diff --git a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/InvalidIndexesCheckOnClusterTest.java b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/InvalidIndexesCheckOnClusterTest.java index 94063240..e75cc640 100644 --- a/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/InvalidIndexesCheckOnClusterTest.java +++ b/pg-index-health/src/test/java/io/github/mfvanek/pg/health/checks/cluster/InvalidIndexesCheckOnClusterTest.java @@ -42,8 +42,7 @@ void onDatabaseWithThem(final String schemaName) { assertThat(check) .executing(ctx) .hasSize(1) - .containsExactly( - Index.of(ctx.enrichWithSchema("clients"), ctx.enrichWithSchema("i_clients_last_name_first_name"))); + .containsExactly(Index.of(ctx, "clients", "i_clients_last_name_first_name")); assertThat(check) .executing(ctx, SkipTablesByNamePredicate.ofName(ctx, "clients"))