github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/group_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package fftypes
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestGroupValidation(t *testing.T) {
    28  
    29  	group := &Group{
    30  		GroupIdentity: GroupIdentity{
    31  			Name: "!wrong",
    32  		},
    33  	}
    34  	assert.Regexp(t, "FF10131.*name", group.Validate(context.Background(), false))
    35  
    36  	group = &Group{
    37  		GroupIdentity: GroupIdentity{
    38  			Name:      "ok",
    39  			Namespace: "!wrong",
    40  		},
    41  	}
    42  	assert.Regexp(t, "FF10131.*namespace", group.Validate(context.Background(), false))
    43  
    44  	group = &Group{
    45  		GroupIdentity: GroupIdentity{
    46  			Name:      "ok",
    47  			Namespace: "ok",
    48  		},
    49  	}
    50  	assert.Regexp(t, "FF10219.*member", group.Validate(context.Background(), false))
    51  
    52  	group = &Group{
    53  		GroupIdentity: GroupIdentity{
    54  			Name:      "ok",
    55  			Namespace: "ok",
    56  			Members:   Members{{Node: NewUUID()}},
    57  		},
    58  	}
    59  	assert.Regexp(t, "FF10220.*member", group.Validate(context.Background(), false))
    60  
    61  	group = &Group{
    62  		GroupIdentity: GroupIdentity{
    63  			Name:      "ok",
    64  			Namespace: "ok",
    65  			Members:   Members{{Identity: "0x12345"}},
    66  		},
    67  	}
    68  	assert.Regexp(t, "FF10221.*member", group.Validate(context.Background(), false))
    69  
    70  	group = &Group{
    71  		GroupIdentity: GroupIdentity{
    72  			Name:      "ok",
    73  			Namespace: "ok",
    74  			Members:   Members{{Identity: string(make([]byte, 1025)), Node: NewUUID()}},
    75  		},
    76  	}
    77  	assert.Regexp(t, "FF10188.*identity", group.Validate(context.Background(), false))
    78  
    79  	group = &Group{
    80  		GroupIdentity: GroupIdentity{
    81  			Name:      "ok",
    82  			Namespace: "ok",
    83  			Members:   Members{{Identity: "0x12345", Node: NewUUID()}},
    84  		},
    85  	}
    86  	assert.NoError(t, group.Validate(context.Background(), false))
    87  
    88  	assert.Regexp(t, "FF10230", group.Validate(context.Background(), true))
    89  	group.Seal()
    90  	assert.NoError(t, group.Validate(context.Background(), true))
    91  
    92  	group = &Group{
    93  		GroupIdentity: GroupIdentity{
    94  			Name:      "ok",
    95  			Namespace: "ok",
    96  			Members:   Members{{ /* blank */ }},
    97  		},
    98  	}
    99  	assert.Regexp(t, "FF10220", group.Validate(context.Background(), false))
   100  
   101  	nodeID := MustParseUUID("8b5c0d39-925f-4579-9c60-54f3e846ab99")
   102  	group = &Group{
   103  		GroupIdentity: GroupIdentity{
   104  			Name:      "ok",
   105  			Namespace: "ok",
   106  			Members: Members{
   107  				{Node: nodeID, Identity: "0x12345"},
   108  				{Node: nodeID, Identity: "0x12345"},
   109  			},
   110  		},
   111  	}
   112  	assert.Regexp(t, "FF10222", group.Validate(context.Background(), false))
   113  
   114  	group.Members = Members{
   115  		{Node: nodeID, Identity: "0x12345"},
   116  	}
   117  	b, _ := json.Marshal(&group.Members)
   118  	assert.Equal(t, `[{"identity":"0x12345","node":"8b5c0d39-925f-4579-9c60-54f3e846ab99"}]`, string(b))
   119  	group.Seal()
   120  	assert.Equal(t, "c2e5a42207ce2b48d67a4682a96f914ec0386acc13615550aa503b0841da8824", group.Hash.String())
   121  
   122  	var def Definition = group
   123  	assert.Equal(t, group.Hash.String(), def.Topic())
   124  	def.SetBroadcastMessage(NewUUID())
   125  	assert.NotNil(t, group.Message)
   126  }