diff --git a/.travis.yml b/.travis.yml index 9472a4e..7c33ac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ language: java +jdk: + - openjdk8 + env: - SCALA_VERSION=2.11 - SCALA_VERSION=2.12 diff --git a/build.gradle.kts b/build.gradle.kts index 6a3b6ac..78169b1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,7 @@ allprojects { subprojects { group = "org.taymyr.lagom" - version = "1.0.1-SNAPSHOT" + version = "1.1.0-SNAPSHOT" apply() diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 0b295ec..3dc8b46 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -7,7 +7,6 @@ object Versions { const val `ktlint-plugin` = "8.0.0" const val `kotlin-logging` = "1.6.10" const val config4k = "0.4.1" - const val `lagom-extensions` = "0.1.0" const val swagger = "2.0.7" const val jacoco = "0.8.4" const val junit5 = "5.3.2" diff --git a/java/impl/build.gradle.kts b/java/impl/build.gradle.kts index 37540e9..c27117c 100644 --- a/java/impl/build.gradle.kts +++ b/java/impl/build.gradle.kts @@ -30,7 +30,6 @@ dependencies { compileOnly("com.lightbend.lagom", "lagom-javadsl-server_$scalaBinaryVersion", lagomVersion) implementation("io.github.microutils", "kotlin-logging", Versions.`kotlin-logging`) implementation("io.github.config4k", "config4k", Versions.config4k) - implementation("org.taymyr.lagom", "lagom-extensions-java_$scalaBinaryVersion", Versions.`lagom-extensions`) testImplementation(evaluationDependsOn(":lagom-openapi-core").sourceSets.test.get().output) testImplementation("org.junit.jupiter", "junit-jupiter-api", Versions.junit5) diff --git a/java/impl/src/main/kotlin/org/taymyr/lagom/javadsl/openapi/AbstractOpenAPIService.kt b/java/impl/src/main/kotlin/org/taymyr/lagom/javadsl/openapi/AbstractOpenAPIService.kt index 2055daf..ed67b53 100644 --- a/java/impl/src/main/kotlin/org/taymyr/lagom/javadsl/openapi/AbstractOpenAPIService.kt +++ b/java/impl/src/main/kotlin/org/taymyr/lagom/javadsl/openapi/AbstractOpenAPIService.kt @@ -1,8 +1,10 @@ package org.taymyr.lagom.javadsl.openapi import akka.NotUsed +import akka.japi.Pair import com.lightbend.lagom.javadsl.api.transport.MessageProtocol import com.lightbend.lagom.javadsl.api.transport.NotFound +import com.lightbend.lagom.javadsl.api.transport.ResponseHeader.OK import com.lightbend.lagom.javadsl.server.HeaderServiceCall import com.typesafe.config.Config import io.github.config4k.extract @@ -10,12 +12,12 @@ import io.swagger.v3.core.util.Yaml import io.swagger.v3.oas.annotations.OpenAPIDefinition import mu.KLogging import org.taymyr.lagom.internal.openapi.isAnnotationPresentInherited -import org.taymyr.lagom.javadsl.api.transport.MessageProtocols.JSON -import org.taymyr.lagom.javadsl.api.transport.MessageProtocols.YAML -import org.taymyr.lagom.javadsl.api.transport.MessageProtocols.fromFile -import org.taymyr.lagom.javadsl.api.transport.ResponseHeaders.ok +import java.util.Optional import java.util.concurrent.CompletableFuture.completedFuture +internal val YAML: MessageProtocol = MessageProtocol.fromContentTypeHeader(Optional.of("application/x-yaml")) +internal val JSON: MessageProtocol = MessageProtocol.fromContentTypeHeader(Optional.of("application/json")) + abstract class AbstractOpenAPIService(config: Config? = null) : OpenAPIService { private val spec: OpenAPISpec by lazy { @@ -27,6 +29,13 @@ abstract class AbstractOpenAPIService(config: Config? = null) : OpenAPIService { } } + private fun fromFile(file: String, default: MessageProtocol): MessageProtocol = + when (file.substring(file.lastIndexOf(".") + 1)) { + "json" -> JSON + "yaml", "yml" -> YAML + else -> default + } + private fun generateSpecResource() = OpenAPISpec(Yaml.pretty(SpecGenerator().generate(this)), YAML) @@ -41,7 +50,7 @@ abstract class AbstractOpenAPIService(config: Config? = null) : OpenAPIService { try { val openapiSpec = this.javaClass.getResource("/$filename") spec = openapiSpec.readText() - protocol = fromFile(filename) + protocol = fromFile(filename, YAML) logger.info { "Load OpenAPI specification from $openapiSpec" } break } catch (e: Exception) { } @@ -53,7 +62,10 @@ abstract class AbstractOpenAPIService(config: Config? = null) : OpenAPIService { override fun openapi(): HeaderServiceCall { return HeaderServiceCall { _, _ -> completedFuture( - ok(spec.mimeType, spec.api ?: throw NotFound("OpenAPI specification not found")) + Pair.create( + OK.withProtocol(spec.mimeType), + spec.api ?: throw NotFound("OpenAPI specification not found") + ) ) } } diff --git a/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/file/StaticFileTest.java b/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/file/StaticFileTest.java index f820cbd..73c16fc 100644 --- a/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/file/StaticFileTest.java +++ b/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/file/StaticFileTest.java @@ -1,21 +1,24 @@ package org.taymyr.lagom.javadsl.openapi.file; import com.lightbend.lagom.javadsl.api.transport.NotFound; +import com.lightbend.lagom.javadsl.api.transport.ResponseHeader; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import org.assertj.core.api.Assertions; import org.assertj.core.api.Condition; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.taymyr.lagom.javadsl.openapi.AbstractOpenAPIServiceKt; +import static com.lightbend.lagom.javadsl.api.transport.ResponseHeader.OK; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.taymyr.lagom.javadsl.api.transport.MessageProtocols.YAML; -import static org.taymyr.lagom.javadsl.api.transport.ResponseHeaders.OK_JSON; import static org.taymyr.lagom.internal.openapi.TestUtils.resourceAsString; class StaticFileTest { + private ResponseHeader OK_JSON = OK.withProtocol(AbstractOpenAPIServiceKt.getJSON()); + @Test @DisplayName("Service with incorrect config should be return 404") void shouldReturn404() { @@ -44,7 +47,7 @@ void serviceWithCorrectConfig() { @DisplayName("Service without config should be return default json spec") void serviceWithDefaultJsonSpec() { String originalSpec = resourceAsString("test1.json"); - Test1ServiceImpl service = new Test1ServiceImpl(ConfigFactory.empty()); + Test1ServiceImpl service = new Test1ServiceImpl(ConfigFactory.load()); assertThat(service.openapi().invokeWithHeaders(null, null)) .isCompleted() .isCompletedWithValueMatching(pair -> pair.first().equals(OK_JSON), "Must be equal to OK_JSON") @@ -56,11 +59,11 @@ void serviceWithDefaultJsonSpec() { @DisplayName("Service without config should be return default yaml spec") void serviceWithDefaultYamlSpec() { String originalSpec = resourceAsString("test2.yaml"); - Test2ServiceImpl service = new Test2ServiceImpl(ConfigFactory.empty()); + Test2ServiceImpl service = new Test2ServiceImpl(ConfigFactory.load()); assertThat(service.openapi().invokeWithHeaders(null, null)) .isCompleted() .isCompletedWithValueMatching(pair -> pair.first().status() == 200, "Status code must be 200") - .isCompletedWithValueMatching(pair -> pair.first().protocol().equals(YAML), "Protocol must be YAML") + .isCompletedWithValueMatching(pair -> pair.first().protocol().equals(AbstractOpenAPIServiceKt.getYAML()), "Protocol must be YAML") .isCompletedWithValueMatching(pair -> pair.second().equals(originalSpec), "Must be equal to originalSpec") .isDone(); } @@ -69,11 +72,11 @@ void serviceWithDefaultYamlSpec() { @DisplayName("Service without config should be return default yml spec") void serviceWithDefaultYmlSpec() { String originalSpec = resourceAsString("test3.yml"); - Test3ServiceImpl service = new Test3ServiceImpl(ConfigFactory.empty()); + Test3ServiceImpl service = new Test3ServiceImpl(ConfigFactory.load()); Assertions.assertThat(service.openapi().invokeWithHeaders(null, null)) .isCompleted() .isCompletedWithValueMatching(pair -> pair.first().status() == 200, "Status code must be 200") - .isCompletedWithValueMatching(pair -> pair.first().protocol().equals(YAML), "Protocol must be YAML") + .isCompletedWithValueMatching(pair -> pair.first().protocol().equals(AbstractOpenAPIServiceKt.getYAML()), "Protocol must be YAML") .isCompletedWithValueMatching(pair -> pair.second().equals(originalSpec), "Must be equal to originalSpec") .isDone(); } diff --git a/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/generate/GeneratorTest.java b/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/generate/GeneratorTest.java index 4ced82e..f96d817 100644 --- a/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/generate/GeneratorTest.java +++ b/java/impl/src/test/java/org/taymyr/lagom/javadsl/openapi/generate/GeneratorTest.java @@ -6,6 +6,7 @@ import org.assertj.core.api.Condition; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.taymyr.lagom.javadsl.openapi.AbstractOpenAPIServiceKt; import org.taymyr.lagom.javadsl.openapi.generate.empty.EmptyServiceImpl; import org.taymyr.lagom.javadsl.openapi.generate.pets.PetsServiceImpl; @@ -13,21 +14,20 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; -import static com.typesafe.config.ConfigFactory.empty; +import static com.typesafe.config.ConfigFactory.load; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.taymyr.lagom.internal.openapi.TestUtils.eventually; import static org.taymyr.lagom.internal.openapi.TestUtils.resourceAsString; import static org.taymyr.lagom.internal.openapi.TestUtils.yamlToJson; -import static org.taymyr.lagom.javadsl.api.transport.MessageProtocols.YAML; class GeneratorTest { @Test @DisplayName("Service without OpenAPIDefinition annotation should return 404") void shouldReturn404WithoutAnnotation() { - EmptyServiceImpl service = new EmptyServiceImpl(empty()); + EmptyServiceImpl service = new EmptyServiceImpl(load()); assertThatExceptionOfType(NotFound.class) .isThrownBy(() -> service.openapi().invokeWithHeaders(null, null)) .withMessage("OpenAPI specification not found") @@ -38,10 +38,10 @@ void shouldReturn404WithoutAnnotation() { @DisplayName("Service without OpenAPIDefinition should generate yaml specification") void shouldNormalGenerateYaml() throws InterruptedException, ExecutionException, TimeoutException, IOException { String expected = yamlToJson(resourceAsString("pets.yml")); - PetsServiceImpl service = new PetsServiceImpl(empty()); + PetsServiceImpl service = new PetsServiceImpl(load()); Pair openapi = eventually(service.openapi().invokeWithHeaders(null, null)); assertThat(openapi.first().status()).isEqualTo(200); - assertThat(openapi.first().protocol()).isEqualTo(YAML); + assertThat(openapi.first().protocol()).isEqualTo(AbstractOpenAPIServiceKt.getYAML()); assertThatJson(yamlToJson(openapi.second())).isEqualTo(expected); }