Skip to content

Commit

Permalink
Add unit tests for processor process() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijsiez committed Nov 5, 2023
1 parent 6bfad01 commit 6e7d351
Show file tree
Hide file tree
Showing 7 changed files with 513 additions and 10 deletions.
11 changes: 6 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ allprojects {
configure(subprojects) {
dependencies {
val quarkusVersion: String by project
val mockitoKotlinVersion: String by project
val mockkVersion: String by project

implementation(kotlin("stdlib"))
implementation(platform("io.quarkus.platform:quarkus-bom:$quarkusVersion"))
implementation("io.quarkus:quarkus-hibernate-orm-panache-kotlin")
testImplementation(kotlin("test"))
testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.quarkus:quarkus-junit5-mockito")
testImplementation("org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion")
testImplementation("io.mockk:mockk:$mockkVersion")
}

tasks.test {
Expand All @@ -63,6 +60,10 @@ configure(subprojects) {
}
}

tasks.testCodeCoverageReport {
dependsOn(project(":library").tasks.jacocoTestReport)
dependsOn(project(":examples").tasks.jacocoTestReport)
}
tasks.test {
finalizedBy(tasks.testCodeCoverageReport)
}
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ kotlinVersion=1.9.10
kspVersion=1.9.10-1.0.13
#https://github.com/square/kotlinpoet
kotlinPoetVersion=1.14.2
#https://github.com/mockito/mockito-kotlin
mockitoKotlinVersion=5.1.0
#https://github.com/mockk/mockk
mockkVersion=1.13.8
#https://github.com/tschuchortdev/kotlin-compile-testing
compileTestingVersion=1.5.0
#https://github.com/SonarSource/sonar-scanner-gradle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class PanacheCompanionBaseProcessor(
return invalid
}

private fun createQueryBuilderExtensions(originalPackageName: String, ksClasses: List<KSClassDeclaration>,
internal fun createQueryBuilderExtensions(originalPackageName: String, ksClasses: List<KSClassDeclaration>,
addGeneratedAnnotation: Boolean) {
val packageName = originalPackageName + GENERATED_PACKAGE_SUFFIX
logger.info("Generating $packageName.$EXTENSIONS_FILE")
Expand Down Expand Up @@ -177,7 +177,7 @@ class PanacheCompanionBaseProcessor(
.addAnnotation(jvmNameAnnotation("$OR_GROUP$objectName"))
//endregion

//region
//region count, delete, find, stream
val count = FunSpec.builder(COUNT)
.addModifiers(KModifier.INLINE)
.receiver(whereReceiver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class PanacheEntityBaseProcessor(
return invalid
}

private fun createColumnNamesObject(ksClass: KSClassDeclaration, ksProperties: List<KSPropertyDeclaration>,
internal fun createColumnNamesObject(ksClass: KSClassDeclaration, ksProperties: List<KSPropertyDeclaration>,
addGeneratedAnnotation: Boolean) {
val packageName = ksClass.packageName.asString() + GENERATED_PACKAGE_SUFFIX
val objectName = ksClass.simpleName.asString() + COLUMN_NAME_OBJECT_SUFFIX
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*
* Copyright 2023 Thijs Koppen
*
* 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 ch.icken.processor

import ch.icken.processor.GenerationOptions.ADD_GENERATED_ANNOTATION
import ch.icken.processor.QualifiedNames.HibernatePanacheCompanionBase
import ch.icken.processor.QualifiedNames.HibernatePanacheEntityBase
import ch.icken.processor.QualifiedNames.JakartaPersistenceEntity
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSName
import com.google.devtools.ksp.validate
import io.mockk.*
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class PanacheCompanionBaseTests : TestCommon() {

@MockK
private lateinit var resolver: Resolver

private lateinit var processor: PanacheCompanionBaseProcessor

@BeforeEach
fun beforeEach() {
processor = spyk(PanacheCompanionBaseProcessor(
options = mapOf(ADD_GENERATED_ANNOTATION to "false"),
codeGenerator = mockk<CodeGenerator>(),
logger = mockk<KSPLogger>()
))
}

//region process
@Test
fun testProcessValid() {

// Given
val companionObject = mockk<KSClassDeclaration>()
every { companionObject.isCompanionObject } returns true
every { companionObject.isSubclass(eq(HibernatePanacheCompanionBase)) } returns true

val qualifiedPackageName = "ch.icken.model"
val packageName = mockk<KSName>()
every { packageName.asString() } returns qualifiedPackageName

val validClass = mockk<KSClassDeclaration>()
every { validClass.validate(any()) } returns true
every { validClass.isSubclass(eq(HibernatePanacheEntityBase)) } returns true
every { validClass.declarations } returns sequenceOf(companionObject)
every { validClass.packageName } returns packageName

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validClass)

every { processor.createQueryBuilderExtensions(any(), any(), any()) } just Runs

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 1) {
processor.createQueryBuilderExtensions(
originalPackageName = eq(qualifiedPackageName),
ksClasses = withArg {
assertEquals(1, it.size)
assertEquals(validClass, it[0])
},
addGeneratedAnnotation = eq(false)
)
}
assertEquals(0, invalid.size)
}

@Test
fun testProcessValidWithCompanionNotPanacheCompanion() {

// Given
val companionObject = mockk<KSClassDeclaration>()
every { companionObject.isCompanionObject } returns true
every { companionObject.isSubclass(eq(HibernatePanacheCompanionBase)) } returns false

val validClass = mockk<KSClassDeclaration>()
every { validClass.validate(any()) } returns true
every { validClass.isSubclass(eq(HibernatePanacheEntityBase)) } returns true
every { validClass.declarations } returns sequenceOf(companionObject)

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validClass)

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
assertEquals(0, invalid.size)
}

@Test
fun testProcessValidWithNotCompanion() {

// Given
val companionObject = mockk<KSClassDeclaration>()
every { companionObject.isCompanionObject } returns false

val validClass = mockk<KSClassDeclaration>()
every { validClass.validate(any()) } returns true
every { validClass.isSubclass(eq(HibernatePanacheEntityBase)) } returns true
every { validClass.declarations } returns sequenceOf(companionObject)

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validClass)

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
assertEquals(0, invalid.size)
}

@Test
fun testProcessValidWithNotClass() {

// Given
val function = mockk<KSFunctionDeclaration>()

val validClass = mockk<KSClassDeclaration>()
every { validClass.validate(any()) } returns true
every { validClass.isSubclass(eq(HibernatePanacheEntityBase)) } returns true
every { validClass.declarations } returns sequenceOf(function)

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validClass)

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
assertEquals(0, invalid.size)
}

@Test
fun testProcessValidWithoutCompanion() {

// Given
val validClass = mockk<KSClassDeclaration>()
every { validClass.validate(any()) } returns true
every { validClass.isSubclass(eq(HibernatePanacheEntityBase)) } returns true
every { validClass.declarations } returns emptySequence()

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validClass)

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
assertEquals(0, invalid.size)
}

@Test
fun testProcessValidNotPanacheEntity() {

// Given
val validClass = mockk<KSClassDeclaration>()
every { validClass.validate(any()) } returns true
every { validClass.isSubclass(eq(HibernatePanacheEntityBase)) } returns false

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validClass)

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
assertEquals(0, invalid.size)
}

@Test
fun testProcessNotClass() {

// Given
val validFunction = mockk<KSFunctionDeclaration>()
every { validFunction.validate(any()) } returns true

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validFunction)

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
assertEquals(0, invalid.size)
}

@Test
fun testProcessInvalid() {

// Given
val validClass = mockk<KSClassDeclaration>()
every { validClass.validate(any()) } returns false

every { resolver.getSymbolsWithAnnotation(eq(JakartaPersistenceEntity)) } returns sequenceOf(validClass)

// When
val invalid = processor.process(resolver)

// Then
verify(exactly = 0) { processor.createQueryBuilderExtensions(any(), any(), any()) }
assertEquals(1, invalid.size)
}
//endregion
}
Loading

0 comments on commit 6e7d351

Please sign in to comment.