Skip to content

Commit

Permalink
improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
buuhuu committed Jun 25, 2021
1 parent ee96040 commit 37c19d4
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 24 deletions.
12 changes: 12 additions & 0 deletions sitemap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.12.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.sling.sitemap.SitemapService;
import org.apache.sling.sitemap.common.SitemapUtil;
import org.apache.sling.sitemap.impl.SitemapServiceConfiguration;
import org.jetbrains.annotations.Nullable;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.annotations.Activate;
Expand Down Expand Up @@ -115,17 +116,20 @@ private void printJson(PrintWriter pw) {
while (infoIt.hasNext()) {
SitemapInfo info = infoIt.next();
pw.print('{');
pw.print("\"url\":\"");
pw.print("\"name\":\"");
pw.print(escapeDoubleQuotes(info.getName()));
pw.print('"');
pw.print(",\"url\":\"");
pw.print(escapeDoubleQuotes(info.getUrl()));
pw.print("\",\"status\":\"");
pw.print(info.getStatus());
pw.print('"');
if (info.getStoragePath() != null) {
pw.print(",\"path\":\"");
pw.print(escapeDoubleQuotes(info.getStoragePath()));
pw.print("\",\"status\":\"");
pw.print(info.getStatus());
pw.print("\",\"size\":");
pw.print(info.getSize());
pw.print(",\"entries\":");
pw.print(",\"urls\":");
pw.print(info.getEntries());
pw.print(",\"inLimits\":");
pw.print(isWithinLimits(info));
Expand All @@ -149,8 +153,9 @@ private void printJson(PrintWriter pw) {
}

private void printText(PrintWriter pw) {
pw.println("Apache Sling Sitemap Schedulers");
pw.println("-------------------------------");
pw.println("# Apache Sling Sitemap Schedulers");
pw.println("# -------------------------------");
pw.println("schedulers:");

for (ServiceReference<?> ref : bundleContext.getBundle().getRegisteredServices()) {
Object schedulerExp = ref.getProperty(Scheduler.PROPERTY_SCHEDULER_EXPRESSION);
Expand All @@ -167,35 +172,40 @@ private void printText(PrintWriter pw) {

pw.println();
pw.println();
pw.println("Apache Sling Sitemap Roots");
pw.println("--------------------------");
pw.println("# Apache Sling Sitemap Roots");
pw.println("# --------------------------");
pw.println("roots:");

try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(AUTH)) {
Iterator<Resource> roots = SitemapUtil.findSitemapRoots(resolver, "/");
while (roots.hasNext()) {
Resource root = roots.next();
pw.print(" ");
pw.print(root.getPath());
pw.print(':');
pw.println();
for (SitemapInfo info : sitemapService.getSitemapInfo(root)) {
pw.print(" - Url: ");
pw.print(" - Name: ");
pw.print(info.getName());
pw.println();
pw.print(" Url: ");
pw.print(info.getUrl());
pw.println();
pw.print(" Status: ");
pw.print(info.getStatus());
pw.println();
if (info.getStoragePath() != null) {
pw.print(" Path: ");
pw.print(" Path: ");
pw.print(info.getStoragePath());
pw.println();
pw.print(" Status: ");
pw.print(info.getStatus());
pw.println();
pw.print(" Bytes: ");
pw.print(" Size: ");
pw.print(info.getSize());
pw.println();
pw.print(" Urls: ");
pw.print(" Urls: ");
pw.print(info.getEntries());
pw.println();
pw.print(" Within Limits: ");
pw.print(isWithinLimits(info) ? "yes": "no");
pw.print(" Within Limits: ");
pw.print(isWithinLimits(info) ? "yes" : "no");
pw.println();
}
}
Expand All @@ -210,7 +220,7 @@ private boolean isWithinLimits(SitemapInfo info) {
return info.getSize() <= configuration.getMaxSize() && info.getEntries() <= configuration.getMaxEntries();
}

private static String escapeDoubleQuotes(String text) {
return text.replace("\"", "\\\"");
private static String escapeDoubleQuotes(@Nullable String text) {
return text == null ? "" : text.replace("\"", "\\\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class SitemapServiceImplTest {
private final SitemapServiceImpl subject = new SitemapServiceImpl();
private final SitemapStorage storage = new SitemapStorage();
private final SitemapGeneratorManagerImpl generatorManager = new SitemapGeneratorManagerImpl();
private final SitemapScheduler scheduler = new SitemapScheduler();
private final SitemapServiceConfiguration sitemapServiceConfiguration = new SitemapServiceConfiguration();

private TestGenerator generator = new TestGenerator() {
Expand All @@ -73,6 +74,8 @@ public class SitemapServiceImplTest {

@Mock
private ServiceUserMapped serviceUser;
@Mock
private JobManager jobManager;

private Resource deRoot;
private Resource enRoot;
Expand Down Expand Up @@ -105,11 +108,17 @@ public void setup() {
noRoot = context.create().resource("/content/site/nothing");

context.registerService(ServiceUserMapped.class, serviceUser, "subServiceName", "sitemap-writer");
context.registerService(ServiceUserMapped.class, serviceUser, "subServiceName", "sitemap-reader");
context.registerService(SitemapGenerator.class, generator, "service.ranking", 1);
context.registerService(SitemapGenerator.class, newsGenerator, "service.ranking", 2);
context.registerService(JobManager.class, jobManager);
context.registerInjectActivateService(sitemapServiceConfiguration);
context.registerInjectActivateService(generatorManager);
context.registerInjectActivateService(storage);
context.registerInjectActivateService(scheduler,
"scheduler.name", "default",
"scheduler.expression", "never",
"names", new String[] { SitemapService.DEFAULT_SITEMAP_NAME });
context.registerInjectActivateService(subject);
}

Expand Down Expand Up @@ -137,7 +146,7 @@ public void testSitemapIndexUrlReturned() {
assertThat(enInfo, hasSize(2));
assertThat(enInfo, hasItems(
eqSitemapInfo("/site/en.sitemap-index.xml", -1, -1, SitemapInfo.Status.ON_DEMAND),
eqSitemapInfo("/site/en.sitemap.xml", -1, -1, SitemapInfo.Status.UNKNOWN)
eqSitemapInfo("/site/en.sitemap.xml", -1, -1, SitemapInfo.Status.SCHEDULED)
));
assertThat(frInfo, hasSize(3));
assertThat(frInfo, hasItems(
Expand All @@ -160,13 +169,17 @@ public void testSitemapUrlReturnEmptyCollections() {
@Test
public void testSitemapUrlReturnsProperSelectors() throws IOException {
// given
storage.writeSitemap(deRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 1, 100, 1);
storage.writeSitemap(deRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 1, 100,
1);
storage.writeSitemap(frRoot, "foobar", new ByteArrayInputStream(new byte[0]), 1, 100, 1);
storage.writeSitemap(enRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 1, 100, 1);
storage.writeSitemap(enRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 1, 100,
1);
storage.writeSitemap(enNews, "foo", new ByteArrayInputStream(new byte[0]), 1, 100, 1);
storage.writeSitemap(enNews, "bar", new ByteArrayInputStream(new byte[0]), 1, 100, 1);
storage.writeSitemap(itRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 1, 100, 1);
storage.writeSitemap(itRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 2, 100, 1);
storage.writeSitemap(itRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 1, 100,
1);
storage.writeSitemap(itRoot, SitemapService.DEFAULT_SITEMAP_NAME, new ByteArrayInputStream(new byte[0]), 2, 100,
1);

generator.setNames(deRoot, SitemapService.DEFAULT_SITEMAP_NAME);
generator.setNames(frRoot, "foobar");
Expand Down
Loading

0 comments on commit 37c19d4

Please sign in to comment.