X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Feventservice%2Feventservice_test.go;h=a328e5799010825e0252ff18b509d3b3b38d3e32;hb=487e239542f74384f6557a5001411da56503e7bb;hp=cd03ba7e6d27349b6da580cb0356530341fb77cb;hpb=fccbf5b13fcede6ec48796a04f41ab9f06e119b3;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/eventservice/eventservice_test.go b/capifcore/internal/eventservice/eventservice_test.go index cd03ba7..a328e57 100644 --- a/capifcore/internal/eventservice/eventservice_test.go +++ b/capifcore/internal/eventservice/eventservice_test.go @@ -39,6 +39,7 @@ import ( "github.com/stretchr/testify/assert" "oransc.org/nonrtric/capifcore/internal/common29122" "oransc.org/nonrtric/capifcore/internal/eventsapi" + "oransc.org/nonrtric/capifcore/internal/publishserviceapi" "oransc.org/nonrtric/capifcore/internal/restclient" ) @@ -122,13 +123,12 @@ func TestSendEvent(t *testing.T) { }) serviceUnderTest, _ := getEcho(clientMock) - subscription := eventsapi.EventSubscription{ + serviceUnderTest.addSubscription(subId, eventsapi.EventSubscription{ Events: []eventsapi.CAPIFEvent{ eventsapi.CAPIFEventSERVICEAPIAVAILABLE, }, NotificationDestination: common29122.Uri(notificationUrl), - } - serviceUnderTest.addSubscription(subId, subscription) + }) sub2 := eventsapi.EventSubscription{ Events: []eventsapi.CAPIFEvent{ eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE, @@ -152,29 +152,120 @@ func TestMatchEventType(t *testing.T) { notificationUrl := "url" subId := "sub1" serviceUnderTest := NewEventService(nil) - subscription := eventsapi.EventSubscription{ + serviceUnderTest.addSubscription(subId, eventsapi.EventSubscription{ Events: []eventsapi.CAPIFEvent{ eventsapi.CAPIFEventSERVICEAPIAVAILABLE, }, NotificationDestination: common29122.Uri(notificationUrl), - } - serviceUnderTest.addSubscription(subId, subscription) - sub2 := eventsapi.EventSubscription{ + EventFilters: &[]eventsapi.CAPIFEventFilter{}, + }) + serviceUnderTest.addSubscription("other", eventsapi.EventSubscription{ Events: []eventsapi.CAPIFEvent{ eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE, }, NotificationDestination: common29122.Uri(notificationUrl), - } - serviceUnderTest.addSubscription("other", sub2) + }) event := eventsapi.EventNotification{ SubscriptionId: subId, Events: eventsapi.CAPIFEventSERVICEAPIAVAILABLE, } + matchingSubs := serviceUnderTest.filterOnEventType(event) + assert.Len(t, matchingSubs, 1) + assert.Equal(t, subId, matchingSubs[0]) +} + +func TestMatchEventTypeAndFilters(t *testing.T) { + subId := "sub1" + apiIds := []string{"apiId"} + invokerIds := []string{"invokerId"} + aefId := "aefId" + aefIds := []string{aefId} + serviceUnderTest := NewEventService(nil) + serviceUnderTest.addSubscription(subId, eventsapi.EventSubscription{ + Events: []eventsapi.CAPIFEvent{ + eventsapi.CAPIFEventSERVICEAPIAVAILABLE, + }, + EventFilters: &[]eventsapi.CAPIFEventFilter{ + { + ApiIds: &apiIds, + ApiInvokerIds: &invokerIds, + AefIds: &aefIds, + }, + }, + }) + serviceUnderTest.addSubscription("otherSameType", eventsapi.EventSubscription{ + Events: []eventsapi.CAPIFEvent{ + eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE, + }, + }) + serviceUnderTest.addSubscription("other", eventsapi.EventSubscription{ + Events: []eventsapi.CAPIFEvent{ + eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE, + }, + }) + + event := eventsapi.EventNotification{ + Events: eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE, + } + + // Only match type matchingSubs := serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 2) + + // Match with all filter ids + aefProfiles := []publishserviceapi.AefProfile{ + { + AefId: aefId, + }, + } + serviceDescriptions := []publishserviceapi.ServiceAPIDescription{ + { + AefProfiles: &aefProfiles, + }, + } + event.Events = eventsapi.CAPIFEventSERVICEAPIAVAILABLE + event.EventDetail = &eventsapi.CAPIFEventDetail{ + ApiIds: &apiIds, + ApiInvokerIds: &invokerIds, + ServiceAPIDescriptions: &serviceDescriptions, + } + matchingSubs = serviceUnderTest.getMatchingSubs(event) assert.Len(t, matchingSubs, 1) assert.Equal(t, subId, matchingSubs[0]) + + // Un match apiId + otherApiIds := []string{"otherApiId"} + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = &otherApiIds + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 0) + + // Un match invokerId + otherInvokerIds := []string{"otherInvokerId"} + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = nil + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiInvokerIds = &otherInvokerIds + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 0) + + // Un match aefId + otherAefIds := []string{"otherAefId"} + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiInvokerIds = nil + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].AefIds = &otherAefIds + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 0) + + // Match with empty subscription filter id list + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].AefIds = &[]string{} + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 1) + + // Match with empty event id list + event.EventDetail.ApiIds = nil + event.EventDetail.ApiInvokerIds = nil + event.EventDetail.ServiceAPIDescriptions = &[]publishserviceapi.ServiceAPIDescription{} + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 1) } func getEcho(client restclient.HTTPClient) (*EventService, *echo.Echo) {