SLING-7732 Feature Model Converter ignores inheritance when converting to provisioning model
This change also consults the ArtifactManager to see if the included
feature is available in Maven.
diff --git a/pom.xml b/pom.xml
index a11dcfd..ff6e5b8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -175,5 +175,11 @@
<version>1.0.0</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.18.3</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java b/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java
index d902ab6..14a5265 100644
--- a/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java
+++ b/src/main/java/org/apache/sling/feature/modelconverter/FeatureToProvisioning.java
@@ -28,6 +28,7 @@
import org.apache.sling.feature.builder.BuilderContext;
import org.apache.sling.feature.builder.FeatureBuilder;
import org.apache.sling.feature.builder.FeatureProvider;
+import org.apache.sling.feature.io.ArtifactHandler;
import org.apache.sling.feature.io.ArtifactManager;
import org.apache.sling.feature.io.IOUtils;
import org.apache.sling.feature.io.json.FeatureJSONReader.SubstituteVariables;
@@ -107,7 +108,27 @@
BuilderContext bc = new BuilderContext(new FeatureProvider() {
@Override
public org.apache.sling.feature.Feature provide(ArtifactId id) {
- return features.get(id);
+ // Check first if the feature is part of the provided context
+ org.apache.sling.feature.Feature f = features.get(id);
+ if (f != null) {
+ return f;
+ }
+
+ // If not, see if it is known to Maven
+ try {
+ ArtifactHandler ah = am.getArtifactHandler(id.toMvnUrl());
+ if (ah != null) {
+ org.apache.sling.feature.Feature feat = IOUtils.getFeature(ah.getUrl(), am, SubstituteVariables.NONE);
+ if (feat != null) {
+ // Cache it
+ features.put(feat.getId(), feat);
+ }
+ return feat;
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return null;
}
});
return FeatureBuilder.assemble(feature, bc);
diff --git a/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java b/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
index 0c66e96..f70d4fa 100644
--- a/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
+++ b/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
@@ -21,6 +21,7 @@
import org.apache.sling.feature.Extension;
import org.apache.sling.feature.ExtensionType;
import org.apache.sling.feature.Extensions;
+import org.apache.sling.feature.io.ArtifactHandler;
import org.apache.sling.feature.io.ArtifactManager;
import org.apache.sling.feature.io.ArtifactManagerConfig;
import org.apache.sling.feature.io.IOUtils;
@@ -42,6 +43,9 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
import java.io.File;
import java.io.FileReader;
@@ -184,8 +188,28 @@
@Test
public void testSimpleInheritsToProvModel() throws Exception {
- testConvertToProvisioningModel("/simple.json", "/simple.txt", "/simple_inherits.json");
- testConvertToProvisioningModel("/simple.json", "/simple.txt", "/simple.json", "/simple_inherits.json");
+ testConvertToProvisioningModel("/simple_inherits.json", "/simple_inherits.txt", "/simple.json");
+ testConvertToProvisioningModel("/simple_inherits.json", "/simple_inherits.txt", "/simple.json", "/simple_inherits.json");
+ }
+
+ @Test
+ public void testSimpleInheritsViaMavenRepoToProvModel() throws Exception {
+ artifactManager = Mockito.mock(ArtifactManager.class);
+ Mockito.when(artifactManager.getArtifactHandler(Mockito.anyString())).then(new Answer<ArtifactHandler>() {
+ @Override
+ public ArtifactHandler answer(InvocationOnMock in) throws Throwable {
+ String url = in.getArgument(0).toString();
+
+ if (url.endsWith("simple_inherits.json")) {
+ return new ArtifactHandler(url, new File(url));
+ } else if ("mvn:generated/simple/1.0.0".equals(url)) {
+ return new ArtifactHandler(url, new File(getClass().getResource("/simple2.json").toURI()));
+ }
+ return null;
+ }
+ });
+
+ testConvertToProvisioningModel("/simple_inherits.json", "/simple_inherits.txt");
}
@Test