1
1
/****************************************************************************
2
- * Copyright 2019-2020, Optimizely, Inc. and contributors *
2
+ * Copyright 2019-2020,2022 Optimizely, Inc. and contributors *
3
3
* *
4
4
* Licensed under the Apache License, Version 2.0 (the "License"); *
5
5
* you may not use this file except in compliance with the License. *
6
6
* You may obtain a copy of the License at *
7
7
* *
8
- * http ://www.apache.org/licenses/LICENSE-2.0 *
8
+ * https ://www.apache.org/licenses/LICENSE-2.0 *
9
9
* *
10
10
* Unless required by applicable law or agreed to in writing, software *
11
11
* distributed under the License is distributed on an "AS IS" BASIS, *
@@ -23,6 +23,7 @@ import (
23
23
"strconv"
24
24
"sync"
25
25
"testing"
26
+ "time"
26
27
27
28
"github.com/optimizely/go-sdk/pkg/config"
28
29
"github.com/optimizely/go-sdk/pkg/decide"
@@ -31,6 +32,9 @@ import (
31
32
"github.com/optimizely/go-sdk/pkg/event"
32
33
"github.com/optimizely/go-sdk/pkg/logging"
33
34
"github.com/optimizely/go-sdk/pkg/notification"
35
+ "github.com/optimizely/go-sdk/pkg/odp"
36
+ "github.com/optimizely/go-sdk/pkg/odp/segment"
37
+ pkgOdpUtils "github.com/optimizely/go-sdk/pkg/odp/utils"
34
38
"github.com/optimizely/go-sdk/pkg/utils"
35
39
36
40
"github.com/stretchr/testify/assert"
@@ -172,6 +176,84 @@ func (TestConfig) GetClientVersion() string {
172
176
return "1.0.0"
173
177
}
174
178
179
+ type MockODPManager struct {
180
+ odp.Manager
181
+ mock.Mock
182
+ }
183
+
184
+ func (m * MockODPManager ) FetchQualifiedSegments (userID string , options []segment.OptimizelySegmentOption ) (segments []string , err error ) {
185
+ args := m .Called (userID , options )
186
+ if segArray , ok := args .Get (0 ).([]string ); ok {
187
+ segments = segArray
188
+ }
189
+ return segments , args .Error (1 )
190
+ }
191
+
192
+ func (m * MockODPManager ) IdentifyUser (userID string ) {
193
+ m .Called (userID )
194
+ }
195
+
196
+ func (m * MockODPManager ) SendOdpEvent (eventType , action string , identifiers map [string ]string , data map [string ]interface {}) bool {
197
+ return m .Called (eventType , action , identifiers , data ).Get (0 ).(bool )
198
+ }
199
+
200
+ func (m * MockODPManager ) Update (apiKey , apiHost string , segmentsToCheck []string ) {
201
+ m .Called (apiKey , apiHost , segmentsToCheck )
202
+ }
203
+
204
+ func TestSendODPEventWhenODPDisabled (t * testing.T ) {
205
+ factory := OptimizelyFactory {SDKKey : "1212" }
206
+ var segmentsCacheSize = 1
207
+ var segmentsCacheTimeout = 1 * time .Second
208
+ var disableOdp = true
209
+ optimizelyClient , err := factory .Client (WithSegmentsCacheSize (segmentsCacheSize ), WithSegmentsCacheTimeout (segmentsCacheTimeout ), WithOdpDisabled (disableOdp ))
210
+ assert .NoError (t , err )
211
+ success := optimizelyClient .SendOdpEvent ("123" , "456" , map [string ]string {
212
+ "abc" : "123" ,
213
+ }, map [string ]interface {}{
214
+ "abc" : nil ,
215
+ "idempotence_id" : 234 ,
216
+ "data_source_type" : "456" ,
217
+ "data_source" : true ,
218
+ "data_source_version" : 6.78 ,
219
+ })
220
+ assert .False (t , success )
221
+ }
222
+
223
+ func TestSendODPEventEmptyType (t * testing.T ) {
224
+ eventType := pkgOdpUtils .OdpEventType
225
+ action := "456"
226
+ identifiers := map [string ]string {
227
+ "abc" : "123" ,
228
+ }
229
+ data := map [string ]interface {}{
230
+ "abc" : nil ,
231
+ "idempotence_id" : 234 ,
232
+ "data_source_type" : "456" ,
233
+ "data_source" : true ,
234
+ "data_source_version" : 6.78 ,
235
+ }
236
+ mockOdpManager := & MockODPManager {}
237
+ mockOdpManager .On ("SendOdpEvent" , eventType , action , identifiers , data ).Return (true )
238
+ optimizelyClient := OptimizelyClient {
239
+ OdpManager : mockOdpManager ,
240
+ }
241
+ success := optimizelyClient .SendOdpEvent ("" , action , identifiers , data )
242
+ assert .True (t , success )
243
+ mockOdpManager .AssertExpectations (t )
244
+ }
245
+
246
+ func TestSendODPEvent (t * testing.T ) {
247
+ mockOdpManager := & MockODPManager {}
248
+ mockOdpManager .On ("SendOdpEvent" , "123" , "" , mock .Anything , mock .Anything ).Return (true )
249
+ optimizelyClient := OptimizelyClient {
250
+ OdpManager : mockOdpManager ,
251
+ }
252
+ success := optimizelyClient .SendOdpEvent ("123" , "" , nil , nil )
253
+ assert .True (t , success )
254
+ mockOdpManager .AssertExpectations (t )
255
+ }
256
+
175
257
func TestTrack (t * testing.T ) {
176
258
mockProcessor := new (MockProcessor )
177
259
mockDecisionService := new (MockDecisionService )
@@ -2459,6 +2541,35 @@ func TestCreateUserContext(t *testing.T) {
2459
2541
assert .Equal (t , userAttributes , optimizelyUserContext .GetUserAttributes ())
2460
2542
}
2461
2543
2544
+ func TestCreateUserContextIdentifiesUser (t * testing.T ) {
2545
+ userID := "1212121"
2546
+ userAttributes := map [string ]interface {}{"key" : 1212 }
2547
+ factory := OptimizelyFactory {SDKKey : "1212" }
2548
+ mockOdpManager := & MockODPManager {}
2549
+ mockOdpManager .On ("IdentifyUser" , userID )
2550
+ client , err := factory .Client (WithOdpManager (mockOdpManager ))
2551
+ assert .NoError (t , err )
2552
+ optimizelyUserContext := client .CreateUserContext (userID , userAttributes )
2553
+ mockOdpManager .AssertExpectations (t )
2554
+ assert .NotNil (t , optimizelyUserContext .optimizely .OdpManager )
2555
+ assert .Equal (t , userID , optimizelyUserContext .GetUserID ())
2556
+ assert .Equal (t , userAttributes , optimizelyUserContext .GetUserAttributes ())
2557
+ }
2558
+
2559
+ func TestCreateUserContextWithNilODPManager (t * testing.T ) {
2560
+ userID := "1212121"
2561
+ userAttributes := map [string ]interface {}{"key" : 1212 }
2562
+ factory := OptimizelyFactory {SDKKey : "1212" }
2563
+ mockOdpManager := & MockODPManager {}
2564
+ client , err := factory .Client (WithOdpManager (mockOdpManager ))
2565
+ assert .NoError (t , err )
2566
+ client .OdpManager = nil
2567
+ optimizelyUserContext := client .CreateUserContext (userID , userAttributes )
2568
+ mockOdpManager .AssertNotCalled (t , "IdentifyUser" , userID )
2569
+ assert .Equal (t , userID , optimizelyUserContext .GetUserID ())
2570
+ assert .Equal (t , userAttributes , optimizelyUserContext .GetUserAttributes ())
2571
+ }
2572
+
2462
2573
func TestChangingAttributesDoesntEffectUserContext (t * testing.T ) {
2463
2574
client := OptimizelyClient {}
2464
2575
userID := "1"
0 commit comments