github.com/CyCoreSystems/ari@v4.8.4+incompatible/key_test.go (about) 1 package ari 2 3 import "testing" 4 5 func TestKeyMatch(t *testing.T) { 6 7 // two empty keys should match 8 ok := NewKey("", "").Match(NewKey("", "")) 9 if !ok { 10 t.Errorf("Two empty keys should match") 11 } 12 13 ok = AppKey("app").Match(NewKey("", "")) 14 if !ok { 15 t.Errorf("App key should match any subkey") 16 } 17 18 ok = AppKey("app").Match(AppKey("app2")) 19 if ok { 20 t.Errorf("Two separate app keys should not match") 21 } 22 23 ok = NodeKey("app", "node").Match(NewKey("", "")) 24 if !ok { 25 t.Errorf("Node key should match any subkey") 26 } 27 28 ok = NewKey("application", "id1").Match(NewKey("application", "id1")) 29 if !ok { 30 t.Errorf("Application/id1 should match") 31 } 32 33 ok = NewKey("application", "").Match(NewKey("application", "id1")) 34 if !ok { 35 t.Errorf("Application/* should match application/id") 36 } 37 38 ok = NewKey("channel", "id1").Match(NewKey("channel", "id2")) 39 if ok { 40 t.Errorf("Differing IDs should not match") 41 } 42 } 43 44 func TestKeysFilter(t *testing.T) { 45 46 keys := Keys{ 47 NewKey(ApplicationKey, "app1"), 48 NewKey(ChannelKey, "ch1"), 49 NewKey(BridgeKey, "br1"), 50 } 51 52 newKeys := keys.Filter(KindKey(ApplicationKey)) 53 54 if len(newKeys) != 1 { 55 t.Errorf("Expected filters keys by app to be of length 1, got %d", len(newKeys)) 56 } else { 57 if newKeys[0].Kind != ApplicationKey && newKeys[0].ID != "app1" { 58 t.Errorf("Unexpected first index %v", newKeys[0]) 59 } 60 } 61 62 newKeys = keys.Without(KindKey(ApplicationKey)) 63 64 if len(newKeys) != 2 { 65 t.Errorf("Expected without keys by app to be of length 2, got %d", len(newKeys)) 66 } else { 67 if newKeys[0].Kind != ChannelKey && newKeys[0].ID != "ch1" { 68 t.Errorf("Unexpected first index %v", newKeys[0]) 69 } 70 if newKeys[1].Kind != BridgeKey && newKeys[1].ID != "br1" { 71 t.Errorf("Unexpected second index %v", newKeys[1]) 72 } 73 } 74 75 newKeys = keys.Filter(KindKey(ChannelKey), KindKey(BridgeKey)) 76 77 if len(newKeys) != 2 { 78 t.Errorf("Expected without keys by app to be of length 2, got %d", len(newKeys)) 79 } else { 80 if newKeys[0].Kind != ChannelKey && newKeys[0].ID != "ch1" { 81 t.Errorf("Unexpected first index %v", newKeys[0]) 82 } 83 if newKeys[1].Kind != BridgeKey && newKeys[1].ID != "br1" { 84 t.Errorf("Unexpected second index %v", newKeys[1]) 85 } 86 } 87 88 newKeys = keys.Filter(KindKey(ChannelKey), KindKey(BridgeKey)).Without(MatchFunc(func(k *Key) bool { 89 return k.ID == "br1" 90 })) 91 92 if len(newKeys) != 1 { 93 t.Errorf("Expected without keys by app to be of length 2, got %d", len(newKeys)) 94 } else { 95 if newKeys[0].Kind != ChannelKey && newKeys[0].ID != "ch1" { 96 t.Errorf("Unexpected first index %v", newKeys[0]) 97 } 98 } 99 }