kwin commented on a change in pull request #74:
URL: 
https://github.com/apache/sling-org-apache-sling-feature-cpconverter/pull/74#discussion_r622893923



##########
File path: 
src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerGAVTest.java
##########
@@ -16,73 +16,168 @@
  */
 package org.apache.sling.feature.cpconverter.handlers;
 
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
 import org.apache.jackrabbit.vault.fs.io.Archive;
+import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
+import org.apache.jackrabbit.vault.packaging.PackageId;
+import org.apache.jackrabbit.vault.packaging.VaultPackage;
+import org.apache.jackrabbit.vault.packaging.impl.PackageManagerImpl;
 import org.apache.sling.feature.ArtifactId;
 import 
org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
+import 
org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter.SlingInitialContentPolicy;
 import org.apache.sling.feature.cpconverter.artifacts.ArtifactsDeployer;
 import org.apache.sling.feature.cpconverter.features.FeaturesManager;
+import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Captor;
+import org.mockito.Mock;
 import org.mockito.Mockito;
+import org.mockito.Spy;
+import org.mockito.junit.MockitoJUnitRunner;
 
-import static org.junit.Assert.assertThrows;
-import static org.mockito.Mockito.when;
-
+@RunWith(MockitoJUnitRunner.class)
 public class BundleEntryHandlerGAVTest {
-    private final BundleEntryHandler handler = new BundleEntryHandler();
 
-    @Test
-    public void testGAVwithProperties() throws Exception {
-        String path = "/jcr_root/apps/gav/install/core-1.0.0-SNAPSHOT.jar";
-        Archive.Entry entry = Mockito.mock(Archive.Entry.class);
-        when(entry.getName()).thenReturn(path);
-        Archive archive = Mockito.mock(Archive.class);
-        
when(archive.openInputStream(entry)).thenReturn(BundleEntryHandlerGAVTest.class.getResourceAsStream("core-1.0.0-SNAPSHOT.jar"));
-        ContentPackage2FeatureModelConverter converter = 
Mockito.mock(ContentPackage2FeatureModelConverter.class);
+    @Mock
+    private Archive.Entry entry;
+    @Mock
+    private Archive archive;
+    @Spy
+    private ContentPackage2FeatureModelConverter converter;
+    @Spy
+    private FeaturesManager featuresManager;
+    @Rule
+    public TemporaryFolder tmpFolder = new TemporaryFolder();
+    @Captor
+    ArgumentCaptor<Dictionary<String, Object>> dictionaryCaptor;
+    
+    private BundleEntryHandler handler;
+
+    @Before
+    public void setUp() throws IOException {
+        handler = new BundleEntryHandler();
         ArtifactsDeployer deployer = Mockito.spy(ArtifactsDeployer.class);
         when(converter.getArtifactsDeployer()).thenReturn(deployer);
-        FeaturesManager manager = Mockito.spy(FeaturesManager.class);
-        when(converter.getFeaturesManager()).thenReturn(manager);
-        handler.handle(path, archive, entry, converter);
-        Mockito.verify(manager).addArtifact(null, 
ArtifactId.fromMvnId("com.madplanet.sling.cp2sf:core:1.0.0-SNAPSHOT"),null);
+        when(converter.getTempDirectory()).thenReturn(tmpFolder.getRoot());
+        featuresManager = Mockito.spy(FeaturesManager.class);
+        when(converter.getFeaturesManager()).thenReturn(featuresManager);
+    }
+
+    private void setUpArchive(String entryPath, String resourcePath) throws 
IOException {
+        when(entry.getName()).thenReturn(entryPath);
+        
when(archive.openInputStream(entry)).thenReturn(BundleEntryHandlerGAVTest.class.getResourceAsStream(resourcePath));
+    }
+
+    @Test
+    public void testGAV() throws Exception {
+        setUpArchive("/jcr_root/apps/gav/install/core-1.0.0-SNAPSHOT.jar", 
"core-1.0.0-SNAPSHOT.jar");
+        handler.handle("/jcr_root/apps/gav/install/core-1.0.0-SNAPSHOT.jar", 
archive, entry, converter);
+        Mockito.verify(featuresManager).addArtifact(null, 
ArtifactId.fromMvnId("com.madplanet.sling.cp2sf:core:1.0.0-SNAPSHOT"), null);
     }
 
     @Test
     public void testGAVwithPom() throws Exception{
-        String path = 
"/jcr_root/apps/gav/install/org.osgi.service.jdbc-1.0.0.jar";
-        Archive.Entry entry = Mockito.mock(Archive.Entry.class);
-        when(entry.getName()).thenReturn(path);
-        Archive archive = Mockito.mock(Archive.class);
-        
when(archive.openInputStream(entry)).thenReturn(BundleEntryHandlerGAVTest.class.getResourceAsStream("org.osgi.service.jdbc-1.0.0.jar"));
-        ContentPackage2FeatureModelConverter converter = 
Mockito.mock(ContentPackage2FeatureModelConverter.class);
-        ArtifactsDeployer deployer = Mockito.spy(ArtifactsDeployer.class);
-        when(converter.getArtifactsDeployer()).thenReturn(deployer);
-        FeaturesManager manager = Mockito.spy(FeaturesManager.class);
-        when(converter.getFeaturesManager()).thenReturn(manager);
-        handler.handle(path, archive, entry, converter);
-        Mockito.verify(manager).addArtifact(null, 
ArtifactId.fromMvnId("org.osgi:org.osgi.service.jdbc:1.0.0"),null);
+        
setUpArchive("/jcr_root/apps/gav/install/org.osgi.service.jdbc-1.0.0.jar", 
"org.osgi.service.jdbc-1.0.0.jar");
+        
handler.handle("/jcr_root/apps/gav/install/org.osgi.service.jdbc-1.0.0.jar", 
archive, entry, converter);
+        Mockito.verify(featuresManager).addArtifact(null, 
ArtifactId.fromMvnId("org.osgi:org.osgi.service.jdbc:1.0.0"), null);
     }
 
     @Test
     public void testNoGAV() throws Exception {
-        String path = 
"/jcr_root/apps/gav/install/org.osgi.service.jdbc-1.0.0-nogav.jar";
-        Archive.Entry entry = Mockito.mock(Archive.Entry.class);
-        when(entry.getName()).thenReturn(path);
-        Archive archive = Mockito.mock(Archive.class);
-        
when(archive.openInputStream(entry)).thenReturn(BundleEntryHandlerGAVTest.class.getResourceAsStream("org.osgi.service.jdbc-1.0.0-nogav.jar"));
-        ContentPackage2FeatureModelConverter converter = 
Mockito.mock(ContentPackage2FeatureModelConverter.class);
-        ArtifactsDeployer deployer = Mockito.spy(ArtifactsDeployer.class);
-        when(converter.getArtifactsDeployer()).thenReturn(deployer);
-        FeaturesManager manager = Mockito.spy(FeaturesManager.class);
-        when(converter.getFeaturesManager()).thenReturn(manager);
-        handler.handle(path, archive, entry, converter);
-        Mockito.verify(manager).addArtifact(null, 
ArtifactId.fromMvnId("org.osgi.service:jdbc:1.0.0-201505202023"),null);
+        
setUpArchive("/jcr_root/apps/gav/install/org.osgi.service.jdbc-1.0.0-nogav.jar",
 "org.osgi.service.jdbc-1.0.0-nogav.jar");
+        
handler.handle("/jcr_root/apps/gav/install/org.osgi.service.jdbc-1.0.0-nogav.jar",
 archive, entry, converter);
+        Mockito.verify(featuresManager).addArtifact(null, 
ArtifactId.fromMvnId("org.osgi.service:jdbc:1.0.0-201505202023"), null);
     }
 
     @Test
     public void testBundleBelowConfigFolderWithEnforcement() throws Exception {
         handler.setEnforceBundlesBelowInstallFolder(true);
-        Archive.Entry entry = Mockito.mock(Archive.Entry.class);
         when(entry.getName()).thenReturn("mybundle.jar");
         assertThrows(IllegalStateException.class, () -> { 
handler.handle("/jcr_root/apps/myapp/config/mybundle.jar", null, entry, null); 
});
     }
+
+    @Test
+    public void testSlingInitialContent() throws Exception {
+        
setUpArchive("/jcr_root/apps/gav/install/io.wcm.handler.media-1.11.6.jar", 
"io.wcm.handler.media-1.11.6.jar");
+        DefaultEntryHandlersManager handlersManager = new 
DefaultEntryHandlersManager();
+        converter.setEntryHandlersManager(handlersManager);
+        Map<String, String> namespaceRegistry = 
Collections.singletonMap("granite", "http://www.adobe.com/jcr/granite/1.0";);
+        
when(featuresManager.getNamespaceUriByPrefix()).thenReturn(namespaceRegistry);
+        
+        File newPackageFile = tmpFolder.newFile();
+        Mockito.doAnswer(invocation -> {
+            // capture package
+            File sourcePackage = (File)invocation.getArgument(0);
+            FileUtils.copyFile(sourcePackage, newPackageFile);
+            return null;
+        }).when(converter).processContentPackageArchive(Mockito.any(), 
Mockito.isNull());
+        
handler.setSlingInitialContentPolicy(SlingInitialContentPolicy.EXTRACT_AND_REMOVE);
+        
handler.handle("/jcr_root/apps/gav/install/io.wcm.handler.media-1.11.6.jar", 
archive, entry, converter);
+        // verify package contents
+        try (VaultPackage vaultPackage = new 
PackageManagerImpl().open(newPackageFile);
+             Archive archive = vaultPackage.getArchive()) {
+            archive.open(true);
+            PackageId targetId = 
PackageId.fromString("io.wcm:io.wcm.handler.media-apps:1.11.6-cp2fm-converted");
+            assertEquals(targetId, vaultPackage.getId());
+            Entry entry = 
archive.getEntry("jcr_root/apps/wcm-io/handler/media/components/global/include/responsiveImageSettings.xml");
+            assertNotNull("Archive does not contain expected item", entry);
+        }
+        // changed id
+        Mockito.verify(featuresManager).addArtifact(null, 
ArtifactId.fromMvnId("io.wcm:io.wcm.handler.media:1.11.6-cp2fm-converted"), 
null);
+    }
+
+    @Test
+    public void 
testSlingInitialContentContainingConfigurationExtractAndRemove() throws 
Exception {
+        
setUpArchive("/jcr_root/apps/gav/install/composum-nodes-config-2.5.3.jar", 
"composum-nodes-config-2.5.3.jar");
+        DefaultEntryHandlersManager handlersManager = new 
DefaultEntryHandlersManager();
+        converter.setEntryHandlersManager(handlersManager);
+        
handler.setSlingInitialContentPolicy(SlingInitialContentPolicy.EXTRACT_AND_REMOVE);
+        
handler.handle("/jcr_root/apps/gav/install/composum-nodes-config-2.5.3.jar", 
archive, entry, converter);
+        // verify no additional content package created (as it only contains 
the configuration which should end up in the feature model only)
+        Mockito.verify(converter, 
Mockito.never()).processContentPackageArchive(Mockito.any(), Mockito.isNull());
+        // modified bundle
+        Mockito.verify(featuresManager).addArtifact(null, 
ArtifactId.fromMvnId("com.composum.nodes:composum-nodes-config:2.5.3-cp2fm-converted"),
 null);
+        // need to use ArgumentCaptur to properly compare string arrays
+        
Mockito.verify(featuresManager).addConfiguration(ArgumentMatchers.isNull(), 
ArgumentMatchers.eq("org.apache.sling.jcr.base.internal.LoginAdminWhitelist.fragment~composum_core_v2"),
 
ArgumentMatchers.eq("/jcr_root/libs/composum/nodes/install/org.apache.sling.jcr.base.internal.LoginAdminWhitelist.fragment-composum_core_v2.config"),
 dictionaryCaptor.capture());
+        assertEquals("composum_core", 
dictionaryCaptor.getValue().get("whitelist.name"));
+        assertArrayEquals(new String[] {
+                "com.composum.nodes.commons",
+                "com.composum.nodes.pckgmgr",
+                "com.composum.nodes.pckginstall" }, 
(String[])dictionaryCaptor.getValue().get("whitelist.bundles"));
+    }
+    
+    @Test
+    public void testSlingInitialContentContainingConfigurationExtractAndKeep() 
throws Exception {
+        
setUpArchive("/jcr_root/apps/gav/install/composum-nodes-config-2.5.3.jar", 
"composum-nodes-config-2.5.3.jar");
+        DefaultEntryHandlersManager handlersManager = new 
DefaultEntryHandlersManager();
+        converter.setEntryHandlersManager(handlersManager);
+        
handler.setSlingInitialContentPolicy(SlingInitialContentPolicy.EXTRACT_AND_KEEP);
+        
handler.handle("/jcr_root/apps/gav/install/composum-nodes-config-2.5.3.jar", 
archive, entry, converter);
+        // original bundle
+        Mockito.verify(featuresManager).addArtifact(null, 
ArtifactId.fromMvnId("com.composum.nodes:composum-nodes-config:2.5.3"), null);
+        // need to use ArgumentCaptur to properly compare string arrays
+        
Mockito.verify(featuresManager).addConfiguration(ArgumentMatchers.isNull(), 
ArgumentMatchers.eq("org.apache.sling.jcr.base.internal.LoginAdminWhitelist.fragment~composum_core_v2"),
 
ArgumentMatchers.eq("/jcr_root/libs/composum/nodes/install/org.apache.sling.jcr.base.internal.LoginAdminWhitelist.fragment-composum_core_v2.config"),
 dictionaryCaptor.capture());
+        assertEquals("composum_core", 
dictionaryCaptor.getValue().get("whitelist.name"));
+        assertArrayEquals(new String[] {
+                "com.composum.nodes.commons",
+                "com.composum.nodes.pckgmgr",
+                "com.composum.nodes.pckginstall" }, 
(String[])dictionaryCaptor.getValue().get("whitelist.bundles"));
+    }

Review comment:
       BundleEntryHandlerTest is a parameterized test which is not suitable for 
this test. I can move them to a dedicated test class.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to