|
| 1 | +// Copyright 2022, Optimizely, Inc. and contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.optimizely.ab.android.odp |
| 16 | + |
| 17 | +import android.content.Context |
| 18 | +import androidx.test.core.app.ApplicationProvider |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import androidx.work.Data |
| 21 | +import androidx.work.ListenableWorker |
| 22 | +import androidx.work.testing.TestWorkerBuilder |
| 23 | +import org.hamcrest.Matchers.`is` |
| 24 | +import org.junit.Assert.assertEquals |
| 25 | +import org.junit.Assert.assertNotNull |
| 26 | +import org.junit.Assert.assertNull |
| 27 | +import org.junit.Assert.assertThat |
| 28 | +import org.junit.Assert.assertTrue |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | +import org.mockito.Mockito.`when` |
| 32 | +import org.mockito.Mockito.mock |
| 33 | +import org.mockito.Mockito.verify |
| 34 | +import java.util.concurrent.Executor |
| 35 | +import java.util.concurrent.Executors |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests [ODPEventWorker] |
| 39 | + */ |
| 40 | +@RunWith(AndroidJUnit4::class) |
| 41 | +class ODPEventWorkerTest { |
| 42 | + private val apiKey = "valid-key" |
| 43 | + private var apiEndpoint = "http://valid-endpoint" |
| 44 | + private val smallBody = "valid-payload" |
| 45 | + private val largeBody = makeLongString(20000) |
| 46 | + |
| 47 | + private var context: Context = ApplicationProvider.getApplicationContext() |
| 48 | + private var executor: Executor = Executors.newSingleThreadExecutor() |
| 49 | + private var worker: ODPEventWorker = makeODPEventWorker(apiEndpoint, apiKey, smallBody) |
| 50 | + |
| 51 | + @Test |
| 52 | + fun dispatch_success() { |
| 53 | + val eventClient = mock(ODPEventClient::class.java) |
| 54 | + `when`(eventClient.dispatch(apiKey, apiEndpoint, smallBody)).thenReturn(true) |
| 55 | + worker.eventClient = eventClient |
| 56 | + |
| 57 | + val result = worker.doWork() |
| 58 | + assertThat(result, `is`(ListenableWorker.Result.success())) |
| 59 | + verify(eventClient).dispatch(apiKey, apiEndpoint, smallBody) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun dispatch_failure() { |
| 64 | + val eventClient = mock(ODPEventClient::class.java) |
| 65 | + `when`(eventClient.dispatch(apiKey, apiEndpoint, smallBody)).thenReturn(false) |
| 66 | + worker.eventClient = eventClient |
| 67 | + |
| 68 | + val result = worker.doWork() |
| 69 | + assertThat(result, `is`(ListenableWorker.Result.failure())) |
| 70 | + verify(eventClient).dispatch(apiKey, apiEndpoint, smallBody) |
| 71 | + } |
| 72 | + |
| 73 | + // Data |
| 74 | + |
| 75 | + @Test |
| 76 | + fun getDataForSmallEvent() { |
| 77 | + val data: Data = ODPEventWorker.getData(apiKey, apiEndpoint, smallBody) |
| 78 | + |
| 79 | + assertEquals(worker.getApiEndpointFromInputData(data), apiEndpoint) |
| 80 | + assertEquals(worker.getApiKeyFromInputData(data), apiKey) |
| 81 | + assertEquals(worker.getEventBodyFromInputData(data), smallBody) |
| 82 | + assertNotNull(data.getString("body")) |
| 83 | + assertNull(data.getString("bodyCompressed")) |
| 84 | + assertTrue(smallBody.length < 1000) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun getDataForCompressedEvent() { |
| 89 | + val data: Data = ODPEventWorker.getData(apiKey, apiEndpoint, largeBody) |
| 90 | + |
| 91 | + assertEquals(worker.getApiEndpointFromInputData(data), apiEndpoint) |
| 92 | + assertEquals(worker.getApiKeyFromInputData(data), apiKey) |
| 93 | + assertEquals(worker.getEventBodyFromInputData(data), largeBody) |
| 94 | + assertNull(data.getString("body")) |
| 95 | + assertNotNull(data.getString("bodyCompressed")) |
| 96 | + assertTrue(largeBody.length > 15000) |
| 97 | + } |
| 98 | + |
| 99 | + // Helpers |
| 100 | + |
| 101 | + private fun makeODPEventWorker(apiEndpoint: String, apiKey: String, payload: String): ODPEventWorker { |
| 102 | + val inputData: Data = ODPEventWorker.getData(apiKey, apiEndpoint, payload) |
| 103 | + return TestWorkerBuilder.from<ODPEventWorker>(context, ODPEventWorker::class.java, executor) |
| 104 | + .setInputData(inputData) |
| 105 | + .build() |
| 106 | + } |
| 107 | + |
| 108 | + private fun makeLongString(maxSize: Int): String { |
| 109 | + val builder = StringBuilder() |
| 110 | + val str = "random-string" |
| 111 | + val repeat = (maxSize / str.length) + 1 |
| 112 | + for (i in 1..repeat) builder.append(str) |
| 113 | + return builder.toString() |
| 114 | + } |
| 115 | +} |
0 commit comments