018dee8b10711c41e2a5eac39d1922fdd257f9d7
[ric-plt/appmgr.git] / pkg / resthooks / resthooks_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package resthooks
21
22 import (
23         "github.com/stretchr/testify/assert"
24         "os"
25         "testing"
26
27         "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/appmgr"
28         "gerrit.oran-osc.org/r/ric-plt/appmgr/pkg/models"
29 )
30
31 var resp models.SubscriptionResponse
32
33 // Test cases
34 func TestMain(m *testing.M) {
35         appmgr.Init()
36         appmgr.Logger.SetLevel(0)
37
38         code := m.Run()
39         os.Exit(code)
40 }
41
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)
46 }
47
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(""))
52 }
53
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)
58
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)
63 }
64
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(""))
70 }
71
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)
76
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)
81 }
82
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(""))
88 }
89
90 func TestGetAllSubscriptionSuccess(t *testing.T) {
91         NewResthook().FlushSubscriptions()
92         subscriptions := NewResthook().GetAllSubscriptions()
93         assert.Equal(t, len(subscriptions), 0)
94
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"))
97
98         subscriptions = NewResthook().GetAllSubscriptions()
99         assert.Equal(t, len(subscriptions), 2)
100 }
101
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)
108
109         resp1, ok := NewResthook().GetSubscriptionById(r1.ID)
110         assert.Equal(t, ok, true)
111         assert.Equal(t, resp1.Data, sub1.Data)
112
113         resp2, ok := NewResthook().GetSubscriptionById(r2.ID)
114         assert.Equal(t, ok, true)
115         assert.Equal(t, resp2.Data, sub2.Data)
116 }
117
118 func TestTeardown(t *testing.T) {
119         NewResthook().FlushSubscriptions()
120 }
121
122 func CreateSubscription(et models.EventType, maxRetries, retryTimer int64, targetUrl string) models.SubscriptionRequest {
123         return models.SubscriptionRequest{&models.SubscriptionData{et, &maxRetries, &retryTimer, &targetUrl}}
124 }