Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 AT&T Intellectual Property. |
| 3 | * Copyright (c) 2018-2019 Nokia. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Roni Riska | 6ffba08 | 2019-11-27 10:59:54 +0200 | [diff] [blame] | 16 | * |
| 17 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | * platform project (RICP). |
| 19 | * |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | package main |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "encoding/json" |
| 27 | "fmt" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 28 | "io/ioutil" |
| 29 | "net/http" |
| 30 | "net/http/httptest" |
| 31 | "testing" |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 32 | |
| 33 | "github.com/stretchr/testify/suite" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 34 | ) |
| 35 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 36 | type AppmgrHTTPServerTestSuite struct { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 37 | suite.Suite |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 38 | subscriptions chan subscriptionNotification |
| 39 | xappNotifURL string |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // suite setup |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 43 | func (suite *AppmgrHTTPServerTestSuite) SetupSuite() { |
| 44 | // the url here is not actually used anywhere |
| 45 | suite.xappNotifURL = "http://127.0.0.1:8080" + vesmgrXappNotifPath |
| 46 | suite.subscriptions = make(chan subscriptionNotification) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // test setup |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 50 | func (suite *AppmgrHTTPServerTestSuite) SetupTest() { |
| 51 | suite.subscriptions = make(chan subscriptionNotification) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 52 | } |
| 53 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 54 | func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotifications() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 55 | testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 56 | body, _ := ioutil.ReadAll(req.Body) |
| 57 | var result map[string]interface{} |
| 58 | err := json.Unmarshal([]byte(body), &result) |
| 59 | suite.Nil(err) |
Roni Riska | ce6ecc6 | 2020-01-28 11:58:06 +0200 | [diff] [blame^] | 60 | data := result["Data"].(map[string]interface{}) |
| 61 | suite.Equal(5, int(data["maxRetries"].(float64))) |
| 62 | suite.Equal(5, int(data["retryTimer"].(float64))) |
| 63 | suite.Equal("all", data["eventType"].(string)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 64 | suite.Equal("POST", req.Method) |
| 65 | res.Header().Add("Content-Type", "application/json") |
| 66 | res.WriteHeader(http.StatusCreated) |
| 67 | res.Write([]byte(`{"id":"deadbeef1234567890", "version":0, "eventType":"all"}`)) |
| 68 | })) |
| 69 | defer testServer.Close() |
| 70 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 71 | go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 72 | isSubscribed := <-suite.subscriptions |
| 73 | suite.Nil(isSubscribed.err) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 74 | suite.Equal("deadbeef1234567890", isSubscribed.subsID) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 75 | } |
| 76 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 77 | func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongStatus() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 78 | testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 79 | res.Header().Add("Content-Type", "application/json") |
| 80 | res.WriteHeader(http.StatusUnauthorized) |
| 81 | res.Write([]byte(`{"id":"deadbeef1234567890", "version":0, "eventType":"all"}`)) |
| 82 | })) |
| 83 | defer testServer.Close() |
| 84 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 85 | requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 86 | req, _ := http.NewRequest("POST", testServer.URL, bytes.NewBuffer(requestBody)) |
| 87 | req.Header.Set("Content-Type", "application/json") |
| 88 | client := &http.Client{} |
| 89 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 90 | subsID, err := subscribexAppNotificationsClientDo(req, client) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 91 | suite.Equal(errWrongStatusCode, err) |
| 92 | // after failed POST vesmgr.appmgrSubsId holds an initial values |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 93 | suite.Equal("", subsID) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 94 | } |
| 95 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 96 | func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsWrongUrl() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 97 | // use fake appmgrUrl that is not served in unit test |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 98 | appmgrURL := "/I_do_not_exist/" |
| 99 | requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, suite.xappNotifURL)) |
| 100 | req, _ := http.NewRequest("POST", appmgrURL, bytes.NewBuffer(requestBody)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 101 | req.Header.Set("Content-Type", "application/json") |
| 102 | client := &http.Client{} |
| 103 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 104 | subsID, err := subscribexAppNotificationsClientDo(req, client) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 105 | suite.Equal(errPostingFailed, err) |
| 106 | // after failed POST vesmgr.appmgrSubsId holds an initial values |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 107 | suite.Equal("", subsID) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 108 | } |
| 109 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 110 | func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsReadBodyFails() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 111 | testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 112 | res.Header().Set("Content-Length", "1") |
| 113 | res.Header().Add("Content-Type", "application/json") |
| 114 | res.WriteHeader(http.StatusCreated) |
| 115 | })) |
| 116 | defer testServer.Close() |
| 117 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 118 | go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 119 | isSubscribed := <-suite.subscriptions |
| 120 | suite.Equal("unexpected EOF", isSubscribed.err.Error()) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 121 | suite.Equal("", isSubscribed.subsID) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 122 | } |
| 123 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 124 | func (suite *AppmgrHTTPServerTestSuite) TestSubscribexAppNotificationsUnMarshalFails() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 125 | testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 126 | res.Header().Add("Content-Type", "application/json") |
| 127 | res.WriteHeader(http.StatusCreated) |
| 128 | res.Write([]byte(`{""dump for UT": make(chan int),"}`)) |
| 129 | })) |
| 130 | defer testServer.Close() |
| 131 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 132 | go subscribexAppNotifications(suite.xappNotifURL, suite.subscriptions, 1, testServer.URL) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 133 | isSubscribed := <-suite.subscriptions |
| 134 | suite.Equal("invalid character 'd' after object key", isSubscribed.err.Error()) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 135 | suite.Equal("", isSubscribed.subsID) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | func TestAppmgrHttpServerTestSuite(t *testing.T) { |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 139 | suite.Run(t, new(AppmgrHTTPServerTestSuite)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 140 | } |