You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/11/02 13:24:21 UTC

[sling-whiteboard] branch master updated: SLING-7779 API Region support for the Sling Feature Model

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1977350  SLING-7779 API Region support for the Sling Feature Model
1977350 is described below

commit 1977350acad725e91d0a1c367921c09fe5860daa
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Fri Nov 2 13:23:49 2018 +0000

    SLING-7779 API Region support for the Sling Feature Model
    
    Unit tests for the BundleArtifactFeatureHandler
---
 featuremodel/feature-extension-apiregions/pom.xml  |   6 ++
 .../extensions/BundleArtifactFeatureHandler.java   |   7 +-
 .../extensions/BundleMappingHandler.java           |   2 +-
 .../BundleArtifactFeatureHandlerTest.java          | 112 +++++++++++++++++++++
 4 files changed, 123 insertions(+), 4 deletions(-)

diff --git a/featuremodel/feature-extension-apiregions/pom.xml b/featuremodel/feature-extension-apiregions/pom.xml
index 857060f..ee8416f 100644
--- a/featuremodel/feature-extension-apiregions/pom.xml
+++ b/featuremodel/feature-extension-apiregions/pom.xml
@@ -65,5 +65,11 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.johnzon</groupId>
+            <artifactId>johnzon-core</artifactId>
+            <version>1.0.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleArtifactFeatureHandler.java b/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleArtifactFeatureHandler.java
index 79558c4..786ed77 100644
--- a/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleArtifactFeatureHandler.java
+++ b/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleArtifactFeatureHandler.java
@@ -48,8 +48,7 @@ public class BundleArtifactFeatureHandler extends AbstractHandler implements Pos
             writeBundleToFeatureMap(feature);
             writeFeatureToRegionAndPackageMap(feature, extension);
         } catch (IOException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+            throw new RuntimeException(e);
         }
     }
 
@@ -90,7 +89,9 @@ public class BundleArtifactFeatureHandler extends AbstractHandler implements Pos
         for (JsonValue jv : ja) {
             if (jv instanceof JsonObject) {
                 JsonObject jo = (JsonObject) jv;
-                String fid = jo.getString("org-feature");
+                String fid = null;
+                if (jo.containsKey("org-feature"))
+                    fid = jo.getString("org-feature");
                 if (fid == null)
                     fid = feature.getId().toMvnId();
 
diff --git a/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleMappingHandler.java b/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleMappingHandler.java
index bf68d02..02ab958 100644
--- a/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleMappingHandler.java
+++ b/featuremodel/feature-extension-apiregions/src/main/java/org/apache/sling/feature/whitelisting/extensions/BundleMappingHandler.java
@@ -56,7 +56,7 @@ public class BundleMappingHandler extends AbstractHandler implements PostProcess
 
             storeProperties(map, idBSNFile);
         } catch (IOException e) {
-            e.printStackTrace();
+            throw new RuntimeException(e);
         }
     }
 }
diff --git a/featuremodel/feature-extension-apiregions/src/test/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandlerTest.java b/featuremodel/feature-extension-apiregions/src/test/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandlerTest.java
new file mode 100644
index 0000000..06942ea
--- /dev/null
+++ b/featuremodel/feature-extension-apiregions/src/test/java/org/apache/sling/feature/extension/apiregions/BundleArtifactFeatureHandlerTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.whitelisting.extensions.BundleArtifactFeatureHandler;
+import org.junit.Test;
+
+import java.io.FileReader;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+public class BundleArtifactFeatureHandlerTest {
+    @Test
+    public void testBundleToFeatureMap() throws Exception {
+        BundleArtifactFeatureHandler bafh = new BundleArtifactFeatureHandler();
+
+        Feature f = new Feature(ArtifactId.fromMvnId("org.sling:something:1.2.3:slingosgifeature:myclassifier"));
+        Artifact b1 = new Artifact(ArtifactId.fromMvnId("org.sling:b1:1"));
+        Artifact b2 = new Artifact(ArtifactId.fromMvnId("org.sling:b2:1"));
+        Artifact b3a = new Artifact(ArtifactId.fromMvnId("org.sling:b3:1"));
+        b3a.getMetadata().put("org-feature", "some.other:feature:123");
+        Artifact b3b = new Artifact(ArtifactId.fromMvnId("org.sling:b3:1"));
+        f.getBundles().addAll(Arrays.asList(b1, b2, b3a, b3b));
+
+        Extension ex = new Extension(ExtensionType.JSON, "api-regions", false);
+        ex.setJSON("[]");
+        bafh.postProcess(null, f, ex);
+
+        String p = System.getProperty("whitelisting.bundles.properties");
+        Properties actual = new Properties();
+        actual.load(new FileReader(p));
+
+        Properties expected = new Properties();
+        expected.put("org.sling:b1:1", "org.sling:something:1.2.3:slingosgifeature:myclassifier");
+        expected.put("org.sling:b2:1", "org.sling:something:1.2.3:slingosgifeature:myclassifier");
+        expected.put("org.sling:b3:1", "some.other:feature:123,org.sling:something:1.2.3:slingosgifeature:myclassifier");
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testFeatureToRegionMap() throws Exception {
+        BundleArtifactFeatureHandler bafh = new BundleArtifactFeatureHandler();
+
+        Feature f = new Feature(ArtifactId.fromMvnId("org.sling:something:1.2.3"));
+        Extension ex = new Extension(ExtensionType.JSON, "api-regions", false);
+        ex.setJSON("[{\"name\":\"global\","
+                + "\"exports\": [\"a.b.c\",\"d.e.f\"]},"
+                + "{\"name\":\"internal\","
+                + "\"exports\":[\"xyz\"]},"
+                + "{\"name\":\"global\","
+                + "\"exports\":[\"test\"],"
+                + "\"org-feature\":\"an.other:feature:123\"}]");
+
+        bafh.postProcess(null, f, ex);
+
+        String p = System.getProperty("whitelisting.features.properties");
+        Properties actual = new Properties();
+        actual.load(new FileReader(p));
+
+        Properties expected = new Properties();
+        expected.put("an.other:feature:123", "global");
+        expected.put("org.sling:something:1.2.3", "internal,global");
+
+        String[] al = ((String) actual.remove("org.sling:something:1.2.3")).split(",");
+        String[] el = ((String) expected.remove("org.sling:something:1.2.3")).split(",");
+        assertEquals(new HashSet<>(Arrays.asList(el)), new HashSet<>(Arrays.asList(al)));
+        assertEquals(expected, actual);
+
+        String p2 = System.getProperty("whitelisting.regions.properties");
+        Properties actual2 = new Properties();
+        actual2.load(new FileReader(p2));
+
+        Properties expected2 = new Properties();
+        expected2.put("internal", "xyz");
+        expected2.put("global", "test,a.b.c,d.e.f");
+
+        String[] agl2 = ((String) actual2.remove("global")).split(",");
+        String[] egl2 = ((String) expected2.remove("global")).split(",");
+        assertEquals(new HashSet<>(Arrays.asList(egl2)), new HashSet<>(Arrays.asList(agl2)));
+        assertEquals(expected2, actual2);
+    }
+
+    @Test
+    public void testUnrelatedExtension() {
+        BundleArtifactFeatureHandler bafh = new BundleArtifactFeatureHandler();
+        Extension ex = new Extension(ExtensionType.JSON, "foobar", false);
+        bafh.postProcess(null, null, ex);
+        // Should not do anything and definitely not throw an exception
+    }
+}