Skip to content

Commit

Permalink
Update to kotlin 1.4 (#844)
Browse files Browse the repository at this point in the history
* Update to kotlin 1.4

* Update tests for coverage

* Update to corountines 1.3.9

* Update GraphQLGradlePluginAbstractIT.kt

Co-authored-by: Shane Myrick <[email protected]>
  • Loading branch information
smyrick and Shane Myrick authored Aug 26, 2020
1 parent bfd02e6 commit 050549a
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/client/gradle-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import com.expediagroup.graphql.plugin.gradle.graphql

plugins {
application
id("org.jetbrains.kotlin.jvm") version "1.3.72"
id("org.jetbrains.kotlin.jvm") version "1.4.0"
id("com.expediagroup.graphql") version "3.1.0"
}

Expand Down
4 changes: 2 additions & 2 deletions examples/client/maven-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<!-- those properties are set by gradle -->
<graphql-kotlin.version>3.1.0</graphql-kotlin.version>
<!-- lib versions -->
<kotlin.version>1.3.72</kotlin.version>
<kotlin-coroutines.version>1.3.6</kotlin-coroutines.version>
<kotlin.version>1.4.0</kotlin.version>
<kotlin-coroutines.version>1.3.9</kotlin-coroutines.version>
<ktor.version>1.3.1</ktor.version>
</properties>

Expand Down
4 changes: 2 additions & 2 deletions examples/client/server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("org.jetbrains.kotlin.jvm") version "1.3.72"
id("org.jetbrains.kotlin.plugin.spring") version "1.3.72"
id("org.jetbrains.kotlin.jvm") version "1.4.0"
id("org.jetbrains.kotlin.plugin.spring") version "1.4.0"
id("org.springframework.boot") version "2.2.7.RELEASE"
}

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ org.gradle.jvmargs=-Xmx2g -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError

# dependencies
kotlinJvmVersion = 1.8
kotlinVersion = 1.3.72
kotlinCoroutinesVersion = 1.3.7
kotlinVersion = 1.4.0
kotlinCoroutinesVersion = 1.3.9

classGraphVersion = 4.8.87
graphQLJavaVersion = 15.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.full.createType
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.jvmErasure

private val primitiveArrayTypes = mapOf(
Expand All @@ -36,7 +35,7 @@ private val primitiveArrayTypes = mapOf(

internal fun KType.getKClass() = this.jvmErasure

internal fun KType.getJavaClass() = this.javaType as Class<*>
internal fun KType.getJavaClass(): Class<*> = this.getKClass().java

internal fun KType.isSubclassOf(kClass: KClass<*>) = this.getKClass().isSubclassOf(kClass)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class KotlinDirectiveWiringFactoryTest {
}

@Test
fun `verify exception is thrown if no coordinates or code registry is specified for the field`() {
fun `verify exception is thrown if no coordinates are specified for the field`() {
val myTestField = GraphQLFieldDefinition.newFieldDefinition()
.name("MyField")
.type { context, visitor -> context.thisNode().accept(context, visitor) }
Expand All @@ -220,7 +220,21 @@ class KotlinDirectiveWiringFactoryTest {
.build()

assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
SimpleWiringFactory().onWire(graphQLSchemaElement = myTestField, coordinates = null, codeRegistry = null)
SimpleWiringFactory().onWire(graphQLSchemaElement = myTestField, coordinates = null, codeRegistry = mockk())
}
}

@Test
fun `verify exception is thrown if no code registry is specified for the field`() {
val myTestField = GraphQLFieldDefinition.newFieldDefinition()
.name("MyField")
.type { context, visitor -> context.thisNode().accept(context, visitor) }
.description("My Field Description")
.withDirective(graphQLLowercaseDirective)
.build()

assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
SimpleWiringFactory().onWire(graphQLSchemaElement = myTestField, coordinates = mockk(), codeRegistry = null)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ import org.junit.jupiter.api.condition.EnabledOnJre
import org.junit.jupiter.api.condition.JRE
import kotlin.reflect.KType
import kotlin.reflect.KTypeProjection
import kotlin.reflect.full.createType
import kotlin.reflect.full.findParameterByName
import kotlin.reflect.full.starProjectedType
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

internal class KTypeExtensionsKtTest {
class KTypeExtensionsKtTest {

internal class MyClass {
class MyClass {
fun listFun(list: List<String>) = list.joinToString(separator = ",") { it }

fun arrayFun(array: Array<String>) = array.joinToString(separator = ",") { it }
Expand All @@ -44,9 +46,9 @@ internal class KTypeExtensionsKtTest {
fun stringFun(string: String) = "hello $string"
}

internal interface SimpleInterface
interface SimpleInterface

internal class SimpleClass(val id: String) : SimpleInterface
class SimpleClass(val id: String) : SimpleInterface

@Test
fun getTypeOfFirstArgument() {
Expand Down Expand Up @@ -77,7 +79,22 @@ internal class KTypeExtensionsKtTest {

@Test
fun getKClass() {
assertEquals(MyClass::class, MyClass::class.starProjectedType.getKClass())
assertEquals(MyClass::class, MyClass::class.createType().getKClass())
}

@Test
fun getJavaClass() {
val listType = assertNotNull(MyClass::listFun.findParameterByName("list")?.type)
assertEquals(List::class.java, listType.getJavaClass())

val arrayType = assertNotNull(MyClass::arrayFun.findParameterByName("array")?.type)
assertEquals(Array<String>::class.java, arrayType.getJavaClass())

val primitiveArrayType = assertNotNull(MyClass::primitiveArrayFun.findParameterByName("intArray")?.type)
assertEquals(IntArray::class.java, primitiveArrayType.getJavaClass())

val stringType = assertNotNull(MyClass::stringFun.findParameterByName("string")?.type)
assertEquals(String::class.java, stringType.getJavaClass())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.expediagroup.graphql.test.utils.SimpleDirective
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.test.assertEquals
import kotlin.test.assertTrue

Expand Down Expand Up @@ -92,40 +93,47 @@ class GenerateDirectiveTest {

@Test
fun `no annotation`() {
assertTrue(generateDirectives(basicGenerator, MyClass::noAnnotation).isEmpty().isTrue())
val noAnnotation: KFunction<String> = MyClass::noAnnotation
assertTrue(generateDirectives(basicGenerator, noAnnotation).isEmpty().isTrue())
}

@Test
fun `no directive`() {
assertTrue(generateDirectives(basicGenerator, MyClass::noDirective).isEmpty().isTrue())
val noDirective: KFunction<String> = MyClass::noDirective
assertTrue(generateDirectives(basicGenerator, noDirective).isEmpty().isTrue())
}

@Test
fun `has directive`() {
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, MyClass::simpleDirective).size)
val simpleDirective: KFunction<String> = MyClass::simpleDirective
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, simpleDirective).size)
}

@Test
fun `has directive with string`() {
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, MyClass::directiveWithString).size)
val directiveWithString: KFunction<String> = MyClass::directiveWithString
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, directiveWithString).size)
}

@Test
fun `has directive with enum`() {
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, MyClass::directiveWithEnum).size)
val directiveWithEnum: KFunction<String> = MyClass::directiveWithEnum
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, directiveWithEnum).size)
}

@Test
fun `has directive with class`() {
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, MyClass::directiveWithClass).size)
val directiveWithClass: KFunction<String> = MyClass::directiveWithClass
assertEquals(expected = 1, actual = generateDirectives(basicGenerator, directiveWithClass).size)
}

@Test
fun `directives are only added to the schema once`() {
val initialCount = basicGenerator.directives.size
val firstInvocation = generateDirectives(basicGenerator, MyClass::simpleDirective)
val simpleDirective: KFunction<String> = MyClass::simpleDirective
val firstInvocation = generateDirectives(basicGenerator, simpleDirective)
assertEquals(1, firstInvocation.size)
val secondInvocation = generateDirectives(basicGenerator, MyClass::simpleDirective)
val secondInvocation = generateDirectives(basicGenerator, simpleDirective)
assertEquals(1, secondInvocation.size)
assertEquals(firstInvocation.first(), secondInvocation.first())
assertEquals(initialCount + 1, basicGenerator.directives.size)
Expand Down Expand Up @@ -154,8 +162,10 @@ class GenerateDirectiveTest {
@Test
fun `directives are created per each declaration`() {
val initialCount = basicGenerator.directives.size
val directivesOnFirstField = generateDirectives(basicGenerator, MyClass::directiveWithString)
val directivesOnSecondField = generateDirectives(basicGenerator, MyClass::directiveWithAnotherString)
val directiveWithString: KFunction<String> = MyClass::directiveWithString
val directiveWithAnotherString: KFunction<String> = MyClass::directiveWithAnotherString
val directivesOnFirstField = generateDirectives(basicGenerator, directiveWithString)
val directivesOnSecondField = generateDirectives(basicGenerator, directiveWithAnotherString)
assertEquals(expected = 1, actual = directivesOnFirstField.size)
assertEquals(expected = 1, actual = directivesOnSecondField.size)

Expand Down Expand Up @@ -191,7 +201,8 @@ class GenerateDirectiveTest {

@Test
fun `exlude directive arguments @GraphQLIgnore`() {
val directives = generateDirectives(basicGenerator, MyClass::directiveWithIgnoredArgs)
val directiveWithIgnoredArgs: KFunction<String> = MyClass::directiveWithIgnoredArgs
val directives = generateDirectives(basicGenerator, directiveWithIgnoredArgs)
assertEquals(expected = 1, actual = directives.size)
assertEquals(expected = 1, actual = directives.first().arguments.size)
assertEquals(expected = "string", actual = directives.first().arguments.first().name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.hooks

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import org.junit.jupiter.api.Test
import java.util.concurrent.CompletableFuture
import kotlin.reflect.full.createType
import kotlin.reflect.jvm.reflect
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class FlowSubscriptionSchemaGeneratorHooksTest {

val hooks = FlowSubscriptionSchemaGeneratorHooks()

@Test
fun `willResolveMonad unwraps Flow`() {
val type = assertNotNull(TestQuery::getFlow.reflect()?.returnType)
val result = hooks.willResolveMonad(type)
assertEquals(String::class.createType(), result)
}

@Test
fun `willResolveMonad does nothing on any other type`() {
val stringType = assertNotNull(TestQuery::getString.reflect()?.returnType)
val cfType = assertNotNull(TestQuery::getCompletableFuture.reflect()?.returnType)

assertEquals(stringType, hooks.willResolveMonad(stringType))
assertEquals(cfType, hooks.willResolveMonad(cfType))
}

@Test
fun isValidSubscriptionReturnType() {
assertTrue(hooks.isValidSubscriptionReturnType(TestQuery::class, TestQuery::getFlow))
assertFalse(hooks.isValidSubscriptionReturnType(TestQuery::class, TestQuery::getString))
assertFalse(hooks.isValidSubscriptionReturnType(TestQuery::class, TestQuery::getCompletableFuture))
}

class TestQuery {
fun getFlow(): Flow<String> = emptyFlow()
fun getString(): String = ""
fun getCompletableFuture(): CompletableFuture<String> = CompletableFuture.completedFuture("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class SchemaGeneratorHooksTest {
@Test
fun `willResolveMonad returns basic type`() {
val hooks = NoopSchemaGeneratorHooks
val type = TestQuery::query.returnType
val type = (TestQuery::query as KFunction<*>).returnType

assertEquals(expected = "SomeData", actual = hooks.willResolveMonad(type).getSimpleName())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ abstract class GraphQLGradlePluginAbstractIT {

// unsure if there is a better way - correct values are set from Gradle build
// when running directly from IDE you will need to manually update those to correct values
private val gqlKotlinVersion = System.getProperty("graphQLKotlinVersion") ?: "3.0.0-SNAPSHOT"
private val kotlinVersion = System.getProperty("kotlinVersion") ?: "1.3.72"
private val gqlKotlinVersion = System.getProperty("graphQLKotlinVersion") ?: "4.0.0-SNAPSHOT"
private val kotlinVersion = System.getProperty("kotlinVersion") ?: "1.4.0"
private val junitVersion = System.getProperty("junitVersion") ?: "5.6.2"

val testSchema = loadResource("mocks/schema.graphql")
Expand Down

0 comments on commit 050549a

Please sign in to comment.