Skip to content

Commit

Permalink
feat: parepare for library as context
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 23, 2023
1 parent 7548b49 commit 30ffcec
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cc.unitmesh.pick.ext

import org.archguard.scanner.core.sca.CompositionDependency

fun CompositionDependency.Companion.from(
name: String,
group: String,
artifactId: String,
): CompositionDependency {
return CompositionDependency(
name = name,
depName = name,
depGroup = group,
depArtifact = artifactId,
packageManager = "",
depVersion = "",
version = "",
path = "",
depScope = "",
id = "",
parentId = "",
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cc.unitmesh.pick.project

import org.archguard.scanner.core.sca.CompositionDependency

object ProjectLibrary {
/**
*
* ```kotlin
*
*
*/
fun prepare(deps: List<CompositionDependency>): TestStack {
val testStack = TestStack()
var hasMatchSpringMvc = false
var hasMatchSpringData = false

deps.forEach { dep ->
SpringLibrary.SPRING_MVC.forEach {
it.coords.forEach { coord ->
if (dep.name.contains(coord)) {
testStack.coreFrameworks.putIfAbsent(it.shortText, true)
hasMatchSpringMvc = true
}
}
}

SpringLibrary.SPRING_DATA.forEach {
it.coords.forEach { coord ->
if (dep.name.contains(coord)) {
testStack.coreFrameworks.putIfAbsent(it.shortText, true)
hasMatchSpringData = true
}
}
}

when {
dep.name.contains("org.springframework.boot:spring-boot-test") -> {
testStack.testFrameworks.putIfAbsent("Spring Boot Test", true)
}

dep.name.contains("org.assertj:assertj-core") -> {
testStack.testFrameworks.putIfAbsent("AssertJ", true)
}

dep.name.contains("org.junit.jupiter:junit-jupiter") -> {
testStack.testFrameworks.putIfAbsent("JUnit 5", true)
}

dep.name.contains("org.mockito:mockito-core") -> {
testStack.testFrameworks.putIfAbsent("Mockito", true)
}

dep.name.contains("com.h2database:h2") -> {
testStack.testFrameworks.putIfAbsent("H2", true)
}
}
}

return testStack
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cc.unitmesh.pick.project

data class SpringDataLibraryDescriptor(val shortText: String, val coords: List<String>)
data class LibraryDescriptor(val shortText: String, val coords: String)

object SpringLibrary {
// Spring
private val SPRING_MVC_MAVEN = "org.springframework:spring-webmvc"
private val SPRING_WEBFLUX_MAVEN = "org.springframework:spring-webflux"

// Spring Data
private val REACTOR_MAVEN = "io.projectreactor:reactor-core"
private val MONGO_REACTIVE_STREAMS_MAVEN = "org.mongodb:mongodb-driver-reactivestreams"
private val SPRING_DATA_COMMONS_MAVEN = "org.springframework.data:spring-data-commons"
private val JPA_MAVEN = "org.springframework.data:spring-data-jpa"
private val CASSANDRA_MAVEN = "org.springframework.data:spring-data-cassandra"
private val COUCHBASE_MAVEN = "org.springframework.data:spring-data-couchbase"
private val JDBC_MAVEN = "org.springframework.data:spring-data-jdbc"
private val MONGO_MAVEN = "org.springframework.data:spring-data-mongodb"
private val NEO4J_MAVEN = "org.springframework.data:spring-data-neo4j"
private val R2DBC_MAVEN = "org.springframework.data:spring-data-r2dbc"
private val REDIS_MAVEN = "org.springframework.data:spring-data-redis"

val SPRING_DATA = listOf(
SpringDataLibraryDescriptor("JPA ", listOf(JPA_MAVEN)),
SpringDataLibraryDescriptor("CASSANDRA", listOf(CASSANDRA_MAVEN)),
SpringDataLibraryDescriptor("REACTIVE CASSANDRA", listOf(CASSANDRA_MAVEN, REACTOR_MAVEN)),
SpringDataLibraryDescriptor("COUCHBASE", listOf(COUCHBASE_MAVEN)),
SpringDataLibraryDescriptor("REACTIVE COUCHBASE", listOf(COUCHBASE_MAVEN, REACTOR_MAVEN)),
SpringDataLibraryDescriptor("JDBC", listOf(JDBC_MAVEN)),
SpringDataLibraryDescriptor("MONGO", listOf(MONGO_MAVEN)),
SpringDataLibraryDescriptor(
"REACTIVE MONGO",
listOf(MONGO_MAVEN, REACTOR_MAVEN, MONGO_REACTIVE_STREAMS_MAVEN)
),
SpringDataLibraryDescriptor("NEO4J", listOf(NEO4J_MAVEN)),
SpringDataLibraryDescriptor("R2DBC", listOf(R2DBC_MAVEN)),
SpringDataLibraryDescriptor("REDIS", listOf(REDIS_MAVEN))
)

val SPRING_MVC = listOf(
LibraryDescriptor("Spring MVC", SPRING_MVC_MAVEN),
LibraryDescriptor("Spring WebFlux", SPRING_WEBFLUX_MAVEN)
)
}
19 changes: 19 additions & 0 deletions unit-picker/src/main/kotlin/cc/unitmesh/pick/project/TestStack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cc.unitmesh.pick.project

/**
* TODO: change to dependency tree
*/
data class TestStack(
val coreFrameworks: MutableMap<String, Boolean> = mutableMapOf(),
val testFrameworks: MutableMap<String, Boolean> = mutableMapOf(),
val deps: MutableMap<String, String> = mutableMapOf(),
val devDeps: MutableMap<String, String> = mutableMapOf()
) {
fun coreFrameworks(): String {
return coreFrameworks.keys.joinToString(", ")
}

fun testFrameworks(): String {
return testFrameworks.keys.joinToString(", ")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cc.unitmesh.pick.worker;

import cc.unitmesh.pick.ext.from
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.shouldBe
import org.archguard.scanner.core.sca.CompositionDependency
Expand All @@ -12,9 +13,9 @@ class TestFrameworkIdentifierTest {
// given
val language = "java"
val dependencies = listOf(
CompositionDependency.default("junit:junit", "org.junit.jupiter", "junit-jupiter"),
CompositionDependency.default("org.mockito:mockito-core", "org.mockito", "mockito-core"),
CompositionDependency.default("com.intuit.karate:karate-junit5", "com.intuit.karate", "karate-junit5")
CompositionDependency.from("junit:junit", "org.junit.jupiter", "junit-jupiter"),
CompositionDependency.from("org.mockito:mockito-core", "org.mockito", "mockito-core"),
CompositionDependency.from("com.intuit.karate:karate-junit5", "com.intuit.karate", "karate-junit5")
)
val testFrameworkIdentifier = TestFrameworkIdentifier(language, dependencies)

Expand All @@ -29,9 +30,9 @@ class TestFrameworkIdentifierTest {
// given
val language = "typescript"
val dependencies = listOf(
CompositionDependency.default("jest", "some.group", "jest"),
CompositionDependency.default("mocha", "another.group", "mocha"),
CompositionDependency.default("jasmine", "third.group", "jasmine")
CompositionDependency.from("jest", "some.group", "jest"),
CompositionDependency.from("mocha", "another.group", "mocha"),
CompositionDependency.from("jasmine", "third.group", "jasmine")
)
val testFrameworkIdentifier = TestFrameworkIdentifier(language, dependencies)

Expand All @@ -48,8 +49,8 @@ class TestFrameworkIdentifierTest {
// given
val language = "unknown"
val dependencies = listOf(
CompositionDependency.default("dummy", "some.group", "dummy"),
CompositionDependency.default("test", "another.group", "test")
CompositionDependency.from("dummy", "some.group", "dummy"),
CompositionDependency.from("test", "another.group", "test")
)
val testFrameworkIdentifier = TestFrameworkIdentifier(language, dependencies)

Expand All @@ -64,7 +65,7 @@ class TestFrameworkIdentifierTest {
// given
val language = "java"
val dependencies = listOf(
CompositionDependency.default("org.springframework.boot:spring-boot-starter-test", "org.springframework.boot", "spring-boot-starter-test"),
CompositionDependency.from("org.springframework.boot:spring-boot-starter-test", "org.springframework.boot", "spring-boot-starter-test"),
)
val testFrameworkIdentifier = TestFrameworkIdentifier(language, dependencies)

Expand All @@ -77,22 +78,3 @@ class TestFrameworkIdentifierTest {
}
}

private fun CompositionDependency.Companion.default(
name: String,
group: String,
artifactId: String,
): CompositionDependency {
return CompositionDependency(
name = name,
depName = name,
depGroup = group,
depArtifact = artifactId,
packageManager = "maven",
depVersion = "1.0.0",
version = "1.0.0",
path = "some/path",
depScope = "compile",
id = "some-id",
parentId = "some-parent-id",
)
}

0 comments on commit 30ffcec

Please sign in to comment.