You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ol...@apache.org on 2021/06/21 11:12:58 UTC

[sling-org-apache-sling-commons-crypto] 01/04: test GET request with no crypto services available and deactivation with no service tracker

This is an automated email from the ASF dual-hosted git repository.

olli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-crypto.git

commit 94e710519cc2dbf73b0934f53082fc7ef2ac8064
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Mon Jun 21 12:33:26 2021 +0200

    test GET request with no crypto services available and deactivation with no service tracker
---
 .../internal/EncryptWebConsolePlugin.java          |  4 +-
 .../internal/EncryptWebConsolePluginTest.java      | 59 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/crypto/webconsole/internal/EncryptWebConsolePlugin.java b/src/main/java/org/apache/sling/commons/crypto/webconsole/internal/EncryptWebConsolePlugin.java
index ce144e9..bbd6bdc 100644
--- a/src/main/java/org/apache/sling/commons/crypto/webconsole/internal/EncryptWebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/commons/crypto/webconsole/internal/EncryptWebConsolePlugin.java
@@ -67,14 +67,14 @@ public class EncryptWebConsolePlugin extends HttpServlet {
     }
 
     @Activate
-    private void activate(final BundleContext bundleContext) {
+    protected void activate(final BundleContext bundleContext) {
         this.bundleContext = bundleContext;
         tracker = new ServiceTracker<>(bundleContext, CryptoService.class, null);
         tracker.open();
     }
 
     @Deactivate
-    private void deactivate() {
+    protected void deactivate() {
         this.bundleContext = null;
         if (Objects.nonNull(tracker)) {
             tracker.close();
diff --git a/src/test/java/org/apache/sling/commons/crypto/webconsole/internal/EncryptWebConsolePluginTest.java b/src/test/java/org/apache/sling/commons/crypto/webconsole/internal/EncryptWebConsolePluginTest.java
new file mode 100644
index 0000000..12fd57b
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/crypto/webconsole/internal/EncryptWebConsolePluginTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.commons.crypto.webconsole.internal;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class EncryptWebConsolePluginTest {
+
+    @Test
+    public void testGetWithNoCryptoServicesAvailable() throws ServletException, IOException {
+        final BundleContext bundleContext = mock(BundleContext.class);
+        final HttpServletRequest request = mock(HttpServletRequest.class);
+        final StringWriter stringWriter = new StringWriter();
+        final PrintWriter printWriter = new PrintWriter(stringWriter, true);
+        final HttpServletResponse response = mock(HttpServletResponse.class);
+        when(response.getWriter()).thenReturn(printWriter);
+        final EncryptWebConsolePlugin plugin = new EncryptWebConsolePlugin();
+        plugin.activate(bundleContext);
+        plugin.doGet(request, response);
+        plugin.deactivate();
+        assertThat(stringWriter.toString()).contains("<p>No crypto service available</p>");
+    }
+
+    @Test
+    public void testDeactivateWithNoServiceTracker() {
+        final EncryptWebConsolePlugin plugin = new EncryptWebConsolePlugin();
+        plugin.deactivate();
+    }
+
+}