2 ==================================================================================
3 Copyright (c) 2021 Nokia
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================================
25 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
26 "github.com/stretchr/testify/assert"
29 type testData struct {
35 func TestDefaultUseCase(t *testing.T) {
37 fmt.Println("##################### TestRetransmissionChecker #####################")
39 var retransCtrl DuplicateCtrl
40 restSubdId := "898dfkjashntgkjasgho4"
41 var name string = "yolo"
42 var someVal int64 = 98765
43 data := testData{Name: &name, Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, SomeVal: &someVal}
47 md5sum, err := CalculateRequestMd5sum(data)
50 duplicate := retransCtrl.IsDuplicateToOngoingTransaction(restSubdId, md5sum)
52 assert.Equal(t, 1, len(retransCtrl.ongoingRequestMap))
53 assert.Equal(t, false, duplicate)
55 retransCtrl.TransactionComplete(md5sum)
57 assert.Equal(t, 0, len(retransCtrl.ongoingRequestMap))
60 func TestDuplicate(t *testing.T) {
62 fmt.Println("##################### TestDuplicate #####################")
64 var retransCtrl DuplicateCtrl
65 restSubdId := "898dfkjashntgkjasgho4"
66 var name string = "yolo"
67 var someVal int64 = 98765
68 data := testData{Name: &name, Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, SomeVal: &someVal}
70 var name2 string = "yolo"
71 var someVal2 int64 = 98765
73 data2 := new(testData)
75 data2.SomeVal = &someVal2
76 datax := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
81 md5sum, err := CalculateRequestMd5sum(data)
84 duplicate := retransCtrl.IsDuplicateToOngoingTransaction(restSubdId, md5sum)
85 assert.Equal(t, 1, len(retransCtrl.ongoingRequestMap))
86 assert.Equal(t, false, duplicate)
88 md5sum, err = CalculateRequestMd5sum(data2)
91 duplicate = retransCtrl.IsDuplicateToOngoingTransaction(restSubdId, md5sum)
92 assert.Equal(t, 1, len(retransCtrl.ongoingRequestMap))
93 assert.Equal(t, true, duplicate)
95 retransCtrl.TransactionComplete(md5sum)
97 assert.Equal(t, 0, len(retransCtrl.ongoingRequestMap))
100 func TestNoneDuplicate(t *testing.T) {
102 fmt.Println("##################### TestNoneDuplicate #####################")
104 var retransCtrl DuplicateCtrl
105 restSubdId := "898dfkjashntgkjasgho4"
106 var name string = "yolo"
107 var someVal int64 = 98765
108 data := testData{Name: &name, Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, SomeVal: &someVal}
110 var name2 string = "yolo"
111 var someVal2 int64 = 98765
113 data2 := new(testData)
115 data2.SomeVal = &someVal2
116 datax := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 1} // One byte changed
121 md5sum, err := CalculateRequestMd5sum(data)
124 duplicate := retransCtrl.IsDuplicateToOngoingTransaction(restSubdId, md5sum)
125 assert.Equal(t, 1, len(retransCtrl.ongoingRequestMap))
126 assert.Equal(t, false, duplicate)
128 md5sum2, err := CalculateRequestMd5sum(data2)
131 duplicate2 := retransCtrl.IsDuplicateToOngoingTransaction(restSubdId, md5sum2)
132 assert.Equal(t, 2, len(retransCtrl.ongoingRequestMap))
133 assert.Equal(t, false, duplicate2)
135 retransCtrl.TransactionComplete(md5sum)
136 retransCtrl.TransactionComplete(md5sum2)
138 assert.Equal(t, 0, len(retransCtrl.ongoingRequestMap))
141 func TestEncodingError(t *testing.T) {
143 fmt.Println("##################### TestEncodingError #####################")
145 var retransCtrl DuplicateCtrl
150 _, err := CalculateRequestMd5sum(data)
151 assert.NotEqual(t, err, nil)
154 func TestRemovalError(t *testing.T) {
156 fmt.Println("##################### TestRemovalError #####################")
158 var retransCtrl DuplicateCtrl
159 restSubdId := "898dfkjashntgkjasgho4"
164 md5sum, err := CalculateRequestMd5sum(data)
167 duplicate := retransCtrl.IsDuplicateToOngoingTransaction(restSubdId, md5sum)
168 assert.Equal(t, 1, len(retransCtrl.ongoingRequestMap))
169 assert.Equal(t, false, duplicate)
171 err = retransCtrl.TransactionComplete(md5sum)
174 err = retransCtrl.TransactionComplete(md5sum)
175 assert.NotEmpty(t, err)
178 func TestXappRestReqDuplicate(t *testing.T) {
180 fmt.Println("##################### TestXappRestReqDuplicate #####################")
182 var retransCtrl DuplicateCtrl
184 msg1 := new(models.SubscriptionParams)
185 msg2 := new(models.SubscriptionParams)
189 md5sum, err := CalculateRequestMd5sum(msg1)
192 duplicate := retransCtrl.IsDuplicateToOngoingTransaction("foobar", md5sum)
193 assert.Equal(t, 1, len(retransCtrl.ongoingRequestMap))
194 assert.Equal(t, false, duplicate)
196 md5sum2, err := CalculateRequestMd5sum(msg2)
199 duplicate = retransCtrl.IsDuplicateToOngoingTransaction("foobar", md5sum2)
200 assert.Equal(t, 1, len(retransCtrl.ongoingRequestMap))
201 assert.Equal(t, true, duplicate)
203 retransCtrl.TransactionComplete(md5sum)
205 assert.Equal(t, 0, len(retransCtrl.ongoingRequestMap))