blob: 405fbe7048f607c4b795e327b348dc56ceb4db4a [file] [log] [blame]
npujar467fe752020-01-16 20:17:45 +05301/*
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
17package mocks
18
19import (
20 "context"
21 "github.com/gogo/protobuf/proto"
22 "github.com/golang/protobuf/ptypes"
23 "github.com/golang/protobuf/ptypes/any"
24 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
25 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
26)
27
28type InvokeRpcArgs struct {
29 Rpc string
30 ToTopic *kafka.Topic
31 ReplyToTopic *kafka.Topic
32 WaitForResponse bool
33 Key string
34 ParentDeviceId string
35 KvArgs map[int]interface{}
36}
37
38type InvokeRpcSpy struct {
39 CallCount int
40 Calls map[int]InvokeRpcArgs
41 Timeout bool
42 Response proto.Message
43}
44
45type MockKafkaICProxy struct {
46 InvokeRpcSpy InvokeRpcSpy
47}
48
49func (s *MockKafkaICProxy) Start() error { return nil }
50func (s *MockKafkaICProxy) GetDefaultTopic() *kafka.Topic {
51 t := kafka.Topic{
52 Name: "test-topic",
53 }
54 return &t
55}
56func (s *MockKafkaICProxy) DeleteTopic(topic kafka.Topic) error { return nil }
57func (s *MockKafkaICProxy) DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error {
58 return nil
59}
60func (s *MockKafkaICProxy) Stop() {}
61func (s *MockKafkaICProxy) InvokeRPC(ctx context.Context, rpc string, toTopic *kafka.Topic, replyToTopic *kafka.Topic, waitForResponse bool, key string, kvArgs ...*kafka.KVArg) (bool, *any.Any) {
62 s.InvokeRpcSpy.CallCount++
63
64 success := true
65
66 args := make(map[int]interface{}, 4)
67 for k, v := range kvArgs {
68 args[k] = v
69 }
70
71 s.InvokeRpcSpy.Calls[s.InvokeRpcSpy.CallCount] = InvokeRpcArgs{
72 Rpc: rpc,
73 ToTopic: toTopic,
74 ReplyToTopic: replyToTopic,
75 WaitForResponse: waitForResponse,
76 Key: key,
77 KvArgs: args,
78 }
79
80 var response any.Any
81 if s.InvokeRpcSpy.Timeout {
82
83 success = false
84
85 err := &ic.Error{Reason: "context deadline exceeded", Code: ic.ErrorCode_DEADLINE_EXCEEDED}
86 res, _ := ptypes.MarshalAny(err)
87 response = *res
88 } else {
89 res, _ := ptypes.MarshalAny(s.InvokeRpcSpy.Response)
90 response = *res
91 }
92
93 return success, &response
94}
95func (s *MockKafkaICProxy) SubscribeWithRequestHandlerInterface(topic kafka.Topic, handler interface{}) error {
96 return nil
97}
98func (s *MockKafkaICProxy) SubscribeWithDefaultRequestHandler(topic kafka.Topic, initialOffset int64) error {
99 return nil
100}
101func (s *MockKafkaICProxy) UnSubscribeFromRequestHandler(topic kafka.Topic) error { return nil }
102func (s *MockKafkaICProxy) EnableLivenessChannel(enable bool) chan bool { return nil }
103func (s *MockKafkaICProxy) SendLiveness() error { return nil }