Skip to content

Commit

Permalink
#26 refactored option validation
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics committed Jun 14, 2020
1 parent 4aee2b8 commit 8bfa505
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 62 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Build
FROM openjdk:8-jdk as baseimage

WORKDIR swagger-brake
COPY . .
RUN sh gradlew clean build shadowJar

# Actual container
FROM openjdk:8-jdk
WORKDIR swagger-brake
COPY --from=baseimage /swagger-brake/swagger-brake-cli/build/libs/swagger-brake-*.jar swagger-brake.jar

ENTRYPOINT ["java", "-jar", "swagger-brake.jar"]

Original file line number Diff line number Diff line change
@@ -1,63 +1,44 @@
package io.redskap.swagger.brake.cli.options;

import static io.redskap.swagger.brake.cli.options.CliOption.*;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import com.google.common.collect.ImmutableMap;
import io.redskap.swagger.brake.runner.Options;
import org.apache.commons.lang3.StringUtils;

import io.redskap.swagger.brake.runner.OptionsValidator;
import org.springframework.stereotype.Component;

@Component
public class CliOptionsValidator {
public static final Map<CliOption, Function<Options, String>> MAVEN_CONFIG_MAP = ImmutableMap.of(
MAVEN_REPO_URL, Options::getMavenRepoUrl,
MAVEN_SNAPSHOT_REPO_URL, Options::getMavenSnapshotRepoUrl,
CURRENT_ARTIFACT_VERSION, Options::getCurrentArtifactVersion,
GROUP_ID, Options::getGroupId,
ARTIFACT_ID, Options::getArtifactId
);

public void validate(Options options) {
if (isBlank(options.getNewApiPath())) {
throw new IllegalArgumentException(format("%s must be provided.", NEW_API_PATH.asCliOption()));
}
if (isNotBlank(options.getOldApiPath()) && isAnyMavenConfigurationSet(options)) {
throw new IllegalArgumentException(format("Maven configuration is detected along with %s. Please use only one of them.", OLD_API_PATH.asCliOption()));
}
if (isBlank(options.getOldApiPath()) && !isAnyMavenConfigurationSet(options)) {
throw new IllegalArgumentException(format("Either %s must be provided or a Maven configuration.", OLD_API_PATH.asCliOption()));
}
if (isAnyMavenConfigurationSet(options)) {
if (!isFullMavenConfigurationSet(options)) {
Collection<CliOption> missingMavenCliOptions = findMissingMavenConfiguration(options);
List<String> mavenCliOptionsFormatted = missingMavenCliOptions.stream().map(CliOption::asCliOption).collect(toList());
String joinedMavenCliOptions = StringUtils.join(mavenCliOptionsFormatted, ",");
throw new IllegalArgumentException(format("Partial Maven configuration detected, make sure you set %s as well.", joinedMavenCliOptions));
}
}
public class CliOptionsValidator extends OptionsValidator {
@Override
protected String getMavenRepoUrlName() {
return MAVEN_REPO_URL.asCliOption();
}

@Override
protected String getMavenSnapshotRepoUrlName() {
return MAVEN_SNAPSHOT_REPO_URL.asCliOption();
}

@Override
protected String getCurrentArtifactVersionName() {
return CURRENT_ARTIFACT_VERSION.asCliOption();
}

@Override
protected String getGroupIdName() {
return GROUP_ID.asCliOption();
}

private boolean isFullMavenConfigurationSet(Options options) {
return findMissingMavenConfiguration(options).isEmpty();
@Override
protected String getArtifactIdName() {
return ARTIFACT_ID.asCliOption();
}

private boolean isAnyMavenConfigurationSet(Options options) {
return findMissingMavenConfiguration(options).size() < MAVEN_CONFIG_MAP.size();
@Override
protected String getOldApiPathName() {
return OLD_API_PATH.asCliOption();
}

private Collection<CliOption> findMissingMavenConfiguration(Options options) {
return MAVEN_CONFIG_MAP.entrySet().stream()
.filter(e -> isBlank(e.getValue().apply(options)))
.map(Map.Entry::getKey)
.collect(toList());
@Override
protected String getNewApiPathName() {
return NEW_API_PATH.asCliOption();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.redskap.swagger.brake.runner;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class OptionsValidator {
public final Map<String, Function<Options, String>> mavenConfigMap;

public OptionsValidator() {
mavenConfigMap = ImmutableMap.of(
getMavenRepoUrlName(), Options::getMavenRepoUrl,
getMavenSnapshotRepoUrlName(), Options::getMavenSnapshotRepoUrl,
getCurrentArtifactVersionName(), Options::getCurrentArtifactVersion,
getGroupIdName(), Options::getGroupId,
getArtifactIdName(), Options::getArtifactId
);
}

public void validate(Options options) {
if (isBlank(options.getNewApiPath())) {
throw new IllegalArgumentException(format("%s must be provided.", getNewApiPathName()));
}
if (isNotBlank(options.getOldApiPath()) && isAnyMavenConfigurationSet(options)) {
throw new IllegalArgumentException(format("Maven configuration is detected along with %s. Please use only one of them.", getOldApiPathName()));
}
if (isBlank(options.getOldApiPath()) && !isAnyMavenConfigurationSet(options)) {
throw new IllegalArgumentException(format("Either %s must be provided or a Maven configuration.", getOldApiPathName()));
}
if (isAnyMavenConfigurationSet(options)) {
if (!isFullMavenConfigurationSet(options)) {
Collection<String> missingMavenCliOptions = findMissingMavenConfiguration(options);
String joinedMavenCliOptions = StringUtils.join(missingMavenCliOptions, ",");
throw new IllegalArgumentException(format("Partial Maven configuration detected, make sure you set %s as well.", joinedMavenCliOptions));
}
}
}

private boolean isFullMavenConfigurationSet(Options options) {
return findMissingMavenConfiguration(options).isEmpty();
}

private boolean isAnyMavenConfigurationSet(Options options) {
return findMissingMavenConfiguration(options).size() < mavenConfigMap.size();
}

private Collection<String> findMissingMavenConfiguration(Options options) {
return mavenConfigMap.entrySet().stream()
.filter(e -> isBlank(e.getValue().apply(options)))
.map(Map.Entry::getKey)
.collect(toList());
}

protected String getMavenRepoUrlName() {
return "mavenRepoUrl";
}

protected String getMavenSnapshotRepoUrlName() {
return "mavenSnapshotRepoUrl";
}

protected String getCurrentArtifactVersionName() {
return "currentArtifactVersion";
}

protected String getGroupIdName() {
return "groupId";
}

protected String getArtifactIdName() {
return "artifactId";
}

protected String getOldApiPathName() {
return "oldApiPath";
}

protected String getNewApiPathName() {
return "newApiPath";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public class Runner {
private final OpenApiFactory openApiFactory;
private final Checker checker;
private final CheckerOptionsFactory checkerOptionsFactory;
private final OptionsValidator optionsValidator;

public Collection<BreakingChange> run(Options options) {
optionsValidator.validate(options);
artifactDownloaderHandler.handle(options);
String oldApiPath = options.getOldApiPath();
if (StringUtils.isBlank(oldApiPath)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.redskap.swagger.brake.cli.options;
package io.redskap.swagger.brake.runner;

import static org.assertj.core.api.Assertions.assertThat;

import io.redskap.swagger.brake.runner.Options;
import org.assertj.core.api.Assertions;
import org.junit.Test;

public class CliOptionsValidatorTest {
private CliOptionsValidator underTest = new CliOptionsValidator();
public class OptionsValidatorTest {
private OptionsValidator underTest = new OptionsValidator();

@Test
public void testValidateThrowsExceptionWhenNewApiPathIsNotSet() {
Expand All @@ -18,7 +17,7 @@ public void testValidateThrowsExceptionWhenNewApiPathIsNotSet() {
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.NEW_API_PATH.asCliOption());
assertThat(result.getMessage()).contains("newApiPath");
}

@Test
Expand All @@ -30,7 +29,7 @@ public void testValidateThrowsExceptionWhenNeitherOldApiPathNorMavenIsSet() {
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.OLD_API_PATH.asCliOption());
assertThat(result.getMessage()).contains("oldApiPath");
assertThat(result.getMessage()).containsIgnoringCase("maven");
}

Expand Down Expand Up @@ -58,7 +57,7 @@ public void testValidateThrowsExceptionWhenMavenRepoUrlIsSetButSnapshotRepoIsNot
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.MAVEN_SNAPSHOT_REPO_URL.asCliOption());
assertThat(result.getMessage()).contains("mavenSnapshotRepoUrl");
}

@Test
Expand All @@ -74,7 +73,7 @@ public void testValidateThrowsExceptionWhenMavenSnapshotRepoUrlIsSetButReleaseRe
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.MAVEN_REPO_URL.asCliOption());
assertThat(result.getMessage()).contains("mavenRepoUrl");
}

@Test
Expand All @@ -90,7 +89,7 @@ public void testValidateThrowsExceptionWhenMavenConfigIsSetExceptCurrentArtifact
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.CURRENT_ARTIFACT_VERSION.asCliOption());
assertThat(result.getMessage()).contains("currentArtifactVersion");
}

@Test
Expand All @@ -106,7 +105,7 @@ public void testValidateThrowsExceptionWhenMavenConfigIsSetExceptGroupId() {
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.GROUP_ID.asCliOption());
assertThat(result.getMessage()).contains("groupId");
}

@Test
Expand All @@ -122,7 +121,7 @@ public void testValidateThrowsExceptionWhenMavenConfigIsSetExceptArtifactId() {
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.ARTIFACT_ID.asCliOption());
assertThat(result.getMessage()).contains("artifactId");
}

@Test
Expand Down Expand Up @@ -155,7 +154,7 @@ public void testValidateThrowsExceptionWhenOldApiPathAndMavenIsConfigured() {
Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
// then
assertThat(result).isInstanceOf(IllegalArgumentException.class);
assertThat(result.getMessage()).contains(CliOption.OLD_API_PATH.asCliOption());
assertThat(result.getMessage()).contains("oldApiPath");
assertThat(result.getMessage()).containsIgnoringCase("maven");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class RunnerTest {
@Mock
private ArtifactDownloaderHandler artifactDownloaderHandler;

@Mock
private OptionsValidator optionsValidator;

@InjectMocks
private Runner underTest;

Expand Down

0 comments on commit 8bfa505

Please sign in to comment.