Skip to content

Commit

Permalink
feat: add in ktlintcheck (mParticle#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchuangatmp authored and willpassidomo committed Mar 2, 2022
1 parent a8df4fe commit f75c5b6
Show file tree
Hide file tree
Showing 51 changed files with 840 additions and 767 deletions.
6 changes: 3 additions & 3 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
3. PR branch prefix should follow this format: https:/mParticle/mparticle-workflows/blob/stable/.github/workflows/pr-branch-check-name.yml

## Summary
{provide a thorough description of the changes}
- {provide a thorough description of the changes}

## Testing Plan
{explain how this has been tested, and what additional testing should be done}
- {explain how this has been tested, and what additional testing should be done}

## Master Issue
Closes https://go.mparticle.com/work/REPLACEME
- Closes https://go.mparticle.com/work/REPLACEME
24 changes: 24 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ jobs:
with:
name: "lint-results"
path: ./**/build/reports/**
kotlin-lint-checks:
name: "Kotlin Lint Checks"
timeout-minutes: 15
runs-on: macos-11
steps:
- name: "Checkout Branch"
uses: actions/checkout@v2
with:
repository: ${{github.event.pull_request.head.repo.full_name}}
ref: ${{github.head_ref}}
submodules: recursive
- name: "Install JDK 11"
uses: actions/setup-java@v2
with:
distribution: "zulu"
java-version: "11"
- name: "Run Android Core SDK Kotlin Lint"
run: ./gradlew ktlintCheck
- name: "Archive Kotlin Lint Test Results"
uses: actions/upload-artifact@v2
if: ${{ always() }}
with:
name: "kotlin-lint-results"
path: ./**/build/reports/**
automerge:
name: "Rebase dependabot PRs"
runs-on: ubuntu-18.04
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import com.mparticle.testutils.BaseCleanStartedEachTest
import org.junit.Test
import kotlin.test.assertNotEquals

class UploadEventKotlinTest: BaseCleanStartedEachTest() {
class UploadEventKotlinTest : BaseCleanStartedEachTest() {
@Test
fun testMPEventUploadBypass() {
val event = MPEvent.Builder("Should Not Upload")
.shouldUploadEvent(false)
.build()
.shouldUploadEvent(false)
.build()
val event2 = MPEvent.Builder("Should Upload 1")
.shouldUploadEvent(true)
.build()
.shouldUploadEvent(true)
.build()
val event3 = MPEvent.Builder("Should Upload 2")
.build()
.build()
MParticle.getInstance()?.logEvent(event)
MParticle.getInstance()?.logEvent(event2)
MParticle.getInstance()?.logEvent(event3)
Expand All @@ -33,9 +33,10 @@ class UploadEventKotlinTest: BaseCleanStartedEachTest() {
// messages are received in an upload message and fail if that, or any previous message, contains the
// "Should Not Upload" message
var numUploadedEvents = 0
mServer.waitForVerify(Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
(0 until messagesArray.length())
mServer.waitForVerify(
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
(0 until messagesArray.length())
.any {
val eventMessageName = messagesArray.getJSONObject(it).optString("n")
assertNotEquals("Should Not Upload", eventMessageName)
Expand All @@ -44,8 +45,9 @@ class UploadEventKotlinTest: BaseCleanStartedEachTest() {
}
numUploadedEvents == 2
}
} ?: false
})
} ?: false
}
)
}

@Test
Expand All @@ -66,9 +68,10 @@ class UploadEventKotlinTest: BaseCleanStartedEachTest() {
// messages are received in an upload message and fail if that, or any previous message, contains the
// "Should Not Upload" message
var numUploadedEvents = 0
mServer.waitForVerify(Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
(0 until messagesArray.length())
mServer.waitForVerify(
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
(0 until messagesArray.length())
.any {
val eventMessageName = messagesArray.getJSONObject(it).optString("n")
assertNotEquals("Should Not Upload", eventMessageName)
Expand All @@ -77,26 +80,27 @@ class UploadEventKotlinTest: BaseCleanStartedEachTest() {
}
numUploadedEvents == 3
}
} ?: false
})
} ?: false
}
)
}

@Test
fun testCommerceEventUploadBypass() {
val product = Product.Builder("Should Not Upload", "sku1", 100.00)
.build()
.build()
val event = CommerceEvent.Builder(Product.ADD_TO_CART, product)
.shouldUploadEvent(false)
.build()
.shouldUploadEvent(false)
.build()
var product2 = Product.Builder("Should Upload 1", "sku2", 100.00)
.build()
.build()
val event2 = CommerceEvent.Builder(Product.ADD_TO_CART, product2)
.shouldUploadEvent(true)
.build()
.shouldUploadEvent(true)
.build()
var product3 = Product.Builder("Should Upload 2", "sku3", 100.00)
.build()
.build()
val event3 = CommerceEvent.Builder(Product.ADD_TO_CART, product3)
.build()
.build()
MParticle.getInstance()?.logEvent(event)
MParticle.getInstance()?.logEvent(event2)
MParticle.getInstance()?.logEvent(event3)
Expand All @@ -112,9 +116,10 @@ class UploadEventKotlinTest: BaseCleanStartedEachTest() {
// messages are received in an upload message and fail if that, or any previous message, contains the
// "Should Not Upload" message
var numUploadedEvents = 0
mServer.waitForVerify(Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
(0 until messagesArray.length())
mServer.waitForVerify(
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
(0 until messagesArray.length())
.any {
val eventProductName = messagesArray.getJSONObject(it).optJSONObject("pd")?.optJSONArray("pl")?.optJSONObject(0)?.optString("nm")
assertNotEquals("Should Not Upload", eventProductName)
Expand All @@ -123,7 +128,8 @@ class UploadEventKotlinTest: BaseCleanStartedEachTest() {
}
numUploadedEvents == 2
}
} ?: false
})
} ?: false
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ import com.mparticle.internal.Constants
import com.mparticle.internal.UploadHandler
import com.mparticle.networking.Matcher
import com.mparticle.testutils.BaseCleanStartedEachTest
import org.junit.After
import org.junit.Test
import java.util.*
import java.util.TreeSet
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class UploadMessageKotlinTest: BaseCleanStartedEachTest() {
class UploadMessageKotlinTest : BaseCleanStartedEachTest() {
val uploadInterval = 100

override fun transformMParticleOptions(builder: MParticleOptions.Builder): MParticleOptions.Builder {
//set a really long upload interval so the results don't get confused by multiple loops
// set a really long upload interval so the results don't get confused by multiple loops
return builder.uploadInterval(uploadInterval)
}

@Test
fun testUploadGreaterThan100() {
try {
//track these, since the way we have our HandlerThreads are setup, we get messages carried over from the last test
// track these, since the way we have our HandlerThreads are setup, we get messages carried over from the last test
val preexistingUploadQueueMessages = AccessUtils.getUploadHandlerMessageQueue()
val numMessages = 10

Expand All @@ -33,32 +32,34 @@ class UploadMessageKotlinTest: BaseCleanStartedEachTest() {
val messages = TreeSet<Int>()
MParticle.getInstance()?.upload()
var uploadsCount = 0
mServer.waitForVerify(Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
for (i in 0 until messagesArray.length()) {
messagesArray.getJSONObject(i).optString("n").toIntOrNull()?.let { messageId -> messages.add(messageId) }
mServer.waitForVerify(
Matcher(mServer.Endpoints().eventsUrl).bodyMatch {
it.optJSONArray("msgs")?.let { messagesArray ->
for (i in 0 until messagesArray.length()) {
messagesArray.getJSONObject(i).optString("n").toIntOrNull()?.let { messageId -> messages.add(messageId) }
}
assertTrue(messagesArray.length() <= Constants.getMaxMessagePerBatch())
}
assertTrue(messagesArray.length() <= Constants.getMaxMessagePerBatch())
uploadsCount++
messages.size >= numMessages
}
uploadsCount++
messages.size >= numMessages
})
)
assertTrue(uploadsCount > 4)

//Check the UploadHandler Message queue,
// Check the UploadHandler Message queue,
val uploadQueueMessages = AccessUtils.getUploadHandlerMessageQueue()
.filter { it.what == UploadHandler.UPLOAD_MESSAGES }
.filter { it.what == UploadHandler.UPLOAD_MESSAGES }

//make sure there is just 1 upload message in the queue (for the upload loop)
// make sure there is just 1 upload message in the queue (for the upload loop)
assertEquals(1, uploadQueueMessages.size, "current: " + uploadQueueMessages.joinToString() + "\npre:" + preexistingUploadQueueMessages.joinToString())
//make sure it has a valid time (less then or equal to the UploadInterval, but not more than 3 seconds less)
// make sure it has a valid time (less then or equal to the UploadInterval, but not more than 3 seconds less)
val uploadQueueMessageScheduledTime = uploadQueueMessages[0].`when`
val uploadIntervalMillis = uploadInterval * 1000
//make sure this is the actual upload message
// make sure this is the actual upload message
assertTrue(uploadQueueMessageScheduledTime < (SystemClock.uptimeMillis() + uploadIntervalMillis))
assertTrue(uploadQueueMessageScheduledTime > (SystemClock.uptimeMillis() + (uploadInterval - 100)))
} finally {
Constants.setMaxMessagePerBatch(100)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ package com.mparticle.internal

import android.app.Activity
import android.app.Application
import android.os.AsyncTask
import android.os.Bundle
import android.os.Looper
import androidx.test.platform.app.InstrumentationRegistry
import com.mparticle.OrchestratorOnly
import com.mparticle.testutils.BaseAbstractTest
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test


class ApplicationContextWrapperITest {


/**
* This test specifically addresses a problem we had where a callback was being registered on a
* new thread which was not initialized as a looper. This setup (not extending BaseAbstractTest)
Expand All @@ -29,16 +24,15 @@ class ApplicationContextWrapperITest {
assertNull(Looper.myLooper())
try {
applicationContextWrapper.registerActivityLifecycleCallbacks(MockCallbacks())
//call it again to make sure we are not initializing the Looper twice
// call it again to make sure we are not initializing the Looper twice
applicationContextWrapper.registerActivityLifecycleCallbacks(MockCallbacks())
} catch (e: Exception) {
exception = e
}
assertNull(exception);
assertNull(exception)
}


class MockCallbacks: Application.ActivityLifecycleCallbacks {
class MockCallbacks : Application.ActivityLifecycleCallbacks {
override fun onActivityPaused(p0: Activity) {}
override fun onActivityResumed(p0: Activity) {}
override fun onActivityStarted(p0: Activity) {}
Expand All @@ -47,4 +41,4 @@ class ApplicationContextWrapperITest {
override fun onActivityStopped(p0: Activity) {}
override fun onActivityCreated(p0: Activity, savedInstanceState: Bundle?) {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import com.mparticle.MParticleOptions
import com.mparticle.networking.Matcher
import com.mparticle.testutils.BaseCleanStartedEachTest
import org.json.JSONObject
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class BatchSessionInfoTest: BaseCleanStartedEachTest() {
class BatchSessionInfoTest : BaseCleanStartedEachTest() {

override fun useInMemoryDatabase() = true

Expand All @@ -21,11 +20,13 @@ class BatchSessionInfoTest: BaseCleanStartedEachTest() {
}

override fun beforeSetup() {
//the condition described in the test only happened when `sessionHistory` is false,
//so set config to return `sessionHistory` == false
mServer.setupConfigResponse(JSONObject()
// the condition described in the test only happened when `sessionHistory` is false,
// so set config to return `sessionHistory` == false
mServer.setupConfigResponse(
JSONObject()
.put(ConfigManager.KEY_INCLUDE_SESSION_HISTORY, false)
.toString())
.toString()
)
}

/**
Expand All @@ -51,34 +52,35 @@ class BatchSessionInfoTest: BaseCleanStartedEachTest() {
assertNotEquals(sessionId, appStateManager.session.mSessionID)
}




var messageCount = 0
MParticle.getInstance()?.upload()
mServer.waitForVerify(Matcher(mServer.Endpoints().getEventsUrl()).bodyMatch {
val version = it.getJSONObject("ai").getString(Constants.MessageKey.INSTALL_REFERRER)
if (it.has("msgs")) {
var messages = it.getJSONArray("msgs")
for (i in 0 until messages.length()) {
if (messages.getJSONObject(i).getString("dt") == "e") {
messageCount++
mServer.waitForVerify(
Matcher(mServer.Endpoints().getEventsUrl()).bodyMatch {
val version = it.getJSONObject("ai").getString(Constants.MessageKey.INSTALL_REFERRER)
if (it.has("msgs")) {
var messages = it.getJSONArray("msgs")
for (i in 0 until messages.length()) {
if (messages.getJSONObject(i).getString("dt") == "e") {
messageCount++
}
}
}
assertEquals("111", version)
MParticle.getInstance()?.upload()
messageCount >= 150
}
assertEquals("111", version)
MParticle.getInstance()?.upload()
messageCount >= 150
})
)

MParticle.getInstance()?.apply {
logEvent(MPEvent.Builder("1").build())
upload()
}
mServer.waitForVerify(Matcher(mServer.Endpoints().getEventsUrl()).bodyMatch {
val version = it.getJSONObject("ai").getString(Constants.MessageKey.INSTALL_REFERRER)
assertEquals("222", version)
true
})
mServer.waitForVerify(
Matcher(mServer.Endpoints().getEventsUrl()).bodyMatch {
val version = it.getJSONObject("ai").getString(Constants.MessageKey.INSTALL_REFERRER)
assertEquals("222", version)
true
}
)
}
}
Loading

0 comments on commit f75c5b6

Please sign in to comment.