Skip to content
Snippets Groups Projects
device_store_test.go 5.45 KiB
Newer Older
  • Learn to ignore specific revisions
  • package store
    
    import (
    	"reflect"
    	"sync"
    	"testing"
    
    	"code.fbi.h-da.de/danet/gosdn/interfaces/device"
    	"code.fbi.h-da.de/danet/gosdn/interfaces/store"
    	"code.fbi.h-da.de/danet/gosdn/mocks"
    
    	"github.com/google/go-cmp/cmp"
    	"github.com/google/go-cmp/cmp/cmpopts"
    
    	"github.com/google/uuid"
    )
    
    func Test_deviceStore_get(t *testing.T) {
    	deviceMock := &mocks.Device{}
    	deviceMock.On("ID").Return(did)
    	deviceMock.On("Name").Return("did")
    	pndMock := &mocks.NetworkDomain{}
    	pndMock.On("ID").Return(did)
    
    	type fields struct {
    		genericStore *genericStore
    	}
    	type args struct {
    		id uuid.UUID
    	}
    	tests := []struct {
    		name    string
    		fields  fields
    		args    args
    		want    device.Device
    		wantErr bool
    	}{
    		{
    			name: "exists",
    			fields: fields{
    				&genericStore{
    					Store: map[uuid.UUID]store.Storable{
    						defaultPndID: deviceMock,
    					},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			args:    args{id: defaultPndID},
    			want:    deviceMock,
    			wantErr: false,
    		},
    		{
    			name: "fails",
    			fields: fields{
    				&genericStore{
    					Store: map[uuid.UUID]store.Storable{
    						defaultPndID: deviceMock,
    					},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			args:    args{id: iid},
    			wantErr: true,
    		},
    		{
    			name: "fails empty",
    			fields: fields{
    				genericStore: &genericStore{},
    			},
    			args:    args{id: defaultPndID},
    			wantErr: true,
    		},
    		{
    			name: "fails wrong type",
    			fields: fields{
    				&genericStore{
    					Store: map[uuid.UUID]store.Storable{
    						defaultPndID: pndMock,
    					},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			args:    args{id: defaultPndID},
    			wantErr: true,
    		},
    	}
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			s := DeviceStore{genericStore: tt.fields.genericStore}
    
    			got, err := s.GetDevice(FromString(tt.args.id.String()))
    			if (err != nil) != tt.wantErr {
    				t.Errorf("get() error = %v, wantErr %v", err, tt.wantErr)
    				return
    			}
    			if !reflect.DeepEqual(got, tt.want) {
    				t.Errorf("get() got = %v, want %v", got, tt.want)
    			}
    		})
    	}
    }
    
    
    func Test_deviceStore_GetDevicesAsssociatedWithSbi(t *testing.T) {
    	mockSBI := &mocks.SouthboundInterface{}
    	mockSBI.On("ID").Return(defaultSbiID)
    
    	createDeviceMock := func(name string, sid uuid.UUID) *mocks.Device {
    		dm := &mocks.Device{}
    		dm.On("ID").Return(uuid.New())
    		dm.On("Name").Return("did")
    		dm.On("SBI").Return(mockSBI)
    		return dm
    	}
    
    	associatedDevices := []device.Device{
    		createDeviceMock("mockDevice1", defaultSbiID),
    		createDeviceMock("mockDevice2", defaultSbiID),
    		createDeviceMock("mockDevice3", defaultSbiID),
    	}
    
    	nonAssociatedDevice := createDeviceMock("mockDevice4", uuid.New())
    
    	pndMock := &mocks.NetworkDomain{}
    	pndMock.On("ID").Return(did)
    
    	// options to apply to cmp.Equal
    	opts := []cmp.Option{
    		// create custom comparer that simply checks if the device ID's are the
    		// same.
    		cmp.Comparer(func(x, y device.Device) bool {
    			return x.ID() == y.ID()
    		}),
    		// compare option to treat slices of length zero as equal
    		cmpopts.EquateEmpty(),
    		// sort the slices based on the ID
    		cmpopts.SortSlices(func(x, y device.Device) bool {
    			return x.ID().ID() < y.ID().ID()
    		}),
    	}
    
    	type fields struct {
    		genericStore *genericStore
    	}
    	type args struct {
    		sid uuid.UUID
    	}
    	tests := []struct {
    		name    string
    		fields  fields
    		args    args
    		want    []device.Device
    		wantErr bool
    	}{
    		{
    			name: "return devices associated with SBI",
    			fields: fields{
    				&genericStore{
    					Store: map[uuid.UUID]store.Storable{
    						associatedDevices[0].ID(): associatedDevices[0],
    						associatedDevices[1].ID(): associatedDevices[1],
    						associatedDevices[2].ID(): associatedDevices[2],
    					},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			args: args{
    				sid: defaultSbiID,
    			},
    			want:    associatedDevices,
    			wantErr: false,
    		},
    		{
    			name: "non associated devices should not be part of the returned slice",
    			fields: fields{
    				&genericStore{
    					Store: map[uuid.UUID]store.Storable{
    						associatedDevices[0].ID(): associatedDevices[0],
    						associatedDevices[1].ID(): associatedDevices[1],
    						associatedDevices[2].ID(): associatedDevices[2],
    						nonAssociatedDevice.ID():  nonAssociatedDevice,
    					},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			args: args{
    				sid: defaultSbiID,
    			},
    			want:    append(associatedDevices, nonAssociatedDevice),
    			wantErr: false,
    		},
    		{
    			name: "empty",
    			fields: fields{
    				&genericStore{
    					Store:     map[uuid.UUID]store.Storable{},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			want:    []device.Device{},
    			wantErr: false,
    		},
    		{
    			name: "no device associated",
    			fields: fields{
    				&genericStore{
    					Store: map[uuid.UUID]store.Storable{
    						nonAssociatedDevice.ID(): nonAssociatedDevice,
    					},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			want:    []device.Device{},
    			wantErr: false,
    		},
    		{
    			name: "fails wrong type",
    			fields: fields{
    				&genericStore{
    					Store: map[uuid.UUID]store.Storable{
    						defaultPndID: pndMock,
    					},
    					storeLock: sync.RWMutex{},
    				},
    			},
    			args:    args{sid: defaultSbiID},
    			want:    nil,
    			wantErr: true,
    		},
    	}
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			s := DeviceStore{genericStore: tt.fields.genericStore}
    
    			got, err := s.GetDevicesAssociatedWithSbi(tt.args.sid)
    			if (err != nil) != tt.wantErr {
    				t.Errorf("GetDevicesAssociatedWithSbi() error = %v, wantErr %v", err, tt.wantErr)
    				return
    			}
    			if !cmp.Equal(got, tt.want, opts...) {
    				t.Errorf("GetDevicesAssociatedWithSbi() got = %v, want %v", got, tt.want)
    			}
    		})
    	}
    }