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

Add the configurable refetchWaitSeconds parameter #99

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Committing with `git commit -s` will add the sign-off at the end of the commit m
- Simon Hülkenberg <[email protected]>
- Sven Strittmatter <[email protected]>
- Timo Pagel <[email protected]>
- Maximilian Dorner <[email protected]>
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ public final class Config {
* Default for {@link #maxPageCountForGets}
*/
static final int DEFAULT_MAX_PAGE_COUNT_FOR_GETS = 100;

/**
* Default for {@link #refetchWaitSeconds}
*/
static final int DEFAULT_REFETCH_WAIT_SECONDS = 0;

/**
* Null pattern object.
*/
public static final Config NULL = new Config("", "", DEFAULT_MAX_PAGE_COUNT_FOR_GETS);
public static final Config NULL = new Config("", "", DEFAULT_MAX_PAGE_COUNT_FOR_GETS, DEFAULT_REFETCH_WAIT_SECONDS);

/**
* URL of the host which serves the DefectDojo API.
Expand All @@ -41,6 +47,15 @@ public final class Config {
@NonNull
private final String apiKey;

/**
* Number of seconds to wait before refetching results from the DefectDojo API.
* <p>
* Defaults to {@link #DEFAULT_REFETCH_WAIT_SECONDS}.
* </p>
*/
@NonNull
private final int refetchWaitSeconds;

/**
* How many pages of objects are fetched from the DefectDojo API
* <p>
Expand All @@ -53,13 +68,13 @@ public final class Config {
private final int maxPageCountForGets;

/**
* Convenience constructor which sets {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS}
* Convenience constructor which sets {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS} and {@link #DEFAULT_REFETCH_WAIT_SECONDS}
*
* @param url not {@code null}
* @param apiKey not {@code null}
*/
public Config(final @NonNull String url, final @NonNull String apiKey) {
this(url, apiKey, DEFAULT_MAX_PAGE_COUNT_FOR_GETS);
this(url, apiKey, DEFAULT_MAX_PAGE_COUNT_FOR_GETS, DEFAULT_REFETCH_WAIT_SECONDS);
}

/**
Expand All @@ -68,12 +83,22 @@ public Config(final @NonNull String url, final @NonNull String apiKey) {
* @param url not {@code null}
* @param apiKey not {@code null}
* @param maxPageCountForGets not less than 1
* @param refetchWaitSeconds not less than 0
*/
public Config(final @NonNull String url, final @NonNull String apiKey, final int maxPageCountForGets) {
public Config(final @NonNull String url, final @NonNull String apiKey, final int maxPageCountForGets, final int refetchWaitSeconds) {
super();
this.url = url;
this.apiKey = apiKey;
this.maxPageCountForGets = validateIsGreaterZero(maxPageCountForGets, "maxPageCountForGets");
this.refetchWaitSeconds = validateIsNotNegative(refetchWaitSeconds, "refetchWaitSeconds");
}

private static int validateIsNotNegative(final int number, final String name) {
if (number < 0) {
throw new IllegalArgumentException(String.format("%s must not be negative!", name));
}

return number;
}

private static int validateIsGreaterZero(final int number, final String name) {
Expand All @@ -93,6 +118,7 @@ public static Config fromEnv() {
final var url = findRequiredEnvVar(EnvVars.DEFECTDOJO_URL);
final var apiKey = findRequiredEnvVar(EnvVars.DEFECTDOJO_APIKEY);
final int maxPageCountForGets;
final int refetchWaitSeconds;

if (hasEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)) {
try {
Expand All @@ -105,7 +131,18 @@ public static Config fromEnv() {
maxPageCountForGets = DEFAULT_MAX_PAGE_COUNT_FOR_GETS;
}

return new Config(url, apiKey, maxPageCountForGets);
if (hasEnvVar(EnvVars.DEFECTDOJO_REFETCH_WAIT_SECONDS)) {
try {
refetchWaitSeconds = Integer.parseInt(findEnvVar(EnvVars.DEFECTDOJO_REFETCH_WAIT_SECONDS));
} catch (final NumberFormatException e) {
throw new ConfigException(String.format("Given value for environment variable '%s' is not a valid number! Given was '%s'.", EnvVars.DEFECTDOJO_REFETCH_WAIT_SECONDS.literal, findEnvVar(EnvVars.DEFECTDOJO_REFETCH_WAIT_SECONDS)),
e);
}
} else {
refetchWaitSeconds = DEFAULT_REFETCH_WAIT_SECONDS;
}

return new Config(url, apiKey, maxPageCountForGets, refetchWaitSeconds);
}

private static boolean hasEnvVar(final @NonNull EnvVars name) {
Expand All @@ -131,7 +168,8 @@ private static String findRequiredEnvVar(final @NonNull EnvVars name) {
public enum EnvVars {
DEFECTDOJO_URL("DEFECTDOJO_URL"),
DEFECTDOJO_APIKEY("DEFECTDOJO_APIKEY"),
DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS");
DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS"),
DEFECTDOJO_REFETCH_WAIT_SECONDS("DEFECTDOJO_REFETCH_WAIT_SECONDS");
/**
* Literal name of configuration environment name
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ConfigTest {
@Test
void constructor_urlMustNotBeNull() {
final var thrown = assertThrows(NullPointerException.class, () -> {
new Config(null, "apiKey", 1);
new Config(null, "apiKey", 1, 1);
});

assertThat(thrown.getMessage(), startsWith("url "));
Expand All @@ -39,17 +39,27 @@ void constructor_urlMustNotBeNull() {
@Test
void constructor_apiKeyMustNotBeNull() {
final var thrown = assertThrows(NullPointerException.class, () -> {
new Config("url", null, 1);
new Config("url", null, 1, 1);
});

assertThat(thrown.getMessage(), startsWith("apiKey "));
}

@ParameterizedTest
@ValueSource(ints = {-1, -2, -23, -42, Integer.MIN_VALUE})
void constructor_refetchWaitSecondsMustNotBeLessThanZero(final int number) {
final var thrown = assertThrows(IllegalArgumentException.class, () -> {
new Config("url", "apiKey", 1, number);
});

assertThat(thrown.getMessage(), startsWith("refetchWaitSeconds "));
}

@ParameterizedTest
@ValueSource(ints = {0, -1, -2, -23, -42, Integer.MIN_VALUE})
void constructor_maxPageCountForGetsMustNotBeLessThanOne(final int number) {
final var thrown = assertThrows(IllegalArgumentException.class, () -> {
new Config("url", "apiKey", number);
new Config("url", "apiKey", number, 60 );
});

assertThat(thrown.getMessage(), startsWith("maxPageCountForGets "));
Expand All @@ -60,14 +70,16 @@ void fromEnv() {
environmentVariables
.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS", "23");
.set("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS", "23")
.set("DEFECTDOJO_REFETCH_WAIT_SECONDS", "60");

final var sut = Config.fromEnv();

assertAll(
() -> assertThat(sut.getUrl(), is("url")),
() -> assertThat(sut.getApiKey(), is("apikey")),
() -> assertThat(sut.getMaxPageCountForGets(), is(23))
() -> assertThat(sut.getMaxPageCountForGets(), is(23)),
() -> assertThat(sut.getRefetchWaitSeconds(), is(60))
);
}

Expand All @@ -91,6 +103,29 @@ void fromEnv_throwsExceptionIfNoApiKeySet() {
assertThat(thrown.getMessage(), is("Missing environment variable 'DEFECTDOJO_APIKEY'!"));
}

@Test
void fromEnv_usesDefaultIfNoRefetchWaitSecondsSet() {
environmentVariables
.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS", 1);

final var sut = Config.fromEnv();
assertThat(sut.getRefetchWaitSeconds(), is(Config.DEFAULT_REFETCH_WAIT_SECONDS));
}

@Test
void fromEnv_throwsExceptionIfRefetchWaitSecondsIsNotParseableToInteger() {
environmentVariables
.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_REFETCH_WAIT_SECONDS", "foo");

final var thrown = assertThrows(ConfigException.class, Config::fromEnv);

assertThat(thrown.getMessage(), is("Given value for environment variable 'DEFECTDOJO_REFETCH_WAIT_SECONDS' is not a valid number! Given was 'foo'."));
}

@Test
void fromEnv_usesDefaultIfNoMaxPageCountForGetSet() {
environmentVariables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class DefaultImportScanServiceTest {
private final Config config = new Config(
"http://localhost",
"apiKey",
23
23,
60
);
private final DefaultImportScanService sut = new DefaultImportScanService(config, ProxyConfig.NULL);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class FindingServiceTest {

@BeforeEach
void setup() {
config = new Config("https://defectdojo.example.com", "abc", 42);
config = new Config("https://defectdojo.example.com", "abc", 42, 60);
underTest = new FindingService(config);
mockServer = MockRestServiceServer.createServer(underTest.getRestTemplate());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void createDefault_passesConfig() {
final var config = new Config(
"url",
"apiKey",
23
23,
60
);

final var sut = (DefaultImportScanService) ImportScanService.createDefault(config, ProxyConfig.NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class UserProfileServiceTest {

@BeforeEach
void setup() {
config = new Config("https://defectdojo.example.com", "abc", 42);
config = new Config("https://defectdojo.example.com", "abc", 42, 60);
underTest = new UserProfileService(config);
mockServer = MockRestServiceServer.createServer(underTest.getRestTemplate());
}
Expand Down