-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from unit-mesh/javaServiceAnalyser
Java service analyser
- Loading branch information
Showing
5 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
27 changes: 24 additions & 3 deletions
27
code-quality/src/main/kotlin/cc/unitmesh/quality/extension/JavaServiceAnalyser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
code-quality/src/main/kotlin/cc/unitmesh/quality/extension/rule/ServiceRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cc.unitmesh.quality.extension.rule | ||
|
||
import chapi.domain.core.CodeDataStruct | ||
import org.archguard.rule.core.IssueEmit | ||
import org.archguard.rule.core.Rule | ||
|
||
open class ServiceRule : Rule() { | ||
open fun visitRoot(rootNode: List<CodeDataStruct>, callback: IssueEmit) {} | ||
} |
30 changes: 30 additions & 0 deletions
30
...y/src/main/kotlin/cc/unitmesh/quality/extension/rule/TooManyRepositoryDependenciesRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cc.unitmesh.quality.extension.rule | ||
|
||
import chapi.domain.core.CodeDataStruct | ||
import org.archguard.rule.core.IssueEmit | ||
import org.archguard.rule.core.IssuePosition | ||
import org.archguard.rule.core.Severity | ||
|
||
/** | ||
* Service should not dependent more than 5 repositories. | ||
*/ | ||
const val LIMIT = 5 | ||
|
||
class TooManyRepositoryDependenciesRule : ServiceRule() { | ||
init { | ||
this.id = "too-many-repository-dependencies" | ||
this.name = "TooManyRepositoryDependencies" | ||
this.key = this.javaClass.name | ||
this.severity = Severity.WARN | ||
this.description = "Service should not dependent more than 5 repositories." | ||
} | ||
|
||
override fun visitRoot(rootNodes: List<CodeDataStruct>, callback: IssueEmit) { | ||
rootNodes.forEach { | ||
val repositoryCount = it.Fields.filter { it.TypeType.contains("Repository", true) }.count() | ||
if (repositoryCount > LIMIT) { | ||
callback(this, IssuePosition()) | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
code-quality/src/test/kotlin/cc/unitmesh/quality/JavaServiceAnalyserTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cc.unitmesh.quality; | ||
|
||
import cc.unitmesh.quality.extension.JavaServiceAnalyser | ||
import chapi.ast.javaast.JavaAnalyser | ||
import chapi.domain.core.CodeDataStruct | ||
import kotlinx.serialization.json.Json | ||
import org.archguard.rule.core.RuleType | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
import java.nio.file.Paths | ||
import kotlin.test.assertEquals | ||
|
||
class JavaServiceAnalyserTest { | ||
private fun loadNodes(source: String): List<CodeDataStruct> { | ||
return Json { ignoreUnknownKeys = true }.decodeFromString( | ||
File(this.javaClass.classLoader.getResource(source)!!.file).readText() | ||
) | ||
} | ||
|
||
@Test | ||
fun `should return empty list of issues when node is not a service`() { | ||
val nodes = loadNodes("java/structs_HelloController.json") | ||
val issues = JavaServiceAnalyser().analysis(nodes) | ||
|
||
assertEquals(0, issues.size) | ||
} | ||
|
||
@Test | ||
fun `should identify too many repository dependencies`() { | ||
val path = getAbsolutePath("java/ServiceWithSixRepositories.java") | ||
val data = JavaAnalyser().analysis(File(path).readText(), "ServiceWithSixRepositories.java").DataStructures | ||
val issues = JavaServiceAnalyser().analysis(data) | ||
|
||
Assertions.assertEquals(1, issues.size) | ||
Assertions.assertEquals("TooManyRepositoryDependencies", issues[0].name) | ||
Assertions.assertEquals("Service should not dependent more than 5 repositories.", issues[0].detail) | ||
Assertions.assertEquals(RuleType.SERVICE_SMELL, issues[0].ruleType) | ||
} | ||
|
||
private fun getAbsolutePath(path: String): String { | ||
val resource = this.javaClass.classLoader.getResource(path) | ||
return Paths.get(resource!!.toURI()).toFile().absolutePath | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
code-quality/src/test/resources/java/ServiceWithSixRepositories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.afs.restapi.service; | ||
|
||
import com.afs.restapi.repository.CompanyRepository; | ||
import com.afs.restapi.repository.EmployeeRepository; | ||
import com.afs.restapi.repository.DepartmentRepository; | ||
import com.afs.restapi.repository.TeamRepository; | ||
import com.afs.restapi.repository.GroupRepository; | ||
import com.afs.restapi.repository.CommunitRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class Example { | ||
private CompanyRepository companyRepository; | ||
private EmployeeRepository employeeRepository; | ||
private DepartmentRepository departmentRepository; | ||
private TeamRepository teamRepository; | ||
private GroupRepository groupRepository; | ||
private CommunitRepository communitRepository; | ||
|
||
public Example(CompanyRepository companyRepository, EmployeeRepository employeeRepository, | ||
DepartmentRepository departmentRepository, TeamRepository teamRepository, | ||
GroupRepository groupRepository, CommunitRepository communitRepository) { | ||
this.companyRepository = companyRepository; | ||
this.employeeRepository = employeeRepository; | ||
this.departmentRepository = departmentRepository; | ||
this.teamRepository = teamRepository; | ||
this.groupRepository = groupRepository; | ||
this.communitRepository = communitRepository; | ||
} | ||
} |