-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspring_rest_test.go
More file actions
91 lines (82 loc) · 2.51 KB
/
spring_rest_test.go
File metadata and controls
91 lines (82 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package java
import (
"sort"
"testing"
"github.com/randomcodespace/codeiq/internal/detector"
)
const springRestSource = `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User get(@PathVariable Long id) { return null; }
@PostMapping
public User create(@RequestBody User u) { return u; }
@ModelAttribute
public void populate() { } // should NOT be detected as endpoint
}
`
func TestSpringRestPositive(t *testing.T) {
d := NewSpringRestDetector()
ctx := &detector.Context{
FilePath: "src/UserController.java",
Language: "java",
Content: springRestSource,
}
r := d.Detect(ctx)
if r == nil {
t.Fatal("Detect returned nil")
}
if len(r.Nodes) != 2 {
t.Fatalf("expected 2 ENDPOINT nodes, got %d: %+v", len(r.Nodes), r.Nodes)
}
// Sort by label for stable assertion.
sort.Slice(r.Nodes, func(i, j int) bool { return r.Nodes[i].Label < r.Nodes[j].Label })
if r.Nodes[0].Properties["http_method"] != "POST" {
t.Errorf("expected POST, got %v", r.Nodes[0].Properties["http_method"])
}
if r.Nodes[1].Properties["http_method"] != "GET" {
t.Errorf("expected GET, got %v", r.Nodes[1].Properties["http_method"])
}
for _, n := range r.Nodes {
if n.Properties["framework"] != "spring_boot" {
t.Errorf("framework property missing or wrong: %v", n.Properties)
}
if n.Source != "SpringRestDetector" {
t.Errorf("source = %q, want SpringRestDetector", n.Source)
}
}
}
func TestSpringRestNegative(t *testing.T) {
d := NewSpringRestDetector()
ctx := &detector.Context{
FilePath: "src/Plain.java",
Language: "java",
Content: "public class Plain { public void noop() { } }",
}
r := d.Detect(ctx)
if len(r.Nodes) != 0 {
t.Fatalf("expected 0 nodes on plain class, got %d", len(r.Nodes))
}
}
func TestSpringRestDeterminism(t *testing.T) {
d := NewSpringRestDetector()
ctx := &detector.Context{
FilePath: "src/UserController.java",
Language: "java",
Content: springRestSource,
}
r1 := d.Detect(ctx)
r2 := d.Detect(ctx)
if len(r1.Nodes) != len(r2.Nodes) {
t.Fatal("non-deterministic node count")
}
sort.Slice(r1.Nodes, func(i, j int) bool { return r1.Nodes[i].ID < r1.Nodes[j].ID })
sort.Slice(r2.Nodes, func(i, j int) bool { return r2.Nodes[i].ID < r2.Nodes[j].ID })
for i := range r1.Nodes {
if r1.Nodes[i].ID != r2.Nodes[i].ID || r1.Nodes[i].Label != r2.Nodes[i].Label {
t.Fatalf("non-deterministic: run1=%+v run2=%+v", r1.Nodes[i], r2.Nodes[i])
}
}
}