github.com/m3db/m3@v1.5.0/src/metrics/rules/view/changes/mapping_test.go (about)

     1  // Copyright (c) 2018 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 changes
    22  
    23  import (
    24  	"encoding/json"
    25  	"sort"
    26  	"testing"
    27  
    28  	"github.com/m3db/m3/src/metrics/policy"
    29  	"github.com/m3db/m3/src/metrics/rules/view"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestSortMappingRuleChanges(t *testing.T) {
    35  	ruleChanges := []MappingRuleChange{
    36  		{
    37  			Op:     DeleteOp,
    38  			RuleID: ptr("rrID5"),
    39  		},
    40  		{
    41  			Op:     DeleteOp,
    42  			RuleID: ptr("rrID4"),
    43  		},
    44  		{
    45  			Op:     ChangeOp,
    46  			RuleID: ptr("rrID1"),
    47  			RuleData: &view.MappingRule{
    48  				Name: "change3",
    49  			},
    50  		},
    51  		{
    52  			Op: AddOp,
    53  			RuleData: &view.MappingRule{
    54  				Name: "Add2",
    55  			},
    56  		},
    57  		{
    58  			Op:     ChangeOp,
    59  			RuleID: ptr("rrID2"),
    60  			RuleData: &view.MappingRule{
    61  				Name: "change1",
    62  			},
    63  		},
    64  		{
    65  			Op:     DeleteOp,
    66  			RuleID: ptr("rrID5"),
    67  		},
    68  		{
    69  			Op:     DeleteOp,
    70  			RuleID: ptr("rrID4"),
    71  		},
    72  		{
    73  			Op:     ChangeOp,
    74  			RuleID: ptr("rrID3"),
    75  			RuleData: &view.MappingRule{
    76  				Name: "change2",
    77  			},
    78  		},
    79  		{
    80  			Op: AddOp,
    81  			RuleData: &view.MappingRule{
    82  				Name: "Add1",
    83  			},
    84  		},
    85  		{
    86  			Op:     ChangeOp,
    87  			RuleID: ptr("rrID2"),
    88  			RuleData: &view.MappingRule{
    89  				Name: "change1",
    90  			},
    91  		},
    92  	}
    93  	expected := []MappingRuleChange{
    94  		{
    95  			Op: AddOp,
    96  			RuleData: &view.MappingRule{
    97  				Name: "Add1",
    98  			},
    99  		},
   100  		{
   101  			Op: AddOp,
   102  			RuleData: &view.MappingRule{
   103  				Name: "Add2",
   104  			},
   105  		},
   106  		{
   107  			Op:     ChangeOp,
   108  			RuleID: ptr("rrID2"),
   109  			RuleData: &view.MappingRule{
   110  				Name: "change1",
   111  			},
   112  		},
   113  		{
   114  			Op:     ChangeOp,
   115  			RuleID: ptr("rrID2"),
   116  			RuleData: &view.MappingRule{
   117  				Name: "change1",
   118  			},
   119  		},
   120  		{
   121  			Op:     ChangeOp,
   122  			RuleID: ptr("rrID3"),
   123  			RuleData: &view.MappingRule{
   124  				Name: "change2",
   125  			},
   126  		},
   127  		{
   128  			Op:     ChangeOp,
   129  			RuleID: ptr("rrID1"),
   130  			RuleData: &view.MappingRule{
   131  				Name: "change3",
   132  			},
   133  		},
   134  		{
   135  			Op:     DeleteOp,
   136  			RuleID: ptr("rrID4"),
   137  		},
   138  		{
   139  			Op:     DeleteOp,
   140  			RuleID: ptr("rrID4"),
   141  		},
   142  		{
   143  			Op:     DeleteOp,
   144  			RuleID: ptr("rrID5"),
   145  		},
   146  		{
   147  			Op:     DeleteOp,
   148  			RuleID: ptr("rrID5"),
   149  		},
   150  	}
   151  
   152  	sort.Sort(mappingRuleChangesByOpAscNameAscIDAsc(ruleChanges))
   153  	require.Equal(t, expected, ruleChanges)
   154  }
   155  
   156  func TestMappingRuleJSONDeserialization(t *testing.T) {
   157  	jsonInput := []byte(`{
   158  		"op": "change",
   159  		"ruleData": {
   160  			"id": "validID",
   161  			"name": "valid rule name",
   162  			"cutoverMillis": 61522,
   163  			"filter": "name:servers.* service:servers",
   164  			"storagePolicies": [
   165  					"10s:2d",
   166  					"1m:40d"
   167  			],
   168  			"lastUpdatedBy": "valid user name",
   169  			"lastUpdatedAtMillis": 1522
   170  		},
   171  		"ruleID": "validID"
   172  	}`)
   173  
   174  	var ruleChange MappingRuleChange
   175  	err := json.Unmarshal(jsonInput, &ruleChange)
   176  	require.NoError(t, err)
   177  
   178  	expected := MappingRuleChange{
   179  		Op: "change",
   180  		RuleData: &view.MappingRule{
   181  			ID:            "validID",
   182  			Name:          "valid rule name",
   183  			CutoverMillis: 61522,
   184  			Filter:        "name:servers.* service:servers",
   185  			StoragePolicies: policy.StoragePolicies{
   186  				policy.MustParseStoragePolicy("10s:2d"),
   187  				policy.MustParseStoragePolicy("1m:40d"),
   188  			},
   189  			LastUpdatedBy:       "valid user name",
   190  			LastUpdatedAtMillis: 1522,
   191  		},
   192  		RuleID: ptr("validID"),
   193  	}
   194  	require.Equal(t, expected, ruleChange)
   195  }