forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add realistic hlrc response serialization test base class.
The base class facilitates generating a server side response test instance, that gets serialized as xcontent, which then gets parsed into a hlrc response instance, which then gets asserted against the server side response instance. This way of testing is more realistic then how hlrc response classes are tested today, which basically tests that serialization works by generating hlrc response instance, serialize that to xcontent and then parse it back to a hlrc response instance. Besides adding a base test class, this change also cuts AcknowledgedResponseTests and BroadcastResponseTests over to use this base class. Relates to elastic#39745
- Loading branch information
Showing
3 changed files
with
112 additions
and
46 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.client; | ||
|
||
import org.elasticsearch.cluster.ClusterModule; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContent; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
public abstract class AbstractResponseTestCase<SERVER_INSTANCE extends ToXContent, HLRC_INSTANCE> extends ESTestCase { | ||
|
||
private static final int NUMBER_OF_TEST_RUNS = 20; | ||
|
||
public final void testFromXContent() throws IOException { | ||
for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { | ||
final SERVER_INSTANCE serverTestInstance = createServerTestInstance(); | ||
|
||
final XContentType xContentType = randomFrom(XContentType.values()); | ||
final BytesReference bytes = toShuffledXContent(serverTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); | ||
|
||
final XContent xContent = XContentFactory.xContent(xContentType); | ||
final XContentParser parser = xContent.createParser( | ||
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()), | ||
LoggingDeprecationHandler.INSTANCE, | ||
bytes.streamInput()); | ||
final HLRC_INSTANCE hlrcInstance = doParseInstance(parser); | ||
assertInstances(serverTestInstance, hlrcInstance); | ||
} | ||
} | ||
|
||
protected abstract SERVER_INSTANCE createServerTestInstance(); | ||
|
||
protected abstract HLRC_INSTANCE doParseInstance(XContentParser parser) throws IOException; | ||
|
||
protected abstract void assertInstances(final SERVER_INSTANCE serverTestInstance, final HLRC_INSTANCE hlrcInstance); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters