go.ligato.io/vpp-agent/v3@v3.5.0/plugins/kvscheduler/api/api_test.go (about) 1 // Copyright (c) 2020 Cisco and/or its affiliates. 2 // 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 package api_test 16 17 import ( 18 "encoding/json" 19 "testing" 20 21 "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 22 ) 23 24 func TestTxnTypeEncode(t *testing.T) { 25 tests := []struct { 26 name string 27 28 txntype api.TxnType 29 30 expectOut string 31 expectErr error 32 }{ 33 {"SBNotification", api.SBNotification, `"SBNotification"`, nil}, 34 {"NBTransaction", api.NBTransaction, `"NBTransaction"`, nil}, 35 {"RetryFailedOps", api.RetryFailedOps, `"RetryFailedOps"`, nil}, 36 } 37 for _, test := range tests { 38 t.Run(test.name, func(t *testing.T) { 39 b, err := json.Marshal(test.txntype) 40 if err != test.expectErr { 41 t.Fatalf("expected error: %v, got %v", test.expectErr, err) 42 } 43 out := string(b) 44 if out != test.expectOut { 45 t.Fatalf("expected output: %q, got %q", test.expectOut, out) 46 } 47 }) 48 } 49 } 50 51 func TestTxnTypeDecode(t *testing.T) { 52 tests := []struct { 53 name string 54 55 input string 56 57 expectTxnType api.TxnType 58 expectErr error 59 }{ 60 {"RetryFailedOps", `"RetryFailedOps"`, api.RetryFailedOps, nil}, 61 {"NBTransaction", `"NBTransaction"`, api.NBTransaction, nil}, 62 {"1 (NBTransaction)", `1`, api.NBTransaction, nil}, 63 {"0 (SBNotification)", `0`, api.SBNotification, nil}, 64 {"invalid", `"INVALID"`, api.TxnType(-1), nil}, 65 } 66 for _, test := range tests { 67 t.Run(test.name, func(t *testing.T) { 68 var txntype api.TxnType 69 err := json.Unmarshal([]byte(test.input), &txntype) 70 if err != test.expectErr { 71 t.Fatalf("expected error: %v, got %v", test.expectErr, err) 72 } 73 if txntype != test.expectTxnType { 74 t.Fatalf("expected TxnType: %v, got %v", test.expectTxnType, txntype) 75 } 76 }) 77 } 78 } 79 80 func TestResyncTypeEncode(t *testing.T) { 81 tests := []struct { 82 name string 83 84 resynctype api.ResyncType 85 86 expectOut string 87 expectErr error 88 }{ 89 {"FullResync", api.FullResync, `"FullResync"`, nil}, 90 {"UpstreamResync", api.UpstreamResync, `"UpstreamResync"`, nil}, 91 {"DownstreamResync", api.DownstreamResync, `"DownstreamResync"`, nil}, 92 } 93 for _, test := range tests { 94 t.Run(test.name, func(t *testing.T) { 95 b, err := json.Marshal(test.resynctype) 96 if err != test.expectErr { 97 t.Fatalf("expected error: %v, got %v", test.expectErr, err) 98 } 99 out := string(b) 100 if out != test.expectOut { 101 t.Fatalf("expected output: %q, got %q", test.expectOut, out) 102 } 103 }) 104 } 105 } 106 107 func TestResyncTypeDecode(t *testing.T) { 108 tests := []struct { 109 name string 110 111 input string 112 113 expectResyncType api.ResyncType 114 expectErr error 115 }{ 116 {"FullResync", `"FullResync"`, api.FullResync, nil}, 117 {"UpstreamResync", `"UpstreamResync"`, api.UpstreamResync, nil}, 118 {"DownstreamResync", `"DownstreamResync"`, api.DownstreamResync, nil}, 119 {"1 (FullResync)", `1`, api.FullResync, nil}, 120 {"2 (UpstreamResync)", `2`, api.UpstreamResync, nil}, 121 {"3 (DownstreamResync)", `3`, api.DownstreamResync, nil}, 122 {"invalid", `"INVALID"`, api.ResyncType(0), nil}, 123 } 124 for _, test := range tests { 125 t.Run(test.name, func(t *testing.T) { 126 var resyncType api.ResyncType 127 err := json.Unmarshal([]byte(test.input), &resyncType) 128 if err != test.expectErr { 129 t.Fatalf("expected error: %v, got %v", test.expectErr, err) 130 } 131 if resyncType != test.expectResyncType { 132 t.Fatalf("expected ResyncType: %v, got %v", test.expectResyncType, resyncType) 133 } 134 }) 135 } 136 }