Run unit tests
[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 rh *Resthook
32 var resp models.SubscriptionResponse
33
34 // Test cases
35 func TestMain(m *testing.M) {
36         appmgr.Init()
37         appmgr.Logger.SetLevel(0)
38
39         rh = NewResthook(false)
40         code := m.Run()
41         os.Exit(code)
42 }
43
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)
48 }
49
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.EventType(""))
54 }
55
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)
60
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)
65 }
66
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(""))
72 }
73
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)
78
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)
83 }
84
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(""))
90 }
91
92 func TestGetAllSubscriptionSuccess(t *testing.T) {
93         rh.FlushSubscriptions()
94         subscriptions := rh.GetAllSubscriptions()
95         assert.Equal(t, len(subscriptions), 0)
96
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"))
99
100         subscriptions = rh.GetAllSubscriptions()
101         assert.Equal(t, len(subscriptions), 2)
102 }
103
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)
110
111         resp1, ok := rh.GetSubscriptionById(r1.ID)
112         assert.Equal(t, ok, true)
113         assert.Equal(t, resp1.Data, sub1.Data)
114
115         resp2, ok := rh.GetSubscriptionById(r2.ID)
116         assert.Equal(t, ok, true)
117         assert.Equal(t, resp2.Data, sub2.Data)
118 }
119
120 func TestTeardown(t *testing.T) {
121         rh.FlushSubscriptions()
122 }
123
124 func CreateSubscription(et models.EventType, maxRetries, retryTimer int64, targetUrl string) models.SubscriptionRequest {
125         return models.SubscriptionRequest{&models.SubscriptionData{et, &maxRetries, &retryTimer, &targetUrl}}
126 }