+func TestGetSubscriptionByIdForNonExistingSubscription(t *testing.T) {
+ resp, ok := rh.GetSubscriptionById("Non-existent-ID")
+ assert.Equal(t, ok, false)
+ assert.Equal(t, resp, models.Subscription{})
+}
+
+func TestNotifyClientsNoXapp(t *testing.T) {
+ rh.NotifyClients(models.AllDeployedXapps{}, models.EventTypeUndeployed)
+}
+
+func TestNotifySuccess(t *testing.T) {
+ flushExistingSubscriptions()
+
+ sub := createSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")
+ resp := rh.AddSubscription(sub)
+
+ xapp := getDummyXapp()
+ ts := createHTTPServer(t, "POST", "/xapps_hook", 8087, http.StatusOK, nil)
+ defer ts.Close()
+
+ v, ok := rh.subscriptions.Get(resp.ID)
+ assert.True(t, ok)
+ err := rh.notify(models.AllDeployedXapps{&xapp}, models.EventTypeUndeployed, v.(SubscriptionInfo), 1)
+ assert.Nil(t, err)
+}
+
+func TestNotifySuccessIfHttpErrorResponse(t *testing.T) {
+ flushExistingSubscriptions()
+
+ sub := createSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")
+ resp := rh.AddSubscription(sub)
+
+ xapp := getDummyXapp()
+ ts := createHTTPServer(t, "POST", "/xapps_hook", 8087, http.StatusInternalServerError, nil)
+ defer ts.Close()
+
+ v, ok := rh.subscriptions.Get(resp.ID)
+ assert.True(t, ok)
+ err := rh.notify(models.AllDeployedXapps{&xapp}, models.EventTypeUndeployed, v.(SubscriptionInfo), 1)
+ assert.Nil(t, err)
+}
+
+func TestNotifyReturnsErrorAfterRetriesIfNoHttpServer(t *testing.T) {
+ flushExistingSubscriptions()
+
+ sub := createSubscription(models.EventTypeCreated, int64(2), int64(1), "http://localhost:8087/xapps_hook")
+ resp := rh.AddSubscription(sub)
+
+ xapp := getDummyXapp()
+
+ v, ok := rh.subscriptions.Get(resp.ID)
+ assert.True(t, ok)
+ err := rh.notify(models.AllDeployedXapps{&xapp}, models.EventTypeUndeployed, v.(SubscriptionInfo), 1)
+ assert.NotNil(t, err)
+ assert.Equal(t, 0, len(rh.subscriptions.Items()))
+}
+
+func TestRestoreSubscriptionsSuccess(t *testing.T) {
+ var mockSdlRetOk error
+ mSdl := new(SdlMock)
+ key := "key-1"
+
+ subsReq := createSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")
+ serializedSubsReq, err := json.Marshal(subsReq)
+ assert.Nil(t, err)
+
+ mockSdlGetRetVal := make(map[string]interface{})
+ //Cast data to string to act like a real SDL/Redis client
+ mockSdlGetRetVal[key] = string(serializedSubsReq)
+ mSdl.On("GetAll").Return([]string{key}, mockSdlRetOk).Twice()
+ mSdl.On("Get", []string{key}).Return(mockSdlGetRetVal, mockSdlRetOk).Once()
+ restHook := createResthook(true, mSdl)
+
+ val, found := restHook.subscriptions.Get(key)
+ assert.True(t, found)
+ assert.Equal(t, subsReq, val.(SubscriptionInfo).req)
+}
+
+func TestRestoreSubscriptionsFailsIfSdlGetAllFails(t *testing.T) {
+ var mockSdlRetStatus error
+ mSdl := new(SdlMock)
+ getCalled := 0
+ mGetAllCall := mSdl.On("GetAll")
+ mGetAllCall.RunFn = func(args mock.Arguments) {
+ if getCalled > 0 {
+ mockSdlRetStatus = errors.New("some SDL error")
+ }
+ getCalled++
+ mGetAllCall.ReturnArguments = mock.Arguments{[]string{}, mockSdlRetStatus}
+ }
+
+ restHook := createResthook(true, mSdl)
+ assert.Equal(t, 0, len(restHook.subscriptions.Items()))
+}
+
+func TestRestoreSubscriptionsFailsIfSdlGetFails(t *testing.T) {
+ var mockSdlRetOk error
+ mSdl := new(SdlMock)
+ mockSdlRetNok := errors.New("some SDL error")
+ key := "key-1"
+ subsReq := createSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")
+ serializedSubsReq, err := json.Marshal(subsReq)
+ assert.Nil(t, err)
+
+ mockSdlGetRetVal := make(map[string]interface{})
+ mockSdlGetRetVal[key] = serializedSubsReq
+
+ mSdl.On("GetAll").Return([]string{key}, mockSdlRetOk).Twice()
+ mSdl.On("Get", []string{key}).Return(mockSdlGetRetVal, mockSdlRetNok).Once()
+
+ restHook := createResthook(true, mSdl)
+ assert.Equal(t, 0, len(restHook.subscriptions.Items()))
+}
+