|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2016-2017, Optimizely and contributors |
| 4 | + * |
| 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 |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 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 | + */ |
| 17 | +package com.optimizely.ab |
| 18 | + |
| 19 | +import com.optimizely.ab.bucketing.UserProfileService |
| 20 | +import com.optimizely.ab.config.ProjectConfig |
| 21 | +import com.optimizely.ab.config.ProjectConfigTestUtils |
| 22 | +import com.optimizely.ab.config.parser.ConfigParseException |
| 23 | +import com.optimizely.ab.error.ErrorHandler |
| 24 | +import com.optimizely.ab.error.NoOpErrorHandler |
| 25 | +import com.optimizely.ab.event.EventHandler |
| 26 | +import com.optimizely.ab.event.internal.BuildVersionInfo |
| 27 | +import com.optimizely.ab.event.internal.EventBuilder |
| 28 | +import com.optimizely.ab.event.internal.payload.EventBatch.ClientEngine |
| 29 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings |
| 30 | +import org.junit.BeforeClass |
| 31 | +import org.junit.Rule |
| 32 | +import org.junit.Test |
| 33 | +import org.junit.rules.ExpectedException |
| 34 | +import org.mockito.Mock |
| 35 | +import org.mockito.junit.MockitoJUnit |
| 36 | +import org.mockito.junit.MockitoRule |
| 37 | + |
| 38 | +import com.optimizely.ab.config.ProjectConfigTestUtils.noAudienceProjectConfigJsonV2 |
| 39 | +import com.optimizely.ab.config.ProjectConfigTestUtils.noAudienceProjectConfigV2 |
| 40 | +import com.optimizely.ab.config.ProjectConfigTestUtils.validConfigJsonV2 |
| 41 | +import com.optimizely.ab.config.ProjectConfigTestUtils.validConfigJsonV3 |
| 42 | +import com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV2 |
| 43 | +import com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV3 |
| 44 | +import com.optimizely.ab.event.internal.payload.EventBatch |
| 45 | +import org.hamcrest.CoreMatchers.instanceOf |
| 46 | +import org.hamcrest.CoreMatchers.`is` |
| 47 | +import org.junit.Assert.assertThat |
| 48 | +import org.mockito.Mockito.mock |
| 49 | + |
| 50 | +/** |
| 51 | + * Tests for [Optimizely.builder]. |
| 52 | + */ |
| 53 | +@SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 54 | +class OptimizelyBuilderTest { |
| 55 | + |
| 56 | + @Rule |
| 57 | + var thrown = ExpectedException.none() |
| 58 | + |
| 59 | + @Rule |
| 60 | + var rule = MockitoJUnit.rule() |
| 61 | + |
| 62 | + @Mock private val mockEventHandler: EventHandler? = null |
| 63 | + |
| 64 | + @Mock private val mockErrorHandler: ErrorHandler? = null |
| 65 | + |
| 66 | + @Test |
| 67 | + @Throws(Exception::class) |
| 68 | + fun withEventHandler() { |
| 69 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 70 | + .build() |
| 71 | + |
| 72 | + assertThat(optimizelyClient.eventHandler, `is`(mockEventHandler)) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @Throws(Exception::class) |
| 77 | + fun projectConfigV2() { |
| 78 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 79 | + .build() |
| 80 | + |
| 81 | + ProjectConfigTestUtils.verifyProjectConfig(optimizelyClient.projectConfig, validProjectConfigV2()) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @Throws(Exception::class) |
| 86 | + fun projectConfigV3() { |
| 87 | + val optimizelyClient = Optimizely.builder(validConfigJsonV3(), mockEventHandler!!) |
| 88 | + .build() |
| 89 | + |
| 90 | + ProjectConfigTestUtils.verifyProjectConfig(optimizelyClient.projectConfig, validProjectConfigV3()) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @Throws(Exception::class) |
| 95 | + fun withErrorHandler() { |
| 96 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 97 | + .withErrorHandler(mockErrorHandler!!) |
| 98 | + .build() |
| 99 | + |
| 100 | + assertThat(optimizelyClient.errorHandler, `is`<ErrorHandler>(mockErrorHandler)) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @Throws(Exception::class) |
| 105 | + fun withDefaultErrorHandler() { |
| 106 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 107 | + .build() |
| 108 | + |
| 109 | + assertThat(optimizelyClient.errorHandler, instanceOf<Any>(NoOpErrorHandler::class.java)) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @Throws(Exception::class) |
| 114 | + fun withUserProfileService() { |
| 115 | + val userProfileService = mock<UserProfileService>(UserProfileService::class.java) |
| 116 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 117 | + .withUserProfileService(userProfileService) |
| 118 | + .build() |
| 119 | + |
| 120 | + assertThat<UserProfileService>(optimizelyClient.userProfileService, `is`(userProfileService)) |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @Throws(Exception::class) |
| 125 | + fun withDefaultClientEngine() { |
| 126 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 127 | + .build() |
| 128 | + |
| 129 | + assertThat<EventBatch.ClientEngine>((optimizelyClient.eventBuilder as EventBuilder).clientEngine, `is`(ClientEngine.JAVA_SDK)) |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + @Throws(Exception::class) |
| 134 | + fun withAndroidSDKClientEngine() { |
| 135 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 136 | + .withClientEngine(ClientEngine.ANDROID_SDK) |
| 137 | + .build() |
| 138 | + |
| 139 | + assertThat<EventBatch.ClientEngine>((optimizelyClient.eventBuilder as EventBuilder).clientEngine, `is`(ClientEngine.ANDROID_SDK)) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + @Throws(Exception::class) |
| 144 | + fun withAndroidTVSDKClientEngine() { |
| 145 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 146 | + .withClientEngine(ClientEngine.ANDROID_TV_SDK) |
| 147 | + .build() |
| 148 | + |
| 149 | + assertThat<EventBatch.ClientEngine>((optimizelyClient.eventBuilder as EventBuilder).clientEngine, `is`(ClientEngine.ANDROID_TV_SDK)) |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + @Throws(Exception::class) |
| 154 | + fun withDefaultClientVersion() { |
| 155 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 156 | + .build() |
| 157 | + |
| 158 | + assertThat((optimizelyClient.eventBuilder as EventBuilder).clientVersion, `is`(BuildVersionInfo.VERSION)) |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + @Throws(Exception::class) |
| 163 | + fun withCustomClientVersion() { |
| 164 | + val optimizelyClient = Optimizely.builder(validConfigJsonV2(), mockEventHandler!!) |
| 165 | + .withClientVersion("0.0.0") |
| 166 | + .build() |
| 167 | + |
| 168 | + assertThat((optimizelyClient.eventBuilder as EventBuilder).clientVersion, `is`("0.0.0")) |
| 169 | + } |
| 170 | + |
| 171 | + @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Testing nullness contract violation") |
| 172 | + @Test |
| 173 | + @Throws(Exception::class) |
| 174 | + fun builderThrowsConfigParseExceptionForNullDatafile() { |
| 175 | + thrown.expect(ConfigParseException::class.java) |
| 176 | + Optimizely.builder(null!!, mockEventHandler!!).build() |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + @Throws(Exception::class) |
| 181 | + fun builderThrowsConfigParseExceptionForEmptyDatafile() { |
| 182 | + thrown.expect(ConfigParseException::class.java) |
| 183 | + Optimizely.builder("", mockEventHandler!!).build() |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + @Throws(Exception::class) |
| 188 | + fun builderThrowsConfigParseExceptionForInvalidDatafile() { |
| 189 | + thrown.expect(ConfigParseException::class.java) |
| 190 | + Optimizely.builder("{invalidDatafile}", mockEventHandler!!).build() |
| 191 | + } |
| 192 | + |
| 193 | + companion object { |
| 194 | + |
| 195 | + |
| 196 | + private val userId = "userId" |
| 197 | + private var noAudienceDatafile: String? = null |
| 198 | + private var noAudienceProjectConfig: ProjectConfig? = null |
| 199 | + |
| 200 | + @BeforeClass |
| 201 | + @Throws(Exception::class) |
| 202 | + fun setUp() { |
| 203 | + noAudienceDatafile = noAudienceProjectConfigJsonV2() |
| 204 | + noAudienceProjectConfig = noAudienceProjectConfigV2() |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments