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.o-ran-sc.org/r/ric-plt/appmgr/pkg/appmgr"
28 "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/models"
32 var resp models.SubscriptionResponse
35 func TestMain(m *testing.M) {
37 appmgr.Logger.SetLevel(0)
39 rh = NewResthook(false)
44 func TestAddSubscriptionSuccess(t *testing.T) {
45 resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
46 assert.Equal(t, resp.Version, int64(0))
47 assert.Equal(t, resp.EventType, models.EventTypeCreated)
50 func TestAddSubscriptionExists(t *testing.T) {
51 resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
52 assert.Equal(t, resp.Version, int64(0))
53 assert.Equal(t, resp.EventType, models.EventTypeCreated)
56 func TestDeletesubscriptionSuccess(t *testing.T) {
57 resp := rh.AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
58 assert.Equal(t, resp.Version, int64(0))
59 assert.Equal(t, resp.EventType, models.EventTypeDeleted)
61 resp, ok := rh.DeleteSubscription(resp.ID)
62 assert.Equal(t, ok, true)
63 assert.Equal(t, resp.Version, int64(0))
64 assert.Equal(t, resp.EventType, models.EventTypeDeleted)
67 func TestDeletesubscriptionInvalid(t *testing.T) {
68 resp, ok := rh.DeleteSubscription("Non-existent-ID")
69 assert.Equal(t, ok, false)
70 assert.Equal(t, resp.Version, int64(0))
71 assert.Equal(t, resp.EventType, models.EventType(""))
74 func TestModifySubscriptionSuccess(t *testing.T) {
75 resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
76 assert.Equal(t, resp.Version, int64(0))
77 assert.Equal(t, resp.EventType, models.EventTypeCreated)
79 resp, ok := rh.ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
80 assert.Equal(t, ok, true)
81 assert.Equal(t, resp.Version, int64(0))
82 assert.Equal(t, resp.EventType, models.EventTypeModified)
85 func TestModifysubscriptionInvalid(t *testing.T) {
86 resp, ok := rh.DeleteSubscription("Non-existent-ID")
87 assert.Equal(t, ok, false)
88 assert.Equal(t, resp.Version, int64(0))
89 assert.Equal(t, resp.EventType, models.EventType(""))
92 func TestGetAllSubscriptionSuccess(t *testing.T) {
93 rh.FlushSubscriptions()
94 subscriptions := rh.GetAllSubscriptions()
95 assert.Equal(t, len(subscriptions), 0)
97 rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
98 rh.AddSubscription(CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
100 subscriptions = rh.GetAllSubscriptions()
101 assert.Equal(t, len(subscriptions), 2)
104 func TestGetSubscriptionByIdSuccess(t *testing.T) {
105 rh.FlushSubscriptions()
106 sub1 := CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")
107 sub2 := CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")
108 r1 := rh.AddSubscription(sub1)
109 r2 := rh.AddSubscription(sub2)
111 resp1, ok := rh.GetSubscriptionById(r1.ID)
112 assert.Equal(t, ok, true)
113 assert.Equal(t, resp1.Data, sub1.Data)
115 resp2, ok := rh.GetSubscriptionById(r2.ID)
116 assert.Equal(t, ok, true)
117 assert.Equal(t, resp2.Data, sub2.Data)
120 func TestTeardown(t *testing.T) {
121 rh.FlushSubscriptions()
124 func CreateSubscription(et models.EventType, maxRetries, retryTimer int64, targetUrl string) models.SubscriptionRequest {
125 return models.SubscriptionRequest{&models.SubscriptionData{et, &maxRetries, &retryTimer, &targetUrl}}