/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.test;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.function.Predicate;

public abstract class AbstractSerializingTestCase<T extends ToXContent & Writeable> extends AbstractWireSerializingTestCase<T> {

    /**
     * Generic test that creates new instance from the test instance and checks
     * both for equality and asserts equality on the two instances.
     */
    public final void testFromXContent() throws IOException {
        AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, this::createTestInstance, supportsUnknownFields(),
                getShuffleFieldsExceptions(), getRandomFieldsExcludeFilter(), this::createParser, this::doParseInstance,
                this::assertEqualInstances, true, getToXContentParams());
    }

    /**
     * Parses to a new instance using the provided {@link XContentParser}
     */
    protected abstract T doParseInstance(XContentParser parser) throws IOException;

    /**
     * Indicates whether the parser supports unknown fields or not. In case it does, such behaviour will be tested by
     * inserting random fields before parsing and checking that they don't make parsing fail.
     */
    protected boolean supportsUnknownFields() {
        return false;
    }

    /**
     * Returns a predicate that given the field name indicates whether the field has to be excluded from random fields insertion or not
     */
    protected Predicate<String> getRandomFieldsExcludeFilter() {
        return field -> false;
    }

    /**
     * Fields that have to be ignored when shuffling as part of testFromXContent
     */
    protected String[] getShuffleFieldsExceptions() {
        return Strings.EMPTY_ARRAY;
    }

    /**
     * Params that have to be provided when calling calling {@link ToXContent#toXContent(XContentBuilder, ToXContent.Params)}
     */
    protected ToXContent.Params getToXContentParams() {
        return ToXContent.EMPTY_PARAMS;
    }
}

@@ -782,16 +782,17 @@
 se.%0A */%0A
+%0A
 package 
@@ -1551,16 +1551,33 @@
 Content(
+%0A                
 NUMBER_O
@@ -1588,16 +1588,32 @@
 ST_RUNS,
+%0A               
  this::c
@@ -1630,16 +1630,32 @@
 nstance,
+%0A               
  support
@@ -1717,16 +1717,32 @@
 tions(),
+%0A               
  getRand
@@ -1765,16 +1765,32 @@
 ilter(),
+%0A               
  this::c
@@ -1801,16 +1801,32 @@
 eParser,
+%0A               
  this::d
@@ -1888,14 +1888,71 @@
 ces,
- true,
+%0A                assertToXContentEquivalence(),%0A               
  get
@@ -3146,16 +3146,429 @@
 PARAMS;%0A
-
     %7D%0A
+%0A    /**%0A     * Whether or not to assert equivalence of the %7B@link org.elasticsearch.common.xcontent.XContent%7D of the test instance and the instance%0A     * parsed from the %7B@link org.elasticsearch.common.xcontent.XContent%7D of the test instance.%0A     *%0A     * @return true if equivalence should be asserted, otherwise false%0A     */%0A    protected boolean assertToXContentEquivalence() %7B%0A        return true;%0A    %7D%0A%0A
 %7D%0A

Add hook to skip asserting x-content equivalence (#33114)