diff --git a/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaFieldCheckLinterInspectionTest.kt b/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaFieldCheckLinterInspectionTest.kt index f3d40210..82a1d81b 100644 --- a/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaFieldCheckLinterInspectionTest.kt +++ b/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaFieldCheckLinterInspectionTest.kt @@ -27,6 +27,7 @@ class SpringCriteriaFieldCheckLinterInspectionTest { fileName = "Repository.java", value = """ import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.mapping.Document; import static org.springframework.data.mongodb.core.query.Query.query; @@ -50,6 +51,18 @@ class BookRepository { Book.class ); } + + public void allReleasedBooksAggregate() { + template.aggregate( + Aggregation.newAggregation( + Aggregation.match( + where("released").is(true) + ) + ), + Book.class, + Book.class + ); + } } """, ) @@ -65,6 +78,7 @@ class BookRepository { fileName = "Repository.java", value = """ import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.mapping.Document; import static org.springframework.data.mongodb.core.query.Query.query; @@ -86,6 +100,18 @@ class BookRepository { Book.class ); } + + public void allReleasedBooksAggregate() { + template.aggregate( + Aggregation.newAggregation( + Aggregation.match( + where("released").is(true) + ) + ), + Book.class, + Book.class + ); + } } """, ) @@ -141,6 +167,7 @@ class BookRepository { fileName = "Repository.java", value = """ import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.mapping.Document; import static org.springframework.data.mongodb.core.query.Query.query; @@ -162,6 +189,18 @@ class BookRepository { Book.class ); } + + public void allReleasedBooksAggregate() { + template.aggregate( + Aggregation.newAggregation( + Aggregation.match( + where("released").is(true) + ) + ), + Book.class, + Book.class + ); + } } """, ) @@ -222,6 +261,7 @@ class BookRepository { fileName = "Repository.java", value = """ import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.mapping.Document; import static org.springframework.data.mongodb.core.query.Query.query; @@ -243,6 +283,18 @@ class BookRepository { Book.class ); } + + public void allReleasedBooksAggregate() { + template.aggregate( + Aggregation.newAggregation( + Aggregation.match( + where("released").is("true") + ) + ), + Book.class, + Book.class + ); + } } """, ) @@ -306,6 +358,7 @@ class BookRepository { fileName = "Repository.java", value = """ import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.mapping.Document; import static org.springframework.data.mongodb.core.query.Query.query; @@ -327,6 +380,18 @@ class BookRepository { Book.class ); } + + public void allReleasedBooksAggregate() { + template.aggregate( + Aggregation.newAggregation( + Aggregation.match( + where("released").is("true") + ) + ), + Book.class, + Book.class + ); + } } """, ) diff --git a/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaNamespaceCheckLinterInspectionTest.kt b/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaNamespaceCheckLinterInspectionTest.kt index 0f8059da..46ef502b 100644 --- a/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaNamespaceCheckLinterInspectionTest.kt +++ b/packages/jetbrains-plugin/src/test/kotlin/com/mongodb/jbplugin/inspections/impl/SpringCriteriaNamespaceCheckLinterInspectionTest.kt @@ -9,12 +9,6 @@ import org.mockito.Mockito.`when` import org.mockito.kotlin.any import org.mockito.kotlin.eq -// Suppressing -// - LONG_LINE because the complaint is about the templated error description which needs to be in the same line for the -// match to happen correctly -// - TOO_LONG_FUNCTION because it is better to keep test logic within the tests and not make them "too smart" otherwise -// reading through them becomes a task in its own -@Suppress("LONG_LINE", "TOO_LONG_FUNCTION") @CodeInsightTest class SpringCriteriaNamespaceCheckLinterInspectionTest { @ParsingTest( @@ -65,4 +59,69 @@ class BookRepository { fixture.enableInspections(NamespaceCheckInspectionBridge::class.java) fixture.testHighlighting() } + + @ParsingTest( + fileName = "Repository.java", + value = """ +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.mapping.Document; + +import static org.springframework.data.mongodb.core.query.Query.query; +import static org.springframework.data.mongodb.core.query.Criteria.where; + +@Document +record Book() {} + +class BookRepository { + private final MongoTemplate template; + + public BookRepository(MongoTemplate template) { + this.template = template; + } + + public void allReleasedBooks() { + template.aggregate( + Aggregation.newAggregation( + Aggregation.match( + where("released").is(true) + ) + ), + Book.class, + Book.class + ); + } + + public void allReleasedBooksVariant2() { + template.aggregate( + Aggregation.newAggregation( + Aggregation.match( + where("released").is(true) + ) + ), + "book", + Book.class + ); + } +} + """, + ) + fun `shows an inspection when the collection does not exist in the current data source for an aggregation`( + fixture: CodeInsightTestFixture, + ) { + val (dataSource, readModelProvider) = fixture.setupConnection() + fixture.specifyDatabase("myDb") + fixture.specifyDialect(SpringCriteriaDialect) + + `when`(readModelProvider.slice(eq(dataSource), eq(ListDatabases.Slice))).thenReturn( + ListDatabases(listOf(ListDatabases.Database("myDb"))) + ) + + `when`(readModelProvider.slice(eq(dataSource), any())).thenReturn( + ListCollections(emptyList()) + ) + + fixture.enableInspections(NamespaceCheckInspectionBridge::class.java) + fixture.testHighlighting() + } }