Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kotlin: improve rest dsl #162

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package org.apache.camel.k.loader.groovy.dsl

import org.apache.camel.builder.RouteBuilder
import org.apache.camel.model.rest.RestConfigurationDefinition
import org.apache.camel.model.rest.RestDefinition

class RestConfiguration extends RestVerbConfiguration {
RestConfiguration(RouteBuilder builder) {
Expand All @@ -37,7 +36,7 @@ class RestConfiguration extends RestVerbConfiguration {
callable.call()
}

def path(String path, @DelegatesTo(RestDefinition) Closure<?> callable) {
def path(String path, @DelegatesTo(RestVerbConfiguration) Closure<?> callable) {
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = new RestVerbConfiguration(builder, path)
callable.call()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import org.apache.camel.Predicate
import org.apache.camel.Processor
import org.apache.camel.builder.endpoint.EndpointBuilderFactory

class BeansConfiguration(val context: CamelContext) : EndpointBuilderFactory {
class BeansConfiguration(
val context: CamelContext) : EndpointBuilderFactory {

inline fun <reified T : Any> bean(name: String, block: T.() -> Unit) {
var bean = T::class.java.newInstance()
bean.block()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package org.apache.camel.k.loader.kotlin.dsl
import org.apache.camel.CamelContext
import org.apache.camel.spi.Registry

class ContextConfiguration (val registry: Registry, val context: CamelContext) {
class ContextConfiguration (
private val context: CamelContext,
private val registry: Registry) {

fun registry(block: RegistryConfiguration.() -> Unit): RegistryConfiguration {
val delegate = RegistryConfiguration(registry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.apache.camel.builder.endpoint.EndpointBuilderFactory
import org.apache.camel.builder.endpoint.EndpointRouteBuilder
import org.apache.camel.k.loader.kotlin.KotlinCompilationConfiguration
import org.apache.camel.model.*
import org.apache.camel.model.rest.RestDefinition
import org.apache.camel.spi.Registry
import kotlin.script.experimental.annotations.KotlinScript

Expand All @@ -30,16 +31,20 @@ abstract class IntegrationConfiguration(
private val registry : Registry,
private val builder : EndpointRouteBuilder) : BuilderSupport(builder.context), Support, EndpointBuilderFactory {

fun rest(): RestDefinition {
return builder.rest()
}

fun rest(block: RestConfiguration.() -> Unit) {
RestConfiguration(builder).block()
}

fun beans(block: BeansConfiguration.() -> Unit) {
BeansConfiguration(context = context).block()
BeansConfiguration(context).block()
}

fun context(block: ContextConfiguration.() -> Unit) {
ContextConfiguration(context = context, registry = registry).block()
ContextConfiguration(context, registry).block()
}

fun from(uri: String): RouteDefinition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package org.apache.camel.k.loader.kotlin.dsl

import org.apache.camel.builder.RouteBuilder
import org.apache.camel.model.rest.RestConfigurationDefinition
import org.apache.camel.model.rest.RestDefinition

class RestConfiguration(val builder: RouteBuilder) {
class RestConfiguration(
private val builder: RouteBuilder) : RestVerbConfiguration(builder, builder.rest()) {

fun configuration(block: RestConfigurationDefinition.() -> Unit) {
val delegate = builder.restConfiguration()
Expand All @@ -32,8 +32,7 @@ class RestConfiguration(val builder: RouteBuilder) {
delegate.block()
}

fun path(path: String, block: RestDefinition.() -> Unit) {
val delegate = builder.rest(path)
delegate.block()
fun path(path: String, block: RestVerbConfiguration.() -> Unit) {
RestVerbConfiguration(builder, path).block()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.camel.k.loader.kotlin.dsl

import org.apache.camel.builder.RouteBuilder
import org.apache.camel.model.rest.RestDefinition

open class RestVerbConfiguration(
private val builder: RouteBuilder,
private val definition: RestDefinition) {

constructor(builder: RouteBuilder, path: String): this(builder, builder.rest(path))

fun get(path: String, block: RestDefinition.() -> Unit) = definition.get(path).block()
fun get(block: RestDefinition.() -> Unit) = definition.get().block()

fun post(path: String, block: RestDefinition.() -> Unit) = definition.post(path).block()
fun post(block: RestDefinition.() -> Unit) = definition.post().block()

fun delete(path: String, block: RestDefinition.() -> Unit) = definition.delete(path).block()
fun delete(block: RestDefinition.() -> Unit) = definition.delete().block()

fun head(path: String, block: RestDefinition.() -> Unit) = definition.head(path).block()
fun head(block: RestDefinition.() -> Unit) = definition.head().block()

fun put(path: String, block: RestDefinition.() -> Unit) = definition.put(path).block()
fun put(block: RestDefinition.() -> Unit) = definition.put().block()

fun patch(path: String, block: RestDefinition.() -> Unit) = definition.patch(path).block()
fun patch(block: RestDefinition.() -> Unit) = definition.patch().block()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Runtime
import org.apache.camel.k.listener.RoutesConfigurer.forRoutes
import org.apache.camel.model.ModelCamelContext
import org.apache.camel.model.rest.GetVerbDefinition
import org.apache.camel.model.rest.PostVerbDefinition
import org.apache.camel.processor.FatalFallbackErrorHandler
import org.apache.camel.support.DefaultHeaderFilterStrategy
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -42,8 +44,29 @@ class IntegrationTest {
assertThat(context.restConfiguration.port).isEqualTo(9192)
assertThat(context.getRestConfiguration("undertow", false).host).isEqualTo("my-undertow-host")
assertThat(context.getRestConfiguration("undertow", false).port).isEqualTo(9193)
assertThat(context.adapt(ModelCamelContext::class.java).restDefinitions.size).isEqualTo(1)
assertThat(context.adapt(ModelCamelContext::class.java).restDefinitions[0].path).isEqualTo("/my/path")
assertThat(context.adapt(ModelCamelContext::class.java).restDefinitions.size).isEqualTo(2)

with(context.adapt(ModelCamelContext::class.java).restDefinitions.find { it.path == "/my/path" }) {
assertThat(this?.verbs).hasSize(1)

with(this?.verbs?.get(0) as GetVerbDefinition) {
assertThat(uri).isEqualTo("/get")
assertThat(consumes).isEqualTo("application/json")
assertThat(produces).isEqualTo("application/json")
assertThat(to).hasFieldOrPropertyWithValue("endpointUri", "direct:get")
}
}

with(context.adapt(ModelCamelContext::class.java).restDefinitions.find { it.path == "/post" }) {
assertThat(this?.verbs).hasSize(1)

with(this?.verbs?.get(0) as PostVerbDefinition) {
assertThat(uri).isNull()
assertThat(consumes).isEqualTo("application/json")
assertThat(produces).isEqualTo("application/json")
assertThat(to).hasFieldOrPropertyWithValue("endpointUri", "direct:post")
}
}
}

@Test
Expand Down
12 changes: 12 additions & 0 deletions camel-k-loader-kotlin/src/test/resources/routes-with-rest.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ rest {
}

path("/my/path") {
get("/get") {
consumes("application/json")
produces("application/json")
to("direct:get")
}
}

post {
path("/post")
consumes("application/json")
produces("application/json")
to("direct:post")
}
}

Expand Down