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

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 0119a02  SLING-10214 : OsgiConsoleClient#getConfiguration should not 
include metatype information
0119a02 is described below

commit 0119a020959948a29758b0e074048a9b3613b7ff
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Sun Mar 14 13:36:30 2021 +0100

    SLING-10214 : OsgiConsoleClient#getConfiguration should not include 
metatype information
---
 .../testing/clients/osgi/OsgiConsoleClient.java    | 108 ++++++++++++++++-----
 .../sling/testing/clients/osgi/package-info.java   |   2 +-
 .../clients/osgi/OsgiConsoleClientTest.java        |  63 ++++++++++++
 3 files changed, 148 insertions(+), 25 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java 
b/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java
index 528117c..842015b 100644
--- a/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java
+++ b/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java
@@ -17,6 +17,23 @@
 
 package org.apache.sling.testing.clients.osgi;
 
+import static org.apache.http.HttpStatus.SC_MOVED_TEMPORARILY;
+import static org.apache.http.HttpStatus.SC_OK;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeoutException;
+import java.util.jar.JarInputStream;
+import java.util.jar.Manifest;
+
 import org.apache.http.Header;
 import org.apache.http.entity.mime.MultipartEntityBuilder;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -34,23 +51,6 @@ import org.osgi.framework.Constants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.TimeoutException;
-import java.util.jar.JarInputStream;
-import java.util.jar.Manifest;
-
-import static org.apache.http.HttpStatus.SC_MOVED_TEMPORARILY;
-import static org.apache.http.HttpStatus.SC_OK;
-
 /**
  * A client that wraps the Felix OSGi Web Console REST API calls.
  * @see <a 
href=http://felix.apache.org/documentation/subprojects/apache-felix-web-console/web-console-restful-api.html>
@@ -78,7 +78,7 @@ public class OsgiConsoleClient extends SlingClient {
      * The URL for components requests
      */
     private final String URL_COMPONENTS = CONSOLE_ROOT_URL + "/components";
-    
+
     /**
      * The URL for service requests
      */
@@ -181,7 +181,7 @@ public class OsgiConsoleClient extends SlingClient {
         }
         return null;
     }
-    
+
     /**
      * Returns the service info wrapper for all services implementing the 
given type.
      *
@@ -226,11 +226,11 @@ public class OsgiConsoleClient extends SlingClient {
         };
         p.poll(timeout, delay);
     }
-    
+
     /**
      * Wait until the service with the given name is registered. This means 
the component must be either in state "Registered" or "Active".
      * @param type the type of the service (usually the name of a Java 
interface)
-     * @param bundleSymbolicName the symbolic name of the bundle supposed to 
register that service. 
+     * @param bundleSymbolicName the symbolic name of the bundle supposed to 
register that service.
      * May be {@code null} in which case this method just waits for any 
service with the requested type being registered (independent of the 
registering bundle).
      * @param timeout how long to wait for the component to become registered 
before throwing a {@code TimeoutException} in milliseconds
      * @param delay time to wait between checks of the state in milliseconds
@@ -274,7 +274,8 @@ public class OsgiConsoleClient extends SlingClient {
 
     /**
      * Returns a map of all properties set for the config referenced by the 
PID, where the map keys
-     * are the property names.
+     * are the property names. If properties are not set in the corresponding 
OSGi configuration but
+     * metatype information is available, the defaults from the metatype will 
be included.
      *
      * @param pid the pid of the configuration
      * @param expectedStatus list of accepted statuses of the response
@@ -312,6 +313,65 @@ public class OsgiConsoleClient extends SlingClient {
         }
         return props;
     }
+
+    /**
+     * Returns a map of all properties set for the config referenced by the 
PID, where the map keys
+     * are the property names.
+     *
+     * @param pid the pid of the configuration
+     * @param expectedStatus list of accepted statuses of the response
+     * @return the properties as a map or {@code null} if the configuration 
does not exist
+     * @throws ClientException if the response status does not match any of 
the expectedStatus
+     * @since 2.1.0
+     */
+    public Map<String, Object> getOSGiConfiguration(String pid, int... 
expectedStatus) throws ClientException {
+        // make the request
+        SlingHttpResponse resp = this.doPost(URL_CONFIGURATION + "/" + pid, 
null);
+        // check the returned status
+        HttpUtils.verifyHttpStatus(resp, HttpUtils.getExpectedStatus(SC_OK, 
expectedStatus));
+        // get the JSON node
+        final JsonNode rootNode = 
JsonUtils.getJsonNodeFromString(resp.getContent());
+
+        return extractOSGiConfiguration(rootNode);
+    }
+
+    static Map<String, Object> extractOSGiConfiguration(final JsonNode 
rootNode) {
+        // bundle_location is not set, the configuration does not exist
+        if ( rootNode.get("bundle_location") == null ) {
+            return null;
+        }
+
+        final Map<String, Object> result = new HashMap<String, Object>();
+        // go through the properties
+        final JsonNode propertiesNode = rootNode.get("properties");
+        if ( propertiesNode != null ) {
+            for(Iterator<String> it = propertiesNode.getFieldNames(); 
it.hasNext();) {
+                final String propName = it.next();
+                final JsonNode propNode = propertiesNode.get(propName);
+
+                final boolean isSet = propNode.get("is_set").getBooleanValue();
+                if ( isSet ) {
+                    JsonNode value = propNode.get("value");
+                    if (value != null) {
+                        result.put(propName, value.getValueAsText());
+                    } else {
+                        value = propNode.get("values");
+                        if (value != null) {
+                            final Iterator<JsonNode> iter = 
value.getElements();
+                            List<String> list = new ArrayList<String>();
+                            while(iter.hasNext()) {
+                                list.add(iter.next().getValueAsText());
+                            }
+                            result.put(propName, list.toArray(new 
String[list.size()]));
+                        }
+                    }
+                }
+            }
+        }
+
+        return result;
+    }
+
     /**
      * Returns a map of all properties set for the config referenced by the 
PID, where the map keys
      * are the property names. The method waits until the configuration has 
been set.
@@ -616,7 +676,7 @@ public class OsgiConsoleClient extends SlingClient {
 
         p.poll(timeout, delay);
     }
-    
+
     /**
      * Wait until the bundle is started
      * @param symbolicName symbolic name of bundle
@@ -712,7 +772,7 @@ public class OsgiConsoleClient extends SlingClient {
         LOG.info("Starting bundle {} via {}", symbolicName, path);
         this.doPost(path, FormEntityBuilder.create().addParameter("action", 
"start").build(), SC_OK);
     }
-    
+
     /**
      * Stop a bundle
      * @param symbolicName the name of the bundle
diff --git 
a/src/main/java/org/apache/sling/testing/clients/osgi/package-info.java 
b/src/main/java/org/apache/sling/testing/clients/osgi/package-info.java
index 33bde1e..7d0e647 100644
--- a/src/main/java/org/apache/sling/testing/clients/osgi/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/osgi/package-info.java
@@ -19,7 +19,7 @@
 /**
  * OSGI testing tools.
  */
-@Version("2.0.0")
+@Version("2.1.0")
 package org.apache.sling.testing.clients.osgi;
 
 import org.osgi.annotation.versioning.Version;
diff --git 
a/src/test/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClientTest.java
 
b/src/test/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClientTest.java
new file mode 100644
index 0000000..289323f
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClientTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.testing.clients.osgi;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Map;
+
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.util.JsonUtils;
+import org.codehaus.jackson.JsonNode;
+import org.junit.Test;
+
+public class OsgiConsoleClientTest {
+
+    @Test public void testExtractOSGiConfigurationNoConfig() throws 
ClientException {
+        final String jsonResult = "{\"pid\":\"org.apache.sling.Factory\","
+                + "\"title\":\"Factory\","
+                + "\"description\":\"A factory\","
+                + "\"properties\":{"
+                  + 
"\"prop\":{\"name\":\"Prop\",\"optional\":false,\"is_set\":false,\"type\":1,\"values\":[\"a\",\"b\"],"
+                  + "\"description\":\"A property\"}}}";
+
+        final JsonNode rootNode = JsonUtils.getJsonNodeFromString(jsonResult);
+
+        // configuration does not exist, expect null
+        assertNull(OsgiConsoleClient.extractOSGiConfiguration(rootNode));
+    }
+
+    @Test public void testExtractOSGiConfiguration() throws ClientException {
+        final String jsonResult = "{\"pid\":\"org.apache.sling.Factory\","
+                + "\"title\":\"Factory\","
+                + "\"description\":\"A factory\","
+                + "\"properties\":{"
+                  + 
"\"propset\":{\"name\":\"Prop\",\"optional\":false,\"is_set\":true,\"type\":1,\"value\":\"a\","
+                  + "\"description\":\"A property\"},"
+                  + 
"\"prop\":{\"name\":\"Prop\",\"optional\":false,\"is_set\":false,\"type\":1,\"values\":[\"a\",\"b\"],"
+                  + "\"description\":\"A property\"}"
+                + 
"},\"bundleLocation\":\"\",\"bundle_location\":null,\"service_location\":\"\"}";
+
+        final JsonNode rootNode = JsonUtils.getJsonNodeFromString(jsonResult);
+
+        // one property is set
+        final Map<String, Object> result = 
OsgiConsoleClient.extractOSGiConfiguration(rootNode);
+        assertEquals(1, result.size());
+        assertEquals("a", result.get("propset"));
+    }
+}

Reply via email to