2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
23 "github.com/stretchr/testify/assert"
27 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/appmgr"
28 "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models"
31 var resp models.SubscriptionResponse
34 func TestMain(m *testing.M) {
36 appmgr.Logger.SetLevel(0)
42 func TestAddSubscriptionSuccess(t *testing.T) {
43 resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
44 assert.Equal(t, resp.Version, int64(0))
45 assert.Equal(t, resp.EventType, models.EventTypeCreated)
48 func TestAddSubscriptionExists(t *testing.T) {
49 resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
50 assert.Equal(t, resp.Version, int64(0))
51 assert.Equal(t, resp.EventType, models.EventType(""))
54 func TestDeletesubscriptionSuccess(t *testing.T) {
55 resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
56 assert.Equal(t, resp.Version, int64(0))
57 assert.Equal(t, resp.EventType, models.EventTypeDeleted)
59 resp, ok := NewResthook().DeleteSubscription(resp.ID)
60 assert.Equal(t, ok, true)
61 assert.Equal(t, resp.Version, int64(0))
62 assert.Equal(t, resp.EventType, models.EventTypeDeleted)
65 func TestDeletesubscriptionInvalid(t *testing.T) {
66 resp, ok := NewResthook().DeleteSubscription("Non-existent-ID")
67 assert.Equal(t, ok, false)
68 assert.Equal(t, resp.Version, int64(0))
69 assert.Equal(t, resp.EventType, models.EventType(""))
72 func TestModifySubscriptionSuccess(t *testing.T) {
73 resp := NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
74 assert.Equal(t, resp.Version, int64(0))
75 assert.Equal(t, resp.EventType, models.EventTypeCreated)
77 resp, ok := NewResthook().ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
78 assert.Equal(t, ok, true)
79 assert.Equal(t, resp.Version, int64(0))
80 assert.Equal(t, resp.EventType, models.EventTypeModified)
83 func TestModifysubscriptionInvalid(t *testing.T) {
84 resp, ok := NewResthook().DeleteSubscription("Non-existent-ID")
85 assert.Equal(t, ok, false)
86 assert.Equal(t, resp.Version, int64(0))
87 assert.Equal(t, resp.EventType, models.EventType(""))
90 func TestGetAllSubscriptionSuccess(t *testing.T) {
91 NewResthook().FlushSubscriptions()
92 subscriptions := NewResthook().GetAllSubscriptions()
93 assert.Equal(t, len(subscriptions), 0)
95 NewResthook().AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
96 NewResthook().AddSubscription(CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
98 subscriptions = NewResthook().GetAllSubscriptions()
99 assert.Equal(t, len(subscriptions), 2)
102 func TestGetSubscriptionByIdSuccess(t *testing.T) {
103 NewResthook().FlushSubscriptions()
104 sub1 := CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")
105 sub2 := CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")
106 r1 := NewResthook().AddSubscription(sub1)
107 r2 := NewResthook().AddSubscription(sub2)
109 resp1, ok := NewResthook().GetSubscriptionById(r1.ID)
110 assert.Equal(t, ok, true)
111 assert.Equal(t, resp1.Data, sub1.Data)
113 resp2, ok := NewResthook().GetSubscriptionById(r2.ID)
114 assert.Equal(t, ok, true)
115 assert.Equal(t, resp2.Data, sub2.Data)
118 func TestTeardown(t *testing.T) {
119 NewResthook().FlushSubscriptions()
122 func CreateSubscription(et models.EventType, maxRetries, retryTimer int64, targetUrl string) models.SubscriptionRequest {
123 return models.SubscriptionRequest{&models.SubscriptionData{et, &maxRetries, &retryTimer, &targetUrl}}