Skip to content

Commit

Permalink
#22 Remove lagom-extensions dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage committed Feb 14, 2020
1 parent 3387c69 commit ba8a13c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: java

jdk:
- openjdk8

env:
- SCALA_VERSION=2.11
- SCALA_VERSION=2.12
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ allprojects {

subprojects {
group = "org.taymyr.lagom"
version = "1.0.1-SNAPSHOT"
version = "1.1.0-SNAPSHOT"

apply<JacocoPlugin>()

Expand Down
1 change: 0 additions & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion java/impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
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
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 {
Expand All @@ -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)

Expand All @@ -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) { }
Expand All @@ -53,7 +62,10 @@ abstract class AbstractOpenAPIService(config: Config? = null) : OpenAPIService {
override fun openapi(): HeaderServiceCall<NotUsed, String> {
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")
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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")
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
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;

import java.io.IOException;
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")
Expand All @@ -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<ResponseHeader, String> 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);
}

Expand Down

0 comments on commit ba8a13c

Please sign in to comment.