This is an automated email from the ASF dual-hosted git repository. dklco pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-contentloader.git
commit 712cd4cc52f95cf2060afc94ee8482355460cad9 Author: Dan Klco <[email protected]> AuthorDate: Wed Oct 28 16:12:18 2020 -0400 Adding tests to the bundle content loader healthcheck --- .../contentloader/hc/BundleContentLoadedCheck.java | 2 +- .../internal/BundleContentLoaderTest.java | 9 +- .../internal/hc/BundleContentLoadedCheckTest.java | 139 +++++++++++++++++++++ 3 files changed, 144 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/sling/jcr/contentloader/hc/BundleContentLoadedCheck.java b/src/main/java/org/apache/sling/jcr/contentloader/hc/BundleContentLoadedCheck.java index 2fe432b..17c855b 100644 --- a/src/main/java/org/apache/sling/jcr/contentloader/hc/BundleContentLoadedCheck.java +++ b/src/main/java/org/apache/sling/jcr/contentloader/hc/BundleContentLoadedCheck.java @@ -92,7 +92,7 @@ public class BundleContentLoadedCheck implements HealthCheck { private SlingRepository repository; @Activate - protected void activate(BundleContext bundleContext, Config config) { + public void activate(BundleContext bundleContext, Config config) { this.bundleContext = bundleContext; this.includesRegex = Pattern.compile(config.includesRegex()); String excludesRegex2 = config.excludesRegex(); diff --git a/src/test/java/org/apache/sling/jcr/contentloader/internal/BundleContentLoaderTest.java b/src/test/java/org/apache/sling/jcr/contentloader/internal/BundleContentLoaderTest.java index a985701..44f6571 100644 --- a/src/test/java/org/apache/sling/jcr/contentloader/internal/BundleContentLoaderTest.java +++ b/src/test/java/org/apache/sling/jcr/contentloader/internal/BundleContentLoaderTest.java @@ -67,7 +67,7 @@ public class BundleContentLoaderTest { @Test public void loadContentWithSpecificPath() throws Exception { - Bundle mockBundle = newBundleWithInitialContent("SLING-INF/libs/app;path:=/libs/app"); + Bundle mockBundle = newBundleWithInitialContent(context, "SLING-INF/libs/app;path:=/libs/app"); contentLoader.registerBundle(context.resourceResolver().adaptTo(Session.class), mockBundle, false); @@ -80,7 +80,7 @@ public class BundleContentLoaderTest { @Test public void loadContentWithRootPath() throws Exception { - Bundle mockBundle = newBundleWithInitialContent("SLING-INF/"); + Bundle mockBundle = newBundleWithInitialContent(context, "SLING-INF/"); contentLoader.registerBundle(context.resourceResolver().adaptTo(Session.class), mockBundle, false); @@ -96,7 +96,7 @@ public class BundleContentLoaderTest { dumpRepo("/", 2); - Bundle mockBundle = newBundleWithInitialContent("SLING-INF/libs/app;path:=/libs/app;ignoreImportProviders:=xml"); + Bundle mockBundle = newBundleWithInitialContent(context, "SLING-INF/libs/app;path:=/libs/app;ignoreImportProviders:=xml"); contentLoader.registerBundle(context.resourceResolver().adaptTo(Session.class), mockBundle, false); @@ -113,8 +113,7 @@ public class BundleContentLoaderTest { } - private MockBundle newBundleWithInitialContent(String initialContentHeader) { - + public static MockBundle newBundleWithInitialContent(SlingContext context, String initialContentHeader) { MockBundle mockBundle = new MockBundle(context.bundleContext()); mockBundle.setHeaders(singletonMap("Sling-Initial-Content", initialContentHeader)); return mockBundle; diff --git a/src/test/java/org/apache/sling/jcr/contentloader/internal/hc/BundleContentLoadedCheckTest.java b/src/test/java/org/apache/sling/jcr/contentloader/internal/hc/BundleContentLoadedCheckTest.java new file mode 100644 index 0000000..b85e746 --- /dev/null +++ b/src/test/java/org/apache/sling/jcr/contentloader/internal/hc/BundleContentLoadedCheckTest.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.jcr.contentloader.internal.hc; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.annotation.Annotation; + +import javax.jcr.Session; + +import org.apache.felix.hc.api.Result; +import org.apache.sling.jcr.contentloader.hc.BundleContentLoadedCheck; +import org.apache.sling.jcr.contentloader.hc.BundleContentLoadedCheck.Config; +import org.apache.sling.jcr.contentloader.internal.BundleContentLoader; +import org.apache.sling.jcr.contentloader.internal.BundleContentLoaderTest; +import org.apache.sling.jcr.contentloader.internal.BundleHelper; +import org.apache.sling.jcr.contentloader.internal.ContentLoaderService; +import org.apache.sling.jcr.contentloader.internal.ContentReaderWhiteboard; +import org.apache.sling.jcr.contentloader.internal.readers.JsonReader; +import org.apache.sling.jcr.contentloader.internal.readers.XmlReader; +import org.apache.sling.jcr.contentloader.internal.readers.ZipReader; +import org.apache.sling.testing.mock.osgi.MockBundle; +import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +public class BundleContentLoadedCheckTest { + + @Rule + public final SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK); + private MockBundle bundle; + private Mockery mock = new Mockery(); + private BundleContentLoader contentLoader; + private BundleContentLoadedCheck check; + + @Before + public void setup() { + bundle = BundleContentLoaderTest.newBundleWithInitialContent(context, "SLING-INF/libs/app;path:=/libs/app"); + + // prepare content readers + context.registerInjectActivateService(new JsonReader()); + context.registerInjectActivateService(new XmlReader()); + context.registerInjectActivateService(new ZipReader()); + + // whiteboard which holds readers + context.registerInjectActivateService(new ContentReaderWhiteboard()); + + // register the content loader service + BundleHelper bundleHelper = context.registerInjectActivateService(new ContentLoaderService()); + + ContentReaderWhiteboard whiteboard = context.getService(ContentReaderWhiteboard.class); + + contentLoader = new BundleContentLoader(bundleHelper, whiteboard); + + BundleContext bundleContext = mock.mock(BundleContext.class); + mock.checking(new Expectations() { + { + oneOf(bundleContext).getBundles(); + will(returnValue(new Bundle[] { bundle })); + } + }); + check = context.registerInjectActivateService(new BundleContentLoadedCheck()); + check.activate(bundleContext, new Config() { + + @Override + public Class<? extends Annotation> annotationType() { + return null; + } + + @Override + public String hc_name() { + return "Unity"; + } + + @Override + public String[] hc_tags() { + return new String[] { "test" }; + } + + @Override + public String includesRegex() { + return ".*"; + } + + @Override + public String excludesRegex() { + return ""; + } + + @Override + public boolean useCriticalForNotLoaded() { + return false; + } + + @Override + public String webconsole_configurationFactory_nameHint() { + return null; + } + + }); + } + + @Test + public void testNotInstalled() { + Result result = check.execute(); + assertFalse(result.isOk()); + } + + @Test + public void testInstalled() { + contentLoader.registerBundle(context.resourceResolver().adaptTo(Session.class), bundle, false); + Result result = check.execute(); + assertTrue(result.isOk()); + } + +}
