blob: c53abb4a3cd4879bd5ae526640bbfcc161886dfb [file] [log] [blame]
Matteo Scandolo2ba00d32020-01-16 17:33:03 -08001/*
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"
Matteo Scandolob45cf592020-01-21 16:10:56 -080021 "github.com/gogo/protobuf/proto"
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080022 "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"
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080026)
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
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080038type InvokeRpcSpy struct {
39 CallCount int
40 Calls map[int]InvokeRpcArgs
Matteo Scandolob45cf592020-01-21 16:10:56 -080041 Timeout bool
42 Response proto.Message
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080043}
44
45type MockKafkaICProxy struct {
46 InvokeRpcSpy InvokeRpcSpy
47}
48
49func (s *MockKafkaICProxy) Start() error { return nil }
50func (s *MockKafkaICProxy) DeleteTopic(topic kafka.Topic) error { return nil }
51func (s *MockKafkaICProxy) DeviceDiscovered(deviceId string, deviceType string, parentId string, publisher string) error {
52 return nil
53}
54func (s *MockKafkaICProxy) Stop() {}
55func (s *MockKafkaICProxy) InvokeRPC(ctx context.Context, rpc string, toTopic *kafka.Topic, replyToTopic *kafka.Topic, waitForResponse bool, key string, kvArgs ...*kafka.KVArg) (bool, *any.Any) {
56 s.InvokeRpcSpy.CallCount++
57
58 success := true
59
60 args := make(map[int]interface{}, 4)
61 for k, v := range kvArgs {
62 args[k] = v
63 }
64
65 s.InvokeRpcSpy.Calls[s.InvokeRpcSpy.CallCount] = InvokeRpcArgs{
66 Rpc: rpc,
67 ToTopic: toTopic,
68 ReplyToTopic: replyToTopic,
69 WaitForResponse: waitForResponse,
70 Key: key,
71 KvArgs: args,
72 }
73
Matteo Scandolob45cf592020-01-21 16:10:56 -080074 var response any.Any
75 if s.InvokeRpcSpy.Timeout {
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080076
77 success = false
78
Matteo Scandolob45cf592020-01-21 16:10:56 -080079 err := &ic.Error{Reason: "context deadline exceeded", Code: ic.ErrorCode_DEADLINE_EXCEEDED}
80 res, _ := ptypes.MarshalAny(err)
81 response = *res
82 } else {
83 res, _ := ptypes.MarshalAny(s.InvokeRpcSpy.Response)
84 response = *res
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080085 }
86
Matteo Scandolob45cf592020-01-21 16:10:56 -080087 return success, &response
Matteo Scandolo2ba00d32020-01-16 17:33:03 -080088}
89func (s *MockKafkaICProxy) SubscribeWithRequestHandlerInterface(topic kafka.Topic, handler interface{}) error {
90 return nil
91}
92func (s *MockKafkaICProxy) SubscribeWithDefaultRequestHandler(topic kafka.Topic, initialOffset int64) error {
93 return nil
94}
95func (s *MockKafkaICProxy) UnSubscribeFromRequestHandler(topic kafka.Topic) error { return nil }