Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web Actions Tab: Add null handling to Application/Network Interceptors
* Application or Network Interceptors are appearing null when they lack a qualified class name * New `ClassNameFormatter` that handles formatting a valid class name
- Loading branch information
Showing
5 changed files
with
115 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
misk/src/main/kotlin/misk/web/formatter/ClassNameFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package misk.web.formatter | ||
|
||
import com.google.common.base.CharMatcher | ||
import kotlin.reflect.KClass | ||
|
||
class ClassNameFormatter { | ||
fun <T : Any> format(kclass: KClass<T>): String { | ||
return when (kclass.qualifiedName) { | ||
null -> kclass.toString().split("class ").last() | ||
else -> kclass.qualifiedName.toString() | ||
} | ||
} | ||
|
||
fun isValid(className: String): Boolean { | ||
require(className.isNotBlank()) { | ||
"Formatted class name can not be blank" | ||
} | ||
require(className != "null") { | ||
"Formatted class name can not be null" | ||
} | ||
require( | ||
CharMatcher.inRange('A', 'Z').or(CharMatcher.inRange('a', 'z')).matchesAnyOf(className)) { | ||
"Formatted class name must include alpha characters [A-Z, a-z]" | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
misk/src/test/kotlin/misk/web/formatter/ClassNameFormatterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package misk.web.formatter | ||
|
||
import misk.inject.KAbstractModule | ||
import misk.testing.MiskTest | ||
import misk.testing.MiskTestModule | ||
import misk.web.NetworkChain | ||
import misk.web.NetworkInterceptor | ||
import misk.web.Response | ||
import org.junit.jupiter.api.Test | ||
import javax.inject.Inject | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
|
||
@MiskTest(startService = false) | ||
class ClassNameFormatterTest { | ||
@MiskTestModule | ||
val module = ClassNameFormatterTestModule() | ||
|
||
@Inject lateinit var classNameFormatter: ClassNameFormatter | ||
|
||
@Test fun validQualifiedName() { | ||
val formatted = ClassNameFormatter().format(ValidQualifiedNameClass::class) | ||
classNameFormatter.isValid(formatted) | ||
assertEquals(formatted, ValidQualifiedNameClass::class.qualifiedName.toString()) | ||
} | ||
|
||
@Test fun missingQualifiedName() { | ||
val noQualifiedNameClass = NoQualifiedNameFactory().create() | ||
assertNull(noQualifiedNameClass::class.qualifiedName) | ||
|
||
val formatted = ClassNameFormatter().format(noQualifiedNameClass::class) | ||
classNameFormatter.isValid(formatted) | ||
assertEquals(formatted, noQualifiedNameClass::class.toString().split("class ").last()) | ||
} | ||
} | ||
|
||
class ValidQualifiedNameClass {} | ||
|
||
class NoQualifiedNameFactory { | ||
fun create(): NetworkInterceptor { | ||
return NoQualifiedNameClass | ||
} | ||
|
||
private companion object { | ||
val NoQualifiedNameClass = object : NetworkInterceptor { | ||
override fun intercept(chain: NetworkChain): Response<*> { | ||
return Response("False") | ||
} | ||
} | ||
} | ||
} | ||
|
||
class ClassNameFormatterTestModule : KAbstractModule() { | ||
override fun configure() { | ||
bind<ClassNameFormatter>().toInstance(ClassNameFormatter()) | ||
} | ||
} |