(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestNodeService(t *testing.T) { |
| 18 | logger := logging.NewLogger("node_test") |
| 19 | nodeService := NewNodeService(logger) |
| 20 | |
| 21 | t.Run("GetNode", func(t *testing.T) { |
| 22 | node := NewNode("node-1", "Node 1", "192.168.1.1", 8080) |
| 23 | nodeService.CreateNode(context.Background(), node) |
| 24 | |
| 25 | req, err := http.NewRequest("GET", "/api/v1/nodes/node-1", nil) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | w := httptest.NewRecorder() |
| 30 | nodeService.ServeHTTP(w, req) |
| 31 | |
| 32 | if w.Code != http.StatusOK { |
| 33 | t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code) |
| 34 | } |
| 35 | |
| 36 | var gotNode Node |
| 37 | err = json.NewDecoder(w.Body).Decode(&gotNode) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | |
| 42 | if gotNode.ID != node.ID { |
| 43 | t.Errorf("Expected node ID %s, got %s", node.ID, gotNode.ID) |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | t.Run("CreateNode", func(t *testing.T) { |
| 48 | node := NewNode("node-2", "Node 2", "192.168.1.2", 8081) |
| 49 | |
| 50 | req, err := http.NewRequest("POST", "/api/v1/nodes", strings.NewReader(`{"name": "Node 2", "ip_address": "192.168.1.2", "port": 8081}`)) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | w := httptest.NewRecorder() |
| 55 | nodeService.ServeHTTP(w, req) |
| 56 | |
| 57 | if w.Code != http.StatusCreated { |
| 58 | t.Errorf("Expected status code %d, got %d", http.StatusCreated, w.Code) |
| 59 | } |
| 60 | |
| 61 | gotNode, err := nodeService.GetNode(context.Background(), node.ID) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | |
| 66 | if gotNode.ID != node.ID { |
| 67 | t.Errorf("Expected node ID %s, got %s", node.ID, gotNode.ID) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | t.Run("UpdateNode", func(t *testing.T) { |
| 72 | node := NewNode("node-3", "Node 3", "192.168.1.3", 8082) |
| 73 | nodeService.CreateNode(context.Background(), node) |
| 74 |
nothing calls this directly
no test coverage detected