dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/notification_test.go (about)

     1  //
     2  // Copyright (C) 2021 IOTech Ltd
     3  // Copyright (C) 2023 Intel Corporation
     4  //
     5  // SPDX-License-Identifier: Apache-2.0
     6  
     7  package http
     8  
     9  import (
    10  	"context"
    11  	"net/http"
    12  	"path"
    13  	"strconv"
    14  	"testing"
    15  
    16  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    17  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    18  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    19  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/requests"
    20  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses"
    21  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    22  
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func addNotificationRequest() requests.AddNotificationRequest {
    27  	return requests.NewAddNotificationRequest(
    28  		dtos.Notification{
    29  			Id:       ExampleUUID,
    30  			Content:  "testContent",
    31  			Sender:   "testSender",
    32  			Labels:   []string{TestLabel},
    33  			Severity: models.Critical,
    34  		},
    35  	)
    36  }
    37  
    38  func TestNotificationClient_SendNotification(t *testing.T) {
    39  	ts := newTestServer(http.MethodPost, common.ApiNotificationRoute, []dtoCommon.BaseWithIdResponse{})
    40  	defer ts.Close()
    41  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
    42  	res, err := client.SendNotification(context.Background(), []requests.AddNotificationRequest{addNotificationRequest()})
    43  	require.NoError(t, err)
    44  	require.IsType(t, []dtoCommon.BaseWithIdResponse{}, res)
    45  }
    46  
    47  func TestNotificationClient_NotificationById(t *testing.T) {
    48  	testId := ExampleUUID
    49  	path := path.Join(common.ApiNotificationRoute, common.Id, testId)
    50  	ts := newTestServer(http.MethodGet, path, responses.NotificationResponse{})
    51  	defer ts.Close()
    52  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
    53  	res, err := client.NotificationById(context.Background(), testId)
    54  	require.NoError(t, err)
    55  	require.IsType(t, responses.NotificationResponse{}, res)
    56  }
    57  
    58  func TestNotificationClient_NotificationsByCategory(t *testing.T) {
    59  	category := TestCategory
    60  	urlPath := path.Join(common.ApiNotificationRoute, common.Category, category)
    61  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiNotificationsResponse{})
    62  	defer ts.Close()
    63  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
    64  	res, err := client.NotificationsByCategory(context.Background(), category, 0, 10)
    65  	require.NoError(t, err)
    66  	require.IsType(t, responses.MultiNotificationsResponse{}, res)
    67  }
    68  
    69  func TestNotificationClient_NotificationsByLabel(t *testing.T) {
    70  	label := TestLabel
    71  	urlPath := path.Join(common.ApiNotificationRoute, common.Label, label)
    72  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiNotificationsResponse{})
    73  	defer ts.Close()
    74  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
    75  	res, err := client.NotificationsByLabel(context.Background(), label, 0, 10)
    76  	require.NoError(t, err)
    77  	require.IsType(t, responses.MultiNotificationsResponse{}, res)
    78  }
    79  
    80  func TestNotificationClient_NotificationsByStatus(t *testing.T) {
    81  	status := models.Processed
    82  	urlPath := path.Join(common.ApiNotificationRoute, common.Status, status)
    83  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiNotificationsResponse{})
    84  	defer ts.Close()
    85  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
    86  	res, err := client.NotificationsByStatus(context.Background(), status, 0, 10)
    87  	require.NoError(t, err)
    88  	require.IsType(t, responses.MultiNotificationsResponse{}, res)
    89  }
    90  
    91  func TestNotificationClient_NotificationsBySubscriptionName(t *testing.T) {
    92  	subscriptionName := TestSubscriptionName
    93  	urlPath := path.Join(common.ApiNotificationRoute, common.Subscription, common.Name, subscriptionName)
    94  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiNotificationsResponse{})
    95  	defer ts.Close()
    96  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
    97  	res, err := client.NotificationsBySubscriptionName(context.Background(), subscriptionName, 0, 10)
    98  	require.NoError(t, err)
    99  	require.IsType(t, responses.MultiNotificationsResponse{}, res)
   100  }
   101  
   102  func TestNotificationClient_NotificationsByTimeRange(t *testing.T) {
   103  	start := 1
   104  	end := 10
   105  	urlPath := path.Join(common.ApiNotificationRoute, common.Start, strconv.Itoa(start), common.End, strconv.Itoa(end))
   106  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiNotificationsResponse{})
   107  	defer ts.Close()
   108  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
   109  	res, err := client.NotificationsByTimeRange(context.Background(), start, end, 0, 10)
   110  	require.NoError(t, err)
   111  	require.IsType(t, responses.MultiNotificationsResponse{}, res)
   112  }
   113  
   114  func TestNotificationClient_CleanupNotifications(t *testing.T) {
   115  	ts := newTestServer(http.MethodDelete, common.ApiNotificationCleanupRoute, dtoCommon.BaseResponse{})
   116  	defer ts.Close()
   117  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
   118  	res, err := client.CleanupNotifications(context.Background())
   119  	require.NoError(t, err)
   120  	require.IsType(t, dtoCommon.BaseResponse{}, res)
   121  }
   122  
   123  func TestNotificationClient_CleanupNotificationsByAge(t *testing.T) {
   124  	age := 0
   125  	path := path.Join(common.ApiNotificationCleanupRoute, common.Age, strconv.Itoa(age))
   126  	ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{})
   127  	defer ts.Close()
   128  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
   129  	res, err := client.CleanupNotificationsByAge(context.Background(), age)
   130  	require.NoError(t, err)
   131  	require.IsType(t, dtoCommon.BaseResponse{}, res)
   132  }
   133  
   134  func TestNotificationClient_DeleteNotificationById(t *testing.T) {
   135  	id := ExampleUUID
   136  	path := path.Join(common.ApiNotificationRoute, common.Id, id)
   137  	ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{})
   138  	defer ts.Close()
   139  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
   140  	res, err := client.DeleteNotificationById(context.Background(), id)
   141  	require.NoError(t, err)
   142  	require.IsType(t, dtoCommon.BaseResponse{}, res)
   143  }
   144  
   145  func TestNotificationClient_DeleteProcessedNotificationsByAge(t *testing.T) {
   146  	age := 0
   147  	path := path.Join(common.ApiNotificationRoute, common.Age, strconv.Itoa(age))
   148  	ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{})
   149  	defer ts.Close()
   150  	client := NewNotificationClient(ts.URL, NewNullAuthenticationInjector(), false)
   151  	res, err := client.DeleteProcessedNotificationsByAge(context.Background(), age)
   152  	require.NoError(t, err)
   153  	require.IsType(t, dtoCommon.BaseResponse{}, res)
   154  }