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

feat(core): improve configuration validation #641

Merged
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 @@ -2,6 +2,7 @@
package io.github.springwolf.core.configuration.docket;

import io.github.springwolf.asyncapi.v3.model.info.Info;
import io.github.springwolf.asyncapi.v3.model.server.Server;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigConstants;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -31,22 +32,21 @@ public AsyncApiDocket getAsyncApiDocket() {
}

private AsyncApiDocket createDocket() {
log.debug("Reading springwolf configuration from application.properties files");

if (configProperties.getDocket() == null || configProperties.getDocket().getBasePackage() == null) {
if (configProperties.getDocket() == null
|| !StringUtils.hasText(configProperties.getDocket().getBasePackage())) {
throw new IllegalArgumentException(
"One or more required fields (docket, basePackage) " + "in application.properties with path prefix "
+ SpringwolfConfigConstants.SPRINGWOLF_CONFIG_PREFIX + " is not set.");
}

Info info = buildInfo(configProperties.getDocket().getInfo());

AsyncApiDocket.AsyncApiDocketBuilder builder = AsyncApiDocket.builder()
.basePackage(configProperties.getDocket().getBasePackage())
.info(info)
.servers(configProperties.getDocket().getServers())
.id(configProperties.getDocket().getId());
.info(buildInfo(configProperties.getDocket().getInfo()))
.servers(buildServers(configProperties.getDocket().getServers()));

if (configProperties.getDocket().getId() != null) {
builder.id(configProperties.getDocket().getId());
}
if (configProperties.getDocket().getDefaultContentType() != null) {
builder.defaultContentType(configProperties.getDocket().getDefaultContentType());
}
Expand Down Expand Up @@ -79,4 +79,22 @@ private static Info buildInfo(@Nullable SpringwolfConfigProperties.ConfigDocket.

return asyncapiInfo;
}

private static Map<String, Server> buildServers(Map<String, Server> servers) {
if (servers == null || servers.isEmpty()) {
throw new IllegalArgumentException("No server has been defined in application.properties "
+ "with path prefix " + SpringwolfConfigConstants.SPRINGWOLF_CONFIG_PREFIX);
} else {
servers.forEach((serverName, server) -> {
if (!StringUtils.hasText(server.getProtocol()) || !StringUtils.hasText(server.getHost())) {
throw new IllegalArgumentException(
"One or more required fields (protocol, host) " + "of the server object (name="
+ serverName + ") " + "has been defined in application.properties "
+ "with path prefix " + SpringwolfConfigConstants.SPRINGWOLF_CONFIG_PREFIX);
}
});
}

return servers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import io.github.springwolf.asyncapi.v3.model.server.Server;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties.ConfigDocket;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.Maps.newHashMap;

class DefaultAsyncApiDocketServiceTest {
Expand Down Expand Up @@ -87,4 +92,116 @@ void docketServiceShouldDeliverCachedDocket() {
// second invocation should again return same instance
assertThat(docketService.getAsyncApiDocket()).isSameAs(asyncApiDocket);
}

@Nested
class MissingProperties {

private ConfigDocket validDocket;
private AsyncApiDocketService docketService;

@BeforeEach
void setUp() {
validDocket = new ConfigDocket();
validDocket.setBasePackage("test-base-package");

Server server =
Server.builder().protocol("some-protocol").host("some-url").build();
validDocket.setServers(newHashMap("some-protocol", server));

ConfigDocket.Info info = new ConfigDocket.Info();
info.setTitle("some-title");
info.setVersion("some-version");
validDocket.setInfo(info);

SpringwolfConfigProperties properties = new SpringwolfConfigProperties();
properties.setDocket(validDocket);
docketService = new DefaultAsyncApiDocketService(properties);
}

@ParameterizedTest
@CsvSource(
value = {"''", "null"},
nullValues = {"null"})
void missingBasePackageTest(String value) {
// given
validDocket.setBasePackage(value);

// when
assertThatThrownBy(docketService::getAsyncApiDocket)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("One or more required fields (docket, basePackage)");
}

@ParameterizedTest
@CsvSource(
value = {"''", "null"},
nullValues = {"null"})
void missingInfoTitle(String value) {
// given
validDocket.getInfo().setTitle(value);

// when
assertThatThrownBy(docketService::getAsyncApiDocket)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"One or more required fields of the info object (title, version) in application.properties with path prefix springwolf is not set.");
}

@ParameterizedTest
@CsvSource(
value = {"''", "null"},
nullValues = {"null"})
void missingInfoVersion(String value) {
// given
validDocket.getInfo().setVersion(value);

// when
assertThatThrownBy(docketService::getAsyncApiDocket)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"One or more required fields of the info object (title, version) in application.properties with path prefix springwolf is not set.");
}

@Test
void missingServers() {
// given
validDocket.getServers().clear();

// when
assertThatThrownBy(docketService::getAsyncApiDocket)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"No server has been defined in application.properties with path prefix springwolf");
}

@ParameterizedTest
@CsvSource(
value = {"''", "null"},
nullValues = {"null"})
void missingServerProtocol(String value) {
// given
validDocket.getServers().forEach((k, v) -> v.setProtocol(value));

// when
assertThatThrownBy(docketService::getAsyncApiDocket)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"One or more required fields (protocol, host) of the server object (name=some-protocol) has been defined in application.properties with path prefix springwolf");
}

@ParameterizedTest
@CsvSource(
value = {"''", "null"},
nullValues = {"null"})
void missingServerHost(String value) {
// given
validDocket.getServers().forEach((k, v) -> v.setHost(value));

// when
assertThatThrownBy(docketService::getAsyncApiDocket)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(
"One or more required fields (protocol, host) of the server object (name=some-protocol) has been defined in application.properties with path prefix springwolf");
}
}
}