-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kotlin and Groovy versions of Micronaut Dependency Injection types (#…
- Loading branch information
Showing
11 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...dependency-injection-types/groovy/src/main/groovy/example/micronaut/MessageService.groovy
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,13 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.core.annotation.NonNull | ||
import jakarta.inject.Singleton | ||
|
||
@Singleton | ||
class MessageService { | ||
|
||
@NonNull | ||
String compose() { | ||
"Hello World" | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ction-types/groovy/src/main/groovy/example/micronaut/constructor/MessageController.groovy
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,22 @@ | ||
package example.micronaut.constructor | ||
|
||
import example.micronaut.MessageService | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
|
||
@Controller("/constructor") | ||
class MessageController { | ||
private final MessageService messageService | ||
|
||
MessageController(MessageService messageService) { | ||
this.messageService = messageService | ||
} | ||
|
||
@Get | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String index() { | ||
messageService.compose() | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...y-injection-types/groovy/src/main/groovy/example/micronaut/field/MessageController.groovy
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,20 @@ | ||
package example.micronaut.field | ||
|
||
import example.micronaut.MessageService | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
import jakarta.inject.Inject | ||
|
||
@Controller("/field") | ||
class MessageController { | ||
@Inject | ||
MessageService messageService | ||
|
||
@Get | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String index() { | ||
messageService.compose() | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...n-types/groovy/src/main/groovy/example/micronaut/methodparameter/MessageController.groovy
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,24 @@ | ||
package example.micronaut.methodparameter | ||
|
||
import example.micronaut.MessageService | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
import jakarta.inject.Inject | ||
|
||
@Controller("/setter") | ||
class MessageController { | ||
private MessageService messageService | ||
|
||
@Get | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String index() { | ||
return messageService.compose() | ||
} | ||
|
||
@Inject | ||
void populateMessageService(MessageService messageService) { | ||
this.messageService = messageService | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...dency-injection-types/groovy/src/test/java/example/micronaut/MessageControllerSpec.groovy
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 example.micronaut | ||
|
||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals | ||
|
||
@MicronautTest | ||
class MessageControllerTest { | ||
@Inject | ||
@Client("/") | ||
HttpClient httpClient | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["/constructor", "/field", "/setter"]) | ||
void validateMessageControllers(String path) { | ||
assertEquals("Hello World", httpClient.toBlocking().retrieve( | ||
HttpRequest.GET(path).accept(MediaType.TEXT_PLAIN)) | ||
) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...aut-dependency-injection-types/kotlin/src/main/kotlin/example/micronaut/MessageService.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,10 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.core.annotation.NonNull | ||
import jakarta.inject.Singleton | ||
|
||
@Singleton | ||
class MessageService { | ||
@NonNull | ||
fun compose() = "Hello World" | ||
} |
14 changes: 14 additions & 0 deletions
14
...injection-types/kotlin/src/main/kotlin/example/micronaut/constructor/MessageController.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,14 @@ | ||
package example.micronaut.constructor | ||
|
||
import example.micronaut.MessageService | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
|
||
@Controller("/constructor") | ||
class MessageController(val messageService: MessageService) { | ||
@Get | ||
@Produces(MediaType.TEXT_PLAIN) | ||
fun index() = messageService.compose() | ||
} |
18 changes: 18 additions & 0 deletions
18
...dency-injection-types/kotlin/src/main/kotlin/example/micronaut/field/MessageController.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,18 @@ | ||
package example.micronaut.field | ||
|
||
import example.micronaut.MessageService | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
import jakarta.inject.Inject | ||
|
||
@Controller("/field") | ||
class MessageController { | ||
@Inject | ||
lateinit var messageService: MessageService | ||
|
||
@Get | ||
@Produces(MediaType.TEXT_PLAIN) | ||
fun index() = messageService.compose() | ||
} |
22 changes: 22 additions & 0 deletions
22
...ction-types/kotlin/src/main/kotlin/example/micronaut/methodparameter/MessageController.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,22 @@ | ||
package example.micronaut.methodparameter | ||
|
||
import example.micronaut.MessageService | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Produces | ||
import jakarta.inject.Inject | ||
|
||
@Controller("/setter") | ||
class MessageController { | ||
var messageService: MessageService? = null | ||
|
||
@Get | ||
@Produces(MediaType.TEXT_PLAIN) | ||
fun index() = messageService?.compose() | ||
|
||
@Inject | ||
fun populateMessageService(messageService: MessageService) { | ||
this.messageService = messageService | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...endency-injection-types/kotlin/src/test/kotlin/example/micronaut/MessageControllerTest.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,25 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.ValueSource | ||
|
||
@MicronautTest | ||
class MessageControllerTest { | ||
@Inject | ||
@field:Client("/") | ||
lateinit var httpClient: HttpClient | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = ["/constructor", "/field", "/setter"]) | ||
fun validateMessageControllers(path: String) { | ||
val request = HttpRequest.GET<String>(path).accept(MediaType.TEXT_PLAIN) | ||
assertEquals("Hello World", httpClient.toBlocking().retrieve(request)) | ||
} | ||
} |
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