|
| 1 | +package com.metaobjects.generator.kotlin |
| 2 | + |
| 3 | +import com.metaobjects.metadata.ktx.loadString |
| 4 | +import java.nio.file.Files |
| 5 | +import kotlin.test.Test |
| 6 | +import kotlin.test.assertTrue |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests for [KotlinSpringControllerGenerator]. Covers the cross-port API contract |
| 10 | + * (URL grammar + verbs + withCount envelope + sort allowlist + view-kind skip). |
| 11 | + * Hand-rolled string-search assertions match the style of |
| 12 | + * [KotlinExposedTableGeneratorTest] — snapshot stability is gated separately by |
| 13 | + * [KotlinCodegenSnapshotTest] over the {@code entity-with-controller} fixture. |
| 14 | + */ |
| 15 | +class KotlinSpringControllerGeneratorTest { |
| 16 | + |
| 17 | + private val authorFixture = """{ |
| 18 | + "metadata.root": { "package": "acme::blog", "children": [ |
| 19 | + { "object.entity": { "name": "Author", "children": [ |
| 20 | + { "field.long": { "name": "id" } }, |
| 21 | + { "field.string": { "name": "name", "@maxLength": 100, "@required": true } }, |
| 22 | + { "field.string": { "name": "bio" } }, |
| 23 | + { "field.timestamp": { "name": "createdAt" } }, |
| 24 | + { "source.rdb": { "@table": "authors" } }, |
| 25 | + { "identity.primary": { "name": "pk", "@fields": ["id"], "@generation": "increment" } } |
| 26 | + ] } } |
| 27 | + ] } |
| 28 | + }""".trimIndent() |
| 29 | + |
| 30 | + @Test fun emitsRestControllerWithFiveEndpoints() { |
| 31 | + val outDir = Files.createTempDirectory("kctrl-five-") |
| 32 | + try { |
| 33 | + val gen = KotlinSpringControllerGenerator() |
| 34 | + gen.setArgs(mapOf("outputDir" to outDir.toString())) |
| 35 | + gen.execute(loadString("ctrl-five", authorFixture)) |
| 36 | + |
| 37 | + val controller = outDir.resolve("acme/blog/AuthorController.kt") |
| 38 | + assertTrue(Files.exists(controller), |
| 39 | + "expected $controller; files=${Files.walk(outDir).toList()}") |
| 40 | + val src = Files.readString(controller) |
| 41 | + |
| 42 | + // @GetMapping (list) — the bare-annotation form without a path. |
| 43 | + assertTrue(Regex("""@GetMapping\s*\n\s*fun list""").containsMatchIn(src), |
| 44 | + "expected bare @GetMapping list handler; saw:\n$src") |
| 45 | + // @GetMapping("/{id}") — get by id. |
| 46 | + assertTrue("@GetMapping(\"/{id}\")" in src, |
| 47 | + "expected @GetMapping(\"/{id}\"); saw:\n$src") |
| 48 | + // @PostMapping — create. |
| 49 | + assertTrue(Regex("""@PostMapping\s*\n\s*fun create""").containsMatchIn(src), |
| 50 | + "expected @PostMapping create handler; saw:\n$src") |
| 51 | + // @PatchMapping AND @PutMapping share the update handler (per API contract). |
| 52 | + assertTrue("@PatchMapping(\"/{id}\")" in src, |
| 53 | + "expected @PatchMapping(\"/{id}\"); saw:\n$src") |
| 54 | + assertTrue("@PutMapping(\"/{id}\")" in src, |
| 55 | + "expected @PutMapping(\"/{id}\") on the same update handler; saw:\n$src") |
| 56 | + // @DeleteMapping("/{id}") — delete. |
| 57 | + assertTrue("@DeleteMapping(\"/{id}\")" in src, |
| 58 | + "expected @DeleteMapping(\"/{id}\"); saw:\n$src") |
| 59 | + } finally { |
| 60 | + outDir.toFile().deleteRecursively() |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test fun pathHonorsApiPrefixAndEntityPlural() { |
| 65 | + val outDir = Files.createTempDirectory("kctrl-path-") |
| 66 | + try { |
| 67 | + val gen = KotlinSpringControllerGenerator() |
| 68 | + gen.setArgs(mapOf("outputDir" to outDir.toString())) |
| 69 | + gen.execute(loadString("ctrl-path", authorFixture)) |
| 70 | + |
| 71 | + val src = Files.readString(outDir.resolve("acme/blog/AuthorController.kt")) |
| 72 | + // /api/<entity-plural-lowercase> per the cross-port contract; Author → authors. |
| 73 | + assertTrue("@RequestMapping(\"/api/authors\")" in src, |
| 74 | + "expected @RequestMapping(\"/api/authors\"); saw:\n$src") |
| 75 | + } finally { |
| 76 | + outDir.toFile().deleteRecursively() |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test fun withCountReturnsEnvelope() { |
| 81 | + val outDir = Files.createTempDirectory("kctrl-wc-") |
| 82 | + try { |
| 83 | + val gen = KotlinSpringControllerGenerator() |
| 84 | + gen.setArgs(mapOf("outputDir" to outDir.toString())) |
| 85 | + gen.execute(loadString("ctrl-wc", authorFixture)) |
| 86 | + |
| 87 | + val src = Files.readString(outDir.resolve("acme/blog/AuthorController.kt")) |
| 88 | + // Both the envelope path AND the bare-rows path must be emitted. |
| 89 | + assertTrue("mapOf(\"rows\" to rows, \"total\" to total)" in src, |
| 90 | + "expected {rows,total} envelope on withCount=1; saw:\n$src") |
| 91 | + // Bare rows path: `ResponseEntity.ok(rows as Any)` (no envelope). |
| 92 | + assertTrue("ResponseEntity.ok(rows as Any)" in src, |
| 93 | + "expected bare-rows OK response on default list; saw:\n$src") |
| 94 | + // The withCount parameter itself is declared on the list handler. |
| 95 | + assertTrue("name = \"withCount\"" in src, |
| 96 | + "expected withCount @RequestParam; saw:\n$src") |
| 97 | + } finally { |
| 98 | + outDir.toFile().deleteRecursively() |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test fun sortAllowlistEmittedPerEntity() { |
| 103 | + val outDir = Files.createTempDirectory("kctrl-sort-") |
| 104 | + try { |
| 105 | + val gen = KotlinSpringControllerGenerator() |
| 106 | + gen.setArgs(mapOf("outputDir" to outDir.toString())) |
| 107 | + gen.execute(loadString("ctrl-sort", authorFixture)) |
| 108 | + |
| 109 | + val src = Files.readString(outDir.resolve("acme/blog/AuthorController.kt")) |
| 110 | + // Per-entity prefix on the allowlist name keeps it from colliding with other |
| 111 | + // generated controllers in the same package. |
| 112 | + assertTrue("private val AuthorSortAllowlist = setOf(" in src, |
| 113 | + "expected per-entity AuthorSortAllowlist; saw:\n$src") |
| 114 | + // Every scalar field in Author appears in the allowlist. |
| 115 | + assertTrue("\"id\"," in src && "\"name\"," in src && "\"bio\"," in src && "\"createdAt\"," in src, |
| 116 | + "expected all four scalar fields in the sort allowlist; saw:\n$src") |
| 117 | + // The 400 envelope for invalid sort is `{ "error": "invalid_sort" }`. |
| 118 | + assertTrue("\"invalid_sort\"" in src, |
| 119 | + "expected invalid_sort 400 envelope; saw:\n$src") |
| 120 | + } finally { |
| 121 | + outDir.toFile().deleteRecursively() |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test fun viewKindSkipped() { |
| 126 | + // SalesReport has @kind="view" — must NOT produce a controller (read-only). |
| 127 | + val viewFixture = """{ |
| 128 | + "metadata.root": { "package": "acme::report", "children": [ |
| 129 | + { "object.entity": { "name": "SalesReport", "children": [ |
| 130 | + { "field.long": { "name": "id" } }, |
| 131 | + { "field.string": { "name": "regionName", "@maxLength": 100 } }, |
| 132 | + { "field.long": { "name": "totalCents" } }, |
| 133 | + { "source.rdb": { "@table": "v_sales_report", "@kind": "view" } }, |
| 134 | + { "identity.primary": { "@fields": "id" } } |
| 135 | + ] } } |
| 136 | + ] } |
| 137 | + }""".trimIndent() |
| 138 | + val outDir = Files.createTempDirectory("kctrl-view-") |
| 139 | + try { |
| 140 | + val gen = KotlinSpringControllerGenerator() |
| 141 | + gen.setArgs(mapOf("outputDir" to outDir.toString())) |
| 142 | + gen.execute(loadString("ctrl-view", viewFixture)) |
| 143 | + |
| 144 | + val controller = outDir.resolve("acme/report/SalesReportController.kt") |
| 145 | + assertTrue(!Files.exists(controller), |
| 146 | + "view-kind entities must NOT produce a controller; saw $controller present") |
| 147 | + } finally { |
| 148 | + outDir.toFile().deleteRecursively() |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments