From 2ca7fd30ef096c5eca3a02ccf983e593d5eabdb2 Mon Sep 17 00:00:00 2001 From: Holger Veltrup Date: Mon, 9 Dec 2024 15:19:42 +0100 Subject: [PATCH] test: add tests --- .../domain/entity/DailyScheduling.java | 7 +- .../LinkCheckerBackgroundExecution.java | 35 ++++-- .../domain/entity/LinkCheckerConfig.java | 37 ++++++- .../entity/LinkCheckerExcludePatternType.java | 3 +- .../domain/entity/LinkCheckerLink.java | 17 ++- .../domain/entity/LinkCheckerLinkFilter.java | 6 +- .../domain/entity/LinkCheckerResult.java | 6 +- .../domain/entity/LinkCheckerResultItem.java | 8 ++ .../entity/LinkCheckerResultStatistic.java | 4 +- .../domain/entity/PublishedExternalLink.java | 17 +++ .../LinkCheckerDisabledException.java | 18 ++++ .../port/PublishedExternalLinkRepository.java | 4 + .../LinkCheckerExcludesPatternMatcher.java | 10 +- .../usecase/BackgroundLinkCheck.java | 17 ++- .../usecase/LinkCheckByHashes.java | 4 + .../usecase/StoreLinkCheckerConfig.java | 13 ++- .../domain/entity/DailySchedulingTest.java | 65 ++++++++++++ .../LinkCheckerBackgroundExecutionTest.java | 100 ++++++++++++++++++ .../domain/entity/LinkCheckerConfigTest.java | 46 ++++++++ .../entity/LinkCheckerLinkFilterTest.java | 99 +++++++++++++++++ .../domain/entity/LinkCheckerLinkTest.java | 74 +++++++++++++ .../entity/LinkCheckerResultItemTest.java | 92 ++++++++++++++++ .../LinkCheckerResultStatisticTest.java | 86 +++++++++++++++ .../domain/entity/LinkCheckerResultTest.java | 98 +++++++++++++++++ .../entity/PublishedExternalLinkTest.java | 83 +++++++++++++++ ...LinkCheckerExcludesPatternMatcherTest.java | 74 +++++++++++++ .../usecase/BackgroundLinkCheckTest.java | 94 ++++++++++++++++ .../usecase/GetCheckResultTest.java | 40 +++++++ .../usecase/GetCheckResultsStatisticTest.java | 39 +++++++ .../usecase/GetLinkCheckerConfigTest.java | 39 +++++++ .../usecase/LinkCheckByHashesTest.java | 86 +++++++++++++++ .../usecase/StoreLinkCheckerConfigTest.java | 50 +++++++++ 32 files changed, 1348 insertions(+), 23 deletions(-) create mode 100644 linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/exception/LinkCheckerDisabledException.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailySchedulingTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecutionTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilterTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatisticTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcherTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheckTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultsStatisticTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetLinkCheckerConfigTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashesTest.java create mode 100644 linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfigTest.java diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailyScheduling.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailyScheduling.java index d5a76c2..e80ffd6 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailyScheduling.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailyScheduling.java @@ -7,7 +7,7 @@ import java.util.Objects; @JsonDeserialize(builder = DailyScheduling.Builder.class) -public class DailyScheduling implements Scheduling { +public final class DailyScheduling implements Scheduling { private final LocalTime startTime; @@ -48,6 +48,11 @@ public boolean equals(Object o) { return Objects.equals(this.startTime, that.startTime); } + @Override + public String toString() { + return "DailyScheduling [startTime=" + startTime + "]"; + } + @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build") public static class Builder { diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecution.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecution.java index 14ac6e6..7cdb1ae 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecution.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecution.java @@ -8,7 +8,7 @@ import java.util.Objects; import java.util.function.Consumer; -public class LinkCheckerBackgroundExecution { +public final class LinkCheckerBackgroundExecution { private final int parallel; @@ -44,7 +44,7 @@ public Consumer getOperation() { @Override public final int hashCode() { - return Objects.hash(Arrays.hashCode(this.topic)); + return Objects.hash(this.parallel, Arrays.hashCode(this.topic), this.links, this.operation); } @Override @@ -54,23 +54,33 @@ public final boolean equals(Object o) { return false; } - return Arrays.equals(this.topic, that.topic); + return Arrays.equals(this.topic, that.topic) + && this.parallel == that.parallel + && Objects.equals(this.links, that.links) + && Objects.equals(this.operation, that.operation); } @Override public String toString() { - StringBuilder b = - new StringBuilder(100) - .append("EntityBackgroundExecution[topic:") - .append(Arrays.toString(this.topic)) - .append(']'); - return b.toString(); + return "LinkCheckerBackgroundExecution [parallel=" + + parallel + + ", topic=" + + Arrays.toString(topic) + + ", links=" + + links + + ", operation=" + + operation + + "]"; } public static Builder builder() { return new Builder(); } + public Builder toBuilder() { + return new Builder(this); + } + public static class Builder { private int parallel; @@ -83,6 +93,13 @@ public static class Builder { private Builder() {} + private Builder(LinkCheckerBackgroundExecution instance) { + this.parallel = instance.parallel; + this.topic = instance.topic; + this.links.addAll(instance.links); + this.operation = instance.operation; + } + public Builder parallel(int parallel) { this.parallel = parallel; return this; diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfig.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfig.java index ca16f52..5328d45 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfig.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfig.java @@ -10,6 +10,8 @@ @JsonDeserialize(builder = LinkCheckerConfig.Builder.class) public final class LinkCheckerConfig { + private final boolean enabled; + private final int parallel; private final int timeout; @@ -19,12 +21,17 @@ public final class LinkCheckerConfig { private final List excludes; protected LinkCheckerConfig(Builder builder) { + this.enabled = builder.enabled; this.parallel = builder.parallel; this.timeout = builder.timeout; this.scheduling = builder.scheduling; this.excludes = builder.excludes; } + public boolean isEnabled() { + return this.enabled; + } + public int getParallel() { return this.parallel; } @@ -51,7 +58,7 @@ public Builder toBuilder() { @Override public int hashCode() { - return Objects.hash(this.timeout, this.parallel, this.scheduling, this.excludes); + return Objects.hash(this.enabled, this.timeout, this.parallel, this.scheduling, this.excludes); } @Override @@ -61,15 +68,33 @@ public boolean equals(Object o) { return false; } - return Objects.equals(this.timeout, that.timeout) + return Objects.equals(this.enabled, that.enabled) + && Objects.equals(this.timeout, that.timeout) && Objects.equals(this.parallel, that.parallel) && Objects.equals(this.scheduling, that.scheduling) && Objects.equals(this.excludes, that.excludes); } + @Override + public String toString() { + return "LinkCheckerConfig [enabled=" + + enabled + + ", parallel=" + + parallel + + ", timeout=" + + timeout + + ", scheduling=" + + scheduling + + ", excludes=" + + excludes + + "]"; + } + @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build") public static class Builder { + private boolean enabled; + private int parallel; private int timeout; @@ -81,8 +106,16 @@ public static class Builder { protected Builder() {} protected Builder(LinkCheckerConfig instance) { + this.enabled = instance.enabled; + this.parallel = instance.parallel; this.timeout = instance.timeout; this.excludes.addAll(instance.excludes); + this.scheduling = instance.scheduling; + } + + public Builder enabled(boolean enabled) { + this.enabled = enabled; + return this; } public Builder scheduling(Scheduling scheduling) { diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerExcludePatternType.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerExcludePatternType.java index 60513d4..7d8d3af 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerExcludePatternType.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerExcludePatternType.java @@ -3,5 +3,6 @@ public enum LinkCheckerExcludePatternType { REGEX, CONTAINS, - GLOB + GLOB, + EXACT } diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLink.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLink.java index 185373f..21a2ba0 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLink.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLink.java @@ -1,8 +1,11 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.Objects; -public class LinkCheckerLink { +@JsonDeserialize(builder = LinkCheckerLink.Builder.class) +public final class LinkCheckerLink { private final String hash; @@ -38,7 +41,7 @@ public Builder toBuilder() { @Override public int hashCode() { - return Objects.hash(this.hash, this.url); + return Objects.hash(this.hash, this.url, this.timeout); } @Override @@ -48,9 +51,17 @@ public boolean equals(Object o) { return false; } - return Objects.equals(this.hash, that.hash) && Objects.equals(this.url, that.url); + return Objects.equals(this.hash, that.hash) + && Objects.equals(this.url, that.url) + && Objects.equals(this.timeout, that.timeout); } + @Override + public String toString() { + return "LinkCheckerLink [hash=" + hash + ", url=" + url + ", timeout=" + timeout + "]"; + } + + @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build") public static class Builder { private String hash; diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilter.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilter.java index 4a86b3f..6d2c68a 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilter.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilter.java @@ -1,5 +1,7 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; +import com.fasterxml.jackson.annotation.JsonSetter; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.ArrayList; import java.util.Collection; @@ -7,7 +9,8 @@ import java.util.List; import java.util.Objects; -public class LinkCheckerLinkFilter { +@JsonDeserialize(builder = LinkCheckerLinkFilter.Builder.class) +public final class LinkCheckerLinkFilter { private final List terms; @@ -73,6 +76,7 @@ private Builder(LinkCheckerLinkFilter instance) { this.statusTypes.addAll(instance.statusTypes); } + @JsonSetter public Builder terms(Collection terms) { Objects.requireNonNull(terms, "terms is null"); this.terms.clear(); diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResult.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResult.java index 9d25fed..2c063c5 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResult.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResult.java @@ -1,11 +1,14 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; -public class LinkCheckerResult { +@JsonDeserialize(builder = LinkCheckerResult.Builder.class) +public final class LinkCheckerResult { private final List items; @@ -75,6 +78,7 @@ public String toString() { + "]"; } + @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build") public static class Builder { private final List items = new ArrayList<>(); diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItem.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItem.java index 0112d33..e31abd8 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItem.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItem.java @@ -1,5 +1,7 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -7,6 +9,7 @@ import java.util.List; import java.util.Objects; +@JsonDeserialize(builder = LinkCheckerResultItem.Builder.class) public final class LinkCheckerResultItem { private final String url; private final String hash; @@ -26,6 +29,10 @@ public String getUrl() { return this.url; } + public String getHash() { + return this.hash; + } + public StatusType getStatus() { return this.status; } @@ -80,6 +87,7 @@ public String toString() { + "]"; } + @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build") public static class Builder { private String url; diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatistic.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatistic.java index 3dc371e..b90e8d0 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatistic.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatistic.java @@ -1,12 +1,14 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; -public class LinkCheckerResultStatistic { +@JsonDeserialize(builder = LinkCheckerResultStatistic.Builder.class) +public final class LinkCheckerResultStatistic { private final List statusCounts; diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLink.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLink.java index fae8054..2877efb 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLink.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLink.java @@ -1,7 +1,10 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.Objects; +@JsonDeserialize(builder = PublishedExternalLink.Builder.class) public final class PublishedExternalLink { private final String entity; @@ -61,6 +64,20 @@ public boolean equals(Object o) { && Objects.equals(this.url, that.url); } + @Override + public String toString() { + return "PublishedExternalLink [entity=" + + entity + + ", channel=" + + channel + + ", section=" + + section + + ", url=" + + url + + "]"; + } + + @JsonPOJOBuilder(withPrefix = "", buildMethodName = "build") public static class Builder { private String entity; diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/exception/LinkCheckerDisabledException.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/exception/LinkCheckerDisabledException.java new file mode 100644 index 0000000..04a2f17 --- /dev/null +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/domain/exception/LinkCheckerDisabledException.java @@ -0,0 +1,18 @@ +package com.sitepark.ies.publisher.core.linkchecker.domain.exception; + +public class LinkCheckerDisabledException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public LinkCheckerDisabledException() { + super(); + } + + public LinkCheckerDisabledException(String message) { + super(message); + } + + public LinkCheckerDisabledException(String message, Throwable t) { + super(message, t); + } +} diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/port/PublishedExternalLinkRepository.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/port/PublishedExternalLinkRepository.java index 1133069..4c32f7d 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/port/PublishedExternalLinkRepository.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/port/PublishedExternalLinkRepository.java @@ -16,6 +16,8 @@ public interface PublishedExternalLinkRepository { List getLinks(Collection hashes); + List getLinks(LinkCheckerLinkFilter filter); + LinkCheckerResult getCheckResult(LinkCheckerLinkFilter filter, int start, int limit); LinkCheckerResultStatistic getCheckResultsStatistic(); @@ -23,4 +25,6 @@ public interface PublishedExternalLinkRepository { void updateCheckResult(String hash, LinkCheckerResultItem result); void cleanupUnusedLinks(); + + void resetResults(); } diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcher.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcher.java index 1188d98..859946f 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcher.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcher.java @@ -31,8 +31,10 @@ private boolean matches(String url, LinkCheckerExcludePattern pattern) { return this.matchContains(url, pattern.pattern()); case LinkCheckerExcludePatternType.GLOB: return this.matchGlob(url, pattern.pattern()); + case LinkCheckerExcludePatternType.EXACT: + return this.matchExact(url, pattern.pattern()); } - return true; + throw new IllegalArgumentException("Unsupported pattern type: " + pattern.type()); } private boolean matchRegex(String url, String pattern) { @@ -40,7 +42,7 @@ private boolean matchRegex(String url, String pattern) { } private boolean matchContains(String url, String pattern) { - return url.indexOf("pattern") != -1; + return url.indexOf(pattern) != -1; } private boolean matchGlob(String url, String pattern) { @@ -48,4 +50,8 @@ private boolean matchGlob(String url, String pattern) { java.nio.file.FileSystems.getDefault().getPathMatcher("glob:" + pattern); return pathMatcher.matches(java.nio.file.Paths.get(url)); } + + private boolean matchExact(String url, String pattern) { + return url.equals(pattern); + } } diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheck.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheck.java index 5e76496..4aec8b3 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheck.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheck.java @@ -3,9 +3,11 @@ import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerBackgroundExecution; import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerConfig; import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerLink; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerLinkFilter; import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerResultItem; import com.sitepark.ies.publisher.core.linkchecker.domain.entity.StatusType; import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.LinkCheckerDisabledException; import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; import com.sitepark.ies.publisher.core.linkchecker.port.LinkChecker; import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerBackgroundExecutor; @@ -43,6 +45,15 @@ public BackgroundLinkCheck( * @return BackgroundExecution ID that can be used to track the progress */ public String backgroundLinkCheck() { + return backgroundLinkCheck(LinkCheckerLinkFilter.builder().build()); + } + + /** + * Check the links with the help of a background operation + * + * @return BackgroundExecution ID that can be used to track the progress + */ + public String backgroundLinkCheck(LinkCheckerLinkFilter filter) { if (!this.accessControl.isAllowRunLinkChecker()) { throw new AccessDeniedException("Not allowed to run link checker"); @@ -50,12 +61,16 @@ public String backgroundLinkCheck() { LinkCheckerConfig config = this.linkCheckerConfigStore.get(); + if (!config.isEnabled()) { + throw new LinkCheckerDisabledException("Link checker is disabled"); + } + LinkCheckerExcludesPatternMatcher linkCheckerExcludesPatternMatcher = new LinkCheckerExcludesPatternMatcher(config.getExcludes()); this.publishedExternalLinkRepository.cleanupUnusedLinks(); - List linksToCheck = this.publishedExternalLinkRepository.getLinks(); + List linksToCheck = this.publishedExternalLinkRepository.getLinks(filter); LinkCheckerBackgroundExecution execution = LinkCheckerBackgroundExecution.builder() diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashes.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashes.java index c178d04..6719a42 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashes.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashes.java @@ -5,6 +5,7 @@ import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerResultItem; import com.sitepark.ies.publisher.core.linkchecker.domain.entity.StatusType; import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.LinkCheckerDisabledException; import com.sitepark.ies.publisher.core.linkchecker.domain.exception.LinkCheckerException; import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; import com.sitepark.ies.publisher.core.linkchecker.port.LinkChecker; @@ -48,6 +49,9 @@ public List linkCheckByHashes(Collection hashes) } LinkCheckerConfig config = this.linkCheckerConfigStore.get(); + if (!config.isEnabled()) { + throw new LinkCheckerDisabledException("Link checker is disabled"); + } LinkCheckerExcludesPatternMatcher linkCheckerExcludesPatternMatcher = new LinkCheckerExcludesPatternMatcher(config.getExcludes()); diff --git a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfig.java b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfig.java index 402dcfa..5c13c12 100644 --- a/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfig.java +++ b/linkchecker/src/main/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfig.java @@ -5,6 +5,7 @@ import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerConfigStore; import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerScheduler; +import com.sitepark.ies.publisher.core.linkchecker.port.PublishedExternalLinkRepository; import jakarta.inject.Inject; public class StoreLinkCheckerConfig { @@ -15,14 +16,18 @@ public class StoreLinkCheckerConfig { private final LinkCheckerScheduler linkCheckerScheduler; + private final PublishedExternalLinkRepository publishedExternalLinkRepository; + @Inject public StoreLinkCheckerConfig( AccessControl accessControl, LinkCheckerConfigStore linkCheckerConfigStore, - LinkCheckerScheduler linkCheckerScheduler) { + LinkCheckerScheduler linkCheckerScheduler, + PublishedExternalLinkRepository publishedExternalLinkRepository) { this.accessControl = accessControl; this.linkCheckerConfigStore = linkCheckerConfigStore; this.linkCheckerScheduler = linkCheckerScheduler; + this.publishedExternalLinkRepository = publishedExternalLinkRepository; } public void store(LinkCheckerConfig config) { @@ -31,7 +36,13 @@ public void store(LinkCheckerConfig config) { throw new AccessDeniedException("Not allowed to store link checker config"); } + LinkCheckerConfig oldConfig = this.linkCheckerConfigStore.get(); + this.linkCheckerConfigStore.store(config); + if (!config.isEnabled() && oldConfig.isEnabled()) { + this.publishedExternalLinkRepository.resetResults(); + } + this.linkCheckerScheduler.configChanged(); } } diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailySchedulingTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailySchedulingTest.java new file mode 100644 index 0000000..ffa637f --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/DailySchedulingTest.java @@ -0,0 +1,65 @@ +package com.sitepark.ies.publisher.core.linkchecker.domain.entity; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; + +class DailySchedulingTest { + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + void testEquals() { + EqualsVerifier.forClass(DailyScheduling.class).verify(); + } + + @Test + void testToBuilder() { + DailyScheduling scheduling = DailyScheduling.builder().startTime(3, 0).build(); + + DailyScheduling copy = scheduling.toBuilder().build(); + + assertEquals(scheduling, copy, "Unexpected copy after toBuilder"); + } + + @Test + void testToString() { + DailyScheduling scheduling = DailyScheduling.builder().startTime(3, 0).build(); + + assertEquals( + "DailyScheduling [startTime=03:00]", scheduling.toString(), "Unexpected toString output"); + } + + @Test + void testSerialize() throws JsonProcessingException { + DailyScheduling scheduling = DailyScheduling.builder().startTime(3, 0).build(); + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String json = mapper.writeValueAsString(scheduling); + assertEquals( + "{\"startTime\":\"03:00\",\"type\":\"daily\"}", json, "Unexpected JSON serialization"); + } + + @Test + void testDeserialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String data = "{\"startTime\":\"03:00\",\"type\":\"daily\"}"; + + DailyScheduling scheduling = mapper.readValue(data, DailyScheduling.class); + + DailyScheduling expected = DailyScheduling.builder().startTime(3, 0).build(); + + assertEquals(expected, scheduling, "Unexpected scheduling deserialization"); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecutionTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecutionTest.java new file mode 100644 index 0000000..9b0143b --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerBackgroundExecutionTest.java @@ -0,0 +1,100 @@ +package com.sitepark.ies.publisher.core.linkchecker.domain.entity; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; +import java.util.List; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; + +class LinkCheckerBackgroundExecutionTest { + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + void testEquals() { + EqualsVerifier.forClass(LinkCheckerBackgroundExecution.class).verify(); + } + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(LinkCheckerBackgroundExecution.class) + .withClassName(NameStyle.SIMPLE_NAME) + .verify(); + } + + @Test + void testMissintTopic() { + assertThrows( + IllegalStateException.class, () -> LinkCheckerBackgroundExecution.builder().build()); + } + + @Test + void testParallel() { + LinkCheckerBackgroundExecution execution = + LinkCheckerBackgroundExecution.builder().parallel(2).topic("test").build(); + + assertEquals(2, execution.getParallel(), "Unexpected parallel"); + } + + @Test + void testTopic() { + LinkCheckerBackgroundExecution execution = + LinkCheckerBackgroundExecution.builder().topic(new String[] {"topic1", "topic2"}).build(); + + assertArrayEquals(new String[] {"topic1", "topic2"}, execution.getTopic(), "Unexpected topic"); + } + + @Test + void testLinks() { + LinkCheckerBackgroundExecution execution = + LinkCheckerBackgroundExecution.builder() + .topic("test") + .links( + List.of( + LinkCheckerLink.builder().url("http://example.com").build(), + LinkCheckerLink.builder().url("http://example.org").build())) + .build(); + + assertEquals( + List.of( + LinkCheckerLink.builder().url("http://example.com").build(), + LinkCheckerLink.builder().url("http://example.org").build()), + execution.getLinks(), + "Unexpected links"); + } + + @Test + void testOperation() { + LinkCheckerBackgroundExecution execution = + LinkCheckerBackgroundExecution.builder().topic("test").operation(link -> {}).build(); + + assertNotNull(execution.getOperation(), "operation not set"); + } + + @Test + void testToBuilder() { + LinkCheckerBackgroundExecution execution = + LinkCheckerBackgroundExecution.builder() + .parallel(2) + .topic(new String[] {"topic1", "topic2"}) + .link(LinkCheckerLink.builder().url("http://example.com").build()) + .build() + .toBuilder() + .parallel(3) + .build(); + + LinkCheckerBackgroundExecution expected = + LinkCheckerBackgroundExecution.builder() + .parallel(3) + .topic(new String[] {"topic1", "topic2"}) + .link(LinkCheckerLink.builder().url("http://example.com").build()) + .build(); + + assertEquals(expected, execution, "Unexpected copy after toBuilder"); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfigTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfigTest.java index a66a403..ced983b 100644 --- a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfigTest.java +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerConfigTest.java @@ -1,11 +1,14 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.Test; @@ -17,6 +20,49 @@ void testEquals() { EqualsVerifier.forClass(LinkCheckerConfig.class).verify(); } + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(LinkCheckerConfig.class) + .withClassName(NameStyle.SIMPLE_NAME) + .verify(); + } + + @Test + void testToBuilder() { + LinkCheckerConfig config = + LinkCheckerConfig.builder() + .parallel(3) + .timeout(10) + .scheduling(DailyScheduling.builder().startTime(3, 0).build()) + .exclude(new LinkCheckerExcludePattern(LinkCheckerExcludePatternType.REGEX, ".*\\.pdf")) + .build() + .toBuilder() + .timeout(20) + .build(); + + LinkCheckerConfig expected = + LinkCheckerConfig.builder() + .parallel(3) + .timeout(20) + .scheduling(DailyScheduling.builder().startTime(3, 0).build()) + .exclude(new LinkCheckerExcludePattern(LinkCheckerExcludePatternType.REGEX, ".*\\.pdf")) + .build(); + assertEquals(expected, config, "Unexpected config after toBuilder"); + } + + @Test + void testParallelToLow() { + assertThrows( + IllegalArgumentException.class, () -> LinkCheckerConfig.builder().parallel(0).build()); + } + + @Test + void testParallelToHigh() { + assertThrows( + IllegalArgumentException.class, () -> LinkCheckerConfig.builder().parallel(6).build()); + } + @Test void testDeserialize() throws JsonProcessingException { diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilterTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilterTest.java new file mode 100644 index 0000000..37ca731 --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkFilterTest.java @@ -0,0 +1,99 @@ +package com.sitepark.ies.publisher.core.linkchecker.domain.entity; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; +import java.util.Arrays; +import java.util.List; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; + +class LinkCheckerLinkFilterTest { + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + void testEquals() { + EqualsVerifier.forClass(LinkCheckerLinkFilter.class).verify(); + } + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(LinkCheckerLinkFilter.class) + .withClassName(NameStyle.SIMPLE_NAME) + .verify(); + } + + @Test + void testTermsArray() { + LinkCheckerLinkFilter filter = + LinkCheckerLinkFilter.builder().terms(new String[] {"term1", "term2"}).build(); + + assertEquals(Arrays.asList("term1", "term2"), filter.getTerms(), "Unexpected terms"); + } + + @Test + void testToBuilder() { + LinkCheckerLinkFilter filter = + LinkCheckerLinkFilter.builder() + .terms(List.of("term1", "term2")) + .statusTypes(List.of(StatusType.UNKNOWN)) + .build() + .toBuilder() + .statusTypes(List.of(StatusType.OK)) + .build(); + + LinkCheckerLinkFilter expected = + LinkCheckerLinkFilter.builder() + .terms(List.of("term1", "term2")) + .statusTypes(List.of(StatusType.OK)) + .build(); + + assertEquals(expected, filter, "Unexpected copy after toBuilder"); + } + + @Test + void testSerialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + LinkCheckerLinkFilter filter = + LinkCheckerLinkFilter.builder() + .terms(List.of("term1", "term2")) + .statusTypes(List.of(StatusType.OK)) + .build(); + + String json = mapper.writeValueAsString(filter); + assertEquals( + "{\"terms\":[\"term1\",\"term2\"],\"statusTypes\":[\"OK\"]}", + json, + "Unexpected JSON output"); + } + + @Test + void testDeserialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String data = "{\"terms\":[\"term1\",\"term2\"],\"statusTypes\":[\"OK\"]}"; + + LinkCheckerLinkFilter filter = mapper.readValue(data, LinkCheckerLinkFilter.class); + + LinkCheckerLinkFilter expected = + LinkCheckerLinkFilter.builder() + .terms(List.of("term1", "term2")) + .statusTypes(List.of(StatusType.OK)) + .build(); + + assertEquals(expected, filter, "Unexpected filter deserialization"); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkTest.java new file mode 100644 index 0000000..99c7b3e --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerLinkTest.java @@ -0,0 +1,74 @@ +package com.sitepark.ies.publisher.core.linkchecker.domain.entity; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; + +class LinkCheckerLinkTest { + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + void testEquals() { + EqualsVerifier.forClass(LinkCheckerLink.class).verify(); + } + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(LinkCheckerLink.class).withClassName(NameStyle.SIMPLE_NAME).verify(); + } + + @Test + public void testToBuilder() { + LinkCheckerLink link = + LinkCheckerLink.builder().hash("hash").url("http://example.com").timeout(5).build(); + + LinkCheckerLink copy = link.toBuilder().timeout(10).build(); + + LinkCheckerLink expected = + LinkCheckerLink.builder().hash("hash").url("http://example.com").timeout(10).build(); + + assertEquals(expected, copy, "Unexpected link after toBuilder"); + } + + @Test + void testSerialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + LinkCheckerLink link = + LinkCheckerLink.builder().hash("hash").url("http://example.com").timeout(5).build(); + + String json = mapper.writeValueAsString(link); + assertEquals( + "{\"hash\":\"hash\",\"url\":\"http://example.com\",\"timeout\":5}", + json, + "Unexpected JSON output"); + } + + @Test + void testDeserialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String data = "{\"hash\":\"hash\",\"url\":\"http://example.com\",\"timeout\":5}"; + + LinkCheckerLink link = mapper.readValue(data, LinkCheckerLink.class); + + LinkCheckerLink expected = + LinkCheckerLink.builder().hash("hash").url("http://example.com").timeout(5).build(); + + assertEquals(expected, link, "Unexpected link deserialization"); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItemTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItemTest.java index 7eb1404..6645e34 100644 --- a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItemTest.java +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultItemTest.java @@ -1,5 +1,13 @@ package com.sitepark.ies.publisher.core.linkchecker.domain.entity; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.Test; @@ -10,4 +18,88 @@ class LinkCheckerResultItemTest { void testEquals() { EqualsVerifier.forClass(LinkCheckerResultItem.class).verify(); } + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(LinkCheckerResultItem.class) + .withClassName(NameStyle.SIMPLE_NAME) + .verify(); + } + + @Test + void testToBuilder() { + LinkCheckerResultItem item = + LinkCheckerResultItem.builder() + .url("http://example.com") + .hash("hash") + .status(StatusType.OK) + .message("message") + .entity("123") + .entity("345") + .build() + .toBuilder() + .message("new message") + .build(); + + LinkCheckerResultItem expected = + LinkCheckerResultItem.builder() + .url("http://example.com") + .hash("hash") + .status(StatusType.OK) + .message("new message") + .entity("123") + .entity("345") + .build(); + assertEquals(expected, item, "Unexpected item after toBuilder"); + } + + @Test + void testDeserialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String data = + "{\"url\":\"http://example.com\",\"hash\":\"hash\",\"status\":\"OK\",\"message\":\"message\",\"entities\":[\"123\",\"345\"]}"; + + LinkCheckerResultItem item = mapper.readValue(data, LinkCheckerResultItem.class); + + LinkCheckerResultItem expected = + LinkCheckerResultItem.builder() + .url("http://example.com") + .hash("hash") + .status(StatusType.OK) + .message("message") + .entity("123") + .entity("345") + .build(); + + assertEquals(expected, item, "Unexpected item deserialization"); + } + + @Test + void testSerialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + LinkCheckerResultItem item = + LinkCheckerResultItem.builder() + .url("http://example.com") + .hash("hash") + .status(StatusType.OK) + .message("message") + .entity("123") + .entity("345") + .build(); + + String json = mapper.writeValueAsString(item); + assertEquals( + "{\"url\":\"http://example.com\",\"hash\":\"hash\",\"status\":\"OK\",\"message\":\"message\",\"entities\":[\"123\",\"345\"]}", + json, + "Unexpected JSON output"); + } } diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatisticTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatisticTest.java new file mode 100644 index 0000000..c91de1e --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultStatisticTest.java @@ -0,0 +1,86 @@ +package com.sitepark.ies.publisher.core.linkchecker.domain.entity; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; + +class LinkCheckerResultStatisticTest { + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + void testEquals() { + EqualsVerifier.forClass(LinkCheckerResultStatistic.class).verify(); + } + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(LinkCheckerResultStatistic.class) + .withClassName(NameStyle.SIMPLE_NAME) + .verify(); + } + + @Test + public void testToBuilder() { + LinkCheckerResultStatistic statistic = + LinkCheckerResultStatistic.builder() + .statusCount(new StatusTypeCount(StatusType.OK, 5)) + .build() + .toBuilder() + .build(); + + LinkCheckerResultStatistic expected = + LinkCheckerResultStatistic.builder() + .statusCount(new StatusTypeCount(StatusType.OK, 5)) + .build(); + + assertEquals(expected, statistic, "Unexpected statistic after toBuilder"); + } + + @Test + void testSerialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + LinkCheckerResultStatistic statistic = + LinkCheckerResultStatistic.builder() + .statusCount(new StatusTypeCount(StatusType.OK, 5)) + .build() + .toBuilder() + .build(); + + String json = mapper.writeValueAsString(statistic); + assertEquals( + "{\"statusCounts\":[{\"statusType\":\"OK\",\"count\":5}]}", json, "Unexpected JSON output"); + } + + @Test + void testDeserialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String data = "{\"statusCounts\":[{\"statusType\":\"OK\",\"count\":5}]}"; + + LinkCheckerResultStatistic statistic = mapper.readValue(data, LinkCheckerResultStatistic.class); + + LinkCheckerResultStatistic expected = + LinkCheckerResultStatistic.builder() + .statusCount(new StatusTypeCount(StatusType.OK, 5)) + .build() + .toBuilder() + .build(); + + assertEquals(expected, statistic, "Unexpected statistic deserialization"); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultTest.java new file mode 100644 index 0000000..51e9fa0 --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/LinkCheckerResultTest.java @@ -0,0 +1,98 @@ +package com.sitepark.ies.publisher.core.linkchecker.domain.entity; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.junit.jupiter.api.Test; + +class LinkCheckerResultTest { + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + void testEquals() { + EqualsVerifier.forClass(LinkCheckerResult.class).verify(); + } + + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(LinkCheckerResult.class) + .withClassName(NameStyle.SIMPLE_NAME) + .verify(); + } + + @Test + void testToBuilder() { + LinkCheckerResult result = + LinkCheckerResult.builder() + .total(2) + .start(1) + .limit(3) + .item(LinkCheckerResultItem.builder().url("http://example.com").build()) + .build() + .toBuilder() + .limit(4) + .build(); + + LinkCheckerResult expected = + LinkCheckerResult.builder() + .total(2) + .start(1) + .limit(4) + .item(LinkCheckerResultItem.builder().url("http://example.com").build()) + .build(); + + assertEquals(expected, result, "Unexpected copy after toBuilder"); + } + + @Test + void testSerialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + LinkCheckerResult result = + LinkCheckerResult.builder() + .total(2) + .start(1) + .limit(3) + .item(LinkCheckerResultItem.builder().url("http://example.com").build()) + .build(); + + String json = mapper.writeValueAsString(result); + assertEquals( + "{\"items\":[{\"url\":\"http://example.com\",\"hash\":null,\"status\":null,\"message\":null,\"entities\":[]}],\"total\":2,\"start\":1,\"limit\":3}", + json, + "Unexpected JSON output"); + } + + @Test + void testDeserialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String data = + "{\"items\":[{\"url\":\"http://example.com\",\"hash\":null,\"status\":null,\"message\":null,\"entities\":[]}],\"total\":2,\"start\":1,\"limit\":3}"; + + LinkCheckerResult result = mapper.readValue(data, LinkCheckerResult.class); + + LinkCheckerResult expected = + LinkCheckerResult.builder() + .total(2) + .start(1) + .limit(3) + .item(LinkCheckerResultItem.builder().url("http://example.com").build()) + .build(); + + assertEquals(expected, result, "Unexpected result deserialization"); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLinkTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLinkTest.java index 2b70135..ade1226 100644 --- a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLinkTest.java +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/domain/entity/PublishedExternalLinkTest.java @@ -2,6 +2,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.jparams.verifier.tostring.NameStyle; +import com.jparams.verifier.tostring.ToStringVerifier; import nl.jqno.equalsverifier.EqualsVerifier; import org.junit.jupiter.api.Test; @@ -13,6 +19,14 @@ void testEquals() { EqualsVerifier.forClass(PublishedExternalLink.class).verify(); } + @Test + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + public void testToString() { + ToStringVerifier.forClass(PublishedExternalLink.class) + .withClassName(NameStyle.SIMPLE_NAME) + .verify(); + } + @Test void testSetEntity() { PublishedExternalLink link = PublishedExternalLink.builder().entity("entity").build(); @@ -33,4 +47,73 @@ void testSetUrl() { assertEquals("http://example.com", link.getUrl(), "unexpected url"); } + + @Test + void testToBuilder() { + PublishedExternalLink link = + PublishedExternalLink.builder() + .entity("123") + .channel("2") + .section("abc") + .url("http://example.com") + .build() + .toBuilder() + .section("def") + .build(); + + PublishedExternalLink expected = + PublishedExternalLink.builder() + .entity("123") + .channel("2") + .section("def") + .url("http://example.com") + .build(); + + assertEquals(expected, link, "Unexpected link after toBuilder"); + } + + @Test + void testDeserialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + String data = + "{\"entity\":\"123\",\"channel\":\"2\",\"section\":\"abc\",\"url\":\"http://example.com\"}"; + + PublishedExternalLink link = mapper.readValue(data, PublishedExternalLink.class); + + PublishedExternalLink expected = + PublishedExternalLink.builder() + .url("http://example.com") + .channel("2") + .section("abc") + .entity("123") + .build(); + + assertEquals(expected, link, "Unexpected link deserialization"); + } + + @Test + void testSerialize() throws JsonProcessingException { + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new Jdk8Module()); + mapper.registerModule(new JavaTimeModule()); + + PublishedExternalLink link = + PublishedExternalLink.builder() + .url("http://example.com") + .channel("2") + .section("abc") + .entity("123") + .build(); + + String json = mapper.writeValueAsString(link); + assertEquals( + "{\"entity\":\"123\",\"channel\":\"2\",\"section\":\"abc\",\"url\":\"http://example.com\"}", + json, + "Unexpected JSON output"); + } } diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcherTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcherTest.java new file mode 100644 index 0000000..b46fafd --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/service/LinkCheckerExcludesPatternMatcherTest.java @@ -0,0 +1,74 @@ +package com.sitepark.ies.publisher.core.linkchecker.service; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerExcludePattern; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerExcludePatternType; +import java.util.List; +import org.junit.jupiter.api.Test; + +class LinkCheckerExcludesPatternMatcherTest { + + @Test + void testRegexPatternIsExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.REGEX, ".*\\.pdf"); + assertTrue(matcher.isExcluded("http://example.com/test.pdf")); + } + + @Test + void testRegexPatternIsNotExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.REGEX, ".*\\.pdf"); + assertFalse(matcher.isExcluded("http://example.com/test.html")); + } + + @Test + void testContainsPatternIsExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.CONTAINS, "example.com"); + assertTrue(matcher.isExcluded("http://example.com/test.pdf")); + } + + @Test + void testContainsPatternIsNotExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.CONTAINS, "example.com"); + assertFalse(matcher.isExcluded("http://example.de/test.html")); + } + + @Test + void testGlobPatternIsExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.GLOB, "**.pdf"); + assertTrue(matcher.isExcluded("http://example.com/path/test.pdf")); + } + + @Test + void testGlobPatternIsNotExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.GLOB, "**.pdf"); + assertFalse(matcher.isExcluded("http://example.de/path/test.html")); + } + + @Test + void testExactPatternIsExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.EXACT, "http://example.com/path/test.pdf"); + assertTrue(matcher.isExcluded("http://example.com/path/test.pdf")); + } + + @Test + void testExactPatternIsNotExcluded() { + LinkCheckerExcludesPatternMatcher matcher = + this.creatMatcher(LinkCheckerExcludePatternType.EXACT, "http://example.com/path/test.pdf"); + assertFalse(matcher.isExcluded("http://example.com/path/test.html")); + } + + private LinkCheckerExcludesPatternMatcher creatMatcher( + LinkCheckerExcludePatternType type, String pattern) { + LinkCheckerExcludePattern regex = new LinkCheckerExcludePattern(type, pattern); + return new LinkCheckerExcludesPatternMatcher(List.of(regex)); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheckTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheckTest.java new file mode 100644 index 0000000..894480d --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/BackgroundLinkCheckTest.java @@ -0,0 +1,94 @@ +package com.sitepark.ies.publisher.core.linkchecker.usecase; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerBackgroundExecution; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerConfig; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerExcludePattern; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerExcludePatternType; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerLink; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerResultItem; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.StatusType; +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkChecker; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerBackgroundExecutor; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerConfigStore; +import com.sitepark.ies.publisher.core.linkchecker.port.PublishedExternalLinkRepository; +import java.util.List; +import org.junit.jupiter.api.Test; + +class BackgroundLinkCheckTest { + + @Test + void testAccessDenied() { + AccessControl accessControl = mock(); + when(accessControl.isAllowRunLinkChecker()).thenReturn(false); + BackgroundLinkCheck useCase = new BackgroundLinkCheck(accessControl, null, null, null, null); + assertThrows( + AccessDeniedException.class, + () -> { + useCase.backgroundLinkCheck(); + }); + } + + @Test + void testBackgroundLinkCheck() { + AccessControl accessControl = mock(); + when(accessControl.isAllowRunLinkChecker()).thenReturn(true); + PublishedExternalLinkRepository publishedExternalLinkRepository = mock(); + LinkCheckerConfigStore linkCheckerConfigStore = mock(); + LinkCheckerConfig config = + LinkCheckerConfig.builder() + .exclude(new LinkCheckerExcludePattern(LinkCheckerExcludePatternType.CONTAINS, "test")) + .parallel(1) + .build(); + + when(linkCheckerConfigStore.get()).thenReturn(config); + + List links = + List.of( + LinkCheckerLink.builder().hash("hash1").url("https://www.test.com").build(), + LinkCheckerLink.builder().hash("hash2").url("https://www.sitepark.com").build()); + when(publishedExternalLinkRepository.getLinks()).thenReturn(links); + + LinkChecker linkChecker = mock(); + when(linkChecker.checkLink(any())) + .thenReturn(LinkCheckerResultItem.builder().status(StatusType.OK).build()); + + LinkCheckerBackgroundExecutor linkCheckerBackgroundExecutor = mock(); + when(linkCheckerBackgroundExecutor.execute(any(LinkCheckerBackgroundExecution.class))) + .thenAnswer( + args -> { + LinkCheckerBackgroundExecution execution = args.getArgument(0); + for (LinkCheckerLink link : links) { + execution.getOperation().accept(link); + } + return "123"; + }); + + BackgroundLinkCheck useCase = + new BackgroundLinkCheck( + accessControl, + linkChecker, + linkCheckerBackgroundExecutor, + publishedExternalLinkRepository, + linkCheckerConfigStore); + + useCase.backgroundLinkCheck(); + + verify(publishedExternalLinkRepository, times(1)).cleanupUnusedLinks(); + verify(publishedExternalLinkRepository, never()) + .updateCheckResult("hash1", LinkCheckerResultItem.builder().status(StatusType.OK).build()); + verify(publishedExternalLinkRepository) + .updateCheckResult("hash2", LinkCheckerResultItem.builder().status(StatusType.OK).build()); + verify(linkChecker) + .checkLink(LinkCheckerLink.builder().hash("hash2").url("https://www.sitepark.com").build()); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultTest.java new file mode 100644 index 0000000..b3476cf --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultTest.java @@ -0,0 +1,40 @@ +package com.sitepark.ies.publisher.core.linkchecker.usecase; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerLinkFilter; +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; +import com.sitepark.ies.publisher.core.linkchecker.port.PublishedExternalLinkRepository; +import org.junit.jupiter.api.Test; + +class GetCheckResultTest { + + @Test + void testAccessDenied() { + AccessControl accessControl = mock(); + when(accessControl.isAllowGetCheckResults()).thenReturn(false); + GetCheckResult useCase = new GetCheckResult(accessControl, null); + assertThrows( + AccessDeniedException.class, + () -> { + useCase.getCheckResult(null, 0, 0); + }); + } + + @Test + void testGetCheckResult() { + AccessControl accessControl = mock(); + PublishedExternalLinkRepository publishedExternalLinkRepository = mock(); + LinkCheckerLinkFilter filter = mock(); + + when(accessControl.isAllowGetCheckResults()).thenReturn(true); + GetCheckResult useCase = new GetCheckResult(accessControl, publishedExternalLinkRepository); + useCase.getCheckResult(filter, 1, 2); + + verify(publishedExternalLinkRepository).getCheckResult(filter, 1, 2); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultsStatisticTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultsStatisticTest.java new file mode 100644 index 0000000..86a81b3 --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetCheckResultsStatisticTest.java @@ -0,0 +1,39 @@ +package com.sitepark.ies.publisher.core.linkchecker.usecase; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; +import com.sitepark.ies.publisher.core.linkchecker.port.PublishedExternalLinkRepository; +import org.junit.jupiter.api.Test; + +class GetCheckResultsStatisticTest { + + @Test + void testAccessDenied() { + AccessControl accessControl = mock(); + when(accessControl.isAllowGetCheckResults()).thenReturn(false); + GetCheckResultsStatistic useCase = new GetCheckResultsStatistic(accessControl, null); + assertThrows( + AccessDeniedException.class, + () -> { + useCase.getCheckResultsStatistic(); + }); + } + + @Test + void testGetCheckResultsStatistic() { + AccessControl accessControl = mock(); + when(accessControl.isAllowGetCheckResults()).thenReturn(true); + PublishedExternalLinkRepository publishedExternalLinkRepository = mock(); + + GetCheckResultsStatistic useCase = + new GetCheckResultsStatistic(accessControl, publishedExternalLinkRepository); + useCase.getCheckResultsStatistic(); + + verify(publishedExternalLinkRepository).getCheckResultsStatistic(); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetLinkCheckerConfigTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetLinkCheckerConfigTest.java new file mode 100644 index 0000000..9f80811 --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/GetLinkCheckerConfigTest.java @@ -0,0 +1,39 @@ +package com.sitepark.ies.publisher.core.linkchecker.usecase; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerConfigStore; +import org.junit.jupiter.api.Test; + +class GetLinkCheckerConfigTest { + + @Test + void testAccessDenied() { + AccessControl accessControl = mock(); + when(accessControl.isAllowGetLinkCheckerConfig()).thenReturn(false); + GetLinkCheckerConfig useCase = new GetLinkCheckerConfig(accessControl, null); + assertThrows( + AccessDeniedException.class, + () -> { + useCase.get(); + }); + } + + @Test + void testGetLinkCheckerConfig() { + AccessControl accessControl = mock(); + when(accessControl.isAllowGetLinkCheckerConfig()).thenReturn(true); + + LinkCheckerConfigStore linkCheckerConfigStore = mock(); + + GetLinkCheckerConfig useCase = new GetLinkCheckerConfig(accessControl, linkCheckerConfigStore); + useCase.get(); + + verify(linkCheckerConfigStore).get(); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashesTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashesTest.java new file mode 100644 index 0000000..b35205e --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/LinkCheckByHashesTest.java @@ -0,0 +1,86 @@ +package com.sitepark.ies.publisher.core.linkchecker.usecase; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerConfig; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerExcludePattern; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerExcludePatternType; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerLink; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerResultItem; +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.StatusType; +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkChecker; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerConfigStore; +import com.sitepark.ies.publisher.core.linkchecker.port.PublishedExternalLinkRepository; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.Test; + +class LinkCheckByHashesTest { + + @Test + void testAccessDenied() { + AccessControl accessControl = mock(); + when(accessControl.isAllowRunLinkChecker()).thenReturn(false); + LinkCheckByHashes useCase = new LinkCheckByHashes(accessControl, null, null, null); + assertThrows( + AccessDeniedException.class, + () -> { + useCase.linkCheckByHashes(Collections.emptyList()); + }); + } + + @Test + void testCheck() { + + AccessControl accessControl = mock(); + when(accessControl.isAllowRunLinkChecker()).thenReturn(true); + PublishedExternalLinkRepository publishedExternalLinkRepository = mock(); + LinkCheckerConfigStore linkCheckerConfigStore = mock(); + LinkCheckerConfig config = + LinkCheckerConfig.builder() + .exclude(new LinkCheckerExcludePattern(LinkCheckerExcludePatternType.CONTAINS, "test")) + .parallel(1) + .build(); + + when(linkCheckerConfigStore.get()).thenReturn(config); + + List links = + List.of( + LinkCheckerLink.builder().hash("hash1").url("https://www.test.com").build(), + LinkCheckerLink.builder().hash("hash2").url("https://www.sitepark.com").build()); + + when(publishedExternalLinkRepository.getLinks(List.of("hash1", "hash2"))).thenReturn(links); + + LinkChecker linkChecker = mock(); + when(linkChecker.checkLink(any())) + .thenReturn(LinkCheckerResultItem.builder().status(StatusType.OK).build()); + + LinkCheckByHashes useCase = + new LinkCheckByHashes( + accessControl, linkChecker, publishedExternalLinkRepository, linkCheckerConfigStore); + + List result = useCase.linkCheckByHashes(List.of("hash1", "hash2")); + + verify(publishedExternalLinkRepository, never()) + .updateCheckResult("hash1", LinkCheckerResultItem.builder().status(StatusType.OK).build()); + verify(publishedExternalLinkRepository) + .updateCheckResult("hash2", LinkCheckerResultItem.builder().status(StatusType.OK).build()); + + assertEquals( + List.of( + LinkCheckerResultItem.builder() + .url("https://www.sitepark.com") + .hash("hash2") + .status(StatusType.OK) + .build()), + result); + } +} diff --git a/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfigTest.java b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfigTest.java new file mode 100644 index 0000000..50155ea --- /dev/null +++ b/linkchecker/src/test/java/com/sitepark/ies/publisher/core/linkchecker/usecase/StoreLinkCheckerConfigTest.java @@ -0,0 +1,50 @@ +package com.sitepark.ies.publisher.core.linkchecker.usecase; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sitepark.ies.publisher.core.linkchecker.domain.entity.LinkCheckerConfig; +import com.sitepark.ies.publisher.core.linkchecker.domain.exception.AccessDeniedException; +import com.sitepark.ies.publisher.core.linkchecker.port.AccessControl; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerConfigStore; +import com.sitepark.ies.publisher.core.linkchecker.port.LinkCheckerScheduler; +import com.sitepark.ies.publisher.core.linkchecker.port.PublishedExternalLinkRepository; +import org.junit.jupiter.api.Test; + +class StoreLinkCheckerConfigTest { + + @Test + void testAccessDenied() { + AccessControl accessControl = mock(); + when(accessControl.isAllowGetLinkCheckerConfig()).thenReturn(false); + StoreLinkCheckerConfig useCase = new StoreLinkCheckerConfig(accessControl, null, null, null); + assertThrows( + AccessDeniedException.class, + () -> { + useCase.store(null); + }); + } + + @Test + void testStoreLinkCheckerConfig() { + AccessControl accessControl = mock(); + when(accessControl.isAllowGetLinkCheckerConfig()).thenReturn(true); + LinkCheckerConfigStore linkCheckerConfigStore = mock(); + LinkCheckerScheduler linkCheckerScheduler = mock(); + LinkCheckerConfig config = mock(); + PublishedExternalLinkRepository publishedExternalLinkRepository = mock(); + + StoreLinkCheckerConfig useCase = + new StoreLinkCheckerConfig( + accessControl, + linkCheckerConfigStore, + linkCheckerScheduler, + publishedExternalLinkRepository); + useCase.store(config); + + verify(linkCheckerConfigStore).store(config); + verify(linkCheckerScheduler).configChanged(); + } +}