Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-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 | "encoding/json" |
| 21 | "errors" |
| 22 | "net/http" |
| 23 | "net/http/httptest" |
| 24 | "testing" |
| 25 | "voltha-go-controller/internal/test/mocks" |
| 26 | |
| 27 | app "voltha-go-controller/internal/pkg/controller" |
| 28 | //"voltha-go-controller/internal/test/mocks" |
| 29 | |
| 30 | mocksCntrlr "voltha-go-controller/voltha-go-controller/tests/mocks" |
| 31 | |
| 32 | "github.com/golang/mock/gomock" |
| 33 | "github.com/gorilla/mux" |
| 34 | ) |
| 35 | |
| 36 | func TestFlowHashHandle_PutFlowHash(t *testing.T) { |
| 37 | type args struct { |
| 38 | w http.ResponseWriter |
| 39 | r *http.Request |
| 40 | } |
| 41 | vars := map[string]string{ |
| 42 | "id": "SDX6320031", |
| 43 | } |
| 44 | request := uint32(256) |
| 45 | b, _ := json.Marshal(request) |
| 46 | req, err := http.NewRequest("PUT", "/FlowHah", bytes.NewBuffer(b)) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | req = mux.SetURLVars(req, vars) |
| 51 | req.Header.Set("Content-Type", "application/json") |
| 52 | rr := httptest.NewRecorder() |
| 53 | |
| 54 | vars1 := map[string]string{ |
| 55 | "id": "SDX6320031", |
| 56 | } |
| 57 | request1 := uint32(256) |
| 58 | b1, _ := json.Marshal(request1) |
| 59 | req1, err1 := http.NewRequest("PUT", "/FlowHah", bytes.NewBuffer(b1)) |
| 60 | if err1 != nil { |
| 61 | t.Fatal(err1) |
| 62 | } |
| 63 | req1 = mux.SetURLVars(req1, vars1) |
| 64 | req1.Header.Set("Content-Type", "application/json") |
| 65 | |
| 66 | var jsonStr1 = []byte(`{ |
| 67 | 2 "id": "BBSM00010001-1"}`) |
| 68 | req2, err2 := http.NewRequest("PUT", "/FlowHah", bytes.NewBuffer(jsonStr1)) |
| 69 | if err2 != nil { |
| 70 | t.Fatal(err2) |
| 71 | } |
| 72 | tests := []struct { |
| 73 | name string |
| 74 | fh *FlowHashHandle |
| 75 | args args |
| 76 | }{ |
| 77 | { |
| 78 | name: "PutFlowHash_Success", |
| 79 | fh: &FlowHashHandle{}, |
| 80 | args: args{ |
| 81 | r: req, |
| 82 | w: rr, |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | name: "PutFlowHash_Device_Not_Found", |
| 87 | fh: &FlowHashHandle{}, |
| 88 | args: args{ |
| 89 | r: req1, |
| 90 | w: rr, |
| 91 | }, |
| 92 | }, |
| 93 | { |
| 94 | name: "PutFlowHash_ParseUint_Failure", |
| 95 | fh: &FlowHashHandle{}, |
| 96 | args: args{ |
| 97 | r: req2, |
| 98 | w: rr, |
| 99 | }, |
| 100 | }, |
| 101 | } |
| 102 | for _, tt := range tests { |
| 103 | t.Run(tt.name, func(t *testing.T) { |
| 104 | fh := &FlowHashHandle{} |
| 105 | switch tt.name { |
| 106 | case "PutFlowHash_Device_Not_Found": |
| 107 | appMock := mocks.NewMockApp(gomock.NewController(t)) |
| 108 | app.NewController(ctx, appMock) |
| 109 | idea := mocksCntrlr.NewMockVoltControllerInterface(gomock.NewController(t)) |
| 110 | idea.EXPECT().GetDevice(gomock.Any()).Return(nil, errors.New("not found")).Times(1) |
| 111 | fh.PutFlowHash(tt.args.w, tt.args.r) |
| 112 | case "PutFlowHash_ParseUint_Failure": |
| 113 | fh.PutFlowHash(tt.args.w, tt.args.r) |
| 114 | // case "PutFlowHash_Success": |
| 115 | // appMock := mocks.NewMockApp(gomock.NewController(t)) |
| 116 | // app.NewController(ctx, appMock) |
| 117 | // cntrlr := app.GetController() |
| 118 | // device := &app.Device{ |
| 119 | // ID: "SDX6320031", |
| 120 | // } |
| 121 | // dev := map[string]*app.Device{} |
| 122 | // dev["SDX6320031"] = device |
| 123 | // cntrlr.Devices = dev |
| 124 | // idea := mocksCntrlr.NewMockVoltControllerInterface(gomock.NewController(t)) |
| 125 | // //cntrlr.VoltCntrlrIntr = idea |
| 126 | // idea.EXPECT().GetDevice(gomock.Any()).Return(device, nil).Times(1) |
| 127 | // devIntr := mocksCntrlr.NewMockDeviceInterface(gomock.NewController(t)) |
| 128 | // //device.DeviceIntr = devIntr |
| 129 | // devIntr.EXPECT().SetFlowHash(gomock.Any(), gomock.Any()).Times(1) |
| 130 | // fh.PutFlowHash(tt.args.w, tt.args.r) |
| 131 | } |
| 132 | }) |
| 133 | } |
| 134 | } |