thomasmueller commented on code in PR #3198:
URL: https://github.com/apache/jackrabbit-oak/pull/3198#discussion_r4217660673


##########
oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/diff/DiffIndexLegacyTest.java:
##########
@@ -0,0 +1,272 @@
+/*
+ * 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.jackrabbit.oak.plugins.index.diff;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.json.JsonObject;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.plugins.memory.BinaryPropertyState;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.apache.jackrabbit.oak.spi.toggle.FeatureToggle;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class DiffIndexLegacyTest {
+
+    @Rule
+    public final OsgiContext context = new OsgiContext();
+
+    @Test
+    public void featureToggleSelectsLegacyAndNewCollectors() {
+        DiffIndex component = new DiffIndex();
+        MockOsgi.activate(component, context.bundleContext(), Map.of());
+        try {
+            FeatureToggle toggle = context.getService(FeatureToggle.class);
+            assertNotNull(toggle);
+            assertEquals(DiffIndex.LEGACY_DIFF_INDEX_TOGGLE, toggle.getName());
+            assertFalse(DiffIndex.isLegacyMode());
+            assertProcessedFiles(false);
+            toggle.setEnabled(true);
+            assertTrue(DiffIndex.isLegacyMode());
+            assertProcessedFiles(true);
+            toggle.setEnabled(false);
+            assertFalse(DiffIndex.isLegacyMode());
+            assertProcessedFiles(false);
+        } finally {
+            MockOsgi.deactivate(component, context.bundleContext());
+        }
+        assertNull(context.getService(FeatureToggle.class));
+        assertFalse(DiffIndex.isLegacyMode());
+    }
+
+    @Test
+    public void legacyExtractionIgnoresAdditionalJsonFilesAndFileReferences() {
+        DiffIndex component = new DiffIndex();
+        MockOsgi.activate(component, context.bundleContext(), Map.of());
+        try {
+            context.getService(FeatureToggle.class).setEnabled(true);
+            JsonObject definitions = JsonObject.fromJson("""
+                    {
+                        "/oak:index/diff.index": {
+                            "additional.json": {},
+                            "diff": {
+                                "acme.test": { "jcr:data": ":file:missing.txt" 
}
+                            }
+                        }
+                    }
+                    """, true);
+            HashMap<String, JsonObject> target = new HashMap<>();
+            assertNull(new DiffIndexMerger().tryExtractDiffIndex(definitions, 
"/oak:index/diff.index", target));
+            assertEquals("\":file:missing.txt\"", 
target.get("acme.test").getProperties().get("jcr:data"));
+            context.getService(FeatureToggle.class).setEnabled(false);
+            assertNotNull(new DiffIndexMerger().tryExtractDiffIndex(
+                    definitions, "/oak:index/diff.index", new HashMap<>()));
+        } finally {
+            MockOsgi.deactivate(component, context.bundleContext());
+        }
+    }
+
+    @Test
+    public void legacyExtractorReadsOnlyDiffJsonAndReportsMalformedFiles() {
+        String path = "/oak:index/diff.index";
+        JsonObject definitions = new JsonObject(true);
+        JsonObject index = new JsonObject(true);
+        JsonObject file = new JsonObject(true);
+        JsonObject content = new JsonObject(true);
+        definitions.getChildren().put(path, index);
+        index.getChildren().put("diff.json", file);
+        file.getChildren().put("jcr:content", content);
+        content.getProperties().put("jcr:data", 
JsopBuilder.encode("{\"acme.test\":{\"type\":\"lucene\"}}"));
+        DiffIndexMerger merger = new DiffIndexMerger();
+        HashMap<String, JsonObject> target = new HashMap<>();
+        assertNull(merger.tryExtractDiffIndexLegacy(definitions, path, 
target));
+        assertEquals("\"lucene\"", 
target.get("acme.test").getProperties().get("type"));
+        file.getChildren().clear();
+        assertEquals("jcr:content child node is missing in diff.json",
+                merger.tryExtractDiffIndexLegacy(definitions, path, new 
HashMap<>()));
+        file.getChildren().put("jcr:content", content);
+        content.getProperties().put("jcr:data", JsopBuilder.encode("{broken"));
+        assertTrue(merger.tryExtractDiffIndexLegacy(definitions, path, new 
HashMap<>()).startsWith("Illegal Json"));
+        assertEquals(2, merger.getAndClearWarnings().size());
+        index.getChildren().clear();
+        assertNull(merger.tryExtractDiffIndexLegacy(definitions, path, new 
HashMap<>()));
+    }
+
+    @Test
+    public void legacyModePreservesCustomerVersionNumbering() {
+        DiffIndex component = new DiffIndex();
+        MockOsgi.activate(component, context.bundleContext(), Map.of());
+        try {
+            JsonObject repository = JsonObject.fromJson("""
+                    {
+                        "/oak:index/product-2": { "type": "lucene" },
+                        "/oak:index/product-1-custom-3": {
+                            "type": "lucene", "mergeInfo": "previous 
customization"
+                        }
+                    }
+                    """, true);
+            for (boolean legacy : new boolean[] {false, true}) {
+                context.getService(FeatureToggle.class).setEnabled(legacy);
+                JsonObject definitions = JsonObject.fromJson("""
+                        {
+                            "/oak:index/diff.index": {
+                                "diff": { "product": { "additional": true } }
+                            }
+                        }
+                        """, true);
+                new DiffIndexMerger().merge(definitions, repository, null);
+                String expected = "/oak:index/product-2-custom-" + (legacy ? 
"4" : "1");
+                assertNotNull(definitions.getChildren().get(expected));
+            }
+        } finally {
+            MockOsgi.deactivate(component, context.bundleContext());
+        }
+    }
+
+    @Test
+    public void legacyCollectorSkipsMissingAndUnchangedData() {

Review Comment:
   I'll also remove the test with "setAccessible(true)"



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to