Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 17 | package kafka |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
David Bainbridge | ce5e11a | 2020-06-23 12:41:16 -0700 | [diff] [blame] | 21 | |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 22 | "github.com/gogo/protobuf/proto" |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 23 | "github.com/golang/protobuf/ptypes" |
| 24 | "github.com/golang/protobuf/ptypes/any" |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
| 26 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type InvokeRpcArgs struct { |
| 30 | Rpc string |
| 31 | ToTopic *kafka.Topic |
| 32 | ReplyToTopic *kafka.Topic |
| 33 | WaitForResponse bool |
| 34 | Key string |
| 35 | ParentDeviceId string |
| 36 | KvArgs map[int]interface{} |
| 37 | } |
| 38 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 39 | type InvokeRpcSpy struct { |
| 40 | CallCount int |
| 41 | Calls map[int]InvokeRpcArgs |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 42 | Timeout bool |
| 43 | Response proto.Message |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 46 | type InvokeAsyncRpcSpy struct { |
| 47 | CallCount int |
| 48 | Calls map[int]InvokeRpcArgs |
| 49 | Timeout bool |
| 50 | Response proto.Message |
| 51 | } |
| 52 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 53 | type MockKafkaICProxy struct { |
| 54 | InvokeRpcSpy InvokeRpcSpy |
| 55 | } |
| 56 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 57 | func (s *MockKafkaICProxy) Start(ctx context.Context) error { return nil } |
Matteo Scandolo | f346a2d | 2020-01-24 13:14:54 -0800 | [diff] [blame] | 58 | func (s *MockKafkaICProxy) GetDefaultTopic() *kafka.Topic { |
| 59 | t := kafka.Topic{ |
| 60 | Name: "test-topic", |
| 61 | } |
| 62 | return &t |
| 63 | } |
David Bainbridge | ce5e11a | 2020-06-23 12:41:16 -0700 | [diff] [blame] | 64 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 65 | func (s *MockKafkaICProxy) DeleteTopic(ctx context.Context, topic kafka.Topic) error { return nil } |
| 66 | |
| 67 | func (s *MockKafkaICProxy) Stop(ctx context.Context) {} |
Matteo Scandolo | ed12882 | 2020-02-10 15:52:35 -0800 | [diff] [blame] | 68 | |
| 69 | func (s *MockKafkaICProxy) InvokeAsyncRPC(ctx context.Context, rpc string, toTopic *kafka.Topic, replyToTopic *kafka.Topic, |
| 70 | waitForResponse bool, key string, kvArgs ...*kafka.KVArg) chan *kafka.RpcResponse { |
| 71 | |
| 72 | args := make(map[int]interface{}, 4) |
| 73 | for k, v := range kvArgs { |
| 74 | args[k] = v |
| 75 | } |
| 76 | |
| 77 | s.InvokeRpcSpy.Calls[s.InvokeRpcSpy.CallCount] = InvokeRpcArgs{ |
| 78 | Rpc: rpc, |
| 79 | ToTopic: toTopic, |
| 80 | ReplyToTopic: replyToTopic, |
| 81 | WaitForResponse: waitForResponse, |
| 82 | Key: key, |
| 83 | KvArgs: args, |
| 84 | } |
| 85 | |
| 86 | chnl := make(chan *kafka.RpcResponse) |
| 87 | |
| 88 | return chnl |
| 89 | } |
| 90 | |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 91 | func (s *MockKafkaICProxy) InvokeRPC(ctx context.Context, rpc string, toTopic *kafka.Topic, replyToTopic *kafka.Topic, waitForResponse bool, key string, kvArgs ...*kafka.KVArg) (bool, *any.Any) { |
| 92 | s.InvokeRpcSpy.CallCount++ |
| 93 | |
| 94 | success := true |
| 95 | |
| 96 | args := make(map[int]interface{}, 4) |
| 97 | for k, v := range kvArgs { |
| 98 | args[k] = v |
| 99 | } |
| 100 | |
| 101 | s.InvokeRpcSpy.Calls[s.InvokeRpcSpy.CallCount] = InvokeRpcArgs{ |
| 102 | Rpc: rpc, |
| 103 | ToTopic: toTopic, |
| 104 | ReplyToTopic: replyToTopic, |
| 105 | WaitForResponse: waitForResponse, |
| 106 | Key: key, |
| 107 | KvArgs: args, |
| 108 | } |
| 109 | |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 110 | var response any.Any |
| 111 | if s.InvokeRpcSpy.Timeout { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 112 | |
| 113 | success = false |
| 114 | |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 115 | err := &ic.Error{Reason: "context deadline exceeded", Code: ic.ErrorCode_DEADLINE_EXCEEDED} |
| 116 | res, _ := ptypes.MarshalAny(err) |
| 117 | response = *res |
| 118 | } else { |
| 119 | res, _ := ptypes.MarshalAny(s.InvokeRpcSpy.Response) |
| 120 | response = *res |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Matteo Scandolo | b45cf59 | 2020-01-21 16:10:56 -0800 | [diff] [blame] | 123 | return success, &response |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 124 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 125 | func (s *MockKafkaICProxy) SubscribeWithRequestHandlerInterface(ctx context.Context, topic kafka.Topic, handler interface{}) error { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 126 | return nil |
| 127 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 128 | func (s *MockKafkaICProxy) SubscribeWithDefaultRequestHandler(ctx context.Context, topic kafka.Topic, initialOffset int64) error { |
Matteo Scandolo | 2ba00d3 | 2020-01-16 17:33:03 -0800 | [diff] [blame] | 129 | return nil |
| 130 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 131 | func (s *MockKafkaICProxy) UnSubscribeFromRequestHandler(ctx context.Context, topic kafka.Topic) error { |
| 132 | return nil |
| 133 | } |
| 134 | func (s *MockKafkaICProxy) EnableLivenessChannel(ctx context.Context, enable bool) chan bool { |
| 135 | return nil |
| 136 | } |
| 137 | func (s *MockKafkaICProxy) SendLiveness(ctx context.Context) error { return nil } |