This is an automated email from the ASF dual-hosted git repository.
dulvac 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 feab070 SLING-11611: Provide method to retrieve Pid for OSGI
Configurations in Sling Testing Clients (#39)
feab070 is described below
commit feab07071699964d489a4b37afef4fe9e561f7e2
Author: Nicola Scendoni <[email protected]>
AuthorDate: Mon Jan 15 09:45:15 2024 +0100
SLING-11611: Provide method to retrieve Pid for OSGI Configurations in
Sling Testing Clients (#39)
* SLING-11609: Provide doGetWithRetry methods in Sling Testing Clients
* SLING-11611: Provide method to retrieve Pid for OSGI Configurations in
Sling Testing Clients
* Exception refactoring
* reverted files modified in SLING-11609
* Reverted additional changes
---
.../sling/testing/clients/email/package-info.java | 2 +-
.../sling/testing/clients/html/package-info.java | 2 +-
.../testing/clients/osgi/OsgiConsoleClient.java | 81 +++++++++++++++++++++-
.../sling/testing/clients/osgi/package-info.java | 2 +-
.../clients/osgi/OsgiConsoleClientTest.java | 32 +++++++++
5 files changed, 115 insertions(+), 4 deletions(-)
diff --git
a/src/main/java/org/apache/sling/testing/clients/email/package-info.java
b/src/main/java/org/apache/sling/testing/clients/email/package-info.java
index fd7b7a0..dbe5bab 100644
--- a/src/main/java/org/apache/sling/testing/clients/email/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/email/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("2.0.0")
+@Version("2.1.0")
package org.apache.sling.testing.clients.email;
import org.osgi.annotation.versioning.Version;
diff --git
a/src/main/java/org/apache/sling/testing/clients/html/package-info.java
b/src/main/java/org/apache/sling/testing/clients/html/package-info.java
index bf17816..c80cc1b 100644
--- a/src/main/java/org/apache/sling/testing/clients/html/package-info.java
+++ b/src/main/java/org/apache/sling/testing/clients/html/package-info.java
@@ -17,7 +17,7 @@
* under the License.
*/
-@Version("3.0.0")
+@Version("3.1.0")
package org.apache.sling.testing.clients.html;
import org.osgi.annotation.versioning.Version;
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 a7dc26b..63dd30c 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,15 +17,19 @@
package org.apache.sling.testing.clients.osgi;
+import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.Header;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.message.BasicNameValuePair;
import org.apache.sling.testing.clients.ClientException;
import org.apache.sling.testing.clients.SlingClient;
import org.apache.sling.testing.clients.SlingClientConfig;
import org.apache.sling.testing.clients.SlingHttpResponse;
import org.apache.sling.testing.clients.exceptions.TestingIOException;
+import org.apache.sling.testing.clients.exceptions.TestingSetupException;
import org.apache.sling.testing.clients.exceptions.TestingValidationException;
import org.apache.sling.testing.clients.util.FormEntityBuilder;
import org.apache.sling.testing.clients.util.HttpUtils;
@@ -42,6 +46,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -187,7 +192,7 @@ public class OsgiConsoleClient extends SlingClient {
/**
* Returns the service info wrapper for all services implementing the
given type.
*
- * @param name the type of the service
+ * @param type the name of the service
* @return the service infos or {@code null} if no service for the given
type is registered
*/
private Collection<ServiceInfo> getServiceInfos(String type) throws
ClientException {
@@ -889,6 +894,52 @@ public class OsgiConsoleClient extends SlingClient {
return bundle;
}
+ /**
+ * Performs a search of a config PID through the service information.
+ * <p>
+ * This is useful for the case where:
+ * <ul>
+ * <li>We don't have the exact config PID (common if it is a factory
config we didn't create).</li>
+ * <li>We have to search the config with a property having a defined
value</li>
+ * </ul>
+ *
+ *
+ * @param serviceType The type of service.
+ * @param propertyName The name of the property with the unique value to
search.
+ * @param propertyValue The unique value to be searched.
+ * @return The final config PID. Null if it is not found.
+ * @throws ClientException
+ * @throws InterruptedException
+ * @throws TimeoutException
+ */
+ public String getConfigPIDFromServices(String serviceType, String
propertyName, String propertyValue, final long timeout, final long delay)
throws ClientException, InterruptedException, TimeoutException {
+
+ ConfigurationPollerByFilter p = new
ConfigurationPollerByFilter(String.format("(service.pid=%s.*)", serviceType));
+
+ p.poll(timeout, delay);
+ JsonNode jn = null;
+ try {
+ jn = new ObjectMapper().readTree(p.getConfigAsString());
+ } catch (JsonProcessingException e) {
+ throw new TestingSetupException(e.getMessage(),e);
+ }
+
+ int count = 0;
+ JsonNode configuration = jn.get(count);
+ while(configuration != null) {
+ if (
+ (configuration.get("properties") != null) &&
+ (configuration.get("properties").get(propertyName)
!= null) &&
+
(configuration.get("properties").get(propertyName).get("value") != null) &&
+
configuration.get("properties").get(propertyName).get("value").asText().equals(propertyValue)
+ ) {
+ return configuration.get("pid").asText();
+ }
+ configuration = jn.get(count++);
+ }
+ return null;
+ }
+
//
// static methods
//
@@ -960,4 +1011,32 @@ public class OsgiConsoleClient extends SlingClient {
return config;
}
}
+
+ class ConfigurationPollerByFilter extends Polling {
+ private String configAsString;
+ private String filter;
+
+ public ConfigurationPollerByFilter(String filter) {
+ this.filter = filter;
+ }
+ public String getConfigAsString() {
+ return configAsString;
+ }
+ @Override
+ public Boolean call() {
+ try {
+ SlingHttpResponse resp =
doGet("/system/console/configMgr/*.json",
+ Collections.singletonList(
+ new BasicNameValuePair("pidFilter", filter)
+ )
+ );
+ this.configAsString = resp.getContent();
+ return true;
+ } catch (Exception e) {
+ LOG.debug("Retrying doGet(/system/console/configMgr/*.json).
Exception: "+e.getMessage(),e);
+ return false;
+ }
+ }
+
+ }
}
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 efa5e41..db29f94 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("3.0.0")
+@Version("3.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
index 72db37f..e8a558b 100644
---
a/src/test/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClientTest.java
+++
b/src/test/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClientTest.java
@@ -16,12 +16,23 @@
*/
package org.apache.sling.testing.clients.osgi;
+import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.HttpServerRule;
import org.apache.sling.testing.clients.util.JsonUtils;
+import org.junit.ClassRule;
import org.junit.Test;
+import java.io.IOException;
import java.util.Map;
+import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -62,4 +73,25 @@ public class OsgiConsoleClientTest {
assertEquals(1, result.size());
assertEquals("a", result.get("propset"));
}
+
+ @ClassRule
+ public static HttpServerRule httpServer = new HttpServerRule() {
+ @Override
+ protected void registerHandlers() throws IOException {
+ String JSON_RESPONSE =
"[{\"pid\":\"pidValue\",\"properties\":{\"testPropertyName\":{\"value\":\"testPropertyValue\"}}}]";
+
serverBootstrap.registerHandler("/system/console/configMgr/*.json", new
HttpRequestHandler() {
+ @Override
+ public void handle(HttpRequest request, HttpResponse response,
HttpContext context) throws HttpException, IOException {
+ response.setEntity(new StringEntity(JSON_RESPONSE));
+ }
+ });
+ }
+ };
+
+ @Test
+ public void testGetConfigPIDFromServices() throws ClientException,
InterruptedException, TimeoutException {
+ OsgiConsoleClient c = new
OsgiConsoleClient(httpServer.getURI(),"user","pass");
+ String pid =
c.getConfigPIDFromServices("testServiceType","testPropertyName",
"testPropertyValue",1000, 200);
+ assertEquals("pidValue", pid);
+ }
}