Merge pull request #18 from henrykuijpers/feature/SLING-9750-fix-obtaining-of-id-when-not-int-value
SLING-9750 Fix read string as integer issue
diff --git a/src/main/java/org/apache/sling/testing/clients/osgi/ServiceInfo.java b/src/main/java/org/apache/sling/testing/clients/osgi/ServiceInfo.java
index 8df6463..2d703d8 100644
--- a/src/main/java/org/apache/sling/testing/clients/osgi/ServiceInfo.java
+++ b/src/main/java/org/apache/sling/testing/clients/osgi/ServiceInfo.java
@@ -41,7 +41,7 @@
* @return the service identifier
*/
public int getId() {
- return service.get("id").getIntValue();
+ return Integer.parseInt(service.get("id").getValueAsText());
}
/**
diff --git a/src/test/java/org/apache/sling/testing/clients/osgi/ServiceInfoTest.java b/src/test/java/org/apache/sling/testing/clients/osgi/ServiceInfoTest.java
new file mode 100644
index 0000000..3ca90d6
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/clients/osgi/ServiceInfoTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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 com.google.common.io.Resources;
+import org.apache.sling.testing.clients.ClientException;
+import org.apache.sling.testing.clients.util.JsonUtils;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+
+public class ServiceInfoTest {
+
+ @Test
+ public void testSpecifyServiceIdentifierAsString() throws IOException, ClientException {
+ assertServiceInfo("service-id-as-string");
+ }
+
+ @Test
+ public void testSpecifyServiceIdentifierAsInteger() throws IOException, ClientException {
+ assertServiceInfo("service-id-as-int");
+ }
+
+ private void assertServiceInfo(final String file) throws IOException, ClientException {
+ final ServiceInfo serviceInfo = new ServiceInfo(JsonUtils.getJsonNodeFromString(
+ Resources.toString(Resources.getResource("service-info/" + file + ".json"), StandardCharsets.UTF_8)));
+ assertEquals(10, serviceInfo.getId());
+ assertEquals("org.example.MyService", serviceInfo.getPid());
+ assertEquals(6, serviceInfo.getBundleId());
+ assertEquals(Collections.singletonList("org.example.MyService"), serviceInfo.getTypes());
+ assertEquals("org.example", serviceInfo.getBundleSymbolicName());
+ }
+}
\ No newline at end of file
diff --git a/src/test/resources/service-info/service-id-as-int.json b/src/test/resources/service-info/service-id-as-int.json
new file mode 100644
index 0000000..a78b1f9
--- /dev/null
+++ b/src/test/resources/service-info/service-id-as-int.json
@@ -0,0 +1,39 @@
+{
+ "status": "7450 services in total",
+ "serviceCount": 7450,
+ "data": [
+ {
+ "id": 10,
+ "types": "[org.example.MyService]",
+ "pid": "org.example.MyService",
+ "ranking": "",
+ "bundleId": 6,
+ "bundleName": "My Bundle",
+ "bundleVersion": "1.0.0",
+ "bundleSymbolicName": "org.example",
+ "props": [
+ {
+ "key": "service.bundleid",
+ "value": 6
+ },
+ {
+ "key": "Service Description",
+ "value": "My Implementation"
+ },
+ {
+ "key": "Service PID",
+ "value": "org.example.MyService"
+ },
+ {
+ "key": "service.scope",
+ "value": "bundle"
+ },
+ {
+ "key": "Service Vendor",
+ "value": "The Apache Software Foundation"
+ }
+ ],
+ "usingBundles": []
+ }
+ ]
+}
\ No newline at end of file
diff --git a/src/test/resources/service-info/service-id-as-string.json b/src/test/resources/service-info/service-id-as-string.json
new file mode 100644
index 0000000..aa15ce8
--- /dev/null
+++ b/src/test/resources/service-info/service-id-as-string.json
@@ -0,0 +1,39 @@
+{
+ "status": "7450 services in total",
+ "serviceCount": 7450,
+ "data": [
+ {
+ "id": "10",
+ "types": "[org.example.MyService]",
+ "pid": "org.example.MyService",
+ "ranking": "",
+ "bundleId": 6,
+ "bundleName": "My Bundle",
+ "bundleVersion": "1.0.0",
+ "bundleSymbolicName": "org.example",
+ "props": [
+ {
+ "key": "service.bundleid",
+ "value": 6
+ },
+ {
+ "key": "Service Description",
+ "value": "My Implementation"
+ },
+ {
+ "key": "Service PID",
+ "value": "org.example.MyService"
+ },
+ {
+ "key": "service.scope",
+ "value": "bundle"
+ },
+ {
+ "key": "Service Vendor",
+ "value": "The Apache Software Foundation"
+ }
+ ],
+ "usingBundles": []
+ }
+ ]
+}
\ No newline at end of file