You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2020/11/04 09:08:56 UTC

[sling-org-apache-sling-feature-extension-apiregions] branch SLING-9867 updated: Start adding unit tests

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

cziegeler pushed a commit to branch SLING-9867
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-extension-apiregions.git


The following commit(s) were added to refs/heads/SLING-9867 by this push:
     new 9578fc1  Start adding unit tests
9578fc1 is described below

commit 9578fc17db023280ab57b7f7d7aab6318a6e8570
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Nov 4 10:08:29 2020 +0100

    Start adding unit tests
---
 .../apiregions/api/config/AttributeableEntity.java |   1 -
 .../extension/apiregions/api/config/Option.java    |  48 ++++-----
 .../apiregions/api/config/package-info.java        |  23 +++++
 .../api/config/AttributeableEntityTest.java        | 107 +++++++++++++++++++++
 .../api/config/ConfigurationApiTest.java           |  47 +++++++++
 5 files changed, 201 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java
index 0a81f0d..e1dc59e 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntity.java
@@ -25,7 +25,6 @@ import javax.json.JsonException;
 import javax.json.JsonObject;
 import javax.json.JsonObjectBuilder;
 import javax.json.JsonValue;
-import javax.json.JsonValue.ValueType;
 
 import org.apache.felix.cm.json.Configurations;
 
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Option.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Option.java
index 12ba1e0..9ebd153 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Option.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/Option.java
@@ -30,40 +30,40 @@ public class Option extends DescribableEntity {
     /**
      * Clear the object and remove all metadata
      */
-	public void clear() {
+	  public void clear() {
         super.clear();
-		this.setValue(null);
+		    this.setValue(null);
     }
 
-	/**
-	 * Extract the metadata from the JSON object.
-	 * This method first calls {@link #clear()}
-	 * @param jsonObj The JSON Object
-	 * @throws IOException If JSON parsing fails
-	 */
-	public void fromJSONObject(final JsonObject jsonObj) throws IOException {
+	  /**
+	   * Extract the metadata from the JSON object.
+	   * This method first calls {@link #clear()}
+	   * @param jsonObj The JSON Object
+	   * @throws IOException If JSON parsing fails
+	   */
+	  public void fromJSONObject(final JsonObject jsonObj) throws IOException {
         super.fromJSONObject(jsonObj);
         try {
-			this.setValue(this.getString(Constants.KEY_VALUE));
- 		} catch (final JsonException | IllegalArgumentException e) {
+		      	this.setValue(this.getString(Constants.KEY_VALUE));
+ 		    } catch (final JsonException | IllegalArgumentException e) {
             throw new IOException(e);
         }
     }
     
     /**
      * Get the value for the option
-	 * @return the value
-	 */
-	public String getValue() {
-		return value;
-	}
+  	 * @return the value
+	   */
+	  public String getValue() {
+		    return value;
+	  }
 
-	/**
+	  /**
      * Set the value for the option
-	 * @param value the value to set
-	 */
-	public void setValue(final String value) {
-		this.value = value;
+	   * @param value the value to set
+	   */
+	  public void setValue(final String value) {
+		    this.value = value;
     }
 
     /**
@@ -73,10 +73,10 @@ public class Option extends DescribableEntity {
      * @throws IOException If generating the JSON fails
      */
     JsonObjectBuilder createJson() throws IOException {
-		final JsonObjectBuilder objectBuilder = super.createJson();
+		    final JsonObjectBuilder objectBuilder = super.createJson();
 
-		this.setString(objectBuilder, Constants.KEY_VALUE, this.getValue());
+		    this.setString(objectBuilder, Constants.KEY_VALUE, this.getValue());
 
-		return objectBuilder;
+		    return objectBuilder;
     }
 }
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/package-info.java b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/package-info.java
new file mode 100644
index 0000000..f2582d7
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+@org.osgi.annotation.versioning.Version("1.ΓΌ.0")
+package org.apache.sling.feature.extension.apiregions.api.config;
+
+
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntityTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntityTest.java
new file mode 100644
index 0000000..5152867
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/AttributeableEntityTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.feature.extension.apiregions.api.config;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import javax.json.Json;
+import javax.json.JsonValue;
+
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionState;
+import org.apache.sling.feature.ExtensionType;
+import org.junit.Test;
+
+public class AttributeableEntityTest {
+
+    public static class AE extends AttributeableEntity {
+        // AttributeableEntity is abstract, therefore subclassing for testing
+    }
+
+    @Test public void testClear() {
+        final AE entity = new AE();
+        entity.getAttributes().put("a", Json.createValue(5));
+        entity.clear();
+        assertTrue(entity.getAttributes().isEmpty());
+    }
+
+    @Test public void testFromJSONObject() throws IOException {
+        final Extension ext = new Extension(ExtensionType.JSON, "a", ExtensionState.OPTIONAL);
+        ext.setJSON("{ \"a\" : 1, \"b\" : \"2\"}");
+
+        final AE entity = new AE();
+        entity.fromJSONObject(ext.getJSONStructure().asJsonObject());
+        assertEquals(2, entity.getAttributes().size());
+        assertEquals(Json.createValue(1), entity.getAttributes().get("a"));
+        assertEquals(Json.createValue("2"), entity.getAttributes().get("b"));
+    }
+
+    @Test public void testToJSONObject() throws IOException {
+        final AE entity = new AE();
+        entity.getAttributes().put("a", Json.createValue(1));
+        entity.getAttributes().put("b", Json.createValue("2"));
+        
+        final Extension ext = new Extension(ExtensionType.JSON, "a", ExtensionState.OPTIONAL);
+        ext.setJSON("{ \"a\" : 1, \"b\" : \"2\"}");
+
+        assertEquals(ext.getJSONStructure().asJsonObject(), entity.toJSONObject());
+    }
+
+    @Test public void testGetString() {
+        final AE entity = new AE();
+        assertNull(entity.getString("foo"));
+        entity.getAttributes().put("foo", Json.createValue("bar"));
+        assertEquals("bar", entity.getString("foo"));
+        assertTrue(entity.getAttributes().isEmpty());
+    }
+
+    @Test public void testGetBoolean() throws IOException {
+        final AE entity = new AE();
+        assertTrue(entity.getBoolean("foo", true));
+
+        entity.getAttributes().put("foo", JsonValue.FALSE);
+        assertEquals(false, entity.getBoolean("foo", true));
+        assertTrue(entity.getAttributes().isEmpty());
+
+
+        try {
+            entity.getAttributes().put("foo", Json.createValue(1.0));
+            entity.getBoolean("foo", false);
+            fail();
+        } catch ( final IOException expected) {
+            // this is expected
+        }
+    }
+
+    @Test public void testGetInteger() throws IOException {
+        final AE entity = new AE();
+        assertEquals(7, entity.getInteger("foo", 7));
+
+        entity.getAttributes().put("foo", Json.createValue(9));
+        assertEquals(9, entity.getInteger("foo", 7));
+        assertTrue(entity.getAttributes().isEmpty());
+
+        entity.getAttributes().put("foo", Json.createValue("9"));
+        assertEquals(9, entity.getInteger("foo", 7));
+        assertTrue(entity.getAttributes().isEmpty());
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/ConfigurationApiTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/ConfigurationApiTest.java
new file mode 100644
index 0000000..525797e
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/ConfigurationApiTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.feature.extension.apiregions.api.config;
+
+import static org.junit.Assert.assertNull;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionState;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.Feature;
+import org.junit.Test;
+
+public class ConfigurationApiTest {
+
+    @Test public void testNullFeature() {
+        assertNull(ConfigurationApi.getConfigurationApi((Feature)null));
+    }
+
+    @Test public void testNullExtension() {
+        assertNull(ConfigurationApi.getConfigurationApi((Extension)null));
+        final Feature f = new Feature(ArtifactId.parse("g:a:1.0"));
+        assertNull(ConfigurationApi.getConfigurationApi(f));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWrongExtensionType() {
+        final Feature f = new Feature(ArtifactId.parse("g:a:1.0"));
+        final Extension e = new Extension(ExtensionType.TEXT, ConfigurationApi.EXTENSION_NAME, ExtensionState.OPTIONAL);
+        f.getExtensions().add(e);
+        ConfigurationApi.getConfigurationApi(f);
+    }
+}