Debian Codegraph @

paho/extensions/topicaliases/topicliases_test.go

raw
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package topicaliases
17
18 import (
19 "testing"
20
21 "github.com/eclipse/paho.golang/paho"
22 "github.com/stretchr/testify/assert"
23 )
24
25 func TestTAHandler_PublishHook(t *testing.T) {
26 tests := []struct {
27 name string
28 aliasMax uint16
29 aliases []string
30 p *paho.Publish
31 expected *paho.Publish
32 expectedAliases []string
33 }{
34 {
35 name: "has alias",
36 aliasMax: 4,
37 aliases: []string{"", "", "", "test", ""},
38 p: &paho.Publish{
39 Topic: "test",
40 },
41 expected: &paho.Publish{
42 Properties: &paho.PublishProperties{
43 TopicAlias: paho.Uint16(3),
44 },
45 },
46 expectedAliases: []string{"", "", "", "test", ""},
47 },
48 {
49 name: "reset alias",
50 aliasMax: 4,
51 aliases: []string{"", "", "", "test", ""},
52 p: &paho.Publish{
53 Topic: "test2",
54 Properties: &paho.PublishProperties{
55 TopicAlias: paho.Uint16(3),
56 },
57 },
58 expected: &paho.Publish{
59 Topic: "test2",
60 Properties: &paho.PublishProperties{
61 TopicAlias: paho.Uint16(3),
62 },
63 },
64 expectedAliases: []string{"", "", "", "test2", ""},
65 },
66 {
67 name: "no alias",
68 aliasMax: 4,
69 aliases: []string{"", "", "", "", ""},
70 p: &paho.Publish{
71 Topic: "test",
72 },
73 expected: &paho.Publish{
74 Topic: "test",
75 Properties: &paho.PublishProperties{
76 TopicAlias: paho.Uint16(1),
77 },
78 },
79 expectedAliases: []string{"", "test", "", "", ""},
80 },
81 {
82 name: "properties no alias",
83 aliasMax: 4,
84 aliases: []string{"", "", "", "", ""},
85 p: &paho.Publish{
86 Topic: "test",
87 Properties: &paho.PublishProperties{},
88 },
89 expected: &paho.Publish{
90 Topic: "test",
91 Properties: &paho.PublishProperties{
92 TopicAlias: paho.Uint16(1),
93 },
94 },
95 expectedAliases: []string{"", "test", "", "", ""},
96 },
97 {
98 name: "no alias free",
99 aliasMax: 1,
100 aliases: []string{"", "full"},
101 p: &paho.Publish{
102 Topic: "test",
103 },
104 expected: &paho.Publish{
105 Topic: "test",
106 },
107 expectedAliases: []string{"", "full"},
108 },
109 }
110 for _, tt := range tests {
111 t.Run(tt.name, func(t *testing.T) {
112 ta := &TAHandler{
113 aliasMax: tt.aliasMax,
114 aliases: tt.aliases,
115 }
116 ta.PublishHook(tt.p)
117 assert.Equal(t, tt.expected, tt.p)
118 assert.Equal(t, tt.expectedAliases, ta.aliases)
119 })
120 }
121 }