Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/kotlin/io/reqlith/controller/ProjectController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ProjectController(
model.addAttribute("openSuggestions", reviews.openSuggestions(project))
model.addAttribute("externalLinks", links.all(project))
model.addAttribute("modified", elements.all(project).associate { it.key to it.updatedAt })
model.addAttribute("brokenLinks", document.brokenLinks())
model.addAttribute("brokenLinks", document.brokenLinks(support.baselineKeys()))
model.addAttribute("functionalWithoutScenario", document.functionalWithoutScenario())
return "project/overview"
}
Expand Down
28 changes: 21 additions & 7 deletions src/main/kotlin/io/reqlith/controller/ProjectViewSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ProjectViewSupport(
model.addAttribute("active", active)
val baselineItems = baseline.all()
model.addAttribute("baseline", baselineItems)
// A HashSet: SpEL cannot call methods on Kotlin's internal EmptySet, which toSet() returns for no items.
model.addAttribute("baselineKeys", baselineItems.map { it.key }.toHashSet())
model.addAttribute("sections", sections(project, document, baselineItems.size))
val state = state(project, document)
model.addAttribute("stateLabel", state.first)
Expand Down Expand Up @@ -77,17 +79,29 @@ class ProjectViewSupport(
/** Renders markdown with element and role keys linked into the project's read view. */
fun render(markdownText: String?, project: Project, document: ProjectDocument): String = markdown.render(markdownText, resolver(project, document))

/** Renders markdown of a frozen version: references point to anchors on the same page. */
fun renderInPage(markdownText: String?, document: ProjectDocument): String = markdown.render(markdownText) { key ->
document.element(key)?.let { Reference("#$key", "$key · ${it.title}") }
?: document.role(key)?.let { Reference("#$key", "$key · ${it.name}") }
/** Renders markdown of a frozen version: references point to anchors on the same page (the baseline is shown there too). */
fun renderInPage(markdownText: String?, document: ProjectDocument): String {
val baselineTitles = baselineTitles()
return markdown.render(markdownText) { key ->
document.element(key)?.let { Reference("#$key", "$key · ${it.title}") }
?: document.role(key)?.let { Reference("#$key", "$key · ${it.name}") }
?: baselineTitles[key]?.let { Reference("#$key", "$key · $it") }
}
}

fun resolver(project: Project, document: ProjectDocument) = ReferenceResolver { key ->
document.element(key)?.let { Reference("/p/${project.key}/e/$key", "$key · ${it.title}") }
?: document.role(key)?.let { Reference("/roles/$key", "$key · ${it.name}") }
fun resolver(project: Project, document: ProjectDocument): ReferenceResolver {
val baselineTitles = baselineTitles()
return ReferenceResolver { key ->
document.element(key)?.let { Reference("/p/${project.key}/e/$key", "$key · ${it.title}") }
?: document.role(key)?.let { Reference("/roles/$key", "$key · ${it.name}") }
?: baselineTitles[key]?.let { Reference("/baseline/$key", "$key · $it") }
}
}

fun baselineKeys(): Set<String> = baseline.keys()

private fun baselineTitles(): Map<String, String> = baseline.all().associate { it.key to it.title }

/** Baseline descriptions rendered for the document views (key to HTML). */
fun renderBaseline(): Map<String, String> = baseline.all().associate { it.key to markdown.render(it.bodyMd) }

Expand Down
11 changes: 7 additions & 4 deletions src/main/kotlin/io/reqlith/controller/ReqlithMcpTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ class ReqlithMcpTools(private val read: McpReadModel, private val log: McpAccess

@McpTool(
name = "get_traceability",
description = "Traceability graph of a project (BR → SR → FR/NFR → scenarios, risks, dependencies, roles) " +
"with gaps: functional requirements without a scenario and broken links.",
description = "Traceability graph of a project (BR → SR → FR/NFR → scenarios, risks, dependencies, roles, " +
"and baseline NFRs it links to) with gaps: functional requirements without a scenario and broken links.",
annotations = McpTool.McpAnnotations(readOnlyHint = true, destructiveHint = false, idempotentHint = true),
)
fun getTraceability(
Expand All @@ -228,13 +228,16 @@ class ReqlithMcpTools(private val read: McpReadModel, private val log: McpAccess
audit(context, "get_traceability", p.key, mapOf("version" to version))
val doc = read.document(p, version).let { it.document to it.label }
val (document, label) = doc
val targets = document.elements.flatMap { e -> e.links.map { it.toKey } }.toSet()
val baseline = read.baseline()
Traceability(
project = p.key,
version = label,
nodes = document.elements.map { TraceNode(it.key, it.type.label, it.title) } + document.roles.map { TraceNode(it.key, "Role", it.name) },
nodes = document.elements.map { TraceNode(it.key, it.type.label, it.title) } + document.roles.map { TraceNode(it.key, "Role", it.name) } +
baseline.filter { it.key in targets }.map { TraceNode(it.key, baselineType(it), it.title) },
edges = document.elements.flatMap { e -> e.links.map { TraceEdge(e.key, it.kind.label, it.toKey) } },
functionalWithoutScenario = document.functionalWithoutScenario().map { it.key },
brokenLinks = document.brokenLinks().map { TraceEdge(it.first, it.second.kind.label, it.second.toKey) },
brokenLinks = document.brokenLinks(baseline.map { it.key }.toSet()).map { TraceEdge(it.first, it.second.kind.label, it.second.toKey) },
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/io/reqlith/model/dto/ProjectDocument.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ data class ProjectDocument(
fun incomingLinks(): Map<String, List<Pair<DocLink, String>>> = elements.flatMap { from -> from.links.map { it.toKey to (it to from.key) } }
.groupBy({ it.first }, { it.second })

/** Links whose target key does not exist in this document. */
fun brokenLinks(): List<Pair<String, DocLink>> {
val known = elements.map { it.key }.toSet() + roles.map { it.key }
/** Links whose target key does not exist in this document, nor among [baselineKeys] (the global NFR baseline). */
fun brokenLinks(baselineKeys: Set<String> = emptySet()): List<Pair<String, DocLink>> {
val known = elements.map { it.key }.toSet() + roles.map { it.key } + baselineKeys
return elements.flatMap { e -> e.links.filter { it.toKey !in known }.map { e.key to it } }
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/io/reqlith/service/BaselineService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class BaselineService(private val baseline: BaselineRequirementRepository, priva

fun get(key: String): BaselineRequirement = baseline.findByKey(key) ?: throw NotFoundException("Baseline requirement $key not found")

fun keys(): Set<String> = baseline.findAll().map { it.key }.toSet()

fun canEdit(user: CurrentUser): Boolean = user.isArchitect

/** Changes whenever any baseline requirement is added, changed or removed (part of export cache keys). */
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/reqlith/service/ElementService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ElementService(
if (content.title.isBlank()) throw ValidationException("title", "Title is required")
if (content.title.length > 300) throw ValidationException("title", "Keep the title under 300 characters")
content.links.forEach {
if (!MarkdownRenderer.KEY.matches(it.toKey)) throw ValidationException("links", "${it.toKey} is not an element or role key")
if (!MarkdownRenderer.KEY.matches(it.toKey)) throw ValidationException("links", "${it.toKey} is not an element, role or baseline key")
if (it.toKey == key) throw ValidationException("links", "An element cannot link to itself")
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/templates/element/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ <h1 class="h-sm" th:text="${element.title}">Title</h1>
<a class="mono" th:if="${document.element(l.toKey) != null}" th:href="@{/p/{k}/e/{ek}(k=${project.key},ek=${l.toKey})}"
th:text="${l.toKey}" th:title="${document.element(l.toKey).title}">SR-03</a>
<a class="mono" th:if="${document.role(l.toKey) != null}" th:href="@{/roles/{rk}(rk=${l.toKey})}" th:text="${l.toKey}">RO-01</a>
<span class="mono" style="color: var(--danger)" th:if="${document.element(l.toKey) == null and document.role(l.toKey) == null}"
<a class="mono" th:if="${baselineKeys.contains(l.toKey)}" th:href="@{/baseline/{bk}(bk=${l.toKey})}"
th:text="${l.toKey}" title="Baseline requirement">BL-01</a>
<span class="mono" style="color: var(--danger)"
th:if="${document.element(l.toKey) == null and document.role(l.toKey) == null and !baselineKeys.contains(l.toKey)}"
th:text="|${l.toKey} (missing)|">XX-1</span>
</div>
<div th:each="in : ${incoming}">
Expand Down
43 changes: 43 additions & 0 deletions src/test/kotlin/io/reqlith/controller/BaselinePagesIT.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package io.reqlith.controller

import io.reqlith.model.dto.ElementContent
import io.reqlith.model.dto.ReqlithOidcUser
import io.reqlith.model.entity.BaselineCategory
import io.reqlith.model.entity.ElementType
import io.reqlith.model.entity.LinkKind
import io.reqlith.model.entity.TraceLink
import io.reqlith.service.BaselineService
import io.reqlith.service.ElementService
import io.reqlith.support.Fixtures
import io.reqlith.support.IntegrationTest
import io.reqlith.support.TestUsers
Expand All @@ -26,9 +31,47 @@ class BaselinePagesIT(
@Autowired private val users: TestUsers,
@Autowired private val fixtures: Fixtures,
@Autowired private val baseline: BaselineService,
@Autowired private val elements: ElementService,
) {
private fun login(user: ReqlithOidcUser) = oidcLogin().oidcUser(user)

@Test
fun `trace links and references to baseline requirements are not broken while the requirement exists`() {
val architect = users.create("Ada Archer", architect = true)
val owner = users.create("Olive Owens")
val project = fixtures.project(owner)
val requirement = baseline.create(architect, BaselineCategory.SECURITY, "Encrypt data at rest " + UUID.randomUUID().toString().take(6), null)
val nfr = elements.create(
owner,
project,
ElementType.NFR,
ElementContent(
"Encrypt refund photos",
"Applies `${requirement.key}` to uploaded photos.",
null,
emptySet(),
setOf(TraceLink(LinkKind.DERIVED_FROM, requirement.key)),
),
)

mvc.get("/p/${project.key}/draft") { with(login(owner)) }.andExpect {
content { string(not(containsString("${nfr.key} → ${requirement.key}"))) }
}
mvc.get("/p/${project.key}/e/${nfr.key}") { with(login(owner)) }.andExpect {
content { string(containsString("""title="Baseline requirement">${requirement.key}</a>""")) }
content { string(containsString("""class="ref" rel="noopener noreferrer"><code>${requirement.key}</code>""")) }
content { string(not(containsString("${requirement.key} (missing)"))) }
}

baseline.delete(architect, requirement)
mvc.get("/p/${project.key}/draft") { with(login(owner)) }.andExpect {
content { string(containsString("${nfr.key} → ${requirement.key}")) }
}
mvc.get("/p/${project.key}/e/${nfr.key}") { with(login(owner)) }.andExpect {
content { string(containsString("${requirement.key} (missing)")) }
}
}

@Test
fun `architects add and edit baseline requirements from the baseline pages`() {
val architect = users.create("Ada Archer", architect = true)
Expand Down
14 changes: 14 additions & 0 deletions src/test/kotlin/io/reqlith/controller/McpServerIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ class McpServerIT(
assertContains(hits, security.key)
assertFalse(mcp.call("search_requirements", "project" to key, "type" to "FR", "text" to marker).text().contains(security.key))

val editor = users.create("Carla Conti")
val project = projects.get(key)
fixtures.addMember(project, editor, MemberRole.CONTRIBUTOR)
val nfr = elements.create(
editor,
project,
ElementType.NFR,
ElementContent("Encrypt refund photos", null, null, emptySet(), setOf(TraceLink(LinkKind.DERIVED_FROM, security.key))),
)
val trace = mcp.call("get_traceability", "project" to key, "version" to "draft").text()
assertContains(trace, "{\"from\":\"${nfr.key}\",\"kind\":\"Derived from\",\"to\":\"${security.key}\"}")
assertContains(trace, "{\"key\":\"${security.key}\",")
assertContains(trace, "\"brokenLinks\":[]")

val detail = mcp.call("get_requirement", "project" to key, "key" to security.key.lowercase()).text()
assertContains(detail, "AES-256 for all stores")
assertContains(detail, "/baseline/${security.key}")
Expand Down
7 changes: 7 additions & 0 deletions src/test/kotlin/io/reqlith/model/dto/ProjectDocumentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class ProjectDocumentTest {
assertEquals(listOf("FR-01" to "SR-09"), doc.brokenLinks().map { it.first to it.second.toKey })
}

@Test
fun `links to baseline requirements are not broken while they exist`() {
val doc = document(element("NFR-01", LinkKind.DERIVED_FROM to "BL-03"))
assertEquals(emptyList(), doc.brokenLinks(setOf("BL-01", "BL-03")))
assertEquals(listOf("NFR-01" to "BL-03"), doc.brokenLinks(setOf("BL-01")).map { it.first to it.second.toKey })
}

@Test
fun `incoming links are grouped by target`() {
val doc = document(element("FR-01", LinkKind.DERIVED_FROM to "SR-01"), element("SR-01"))
Expand Down