SLING-8365 - Add unit test for JournalAvailableChecker
diff --git a/src/main/java/org/apache/sling/distribution/journal/impl/shared/JournalAvailableChecker.java b/src/main/java/org/apache/sling/distribution/journal/impl/shared/JournalAvailableChecker.java
index 4fc2540..c0ad8f9 100644
--- a/src/main/java/org/apache/sling/distribution/journal/impl/shared/JournalAvailableChecker.java
+++ b/src/main/java/org/apache/sling/distribution/journal/impl/shared/JournalAvailableChecker.java
@@ -61,6 +61,15 @@
private volatile ServiceRegistration<JournalAvailable> reg;
+ public JournalAvailableChecker(){
+
+ }
+
+ public JournalAvailableChecker(MessagingProvider provider, Topics topics) {
+ this.provider = provider;
+ this.topics = topics;
+ }
+
@Activate
public void activate(BundleContext context) {
requireNonNull(provider);
diff --git a/src/test/java/org/apache/sling/distribution/journal/impl/shared/JournalAvailableCheckerTest.java b/src/test/java/org/apache/sling/distribution/journal/impl/shared/JournalAvailableCheckerTest.java
new file mode 100644
index 0000000..dc2ac70
--- /dev/null
+++ b/src/test/java/org/apache/sling/distribution/journal/impl/shared/JournalAvailableCheckerTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.distribution.journal.impl.shared;
+
+import java.util.Map;
+
+import org.apache.sling.distribution.journal.JournalAvailable;
+import org.apache.sling.distribution.journal.MessagingException;
+import org.apache.sling.distribution.journal.MessagingProvider;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+import static java.util.Collections.emptyMap;
+import static java.util.Collections.singletonMap;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.doThrow;
+import static org.osgi.util.converter.Converters.standardConverter;
+
+public class JournalAvailableCheckerTest {
+
+ private static final String INVALID_TOPIC = "invalid_topic_name";
+
+ private JournalAvailableChecker checker;
+
+ @Spy
+ private Topics topics = new Topics();
+
+ @Mock
+ private MessagingProvider provider;
+
+ @Mock
+ private BundleContext context;
+
+ @Mock
+ private ServiceRegistration<JournalAvailable> reg;
+
+ @Before
+ public void before() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ doThrow(new MessagingException("topic is invalid"))
+ .when(provider).assertTopic(INVALID_TOPIC);
+ when(context.registerService(Mockito.eq(JournalAvailable.class), Mockito.any(JournalAvailable.class), Mockito.any()))
+ .thenReturn(reg);
+ checker = new JournalAvailableChecker(provider, topics);
+ checker.activate(context);
+ }
+
+ @After
+ public void after() throws Exception {
+ checker.deactivate();
+ }
+
+ @Test
+ public void testIsAvailable() throws Exception {
+ topics.activate(topicsConfiguration(singletonMap("packageTopic", INVALID_TOPIC)));
+ checker.run();
+ assertFalse(checker.isAvailable());
+ topics.activate(topicsConfiguration(emptyMap()));
+ checker.run();
+ assertTrue(checker.isAvailable());
+ }
+
+ private Topics.TopicsConfiguration topicsConfiguration(Map<String,String> props) {
+ return standardConverter()
+ .convert(props)
+ .to(Topics.TopicsConfiguration.class);
+ }
+
+}
\ No newline at end of file