|
| 1 | +package cc.unitmesh.quality; |
| 2 | + |
| 3 | +import cc.unitmesh.quality.extension.JavaServiceAnalyser |
| 4 | +import chapi.ast.javaast.JavaAnalyser |
| 5 | +import chapi.domain.core.CodeDataStruct |
| 6 | +import kotlinx.serialization.json.Json |
| 7 | +import org.archguard.rule.core.RuleType |
| 8 | +import org.junit.jupiter.api.Assertions |
| 9 | +import org.junit.jupiter.api.Test |
| 10 | +import java.io.File |
| 11 | +import java.nio.file.Paths |
| 12 | +import kotlin.test.assertEquals |
| 13 | + |
| 14 | +class JavaServiceAnalyserTest { |
| 15 | + private fun loadNodes(source: String): List<CodeDataStruct> { |
| 16 | + return Json { ignoreUnknownKeys = true }.decodeFromString( |
| 17 | + File(this.javaClass.classLoader.getResource(source)!!.file).readText() |
| 18 | + ) |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun `should return empty list of issues when node is not a service`() { |
| 23 | + val nodes = loadNodes("java/structs_HelloController.json") |
| 24 | + val issues = JavaServiceAnalyser().analysis(nodes) |
| 25 | + |
| 26 | + assertEquals(0, issues.size) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `should identify too many repository dependencies`() { |
| 31 | + val path = getAbsolutePath("java/ServiceWithSixRepositories.java") |
| 32 | + val data = JavaAnalyser().analysis(File(path).readText(), "ServiceWithSixRepositories.java").DataStructures |
| 33 | + val issues = JavaServiceAnalyser().analysis(data) |
| 34 | + |
| 35 | + Assertions.assertEquals(1, issues.size) |
| 36 | + Assertions.assertEquals("TooManyRepositoryDependencies", issues[0].name) |
| 37 | + Assertions.assertEquals("Service should not dependent more than 5 repositories.", issues[0].detail) |
| 38 | + Assertions.assertEquals(RuleType.SERVICE_SMELL, issues[0].ruleType) |
| 39 | + } |
| 40 | + |
| 41 | + private fun getAbsolutePath(path: String): String { |
| 42 | + val resource = this.javaClass.classLoader.getResource(path) |
| 43 | + return Paths.get(resource!!.toURI()).toFile().absolutePath |
| 44 | + } |
| 45 | +} |
0 commit comments