bhabegger commented on code in PR #3068:
URL: https://github.com/apache/jackrabbit-oak/pull/3068#discussion_r3734629964


##########
oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/diff/DiffIndexMergerTest.java:
##########
@@ -0,0 +1,1405 @@
+/*
+ * 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 java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.commons.json.JsonObject;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
+import org.apache.jackrabbit.oak.spi.commit.EmptyHook;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.junit.Test;
+
+public class DiffIndexMergerTest {
+
+    public static DiffIndexMerger getMerger() {
+        DiffIndexMerger merger = new DiffIndexMerger().
+                setDeleteCopiesOutOfTheBoxIndex(true).
+                setDeleteCreatesDummyIndex(true).
+                setLogAtInfoLevel(true).
+                setUnsupportedIncludedPaths(new String[] {"/apps", "/libs"});
+        return merger;
+    }
+
+    @Test
+    public void noCustomization() {
+        JsonObject newImageLuceneDefinitions = JsonObject.fromJson("{}", true);
+        JsonObject repositoryDefinitions = JsonObject.fromJson("""
+                {
+                    "/oak:index/damAssetLucene-12": {
+                        "jcr:primaryType": "oak:IndexDefinition",
+                        "type": "lucene",
+                        "async": ["async", "nrt"],
+                        "tags": ["abc"],
+                        "indexRules": {
+                            "dam:Asset": {
+                                "properties": {
+                                    "x": {
+                                        "name": "x",
+                                        "propertyIndex": true
+                                    }
+                                }
+                            }
+                        }
+                    },
+                    "/oak:index/diff.index": {
+                        "jcr:primaryType": "nt:unstructured",
+                        "type": "lucene", "includedPaths": "/a", "queryPaths": 
"/a"
+                    }
+                }
+                """, true);
+
+        getMerger().merge(newImageLuceneDefinitions, repositoryDefinitions, 
null);
+        assertEquals("{}", newImageLuceneDefinitions.toString());
+    }
+
+    // an index was customized in the past, but not any longer
+    @Test
+    public void noCustomizationAnyLonger() {
+        JsonObject newImageLuceneDefinitions = JsonObject.fromJson("{}", true);
+        JsonObject repositoryDefinitions = JsonObject.fromJson("""
+                {
+                    "/oak:index/damAssetLucene-12": {
+                        "jcr:primaryType": "oak:IndexDefinition",
+                        "type": "lucene",
+                        "async": ["async", "nrt"],
+                        "includedPaths": "/content/dam",
+                        "tags": ["abc"],
+                        "indexRules": {
+                            "dam:Asset": {
+                                "properties": {
+                                    "x": {
+                                        "name": "x",
+                                        "propertyIndex": true
+                                    }
+                                }
+                            }
+                        }
+                    },
+                    "/oak:index/damAssetLucene-12-custom-1": {
+                        "jcr:primaryType": "oak:IndexDefinition",
+                        "type": "lucene",
+                        "async": ["async", "nrt"],
+                        "tags": ["abc"],
+                        "includedPaths": "/content/dam",
+                        "mergeInfo": "This index was auto-merged. See also 
https://thomasmueller.github.io/oakTools/simplified.html";,
+                        "mergeChecksum": 
"ef0312b8bf7cc97bb8efd0faeeb4e4b1094268694a2df749b73fac08987b3264",
+                        "merges": ["/oak:index/damAssetLucene"],
+                        "indexRules": {
+                            "jcr:primaryType": "nam:nt:unstructured",
+                            "dam:Asset": {
+                                "jcr:primaryType": "nam:nt:unstructured",
+                                "properties": {
+                                    "jcr:primaryType": "nam:nt:unstructured",
+                                    "x": {
+                                        "name": "x",
+                                        "propertyIndex": true,
+                                        "jcr:primaryType": 
"nam:nt:unstructured"
+                                    },
+                                    "y": {
+                                        "name": "y",
+                                        "propertyIndex": true,
+                                        "jcr:primaryType": 
"nam:nt:unstructured"
+                                    }
+                                }
+                            }
+                        }
+                    },
+                    "/oak:index/diff.index": {
+                        "jcr:primaryType": "nt:unstructured",
+                        "type": "lucene", "includedPaths": "/a", "queryPaths": 
"/a"
+                    }
+                }
+                """, true);
+
+        getMerger().merge(newImageLuceneDefinitions, repositoryDefinitions, 
null);
+        assertEquals("""
+                {
+                  "/oak:index/damAssetLucene-12-custom-2": {
+                    "jcr:primaryType": "oak:IndexDefinition",
+                    "type": "lucene",
+                    "async": ["async", "nrt"],
+                    "includedPaths": "/content/dam",
+                    "tags": ["abc"],
+                    "mergeInfo": "This index was auto-merged. See also 
https://oak-indexing.github.io/oakTools/simplified.html";,
+                    "mergeChecksum": 
"aa02b93e0ba4ff4bcffb8247d38c5cc039be5afa915f822e18df7a578fabff76",
+                    "merges": ["/oak:index/damAssetLucene"],
+                    "indexRules": {
+                      "jcr:primaryType": "nam:nt:unstructured",
+                      "dam:Asset": {
+                        "jcr:primaryType": "nam:nt:unstructured",
+                        "properties": {
+                          "jcr:primaryType": "nam:nt:unstructured",
+                          "x": {
+                            "name": "x",
+                            "propertyIndex": true,
+                            "jcr:primaryType": "nam:nt:unstructured"
+                          }
+                        }
+                      }
+                    }
+                  }
+                }""", newImageLuceneDefinitions.toString());
+    }
+
+    @Test
+    public void firstCustomization() {
+        JsonObject newImageLuceneDefinitions = JsonObject.fromJson("{}", true);
+        JsonObject indexDiff = JsonObject.fromJson("""
+                {
+                    "damAssetLucene": {
+                        "indexRules": {
+                            "dam:Asset": {
+                                "properties": {
+                                    "y": {
+                                        "name": "y",
+                                        "propertyIndex": true
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            """, true);
+        String indexDiffString = indexDiff.toString();
+        String base64Prop =
+                "\":blobId:" + 
Base64.getEncoder().encodeToString(indexDiffString.getBytes(StandardCharsets.UTF_8))
 + "\"";
+        JsonObject repositoryDefinitions = JsonObject.fromJson("""
+                {
+                    "/oak:index/damAssetLucene-12": {
+                        "jcr:primaryType": "oak:IndexDefinition",
+                        "type": "lucene",
+                        "async": ["async", "nrt"],
+                        "tags": ["abc"],
+                        "includedPaths": "/content/dam",
+                        "indexRules": {
+                            "dam:Asset": {
+                                "properties": {
+                                    "x": {
+                                        "name": "x",
+                                        "propertyIndex": true
+                                    }
+                                }
+                            }
+                        }
+                    },
+                    "/oak:index/diff.index": {
+                        "jcr:primaryType": "nt:unstructured",
+                        "type": "lucene", "includedPaths": "/same", 
"queryPaths": "/same",
+                        "diff.json": {
+                            "jcr:primaryType": "nam:nt:file",
+                            "jcr:content": {
+                                "jcr:primaryType": "nam:nt:resource",
+                                "jcr:mimeType": "application/json",
+                                "jcr:data":
+                """ + base64Prop + """
+                            }
+                        }
+                    }
+                }
+                """, true);
+
+        getMerger().merge(newImageLuceneDefinitions, repositoryDefinitions, 
null);
+        assertEquals("""
+                {
+                  "/oak:index/damAssetLucene-12-custom-1": {
+                    "jcr:primaryType": "oak:IndexDefinition",
+                    "type": "lucene",
+                    "async": ["async", "nrt"],
+                    "tags": ["abc"],
+                    "includedPaths": "/content/dam",
+                    "mergeInfo": "This index was auto-merged. See also 
https://oak-indexing.github.io/oakTools/simplified.html";,
+                    "mergeChecksum": 
"ef0312b8bf7cc97bb8efd0faeeb4e4b1094268694a2df749b73fac08987b3264",
+                    "merges": ["/oak:index/damAssetLucene"],
+                    "indexRules": {
+                      "jcr:primaryType": "nam:nt:unstructured",
+                      "dam:Asset": {
+                        "jcr:primaryType": "nam:nt:unstructured",
+                        "properties": {
+                          "jcr:primaryType": "nam:nt:unstructured",
+                          "x": {
+                            "name": "x",
+                            "propertyIndex": true,
+                            "jcr:primaryType": "nam:nt:unstructured"
+                          },
+                          "y": {
+                            "name": "y",
+                            "propertyIndex": true,
+                            "jcr:primaryType": "nam:nt:unstructured"
+                          }
+                        }
+                      }
+                    }
+                  }
+                }""", newImageLuceneDefinitions.toString());
+    }
+
+    @Test
+    public void firstCustomizationAsJson() {
+        JsonObject newImageLuceneDefinitions = JsonObject.fromJson("{}", true);
+        JsonObject repositoryDefinitions = JsonObject.fromJson("""
+                {
+                    "/oak:index/damAssetLucene-12": {
+                        "jcr:primaryType": "oak:IndexDefinition",
+                        "type": "lucene",
+                        "async": ["async", "nrt"],
+                        "tags": ["abc"],
+                        "includedPaths": "/content/dam",
+                        "indexRules": {
+                            "dam:Asset": {
+                                "properties": {
+                                    "x": {
+                                        "name": "x",
+                                        "propertyIndex": true
+                                    }
+                                }
+                            }
+                        }
+                    },
+                    "/oak:index/diff.index.optimizer": {
+                        "jcr:primaryType": "nt:unstructured",
+                        "type": "lucene", "includedPaths": "/same", 
"queryPaths": "/same",
+                        "diff": {
+                           "damAssetLucene": {
+                                "indexRules": {
+                                    "dam:Asset": {
+                                        "properties": {
+                                            "y": {
+                                                "name": "y",
+                                                "propertyIndex": true
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+                """, true);
+
+        getMerger().merge(newImageLuceneDefinitions, repositoryDefinitions, 
null);
+        assertEquals("""
+                {
+                  "/oak:index/damAssetLucene-12-custom-1": {
+                    "jcr:primaryType": "oak:IndexDefinition",
+                    "type": "lucene",
+                    "async": ["async", "nrt"],
+                    "tags": ["abc"],
+                    "includedPaths": "/content/dam",
+                    "mergeInfo": "This index was auto-merged. See also 
https://oak-indexing.github.io/oakTools/simplified.html";,
+                    "mergeChecksum": 
"ef0312b8bf7cc97bb8efd0faeeb4e4b1094268694a2df749b73fac08987b3264",
+                    "merges": ["/oak:index/damAssetLucene"],
+                    "indexRules": {
+                      "jcr:primaryType": "nam:nt:unstructured",
+                      "dam:Asset": {
+                        "jcr:primaryType": "nam:nt:unstructured",
+                        "properties": {
+                          "jcr:primaryType": "nam:nt:unstructured",
+                          "x": {
+                            "name": "x",
+                            "propertyIndex": true,
+                            "jcr:primaryType": "nam:nt:unstructured"
+                          },
+                          "y": {
+                            "name": "y",
+                            "propertyIndex": true,
+                            "jcr:primaryType": "nam:nt:unstructured"
+                          }
+                        }
+                      }
+                    }
+                  }
+                }""", newImageLuceneDefinitions.toString());
+    }
+
+    @Test
+    public void newIndexAndNewCustomization() {
+        JsonObject repositoryDefinitions = JsonObject.fromJson("""
+                {
+                    "/oak:index/damAssetLucene-11": {
+                        "jcr:primaryType": "nam:oak:IndexDefinition",
+                        "type": "lucene",
+                        "async": ["async", "nrt"],
+                        "tags": ["abc"],
+                        "includedPaths": "/content/dam",
+                        "indexRules": {
+                            "dam:Asset": {
+                                "properties": {
+                                    "a": {
+                                        "name": "a",
+                                        "propertyIndex": true
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+                """, true);
+        JsonObject newImageLuceneDefinitions = JsonObject.fromJson("""
+                {
+                    "/oak:index/damAssetLucene-12": {
+                        "jcr:primaryType": "nam:oak:IndexDefinition",
+                        "type": "lucene",
+                        "async": ["async", "nrt"],
+                        "tags": ["abc"],
+                        "includedPaths": "/content/dam",
+                        "indexRules": {
+                            "dam:Asset": {
+                                "properties": {
+                                    "x": {
+                                        "name": "x",
+                                        "propertyIndex": true
+                                    }
+                                }
+                            }
+                        }
+                    },
+                    "/oak:index/diff.index": {
+                        "jcr:primaryType": "nam:nt:unstructured",
+                        "type": "lucene", "includedPaths": "/same", 
"queryPaths": "/same",
+                        "diff": {
+                           "damAssetLucene": {
+                                "indexRules": {
+                                    "dam:Asset": {
+                                        "properties": {
+                                            "y": {
+                                                "name": "y",
+                                                "propertyIndex": true
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            """, true);
+
+        getMerger().merge(newImageLuceneDefinitions, repositoryDefinitions, 
null);
+        assertEquals("""
+                {
+                  "/oak:index/damAssetLucene-12": {
+                    "jcr:primaryType": "nam:oak:IndexDefinition",
+                    "type": "lucene",
+                    "async": ["async", "nrt"],
+                    "tags": ["abc"],
+                    "includedPaths": "/content/dam",
+                    "indexRules": {
+                      "dam:Asset": {
+                        "properties": {
+                          "x": {
+                            "name": "x",
+                            "propertyIndex": true
+                          }
+                        }
+                      }
+                    }
+                  },
+                  "/oak:index/diff.index": {

Review Comment:
   In the previous tests, the diff.index doesn't appear in the 
newImageLuceneDefinitions. Why would this case be different ? 



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