github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/dse_revise_request_test.go (about) 1 // Copyright 2020 DataStax 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 message 16 17 import ( 18 "bytes" 19 "errors" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 24 "github.com/datastax/go-cassandra-native-protocol/primitive" 25 ) 26 27 func TestRevise_DeepCopy(t *testing.T) { 28 obj := &Revise{ 29 RevisionType: primitive.DseRevisionTypeCancelContinuousPaging, 30 TargetStreamId: 5, 31 NextPages: 10, 32 } 33 cloned := obj.DeepCopy() 34 assert.Equal(t, obj, cloned) 35 cloned.RevisionType = primitive.DseRevisionTypeMoreContinuousPages 36 cloned.TargetStreamId = 6 37 cloned.NextPages = 7 38 assert.NotEqual(t, obj, cloned) 39 assert.Equal(t, primitive.DseRevisionTypeCancelContinuousPaging, obj.RevisionType) 40 assert.EqualValues(t, 5, obj.TargetStreamId) 41 assert.EqualValues(t, 10, obj.NextPages) 42 assert.Equal(t, primitive.DseRevisionTypeMoreContinuousPages, cloned.RevisionType) 43 assert.EqualValues(t, 6, cloned.TargetStreamId) 44 assert.EqualValues(t, 7, cloned.NextPages) 45 } 46 47 func TestReviseCodec_Encode(t *testing.T) { 48 codec := &reviseCodec{} 49 version := primitive.ProtocolVersionDse1 50 t.Run(version.String(), func(t *testing.T) { 51 tests := []encodeTestCase{ 52 { 53 "simple revise", 54 &Revise{ 55 RevisionType: primitive.DseRevisionTypeCancelContinuousPaging, 56 TargetStreamId: 123, 57 }, 58 []byte{ 59 0, 0, 0, 1, // revision type 60 0, 0, 0, 123, // stream id 61 }, 62 nil, 63 }, 64 { 65 "not a revise", 66 &AuthChallenge{[]byte{0xca, 0xfe, 0xba, 0xbe}}, 67 nil, 68 errors.New("expected *message.Revise, got *message.AuthChallenge"), 69 }, 70 } 71 for _, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 dest := &bytes.Buffer{} 74 err := codec.Encode(tt.input, dest, version) 75 assert.Equal(t, tt.expected, dest.Bytes()) 76 assert.Equal(t, tt.err, err) 77 }) 78 } 79 }) 80 version = primitive.ProtocolVersionDse2 81 t.Run(version.String(), func(t *testing.T) { 82 tests := []encodeTestCase{ 83 { 84 "revise cancel", 85 &Revise{ 86 RevisionType: primitive.DseRevisionTypeCancelContinuousPaging, 87 TargetStreamId: 123, 88 }, 89 []byte{ 90 0, 0, 0, 1, // revision type 91 0, 0, 0, 123, // stream id 92 }, 93 nil, 94 }, 95 { 96 "revise more", 97 &Revise{ 98 RevisionType: primitive.DseRevisionTypeMoreContinuousPages, 99 TargetStreamId: 123, 100 NextPages: 4, 101 }, 102 []byte{ 103 0, 0, 0, 2, // revision type 104 0, 0, 0, 123, // stream id 105 0, 0, 0, 4, // next pages 106 }, 107 nil, 108 }, 109 { 110 "not a revise", 111 &AuthChallenge{[]byte{0xca, 0xfe, 0xba, 0xbe}}, 112 nil, 113 errors.New("expected *message.Revise, got *message.AuthChallenge"), 114 }, 115 } 116 for _, tt := range tests { 117 t.Run(tt.name, func(t *testing.T) { 118 dest := &bytes.Buffer{} 119 err := codec.Encode(tt.input, dest, version) 120 assert.Equal(t, tt.expected, dest.Bytes()) 121 assert.Equal(t, tt.err, err) 122 }) 123 } 124 }) 125 } 126 127 func TestReviseCodec_EncodedLength(t *testing.T) { 128 codec := &reviseCodec{} 129 version := primitive.ProtocolVersionDse1 130 t.Run(version.String(), func(t *testing.T) { 131 tests := []encodedLengthTestCase{ 132 { 133 "simple revise", 134 &Revise{ 135 RevisionType: primitive.DseRevisionTypeCancelContinuousPaging, 136 TargetStreamId: 123, 137 }, 138 primitive.LengthOfInt * 2, 139 nil, 140 }, 141 { 142 "not a revise", 143 &AuthChallenge{[]byte{0xca, 0xfe, 0xba, 0xbe}}, 144 -1, 145 errors.New("expected *message.Revise, got *message.AuthChallenge"), 146 }, 147 } 148 for _, tt := range tests { 149 t.Run(tt.name, func(t *testing.T) { 150 actual, err := codec.EncodedLength(tt.input, version) 151 assert.Equal(t, tt.expected, actual) 152 assert.Equal(t, tt.err, err) 153 }) 154 } 155 }) 156 version = primitive.ProtocolVersionDse2 157 t.Run(version.String(), func(t *testing.T) { 158 tests := []encodedLengthTestCase{ 159 { 160 "revise cancel", 161 &Revise{ 162 RevisionType: primitive.DseRevisionTypeCancelContinuousPaging, 163 TargetStreamId: 123, 164 }, 165 primitive.LengthOfInt * 2, 166 nil, 167 }, 168 { 169 "revise more", 170 &Revise{ 171 RevisionType: primitive.DseRevisionTypeMoreContinuousPages, 172 TargetStreamId: 123, 173 NextPages: 4, 174 }, 175 primitive.LengthOfInt * 3, 176 nil, 177 }, 178 { 179 "not a revise", 180 &AuthChallenge{[]byte{0xca, 0xfe, 0xba, 0xbe}}, 181 -1, 182 errors.New("expected *message.Revise, got *message.AuthChallenge"), 183 }, 184 } 185 for _, tt := range tests { 186 t.Run(tt.name, func(t *testing.T) { 187 actual, err := codec.EncodedLength(tt.input, version) 188 assert.Equal(t, tt.expected, actual) 189 assert.Equal(t, tt.err, err) 190 }) 191 } 192 }) 193 } 194 195 func TestReviseCodec_Decode(t *testing.T) { 196 codec := &reviseCodec{} 197 version := primitive.ProtocolVersionDse1 198 t.Run(version.String(), func(t *testing.T) { 199 tests := []decodeTestCase{ 200 { 201 "simple revise", 202 []byte{ 203 0, 0, 0, 1, // revision type 204 0, 0, 0, 123, // stream id 205 }, 206 &Revise{ 207 RevisionType: primitive.DseRevisionTypeCancelContinuousPaging, 208 TargetStreamId: 123, 209 }, 210 nil, 211 }, 212 } 213 for _, tt := range tests { 214 t.Run(tt.name, func(t *testing.T) { 215 source := bytes.NewBuffer(tt.input) 216 actual, err := codec.Decode(source, version) 217 assert.Equal(t, tt.expected, actual) 218 assert.Equal(t, tt.err, err) 219 }) 220 } 221 }) 222 version = primitive.ProtocolVersionDse2 223 t.Run(version.String(), func(t *testing.T) { 224 tests := []decodeTestCase{ 225 { 226 "revise cancel", 227 []byte{ 228 0, 0, 0, 1, // revision type 229 0, 0, 0, 123, // stream id 230 }, 231 &Revise{ 232 RevisionType: primitive.DseRevisionTypeCancelContinuousPaging, 233 TargetStreamId: 123, 234 }, 235 nil, 236 }, 237 { 238 "revise more", 239 []byte{ 240 0, 0, 0, 2, // revision type 241 0, 0, 0, 123, // stream id 242 0, 0, 0, 4, // next pages 243 }, 244 &Revise{ 245 RevisionType: primitive.DseRevisionTypeMoreContinuousPages, 246 TargetStreamId: 123, 247 NextPages: 4, 248 }, 249 nil, 250 }, 251 } 252 for _, tt := range tests { 253 t.Run(tt.name, func(t *testing.T) { 254 source := bytes.NewBuffer(tt.input) 255 actual, err := codec.Decode(source, version) 256 assert.Equal(t, tt.expected, actual) 257 assert.Equal(t, tt.err, err) 258 }) 259 } 260 }) 261 }