blob: d1ac790132607a7e07599d3bbad66fdb27e5a81c [file] [log] [blame]
Mohamed Abukar34e43832019-11-13 17:57:15 +02001/*
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
20package resthooks
21
22import (
23 "github.com/stretchr/testify/assert"
24 "os"
25 "testing"
26
Abukar Mohamedf8b99662020-03-02 14:44:30 +000027 "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/appmgr"
28 "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/models"
Mohamed Abukar34e43832019-11-13 17:57:15 +020029)
30
Mohamed Abukard9769772019-11-20 20:39:06 +020031var rh *Resthook
Mohamed Abukar34e43832019-11-13 17:57:15 +020032var resp models.SubscriptionResponse
33
34// Test cases
35func TestMain(m *testing.M) {
36 appmgr.Init()
37 appmgr.Logger.SetLevel(0)
38
Mohamed Abukard9769772019-11-20 20:39:06 +020039 rh = NewResthook(false)
Mohamed Abukar34e43832019-11-13 17:57:15 +020040 code := m.Run()
41 os.Exit(code)
42}
43
44func TestAddSubscriptionSuccess(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +020045 resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
Mohamed Abukar34e43832019-11-13 17:57:15 +020046 assert.Equal(t, resp.Version, int64(0))
47 assert.Equal(t, resp.EventType, models.EventTypeCreated)
48}
49
50func TestAddSubscriptionExists(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +020051 resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook"))
Mohamed Abukar34e43832019-11-13 17:57:15 +020052 assert.Equal(t, resp.Version, int64(0))
Mohamed Abukar45ea51f2020-03-26 15:14:33 +020053 assert.Equal(t, resp.EventType, models.EventTypeCreated)
Mohamed Abukar34e43832019-11-13 17:57:15 +020054}
55
56func TestDeletesubscriptionSuccess(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +020057 resp := rh.AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
Mohamed Abukar34e43832019-11-13 17:57:15 +020058 assert.Equal(t, resp.Version, int64(0))
59 assert.Equal(t, resp.EventType, models.EventTypeDeleted)
60
Mohamed Abukard9769772019-11-20 20:39:06 +020061 resp, ok := rh.DeleteSubscription(resp.ID)
Mohamed Abukar34e43832019-11-13 17:57:15 +020062 assert.Equal(t, ok, true)
63 assert.Equal(t, resp.Version, int64(0))
64 assert.Equal(t, resp.EventType, models.EventTypeDeleted)
65}
66
67func TestDeletesubscriptionInvalid(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +020068 resp, ok := rh.DeleteSubscription("Non-existent-ID")
Mohamed Abukar34e43832019-11-13 17:57:15 +020069 assert.Equal(t, ok, false)
70 assert.Equal(t, resp.Version, int64(0))
71 assert.Equal(t, resp.EventType, models.EventType(""))
72}
73
74func TestModifySubscriptionSuccess(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +020075 resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
Mohamed Abukar34e43832019-11-13 17:57:15 +020076 assert.Equal(t, resp.Version, int64(0))
77 assert.Equal(t, resp.EventType, models.EventTypeCreated)
78
Mohamed Abukard9769772019-11-20 20:39:06 +020079 resp, ok := rh.ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2"))
Mohamed Abukar34e43832019-11-13 17:57:15 +020080 assert.Equal(t, ok, true)
81 assert.Equal(t, resp.Version, int64(0))
82 assert.Equal(t, resp.EventType, models.EventTypeModified)
83}
84
85func TestModifysubscriptionInvalid(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +020086 resp, ok := rh.DeleteSubscription("Non-existent-ID")
Mohamed Abukar34e43832019-11-13 17:57:15 +020087 assert.Equal(t, ok, false)
88 assert.Equal(t, resp.Version, int64(0))
89 assert.Equal(t, resp.EventType, models.EventType(""))
90}
91
92func TestGetAllSubscriptionSuccess(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +020093 rh.FlushSubscriptions()
94 subscriptions := rh.GetAllSubscriptions()
Mohamed Abukar34e43832019-11-13 17:57:15 +020095 assert.Equal(t, len(subscriptions), 0)
96
Mohamed Abukard9769772019-11-20 20:39:06 +020097 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"))
Mohamed Abukar34e43832019-11-13 17:57:15 +020099
Mohamed Abukard9769772019-11-20 20:39:06 +0200100 subscriptions = rh.GetAllSubscriptions()
Mohamed Abukar34e43832019-11-13 17:57:15 +0200101 assert.Equal(t, len(subscriptions), 2)
102}
103
104func TestGetSubscriptionByIdSuccess(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +0200105 rh.FlushSubscriptions()
Mohamed Abukar34e43832019-11-13 17:57:15 +0200106 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")
Mohamed Abukard9769772019-11-20 20:39:06 +0200108 r1 := rh.AddSubscription(sub1)
109 r2 := rh.AddSubscription(sub2)
Mohamed Abukar34e43832019-11-13 17:57:15 +0200110
Mohamed Abukard9769772019-11-20 20:39:06 +0200111 resp1, ok := rh.GetSubscriptionById(r1.ID)
Mohamed Abukar34e43832019-11-13 17:57:15 +0200112 assert.Equal(t, ok, true)
113 assert.Equal(t, resp1.Data, sub1.Data)
114
Mohamed Abukard9769772019-11-20 20:39:06 +0200115 resp2, ok := rh.GetSubscriptionById(r2.ID)
Mohamed Abukar34e43832019-11-13 17:57:15 +0200116 assert.Equal(t, ok, true)
117 assert.Equal(t, resp2.Data, sub2.Data)
118}
119
120func TestTeardown(t *testing.T) {
Mohamed Abukard9769772019-11-20 20:39:06 +0200121 rh.FlushSubscriptions()
Mohamed Abukar34e43832019-11-13 17:57:15 +0200122}
123
124func CreateSubscription(et models.EventType, maxRetries, retryTimer int64, targetUrl string) models.SubscriptionRequest {
125 return models.SubscriptionRequest{&models.SubscriptionData{et, &maxRetries, &retryTimer, &targetUrl}}
126}