github.com/uber/kraken@v0.1.4/lib/store/base/testutils_test.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package base
    15  
    16  import (
    17  	"regexp"
    18  	"strings"
    19  
    20  	"github.com/uber/kraken/lib/store/metadata"
    21  )
    22  
    23  // Mock metadata
    24  func init() {
    25  	metadata.Register(regexp.MustCompile("_mocksuffix_\\w+"), &mockMetadataFactory{})
    26  	metadata.Register(regexp.MustCompile("_mockmovable"), &mockMetadataFactoryMovable{})
    27  }
    28  
    29  type mockMetadataFactory struct{}
    30  
    31  func (f mockMetadataFactory) Create(suffix string) metadata.Metadata {
    32  	if strings.HasSuffix(suffix, getMockMetadataOne().GetSuffix()) {
    33  		return getMockMetadataOne()
    34  	}
    35  	if strings.HasSuffix(suffix, getMockMetadataTwo().GetSuffix()) {
    36  		return getMockMetadataTwo()
    37  	}
    38  	return nil
    39  }
    40  
    41  type mockMetadata struct {
    42  	randomSuffix string
    43  	content      []byte
    44  }
    45  
    46  func getMockMetadataOne() *mockMetadata {
    47  	return &mockMetadata{
    48  		randomSuffix: "_mocksuffix_one",
    49  	}
    50  }
    51  
    52  func getMockMetadataTwo() *mockMetadata {
    53  	return &mockMetadata{
    54  		randomSuffix: "_mocksuffix_two",
    55  	}
    56  }
    57  
    58  func (m *mockMetadata) GetSuffix() string {
    59  	return m.randomSuffix
    60  }
    61  
    62  func (m *mockMetadata) Movable() bool {
    63  	return false
    64  }
    65  
    66  func (m *mockMetadata) Serialize() ([]byte, error) {
    67  	return m.content, nil
    68  }
    69  
    70  func (m *mockMetadata) Deserialize(b []byte) error {
    71  	m.content = b
    72  	return nil
    73  }
    74  
    75  type mockMetadataFactoryMovable struct{}
    76  
    77  func (f mockMetadataFactoryMovable) Create(suffix string) metadata.Metadata {
    78  	return getMockMetadataMovable()
    79  }
    80  
    81  type mockMetadataMovable struct {
    82  	randomSuffix string
    83  	content      []byte
    84  }
    85  
    86  func getMockMetadataMovable() *mockMetadataMovable {
    87  	return &mockMetadataMovable{
    88  		randomSuffix: "_mockmovable",
    89  	}
    90  }
    91  
    92  func (m *mockMetadataMovable) GetSuffix() string {
    93  	return m.randomSuffix
    94  }
    95  
    96  func (m *mockMetadataMovable) Movable() bool {
    97  	return true
    98  }
    99  
   100  func (m *mockMetadataMovable) Serialize() ([]byte, error) {
   101  	return m.content, nil
   102  }
   103  
   104  func (m *mockMetadataMovable) Deserialize(b []byte) error {
   105  	m.content = b
   106  	return nil
   107  }