-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit for MicronautConverter (#34)
- Loading branch information
Showing
29 changed files
with
592 additions
and
12 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# hikaku - micronaut |
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 @@ | ||
group = 'de.codecentric.hikaku' | ||
archivesBaseName = 'hikaku-micronaut' | ||
|
||
dependencies { | ||
api project(':core') | ||
api "io.micronaut:micronaut-http:1.1.0" | ||
} | ||
|
||
uploadArchives { | ||
repositories { | ||
mavenDeployer { | ||
pom.project { | ||
name = 'hikaku-micronaut' | ||
description = 'A library that tests if the implementation of a REST-API meets its specification. This module contains a converter for micronaut implementations.' | ||
} | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
micronaut/src/main/kotlin/de/codecentric/hikaku/converters/micronaut/MicronautConverter.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,89 @@ | ||
package de.codecentric.hikaku.converters.micronaut | ||
|
||
import de.codecentric.hikaku.SupportedFeatures | ||
import de.codecentric.hikaku.converters.AbstractEndpointConverter | ||
import de.codecentric.hikaku.converters.ClassLocator | ||
import de.codecentric.hikaku.converters.EndpointConverterException | ||
import de.codecentric.hikaku.endpoints.Endpoint | ||
import de.codecentric.hikaku.endpoints.HttpMethod | ||
import io.micronaut.http.annotation.* | ||
import java.lang.reflect.Method | ||
|
||
class MicronautConverter(private val packageName: String) : AbstractEndpointConverter() { | ||
|
||
override val supportedFeatures = SupportedFeatures() | ||
|
||
override fun convert(): Set<Endpoint> { | ||
if (packageName.isBlank()) { | ||
throw EndpointConverterException("Package name must not be blank.") | ||
} | ||
|
||
return ClassLocator.getClasses(packageName) | ||
.filter { it.getAnnotation(Controller::class.java) != null } | ||
.flatMap { extractEndpoints(it) } | ||
.toSet() | ||
} | ||
|
||
private fun extractEndpoints(resource: Class<*>): List<Endpoint> { | ||
return resource.methods | ||
.filter { isHttpMethodAnnotationPresent(it) } | ||
.map { createEndpoint(resource, it) } | ||
} | ||
|
||
private fun isHttpMethodAnnotationPresent(method: Method): Boolean { | ||
return when { | ||
method.isAnnotationPresent(Delete::class.java) -> true | ||
method.isAnnotationPresent(Get::class.java) -> true | ||
method.isAnnotationPresent(Head::class.java) -> true | ||
method.isAnnotationPresent(Options::class.java) -> true | ||
method.isAnnotationPresent(Patch::class.java) -> true | ||
method.isAnnotationPresent(Post::class.java) -> true | ||
method.isAnnotationPresent(Put::class.java) -> true | ||
else -> false | ||
} | ||
} | ||
|
||
private fun createEndpoint(resource: Class<*>, method: Method) = Endpoint( | ||
path = extractPath(resource, method), | ||
httpMethod = extractHttpMethod(method) | ||
) | ||
|
||
private fun extractPath(resource: Class<*>, method: Method): String { | ||
var pathOnClass = resource.getAnnotation(Controller::class.java).value | ||
val pathOnFunction = when { | ||
method.isAnnotationPresent(Delete::class.java) -> method.getAnnotation(Delete::class.java).value | ||
method.isAnnotationPresent(Get::class.java) -> method.getAnnotation(Get::class.java).value | ||
method.isAnnotationPresent(Head::class.java) -> method.getAnnotation(Head::class.java).value | ||
method.isAnnotationPresent(Options::class.java) -> method.getAnnotation(Options::class.java).value | ||
method.isAnnotationPresent(Patch::class.java) -> method.getAnnotation(Patch::class.java).value | ||
method.isAnnotationPresent(Post::class.java) -> method.getAnnotation(Post::class.java).value | ||
method.isAnnotationPresent(Put::class.java) -> method.getAnnotation(Put::class.java).value | ||
else -> "" | ||
} | ||
|
||
if (!pathOnClass.startsWith("/")) { | ||
pathOnClass = "/$pathOnClass" | ||
} | ||
|
||
val combinedPath = "$pathOnClass/$pathOnFunction".replace(Regex("/+"), "/") | ||
|
||
return if (combinedPath.endsWith('/')) { | ||
combinedPath.substringBeforeLast('/') | ||
} else { | ||
combinedPath | ||
} | ||
} | ||
|
||
private fun extractHttpMethod(method: Method): HttpMethod { | ||
return when { | ||
method.isAnnotationPresent(Delete::class.java) -> HttpMethod.DELETE | ||
method.isAnnotationPresent(Get::class.java) -> HttpMethod.GET | ||
method.isAnnotationPresent(Head::class.java) -> HttpMethod.HEAD | ||
method.isAnnotationPresent(Options::class.java) -> HttpMethod.OPTIONS | ||
method.isAnnotationPresent(Patch::class.java) -> HttpMethod.PATCH | ||
method.isAnnotationPresent(Post::class.java) -> HttpMethod.POST | ||
method.isAnnotationPresent(Put::class.java) -> HttpMethod.PUT | ||
else -> throw IllegalStateException("Unable to determine http method. Valid annotation not found.") | ||
} | ||
} | ||
} |
Empty file.
22 changes: 22 additions & 0 deletions
22
...lin/de/codecentric/hikaku/converters/micronaut/MicronautConverterPackageDefinitionTest.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 de.codecentric.hikaku.converters.micronaut | ||
|
||
import de.codecentric.hikaku.converters.EndpointConverterException | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertFailsWith | ||
|
||
class MicronautConverterPackageDefinitionTest { | ||
|
||
@Test | ||
fun `invoking converter with empty string leads to EndpointConverterException`() { | ||
assertFailsWith<EndpointConverterException> { | ||
MicronautConverter("").conversionResult | ||
} | ||
} | ||
|
||
@Test | ||
fun `invoking converter with blank string leads to EndpointConverterException`() { | ||
assertFailsWith<EndpointConverterException> { | ||
MicronautConverter(" ").conversionResult | ||
} | ||
} | ||
} |
Oops, something went wrong.