Run unit tests
[ric-plt/appmgr.git] / pkg / resthooks / resthooks_test.go
index 018dee8..176fd2d 100755 (executable)
@@ -28,6 +28,7 @@ import (
        "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models"
 )
 
+var rh *Resthook
 var resp models.SubscriptionResponse
 
 // Test cases
@@ -35,88 +36,89 @@ func TestMain(m *testing.M) {
        appmgr.Init()
        appmgr.Logger.SetLevel(0)
 
+       rh = NewResthook(false)
        code := m.Run()
        os.Exit(code)
 }
 
 func TestAddSubscriptionSuccess(t *testing.T) {
-       resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
+       resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventTypeCreated)
 }
 
 func TestAddSubscriptionExists(t *testing.T) {
-       resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
+       resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventType(""))
 }
 
 func TestDeletesubscriptionSuccess(t *testing.T) {
-       resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
+       resp := rh.AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventTypeDeleted)
 
-       resp, ok := NewResthook().DeleteSubscription(resp.ID)
+       resp, ok := rh.DeleteSubscription(resp.ID)
        assert.Equal(t, ok, true)
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventTypeDeleted)
 }
 
 func TestDeletesubscriptionInvalid(t *testing.T) {
-       resp, ok := NewResthook().DeleteSubscription("Non-existent-ID")
+       resp, ok := rh.DeleteSubscription("Non-existent-ID")
        assert.Equal(t, ok, false)
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventType(""))
 }
 
 func TestModifySubscriptionSuccess(t *testing.T) {
-       resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
+       resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventTypeCreated)
 
-       resp, ok := NewResthook().ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
+       resp, ok := rh.ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
        assert.Equal(t, ok, true)
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventTypeModified)
 }
 
 func TestModifysubscriptionInvalid(t *testing.T) {
-       resp, ok := NewResthook().DeleteSubscription("Non-existent-ID")
+       resp, ok := rh.DeleteSubscription("Non-existent-ID")
        assert.Equal(t, ok, false)
        assert.Equal(t, resp.Version, int64(0))
        assert.Equal(t, resp.EventType, models.EventType(""))
 }
 
 func TestGetAllSubscriptionSuccess(t *testing.T) {
-       NewResthook().FlushSubscriptions()
-       subscriptions := NewResthook().GetAllSubscriptions()
+       rh.FlushSubscriptions()
+       subscriptions := rh.GetAllSubscriptions()
        assert.Equal(t, len(subscriptions), 0)
 
-       NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
-       NewResthook().AddSubscription(CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
+       rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
+       rh.AddSubscription(CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
 
-       subscriptions = NewResthook().GetAllSubscriptions()
+       subscriptions = rh.GetAllSubscriptions()
        assert.Equal(t, len(subscriptions), 2)
 }
 
 func TestGetSubscriptionByIdSuccess(t *testing.T) {
-       NewResthook().FlushSubscriptions()
+       rh.FlushSubscriptions()
        sub1 := CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")
        sub2 := CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")
-       r1 := NewResthook().AddSubscription(sub1)
-       r2 := NewResthook().AddSubscription(sub2)
+       r1 := rh.AddSubscription(sub1)
+       r2 := rh.AddSubscription(sub2)
 
-       resp1, ok := NewResthook().GetSubscriptionById(r1.ID)
+       resp1, ok := rh.GetSubscriptionById(r1.ID)
        assert.Equal(t, ok, true)
        assert.Equal(t, resp1.Data, sub1.Data)
 
-       resp2, ok := NewResthook().GetSubscriptionById(r2.ID)
+       resp2, ok := rh.GetSubscriptionById(r2.ID)
        assert.Equal(t, ok, true)
        assert.Equal(t, resp2.Data, sub2.Data)
 }
 
 func TestTeardown(t *testing.T) {
-       NewResthook().FlushSubscriptions()
+       rh.FlushSubscriptions()
 }
 
 func CreateSubscription(et models.EventType, maxRetries, retryTimer int64, targetUrl string) models.SubscriptionRequest {