github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/routing/config_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package routing
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"gopkg.in/yaml.v2"
    28  )
    29  
    30  // TestBasicConfig test the basic yaml file conversion
    31  func TestBasicConfig(t *testing.T) {
    32  	yamlFile := `
    33  routers:
    34  # routers structure looks like:
    35  # - [scope]
    36  #    [namePrefix_1]: connectorName
    37  #    [namePrefix_2]: connectorName
    38  - production:
    39      default: cassandra
    40      serviceA: cassandra
    41  - development:
    42      default: cassandra
    43      serviceB: cassandra
    44  - ebook:
    45      '*': ebook
    46      apple.*: ebook
    47      default: ebook
    48      ebook-store: ebook
    49  - default:
    50      default: dosa
    51  `
    52  	testCfg := &Config{}
    53  	err := yaml.Unmarshal([]byte(yamlFile), testCfg)
    54  	assert.NoError(t, err)
    55  	assert.Len(t, testCfg.Routers, 9)
    56  	rs := Routers{
    57  		buildRouter("production", "serviceA", "cassandra"),
    58  		buildRouter("production", "default", "cassandra"),
    59  		buildRouter("ebook", "ebook-store", "ebook"),
    60  		buildRouter("ebook", "default", "ebook"),
    61  		buildRouter("ebook", "apple.*", "ebook"),
    62  		buildRouter("ebook", "*", "ebook"),
    63  		buildRouter("development", "serviceB", "cassandra"),
    64  		buildRouter("development", "default", "cassandra"),
    65  		buildRouter("default", "default", "dosa"),
    66  	}
    67  	assert.Equal(t, testCfg.Routers, rs)
    68  	err = yaml.Unmarshal([]byte(`bad yaml file`), testCfg)
    69  	assert.Error(t, err)
    70  }
    71  
    72  func buildRouter(scope, namePrefix, connector string) *Rule {
    73  	rc, _ := NewRule(scope, namePrefix, connector)
    74  	return rc
    75  }
    76  
    77  func TestRouter(t *testing.T) {
    78  	yamlFile := `
    79  routers:
    80  # routers structure looks like:
    81  # - [scope]
    82  #    [namePrefix_1]: connectorName
    83  #    [namePrefix_2]: connectorName
    84  - production:
    85      default: cassandra
    86      serviceA: cassandra
    87  - development:
    88      default: cassandra
    89      serviceB: cassandra
    90  - ebook:
    91      '*': ebook
    92      apple.*: ebook
    93      default: ebook
    94      ebook-store: ebook
    95  - default:
    96      default: dosa
    97  `
    98  	testCfg := &Config{}
    99  	err := yaml.Unmarshal([]byte(yamlFile), testCfg)
   100  	assert.NoError(t, err)
   101  
   102  	cfg := testCfg.findDefaultRouter()
   103  	assert.Equal(t, cfg.Scope, "default")
   104  
   105  	cfg = testCfg.FindRouter("production", "serviceA")
   106  	assert.Equal(t, cfg, buildRouter("production", "serviceA", "cassandra"))
   107  
   108  	cfg = testCfg.FindRouter("ebook", "apple.k")
   109  	assert.Equal(t, cfg, buildRouter("ebook", "apple.*", "ebook"))
   110  
   111  	cfg = testCfg.FindRouter("ebook", "d.k")
   112  	assert.Equal(t, cfg, buildRouter("ebook", "*", "ebook"))
   113  
   114  	cfg = testCfg.FindRouter("a", "d.k")
   115  	assert.Equal(t, cfg, buildRouter("default", "default", "dosa"))
   116  }