package services import ( "context" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" api "edge-infra.dev/pkg/edge/device-registrar/api/v1alpha1" ) func injectmockClientDelete(router *gin.Engine, mockClient client.Client, t *testing.T) { router.Use( // Inject the mock client into the context func(c *gin.Context) { c.Set("k8sClient", mockClient) c.Next() k8sClient, exists := c.Get("k8sClient") // Assert that k8sClient is not nil and exists assert.True(t, exists, "k8sClient should exist in the context") assert.NotNil(t, k8sClient, "k8sClient should not be nil") }, ) } func TestDeleteDeviceEndpoint(t *testing.T) { router, scheme := setupTest() mockClient := GenerateMockClient(scheme, "") injectmockClientDelete(router, mockClient, t) // Inject the mock client into the Gin context router.Use(func(c *gin.Context) { c.Set("k8sClient", mockClient) c.Next() }) // Define the endpoint router.DELETE("/devices/:name", func(c *gin.Context) { DeleteDeviceService(c) c.Status(http.StatusNoContent) }) // Create a test context ctx := context.TODO() // Create a test device device := &api.ExternalDevice{ ObjectMeta: metav1.ObjectMeta{ Name: "test-device-name", Namespace: "device-registrar-clients", }, Spec: api.ExternalDeviceSpec{ Name: "test-device-name", }, } err := mockClient.Create(ctx, device) assert.NoError(t, err) // Verify the device exists retrievedDevice := &api.ExternalDevice{} err = mockClient.Get(ctx, types.NamespacedName{Name: "test-device-name", Namespace: "device-registrar-clients"}, retrievedDevice) assert.NoError(t, err) // Create a new HTTP request req, err := http.NewRequest(http.MethodDelete, "/devices/test-device-name", nil) assert.NoError(t, err) // Create a response recorder w := httptest.NewRecorder() // Serve the request router.ServeHTTP(w, req) // Assert the response assert.Equal(t, http.StatusNoContent, w.Code) // Verify the device has been deleted err = mockClient.Get(ctx, types.NamespacedName{Name: "test-device-name", Namespace: "device-registrar-clients"}, retrievedDevice) assert.Error(t, err) assert.True(t, client.IgnoreNotFound(err) == nil) } func TestDeleteDeviceNotFound(t *testing.T) { router, scheme := setupTest() mockClient := GenerateMockClient(scheme, "") injectmockClientDelete(router, mockClient, t) // Inject the mock client into the Gin context router.Use(func(c *gin.Context) { c.Set("k8sClient", mockClient) c.Next() }) // Define the endpoint router.DELETE("/devices/:name", func(c *gin.Context) { DeleteDeviceService(c) c.Status(http.StatusNoContent) }) // Create a new HTTP request req, err := http.NewRequest(http.MethodDelete, "/devices/test-device-name", nil) assert.NoError(t, err) // Create a response recorder w := httptest.NewRecorder() // Serve the request router.ServeHTTP(w, req) // Assert the response assert.Equal(t, http.StatusNotFound, w.Code) } func TestDeleteDeviceNoName(t *testing.T) { router, scheme := setupTest() mockClient := fake.NewClientBuilder().WithScheme(scheme).Build() injectmockClientDelete(router, mockClient, t) // Define the endpoint router.DELETE("/devices/", func(c *gin.Context) { DeleteDeviceService(c) c.Status(http.StatusBadRequest) }) // Create a new HTTP request without the name parameter req, err := http.NewRequest(http.MethodDelete, "/devices/", nil) assert.NoError(t, err) // Create a response recorder w := httptest.NewRecorder() // Serve the request router.ServeHTTP(w, req) // Assert the response assert.Equal(t, http.StatusBadRequest, w.Code) assert.JSONEq(t, `{"error":"name is required"}`, w.Body.String()) }