Skip to content

Commit

Permalink
feat(core): improve configuration validation
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Mar 5, 2024
1 parent a3b4743 commit 853119d
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 8 deletions.
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");
}
}
}

0 comments on commit 853119d

Please sign in to comment.