Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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 | |
Abukar Mohamed | f8b9966 | 2020-03-02 14:44:30 +0000 | [diff] [blame] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/appmgr" |
| 28 | "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/models" |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 29 | ) |
| 30 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 31 | var rh *Resthook |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 32 | var resp models.SubscriptionResponse |
| 33 | |
| 34 | // Test cases |
| 35 | func TestMain(m *testing.M) { |
| 36 | appmgr.Init() |
| 37 | appmgr.Logger.SetLevel(0) |
| 38 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 39 | rh = NewResthook(false) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 40 | code := m.Run() |
| 41 | os.Exit(code) |
| 42 | } |
| 43 | |
| 44 | func TestAddSubscriptionSuccess(t *testing.T) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 45 | resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 46 | assert.Equal(t, resp.Version, int64(0)) |
| 47 | assert.Equal(t, resp.EventType, models.EventTypeCreated) |
| 48 | } |
| 49 | |
| 50 | func TestAddSubscriptionExists(t *testing.T) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 51 | resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 52 | assert.Equal(t, resp.Version, int64(0)) |
Mohamed Abukar | 45ea51f | 2020-03-26 15:14:33 +0200 | [diff] [blame^] | 53 | assert.Equal(t, resp.EventType, models.EventTypeCreated) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | func TestDeletesubscriptionSuccess(t *testing.T) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 57 | resp := rh.AddSubscription(CreateSubscription(models.EventTypeDeleted, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 58 | assert.Equal(t, resp.Version, int64(0)) |
| 59 | assert.Equal(t, resp.EventType, models.EventTypeDeleted) |
| 60 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 61 | resp, ok := rh.DeleteSubscription(resp.ID) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 68 | resp, ok := rh.DeleteSubscription("Non-existent-ID") |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 75 | resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 76 | assert.Equal(t, resp.Version, int64(0)) |
| 77 | assert.Equal(t, resp.EventType, models.EventTypeCreated) |
| 78 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 79 | resp, ok := rh.ModifySubscription(resp.ID, CreateSubscription(models.EventTypeModified, int64(5), int64(10), "http://localhost:8087/xapps_hook2")) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 86 | resp, ok := rh.DeleteSubscription("Non-existent-ID") |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 93 | rh.FlushSubscriptions() |
| 94 | subscriptions := rh.GetAllSubscriptions() |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 95 | assert.Equal(t, len(subscriptions), 0) |
| 96 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 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")) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 99 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 100 | subscriptions = rh.GetAllSubscriptions() |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 101 | assert.Equal(t, len(subscriptions), 2) |
| 102 | } |
| 103 | |
| 104 | func TestGetSubscriptionByIdSuccess(t *testing.T) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 105 | rh.FlushSubscriptions() |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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") |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 108 | r1 := rh.AddSubscription(sub1) |
| 109 | r2 := rh.AddSubscription(sub2) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 110 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 111 | resp1, ok := rh.GetSubscriptionById(r1.ID) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 112 | assert.Equal(t, ok, true) |
| 113 | assert.Equal(t, resp1.Data, sub1.Data) |
| 114 | |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 115 | resp2, ok := rh.GetSubscriptionById(r2.ID) |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 116 | assert.Equal(t, ok, true) |
| 117 | assert.Equal(t, resp2.Data, sub2.Data) |
| 118 | } |
| 119 | |
| 120 | func TestTeardown(t *testing.T) { |
Mohamed Abukar | d976977 | 2019-11-20 20:39:06 +0200 | [diff] [blame] | 121 | rh.FlushSubscriptions() |
Mohamed Abukar | 34e4383 | 2019-11-13 17:57:15 +0200 | [diff] [blame] | 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 | } |