| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| |
| package command |
| |
| import ( |
| "bytes" |
| "reflect" |
| "testing" |
| |
| iggcon "github.com/apache/iggy/foreign/go/contracts" |
| ) |
| |
| func TestSerialize_TcpCreateStreamRequest(t *testing.T) { |
| // Create a sample CreateStream |
| request := CreateStream{ |
| Name: "test_stream", |
| } |
| |
| // Serialize the request |
| serialized, err := request.MarshalBinary() |
| if err != nil { |
| t.Errorf("Failed to serialize CreateStream: %v", err) |
| } |
| // Expected serialized bytes |
| expectedNameLength := byte(11) // Length of "test_stream" |
| expectedPayload := []byte("test_stream") |
| |
| // Check the length of the serialized bytes |
| if len(serialized) != int(payloadOffset+len(request.Name)) { |
| t.Errorf("Serialized length is incorrect. Expected: %d, Got: %d", payloadOffset+len(request.Name), len(serialized)) |
| } |
| |
| // Check the NameLength field |
| if serialized[nameLengthOffset] != expectedNameLength { |
| t.Errorf("NameLength is incorrect. Expected: %d, Got: %d", expectedNameLength, serialized[nameLengthOffset]) |
| } |
| |
| // Check the Payload field |
| if !reflect.DeepEqual(serialized[payloadOffset:], expectedPayload) { |
| t.Errorf("Payload is incorrect. Expected: %v, Got: %v", expectedPayload, serialized[payloadOffset:]) |
| } |
| } |
| |
| func TestSerialize_UpdateStream(t *testing.T) { |
| streamId, _ := iggcon.NewIdentifier("stream") |
| request := UpdateStream{ |
| StreamId: streamId, |
| Name: "update_stream", |
| } |
| |
| serialized1, err := request.MarshalBinary() |
| if err != nil { |
| t.Errorf("Failed to serialize UpdateStream: %v", err) |
| } |
| |
| expected := []byte{ |
| 0x02, // StreamId Kind (StringId) |
| 0x06, // StreamId Length (2) |
| 0x73, 0x74, 0x72, 0x65, 0x61, 0x6D, // StreamId Value ("stream") |
| 0x0D, // Name Length (13) |
| 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5F, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6D, // Name ("update_stream") |
| } |
| |
| if !bytes.Equal(serialized1, expected) { |
| t.Errorf("Test case 1 failed. \nExpected:\t%v\nGot:\t\t%v", expected, serialized1) |
| } |
| } |