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

joerghoh pushed a commit to branch SLING-11969
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-jacksonexporter.git

commit b7b921eb5c3736aa8422e13b3a7fb2b656ca586f
Author: Joerg Hoh <joerg...@apache.org>
AuthorDate: Sun Dec 31 17:48:52 2023 +0100

    SLING-11969 a few minor improvements to make the limits more explicit
---
 .../ConfigurableSerializationModuleProvider.java   | 36 +++++++----
 .../JacksonExporterLimitSerializationTest.java     | 70 +++++++++-------------
 2 files changed, 53 insertions(+), 53 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java
 
b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java
index d9cf2b9..df246db 100644
--- 
a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java
+++ 
b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ConfigurableSerializationModuleProvider.java
@@ -26,6 +26,8 @@ import org.osgi.service.component.annotations.Component;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.databind.Module;
 import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -39,36 +41,48 @@ public class ConfigurableSerializationModuleProvider 
implements ModuleProvider {
     static @interface Config {
 
         @AttributeDefinition(name ="disable serialization",
-                description = "provide a list of the full classnames which 
should not get serialized")
+                description = "provide a list of the full classnames which 
should not get serialized; currently only \"" + RESOURCERESOLVER + "\" is 
supported")
         String[] disable_serialization() default {};
         
         @AttributeDefinition(name ="warn on serialization",
-                description = "provide a list of the full classnames for which 
a warning should be written when serialized")
+                description = "provide a list of the full classnames for which 
a warning should be written when serialized; currently only \"" + 
RESOURCERESOLVER + "\" is supported")
         String[] enable_warn_logging() default 
{ConfigurableSerializationModuleProvider.RESOURCERESOLVER};
         
 
     }
    
+    private static final Logger LOG = 
LoggerFactory.getLogger(ConfigurableSerializationModuleProvider.class);
     protected static final String RESOURCERESOLVER = 
"org.apache.sling.api.resource.ResourceResolver";
     
+    boolean ignoringRR = false;
     
     SimpleModule moduleInstance;
     
-    
-   
-    
     @Activate
     private void activate(Config config) {
         this.moduleInstance = new SimpleModule();
-        
-        // Currently only the Sling ResourceResolver is supported to be 
disabled, other classes tbd.
+
         List<String> disabled = Arrays.asList(config.disable_serialization());
         List<String> logging = Arrays.asList(config.enable_warn_logging());
         
-        if (disabled.contains(RESOURCERESOLVER)) {
-            moduleInstance.setMixInAnnotation(ResourceResolver.class, 
IgnoringResourceResolverMixin.class);
-        } else if (logging.contains(RESOURCERESOLVER)) {
-            moduleInstance.setMixInAnnotation(ResourceResolver.class, 
WarningResourceResolverMixin.class);
+        // Currently only the Sling ResourceResolver is supported
+        for (String type: disabled) {
+            if (RESOURCERESOLVER.equals(type)) {
+                moduleInstance.setMixInAnnotation(ResourceResolver.class, 
IgnoringResourceResolverMixin.class);
+                ignoringRR = true;
+                LOG.info("Not serializing the RR");
+            } else {
+                LOG.warn("Support to disable the serialization of type {} is 
not implemented", type);
+            }
+        }
+        
+        for (String type: logging) {
+            if (RESOURCERESOLVER.equals(type) && !ignoringRR) {
+                moduleInstance.setMixInAnnotation(ResourceResolver.class, 
WarningResourceResolverMixin.class);
+                LOG.info("Logging the RR");
+            } else {
+                LOG.warn("Support to log any serialization of type {} is not 
implemented", type);
+            }
         }
     }
     
diff --git 
a/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java
 
b/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java
index 74bdc18..2adf307 100644
--- 
a/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java
+++ 
b/src/test/java/org/apache/sling/models/jacksonexporter/impl/JacksonExporterLimitSerializationTest.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -36,8 +37,6 @@ import 
org.apache.sling.models.jacksonexporter.impl.example.PojoWithResourceReso
 import org.apache.sling.models.jacksonexporter.impl.util.LogCapture;
 import org.apache.sling.testing.mock.osgi.junit5.OsgiContext;
 import org.apache.sling.testing.mock.osgi.junit5.OsgiContextExtension;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 
@@ -85,8 +84,29 @@ class JacksonExporterLimitSerializationTest {
 
         String expectedJson = "{\"msg\":\"text\"}";
         assertEquals(expectedJson, underTest.export(pojo, String.class, 
options));
+        // no log is written in this case
+        assertEquals(0,capture.list.size());
+    }
+    
+    
+    @Test
+    void test_givenInvalidTypes_whenActivate_thenWarnLogStatements() throws 
ExportException {
+        LogCapture capture = new 
LogCapture(ConfigurableSerializationModuleProvider.class.getName(),false);
         
-//        assertTrue(capture.anyMatch(p -> 
p.getFormattedMessage().contains(IgnoringResourceResolverMixin.MESSAGE)));
+        Map<String,Object> config = new HashMap<>();
+        config.put("disable.serialization", "foo.bar.disable");
+        config.put("enable.warn.logging", "foo.bar.logging");
+        
+        context.registerInjectActivateService(new 
ConfigurableSerializationModuleProvider(),config);
+
+        assertTrue(capture.anyMatch(event -> {
+           return event.getFormattedMessage().equals("Support to disable the 
serialization of type foo.bar.disable is not implemented") && 
+                   event.getLevel().equals(Level.WARN);
+        }));
+        assertTrue(capture.anyMatch(event -> {
+            return event.getFormattedMessage().equals("Support to log any 
serialization of type foo.bar.logging is not implemented") && 
+                    event.getLevel().equals(Level.WARN);
+         }));
     }
     
     
@@ -95,181 +115,147 @@ class JacksonExporterLimitSerializationTest {
      * when trying to export it with Jackson.
      */
     public class EmptyResourceResolver implements ResourceResolver {
-        
-//        public static final String SERIALIZED_STRING = 
"\"resolver\":{\"live\":false,\"userID\":null,\"searchPath\":null,\"propertyMap\":null,\"userID\":false}";
-        
 
         @Override
         public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Resource resolve(HttpServletRequest request, String absPath) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Resource resolve(String absPath) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Resource resolve(HttpServletRequest request) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public String map(String resourcePath) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public String map(HttpServletRequest request, String resourcePath) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Resource getResource(String path) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Resource getResource(Resource base, String path) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public String[] getSearchPath() {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Iterator<Resource> listChildren(Resource parent) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Iterable<Resource> getChildren(Resource parent) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Iterator<Resource> findResources(String query, String language) 
{
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Iterator<Map<String, Object>> queryResources(String query, 
String language) {
-            // TODO Auto-generated method stub
             return null;
         }
 
 
         @Override
         public ResourceResolver clone(Map<String, Object> authenticationInfo) 
throws LoginException {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public boolean isLive() {
-            // TODO Auto-generated method stub
             return false;
         }
 
         @Override
         public void close() {
-            // TODO Auto-generated method stub
-            
+            // do nothing
         }
 
         @Override
         public String getUserID() {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Iterator<String> getAttributeNames() {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public Object getAttribute(String name) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public void delete(Resource resource) throws PersistenceException {
-            // TODO Auto-generated method stub
-            
+            // do nothing
         }
 
         @Override
         public Resource create(Resource parent, String name, Map<String, 
Object> properties)
                 throws PersistenceException {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public void revert() {
-            // TODO Auto-generated method stub
-            
+            // do nothing
         }
 
         @Override
         public void commit() throws PersistenceException {
-            // TODO Auto-generated method stub
-            
+            // do nothing
         }
 
         @Override
         public boolean hasChanges() {
-            // TODO Auto-generated method stub
             return false;
         }
 
         @Override
         public String getParentResourceType(Resource resource) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public String getParentResourceType(String resourceType) {
-            // TODO Auto-generated method stub
             return null;
         }
 
         @Override
         public boolean isResourceType(Resource resource, String resourceType) {
-            // TODO Auto-generated method stub
             return false;
         }
 
         @Override
         public void refresh() {
-            // TODO Auto-generated method stub
-            
+            // do nothing
         }
-        
     }
-    
-    
 }

Reply via email to