Skip to content

Commit

Permalink
#18 Provide a settings console instead of the OSGi config - Status code
Browse files Browse the repository at this point in the history
  • Loading branch information
vihnatsenka committed Mar 19, 2024
1 parent 3477e5c commit b7bcc08
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface UiConfigService {
String[] getExcludedProperties();
String getLinksType();
boolean isExcludeTags();
Integer[] getStatusCodes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ public class GridResourcesGeneratorImpl implements GridResourcesGenerator {
description = "Finds broken links under the specified path for further outputting them in a report"
)
@interface Configuration {

@AttributeDefinition(
name = "Status codes",
description = "The list of status codes allowed for broken links in the report. " +
"Set a single negative value to allow all http error codes"
)
int[] allowedStatusCodes() default {
HttpStatus.SC_NOT_FOUND
};

@AttributeDefinition(
name = "Threads per core",
description = "The number of threads created per each CPU core for validating links in parallel"
Expand All @@ -103,7 +93,6 @@ int[] allowedStatusCodes() default {

private ExecutorService executorService;

private int[] allowedStatusCodes;
private int threadsPerCore;

/**
Expand All @@ -113,7 +102,6 @@ int[] allowedStatusCodes() default {
@Activate
@Modified
protected void activate(Configuration configuration) {
allowedStatusCodes = configuration.allowedStatusCodes();
threadsPerCore = configuration.threadsPerCore();
}

Expand Down Expand Up @@ -313,6 +301,8 @@ private boolean isExcludedPath(String path) {
}

private boolean isAllowedErrorCode(int linkStatusCode) {
Integer[] allowedStatusCodes = uiConfigService.getStatusCodes();

if (ArrayUtils.isEmpty(allowedStatusCodes) ||
(allowedStatusCodes.length == 1 && allowedStatusCodes[0] < 0)) {
return true;
Expand Down Expand Up @@ -405,7 +395,7 @@ private Map<String, Object> getGenerationStatsMap(LinksCounter allLinksCounter,
stats.put(GenerationStatsProps.PN_REPORT_LINKS_TYPE, uiConfigService.getLinksType());
stats.put(GenerationStatsProps.PN_EXCLUDED_LINK_PATTERNS, getExcludedLinksPatterns());
stats.put(GenerationStatsProps.PN_EXCLUDED_TAGS, uiConfigService.isExcludeTags());
stats.put(GenerationStatsProps.PN_ALLOWED_STATUS_CODES, allowedStatusCodes);
stats.put(GenerationStatsProps.PN_ALLOWED_STATUS_CODES, uiConfigService.getStatusCodes());

stats.put(GenerationStatsProps.PN_ALL_INTERNAL_LINKS, allLinksCounter.getInternalLinks());
stats.put(GenerationStatsProps.PN_BROKEN_INTERNAL_LINKS, brokenLinksCounter.getInternalLinks());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class UiConfigServiceImpl implements UiConfigService {
private static final String PN_EXCLUDED_PROPERTIES = "excludedProperties";
private static final String PN_LINKS_TYPE = "linksType";
private static final String PN_EXCLUDE_TAGS = "excludeTags";
private static final String PN_STATUS_CODES = "statusCodes";
private static final String DEFAULT_PATH = "/content";

@Reference
Expand Down Expand Up @@ -78,6 +79,11 @@ public boolean isExcludeTags() {
return getProperty(PN_EXCLUDE_TAGS, Boolean.class).orElse(true);
}

@Override
public Integer[] getStatusCodes() {
return getProperty(PN_STATUS_CODES, Integer[].class).orElse(new Integer[]{});
}

private <T> Optional<T> getProperty(String name, Class<T> clazz){
try(ResourceResolver resourceResolver = repositoryHelper.getServiceResourceResolver()){
return Optional.ofNullable(resourceResolver.getResource(CONFIG_PATH))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ private GridResourcesGeneratorImpl getGridResourcesGenerator() throws NoSuchFiel
when(uiConfigService.getExcludedPaths()).thenReturn(new String[0]);
when(uiConfigService.getExcludedProperties()).thenReturn(new String[0]);
when(uiConfigService.getLinksType()).thenReturn(GenerationStatsProps.REPORT_LINKS_TYPE_ALL);
when(uiConfigService.getStatusCodes()).thenReturn(new Integer[]{HttpStatus.SC_NOT_FOUND});
PrivateAccessor.setField(gridResourcesGenerator, UI_CONFIG_FIELD, uiConfigService);
GridResourcesGeneratorImplTest.setUpConfig(gridResourcesGenerator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void setup() throws NoSuchFieldException {
when(uiConfigService.getExcludedProperties()).thenReturn(new String[]{TEST_EXCLUDED_PROPERTY});
when(uiConfigService.getLinksType()).thenReturn(GenerationStatsProps.REPORT_LINKS_TYPE_ALL);
when(uiConfigService.isExcludeTags()).thenReturn(true);
when(uiConfigService.getStatusCodes()).thenReturn(new Integer[]{HttpStatus.SC_NOT_FOUND});
PrivateAccessor.setField(fixture, UI_CONFIG_FIELD, uiConfigService);
}

Expand Down Expand Up @@ -329,49 +330,32 @@ void testGenerateGridResources_interruptionException() throws InterruptedExcepti

static void setUpConfig(GridResourcesGeneratorImpl gridResourcesGenerator) {
GridResourcesGeneratorImpl.Configuration config = mockConfig();

int[] defaultStatusCodes = {HttpStatus.SC_NOT_FOUND};
when(config.allowedStatusCodes()).thenReturn(defaultStatusCodes);
gridResourcesGenerator.activate(config);
}

private void setUpConfigNoExcludedPaths(GridResourcesGeneratorImpl gridResourcesGenerator) {
GridResourcesGeneratorImpl.Configuration config = mockConfig();

when(uiConfigService.getExcludedPaths()).thenReturn(ArrayUtils.EMPTY_STRING_ARRAY);

int[] defaultStatusCodes = {HttpStatus.SC_NOT_FOUND};
when(config.allowedStatusCodes()).thenReturn(defaultStatusCodes);

gridResourcesGenerator.activate(config);
}

private void setUpConfigCheckActivation(GridResourcesGeneratorImpl gridResourcesGenerator) {
GridResourcesGeneratorImpl.Configuration config = mockConfig();

int[] defaultStatusCodes = {HttpStatus.SC_NOT_FOUND};
when(config.allowedStatusCodes()).thenReturn(defaultStatusCodes);

when(uiConfigService.getExcludedPaths()).thenReturn(new String[]{TEST_EXCLUDED_PATH});
when(uiConfigService.isActivatedContent()).thenReturn(true);
when(uiConfigService.isSkipContentModifiedAfterActivation()).thenReturn(true);

gridResourcesGenerator.activate(config);
}

private void setUpConfigNoStatusCodes(GridResourcesGeneratorImpl gridResourcesGenerator) {
GridResourcesGeneratorImpl.Configuration config = mockConfig();

when(config.allowedStatusCodes()).thenReturn(ArrayUtils.EMPTY_INT_ARRAY);

when(uiConfigService.getStatusCodes()).thenReturn(new Integer[]{});
gridResourcesGenerator.activate(config);
}

private static GridResourcesGeneratorImpl.Configuration mockConfig() {
GridResourcesGeneratorImpl.Configuration config = mock(GridResourcesGeneratorImpl.Configuration.class);

when(config.threadsPerCore()).thenReturn(60);

return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
const $excludeTagsCheckbox = $('<coral-checkbox value="excludeTags">Exclude tags(If checked, the internal links starting with /content/cq:tags will be excluded)</coral-checkbox>');
$excludeTagsCheckbox.appendTo(dialog.content);

const statusCodesMultifield = createMultifield();
$('<p>').text("Status codes (The list of status codes allowed for broken links in the report. Set a single negative value to allow all http error codes)").appendTo(dialog.content);
dialog.content.appendChild(statusCodesMultifield);

$.ajax({
type: "GET",
url: "/content/etoolbox-link-inspector/data/config.json"
Expand All @@ -129,6 +133,7 @@
populateMultifield(excludedPropertiesMultifield, data.excludedProperties);
linksTypeSelect.value = data.linksType;
$excludeTagsCheckbox.attr("checked", data.excludeTags);
populateMultifield(statusCodesMultifield, data.statusCodes);
})

function createMultifield(){
Expand Down Expand Up @@ -180,7 +185,9 @@
"excludedProperties@TypeHint": "String[]",
"linksType": linksTypeSelect.value,
"excludeTags":!!$excludeTagsCheckbox.attr("checked"),
"excludeTags@TypeHint": "Boolean"
"excludeTags@TypeHint": "Boolean",
"statusCodes": getMultifieldValues(statusCodesMultifield),
"statusCodes@TypeHint": "String[]",
},
dataType: "json",
encode: true
Expand Down

0 comments on commit b7bcc08

Please sign in to comment.