X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Feventservice%2Feventservice_test.go;h=d0b646b299d601858665bc3813d683e3c3aaa3d3;hb=b4cee94bd251b0bb92fc435fbe74ea1079d43356;hp=f2acb9c7c06bebb9bfcce87f339f9b0bfd5b4adb;hpb=19a22c671e873b438cc7ad005bab386674af7bd2;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/eventservice/eventservice_test.go b/capifcore/internal/eventservice/eventservice_test.go index f2acb9c..d0b646b 100644 --- a/capifcore/internal/eventservice/eventservice_test.go +++ b/capifcore/internal/eventservice/eventservice_test.go @@ -24,7 +24,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "path" @@ -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" ) @@ -47,7 +48,7 @@ func TestRegisterSubscriptions(t *testing.T) { Events: []eventsapi.CAPIFEvent{ eventsapi.CAPIFEventSERVICEAPIAVAILABLE, }, - NotificationDestination: common29122.Uri("notificationUrl"), + NotificationDestination: common29122.Uri("http://golang.cafe/"), } serviceUnderTest, requestHandler := getEcho(nil) subscriberId := "subscriberId" @@ -76,6 +77,27 @@ func TestRegisterSubscriptions(t *testing.T) { assert.Equal(t, subscription2, *registeredSub2) } +func TestRegisterInvalidSubscription(t *testing.T) { + subscription1 := eventsapi.EventSubscription{ + Events: []eventsapi.CAPIFEvent{eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE}, + } + serviceUnderTest, requestHandler := getEcho(nil) + subscriberId := "subscriberId" + + result := testutil.NewRequest().Post("/"+subscriberId+"/subscriptions").WithJsonBody(subscription1).Go(t, requestHandler) + assert.Equal(t, http.StatusBadRequest, result.Code()) + var problemDetails common29122.ProblemDetails + err := result.UnmarshalBodyToObject(&problemDetails) + assert.NoError(t, err, "error unmarshaling response") + badRequest := http.StatusBadRequest + assert.Equal(t, &badRequest, problemDetails.Status) + assert.Contains(t, *problemDetails.Cause, "missing") + assert.Contains(t, *problemDetails.Cause, "notificationDestination") + subscriptionId := path.Base(result.Recorder.Header().Get(echo.HeaderLocation)) + registeredSub := serviceUnderTest.getSubscription(subscriptionId) + assert.Nil(t, registeredSub) +} + func TestDeregisterSubscription(t *testing.T) { subscription := eventsapi.EventSubscription{ Events: []eventsapi.CAPIFEvent{ @@ -112,7 +134,7 @@ func TestSendEvent(t *testing.T) { wg.Done() return &http.Response{ StatusCode: 200, - Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)), + Body: io.NopCloser(bytes.NewBufferString(`OK`)), Header: make(http.Header), // Must be set to non-nil value or it panics } } @@ -142,7 +164,7 @@ func TestSendEvent(t *testing.T) { }() if waitTimeout(&wg, 1*time.Second) { - t.Error("Not all calls to server were made") + t.Error("No event notification was sent") t.Fail() } } @@ -170,33 +192,101 @@ func TestMatchEventType(t *testing.T) { Events: eventsapi.CAPIFEventSERVICEAPIAVAILABLE, } - matchingSubs := serviceUnderTest.getMatchingSubs(event) + matchingSubs := serviceUnderTest.filterOnEventType(event) assert.Len(t, matchingSubs, 1) assert.Equal(t, subId, matchingSubs[0]) } -func TestMatchesApiIds(t *testing.T) { - apiId := "apiId" - apiIds := []string{apiId, "otherApiId"} - eventFilters := []eventsapi.CAPIFEventFilter{ - {}, +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{ { - ApiIds: &apiIds, + 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]) - eventApiIds := []string{apiId} - assert.True(t, matchesApiIds(eventApiIds, eventFilters)) - assert.True(t, matchesApiIds(nil, eventFilters)) + // Un match apiId + otherApiIds := []string{"otherApiId"} + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = &otherApiIds + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 0) - altApiIds := []string{"anotherApiId"} - unMatchingFilterAdded := append(eventFilters, eventsapi.CAPIFEventFilter{ - ApiIds: &altApiIds, - }) - assert.False(t, matchesApiIds(eventApiIds, unMatchingFilterAdded)) + // 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) - apiIds[0] = "anotherId" - assert.False(t, matchesApiIds(eventApiIds, eventFilters)) + // 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) {