vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2023-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package nbi |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "context" |
| 21 | "net/http" |
| 22 | "net/http/httptest" |
| 23 | "testing" |
| 24 | |
| 25 | "github.com/gorilla/mux" |
| 26 | ) |
| 27 | |
| 28 | func TestProfileHandle_GetProfile(t *testing.T) { |
| 29 | req, err := http.NewRequest("GET", "/get_profile/", nil) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | req.Header.Set("Content-Type", "application/json") |
| 34 | |
| 35 | vars := map[string]string{ |
| 36 | "id": "upstream_bw_profile_gpon", |
| 37 | } |
| 38 | req = mux.SetURLVars(req, vars) |
| 39 | |
| 40 | rr := httptest.NewRecorder() |
| 41 | |
| 42 | type args struct { |
| 43 | cntx context.Context |
| 44 | w http.ResponseWriter |
| 45 | r *http.Request |
| 46 | } |
| 47 | tests := []struct { |
| 48 | name string |
| 49 | args args |
| 50 | }{ |
| 51 | { |
| 52 | name: "GetProfile", |
| 53 | args: args{ |
| 54 | cntx: context.Background(), |
| 55 | w: rr, |
| 56 | r: req, |
| 57 | }, |
| 58 | }, |
| 59 | } |
| 60 | for _, tt := range tests { |
| 61 | t.Run(tt.name, func(t *testing.T) { |
| 62 | mh := &ProfileHandle{} |
| 63 | mh.GetProfile(tt.args.cntx, tt.args.w, tt.args.r) |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestProfileHandle_DelProfile(t *testing.T) { |
| 69 | var jsonStr = []byte(`{"id":"test_id"}`) |
| 70 | |
| 71 | req, err := http.NewRequest("DELETE", "/del_profile/", bytes.NewBuffer(jsonStr)) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | req.Header.Set("Content-Type", "application/json") |
| 76 | rr := httptest.NewRecorder() |
| 77 | |
| 78 | type args struct { |
| 79 | cntx context.Context |
| 80 | w http.ResponseWriter |
| 81 | r *http.Request |
| 82 | } |
| 83 | tests := []struct { |
| 84 | name string |
| 85 | mh *ProfileHandle |
| 86 | args args |
| 87 | }{ |
| 88 | { |
| 89 | name: "DelProfile", |
| 90 | args: args{ |
| 91 | cntx: context.Background(), |
| 92 | w: rr, |
| 93 | r: req, |
| 94 | }, |
| 95 | }, |
| 96 | } |
| 97 | for _, tt := range tests { |
| 98 | t.Run(tt.name, func(t *testing.T) { |
| 99 | mh := &ProfileHandle{} |
| 100 | mh.DelProfile(tt.args.cntx, tt.args.w, tt.args.r) |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestProfileHandle_AddProfile(t *testing.T) { |
| 106 | var jsonStr = []byte(`{"id": "upstream_bw_profile_gpon", "cir":"1000"}`) |
| 107 | req, err := http.NewRequest("POST", "/profiles/upstream_bw_profile_gpon", bytes.NewBuffer(jsonStr)) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | |
| 112 | req.Header.Set("Content-Type", "application/json") |
| 113 | rr := httptest.NewRecorder() |
| 114 | handler := http.HandlerFunc((&ProfileHandle{}).ServeHTTP) |
| 115 | handler.ServeHTTP(rr, req) |
| 116 | if status := rr.Code; status != http.StatusConflict { |
| 117 | t.Errorf("handler returned wrong status code: got %v want %v", |
| 118 | status, http.StatusOK) |
| 119 | } |
| 120 | } |