Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/src/main/java/io/constructor/core/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Constants {
const val CLIENT = "c"
const val EVENT = "tr"
const val AUTOCOMPLETE_KEY = "autocomplete_key"
const val NUM_RESULTS = "num_results_"
const val GROUP_ID = "group[group_id]"
const val GROUP_DISPLAY_NAME = "group[display_name]"
}
Expand All @@ -30,6 +31,7 @@ class Constants {
const val EVENT_SEARCH = "search"
const val EVENT_SESSION_START = "session_start"
const val SEARCH_SUGGESTIONS = "Search Suggestions"
const val PRODUCTS = "Products"
const val EVENT_SEARCH_RESULTS = "search-results"
const val EVENT_INPUT_FOCUS = "focus"
}
Expand Down
19 changes: 11 additions & 8 deletions library/src/main/java/io/constructor/core/ConstructorIo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.Context
import io.constructor.BuildConfig
import io.constructor.data.DataManager
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.memory.TestCellMemoryHolder
import io.constructor.data.memory.ConfigMemoryHolder
import io.constructor.data.model.SuggestionViewModel
import io.constructor.injection.component.AppComponent
import io.constructor.injection.component.DaggerAppComponent
Expand All @@ -26,7 +26,7 @@ object ConstructorIo {

private lateinit var dataManager: DataManager
private lateinit var preferenceHelper: PreferencesHelper
private lateinit var testCellMemoryHolder: TestCellMemoryHolder
private lateinit var configMemoryHolder: ConfigMemoryHolder
private lateinit var context: Context
private var disposable = CompositeDisposable()

Expand All @@ -49,15 +49,18 @@ object ConstructorIo {
}))
}

fun init(context: Context?, apiKey: String, defaultItemSection: String = BuildConfig.AUTOCOMPLETE_SECTION) {
fun init(context: Context?, apiKey: String, autocompleteResultCount: Map<String, Int> = mapOf(Constants.QueryValues.SEARCH_SUGGESTIONS to 10,
Constants.QueryValues.PRODUCTS to 0), defaultItemSection: String = BuildConfig.AUTOCOMPLETE_SECTION) {
if (context == null) {
throw IllegalStateException("context is null, please init library using ConstructorIo.with(context)")
}
this.context = context.applicationContext
dataManager = component.dataManager()
preferenceHelper = component.preferenceHelper()
testCellMemoryHolder = component.testCellMemoryHolder()
configMemoryHolder = component.configMemoryHolder()
configMemoryHolder.autocompleteResultCount = autocompleteResultCount
preferenceHelper.token = apiKey

preferenceHelper.defaultItemSection = defaultItemSection
if (preferenceHelper.id.isBlank()) {
preferenceHelper.id = UUID.randomUUID().toString()
Expand All @@ -69,21 +72,21 @@ object ConstructorIo {
fun getClientId() = preferenceHelper.id

fun setTestCellValues(pair1: Pair<String, String>, pair2: Pair<String, String>? = null, pair3: Pair<String, String>? = null) {
testCellMemoryHolder.testCellParams = listOf(pair1, pair2, pair3)
configMemoryHolder.testCellParams = listOf(pair1, pair2, pair3)
}

fun clearTestCellValues() {
testCellMemoryHolder.testCellParams = emptyList()
configMemoryHolder.testCellParams = emptyList()
}

internal fun testInit(context: Context?, apiKey: String, dataManager: DataManager, preferenceHelper: PreferencesHelper, testCellMemoryHolder: TestCellMemoryHolder) {
internal fun testInit(context: Context?, apiKey: String, dataManager: DataManager, preferenceHelper: PreferencesHelper, configMemoryHolder: ConfigMemoryHolder) {
if (context == null) {
throw IllegalStateException("Context is null, please init library using ConstructorIo.with(context)")
}
this.context = context.applicationContext
this.dataManager = dataManager
this.preferenceHelper = preferenceHelper
this.testCellMemoryHolder = testCellMemoryHolder
this.configMemoryHolder = configMemoryHolder
preferenceHelper.token = apiKey
if (preferenceHelper.id.isBlank()) {
preferenceHelper.id = UUID.randomUUID().toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import android.content.Context
import io.constructor.BuildConfig
import io.constructor.core.Constants
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.memory.TestCellMemoryHolder
import io.constructor.data.memory.ConfigMemoryHolder
import okhttp3.Interceptor
import okhttp3.Response


class TokenInterceptor(val context: Context, private val preferencesHelper: PreferencesHelper, private val testCellMemoryHolder: TestCellMemoryHolder) : Interceptor {
class TokenInterceptor(val context: Context, private val preferencesHelper: PreferencesHelper, private val configMemoryHolder: ConfigMemoryHolder) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
Expand All @@ -18,11 +18,14 @@ class TokenInterceptor(val context: Context, private val preferencesHelper: Pref
.addQueryParameter(Constants.QueryConstants.IDENTITY, preferencesHelper.id)
.addQueryParameter(Constants.QueryConstants.TIMESTAMP, System.currentTimeMillis().toString())
.addQueryParameter(Constants.QueryConstants.CLIENT, BuildConfig.CLIENT_VERSION)
testCellMemoryHolder.testCellParams.forEach {
configMemoryHolder.testCellParams.forEach {
it?.let {
builder.addQueryParameter(it.first, it.second)
}
}
configMemoryHolder.autocompleteResultCount?.entries?.forEach {
builder.addQueryParameter(Constants.QueryConstants.NUM_RESULTS+it.key, it.value.toString())
}
val url = builder.build()
request = request.newBuilder().url(url).build()
return chain.proceed(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.constructor.util.base64Decode
import io.constructor.util.base64Encode
import javax.inject.Inject

class TestCellMemoryHolder @Inject constructor() {
class ConfigMemoryHolder @Inject constructor() {

private var backingString = ""

Expand Down Expand Up @@ -32,4 +32,5 @@ class TestCellMemoryHolder @Inject constructor() {
backingString = combined
}

var autocompleteResultCount: Map<String, Int>? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import dagger.Component
import io.constructor.data.DataManager
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.memory.TestCellMemoryHolder
import io.constructor.data.memory.ConfigMemoryHolder
import io.constructor.data.remote.ConstructorApi
import io.constructor.injection.ApplicationContext
import io.constructor.injection.module.AppModule
Expand All @@ -21,7 +21,7 @@ interface AppComponent {

fun preferenceHelper(): PreferencesHelper

fun testCellMemoryHolder(): TestCellMemoryHolder
fun configMemoryHolder(): ConfigMemoryHolder

fun constructorApi(): ConstructorApi
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import dagger.Module
import dagger.Provides
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.memory.TestCellMemoryHolder
import io.constructor.data.memory.ConfigMemoryHolder
import io.constructor.injection.ApplicationContext
import javax.inject.Singleton

Expand All @@ -24,7 +24,7 @@ class AppModule(private val application: Context) {

@Provides
@Singleton
internal fun provideTestCellHolder(): TestCellMemoryHolder {
return TestCellMemoryHolder()
internal fun provideConfigMemoryHolder(): ConfigMemoryHolder {
return ConfigMemoryHolder()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dagger.Provides
import io.constructor.BuildConfig
import io.constructor.data.interceptor.TokenInterceptor
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.memory.TestCellMemoryHolder
import io.constructor.data.memory.ConfigMemoryHolder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
Expand Down Expand Up @@ -50,7 +50,7 @@ class NetworkModule(private val context: Context) {

@Provides
@Singleton
internal fun provideTokenInterceptor(prefHelper: PreferencesHelper, testCellMemoryHolder: TestCellMemoryHolder): TokenInterceptor = TokenInterceptor(context, prefHelper, testCellMemoryHolder)
internal fun provideTokenInterceptor(prefHelper: PreferencesHelper, configMemoryHolder: ConfigMemoryHolder): TokenInterceptor = TokenInterceptor(context, prefHelper, configMemoryHolder)

@Provides
@Singleton
Expand Down
15 changes: 8 additions & 7 deletions library/src/test/java/io/constructor/core/ConstructorIoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.constructor.BuildConfig
import io.constructor.data.DataManager
import io.constructor.data.interceptor.TokenInterceptor
import io.constructor.data.local.PreferencesHelper
import io.constructor.data.memory.TestCellMemoryHolder
import io.constructor.data.memory.ConfigMemoryHolder
import io.constructor.data.model.Group
import io.constructor.data.model.SuggestionViewModel
import io.constructor.util.RxSchedulersOverrideRule
Expand All @@ -31,7 +31,7 @@ class ConstructorIoTest {

private val ctx = mockk<Context>()
private val pref = mockk<PreferencesHelper>()
private val testCellMemoryHolder = mockk<TestCellMemoryHolder>()
private val configMemoryHolder = mockk<ConfigMemoryHolder>()
private val data = mockk<DataManager>()
private var constructorIo = ConstructorIo
private val sampleMillis = "1520000000000"
Expand All @@ -44,7 +44,7 @@ class ConstructorIoTest {
every { pref.id } returns "1"
every { pref.getSessionId() } returns 1
every { pref.getSessionId(any()) } returns 1
constructorIo.testInit(ctx, "dummyKey", data, pref, testCellMemoryHolder)
constructorIo.testInit(ctx, "dummyKey", data, pref, configMemoryHolder)
}

@After
Expand Down Expand Up @@ -210,13 +210,14 @@ class ConstructorIoTest {
val mockServer = MockWebServer()
every { pref.token } returns "123"
every { pref.id } returns "1"
every { testCellMemoryHolder.testCellParams = any() } just Runs
every { testCellMemoryHolder.testCellParams } returns listOf("ef-1" to "2", "ef-3" to "4")
every { configMemoryHolder.testCellParams = any() } just Runs
every { configMemoryHolder.autocompleteResultCount } returns mapOf(Constants.QueryValues.SEARCH_SUGGESTIONS to 10, Constants.QueryValues.PRODUCTS to 0)
every { configMemoryHolder.testCellParams } returns listOf("ef-1" to "2", "ef-3" to "4")
constructorIo.setTestCellValues("1" to "2", "3" to "4")
verify(exactly = 1) { testCellMemoryHolder.testCellParams = any() }
verify(exactly = 1) { configMemoryHolder.testCellParams = any() }
mockServer.start()
mockServer.enqueue(MockResponse())
var client = OkHttpClient.Builder().addInterceptor(TokenInterceptor(ctx, pref, testCellMemoryHolder)).build()
var client = OkHttpClient.Builder().addInterceptor(TokenInterceptor(ctx, pref, configMemoryHolder)).build()
client.newCall(Request.Builder().url(mockServer.url("/")).build()).execute()
var recordedRequest = mockServer.takeRequest()
assert(recordedRequest.path.contains("ef-1=2"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class TestCellMemoryHolderTest {
class ConfigMemoryHolderTest {

private lateinit var testCellMemoryHolder: TestCellMemoryHolder
private lateinit var configMemoryHolder: ConfigMemoryHolder

@Before
fun setUp() {
testCellMemoryHolder = TestCellMemoryHolder()
configMemoryHolder = ConfigMemoryHolder()
}

@Test
fun verifyTestCellsWrittenAndEncoded() {
testCellMemoryHolder.testCellParams = listOf("1" to "2", "3" to "4")
val params = testCellMemoryHolder.testCellParams
configMemoryHolder.testCellParams = listOf("1" to "2", "3" to "4")
val params = configMemoryHolder.testCellParams
assert(params[0]!!.first == "ef-1" && params[0]!!.second == "2")
assert(params[1]!!.first == "ef-3" && params[1]!!.second == "4")
}
Expand Down