[TEST] randomize request content_type between all of the supported formats

This commit is contained in:
javanna 2017-02-17 22:39:01 +01:00 committed by Luca Cavanna
parent 9a2dba3036
commit 4f487ab1b9
4 changed files with 73 additions and 15 deletions

View File

@ -48,5 +48,10 @@ public class DocsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
logger.error("This failing test was generated by documentation starting at {}. It may include many snippets. " logger.error("This failing test was generated by documentation starting at {}. It may include many snippets. "
+ "See docs/README.asciidoc for an explanation of test generation.", name); + "See docs/README.asciidoc for an explanation of test generation.", name);
} }
@Override
protected boolean randomizeContentType() {
return false;
}
} }

View File

@ -39,5 +39,10 @@ public class Backwards50ClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase
public static Iterable<Object[]> parameters() throws IOException { public static Iterable<Object[]> parameters() throws IOException {
return createParameters(); return createParameters();
} }
@Override
protected boolean randomizeContentType() {
return false;
}
} }

View File

@ -18,15 +18,21 @@
*/ */
package org.elasticsearch.test.rest.yaml; package org.elasticsearch.test.rest.yaml;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -41,13 +47,18 @@ public class ClientYamlTestExecutionContext {
private static final Logger logger = Loggers.getLogger(ClientYamlTestExecutionContext.class); private static final Logger logger = Loggers.getLogger(ClientYamlTestExecutionContext.class);
private static final XContentType[] STREAMING_CONTENT_TYPES = new XContentType[]{XContentType.JSON, XContentType.SMILE};
private final Stash stash = new Stash(); private final Stash stash = new Stash();
private final ClientYamlTestClient clientYamlTestClient; private final ClientYamlTestClient clientYamlTestClient;
private ClientYamlTestResponse response; private ClientYamlTestResponse response;
public ClientYamlTestExecutionContext(ClientYamlTestClient clientYamlTestClient) { private final boolean randomizeContentType;
public ClientYamlTestExecutionContext(ClientYamlTestClient clientYamlTestClient, boolean randomizeContentType) {
this.clientYamlTestClient = clientYamlTestClient; this.clientYamlTestClient = clientYamlTestClient;
this.randomizeContentType = randomizeContentType;
} }
/** /**
@ -65,7 +76,7 @@ public class ClientYamlTestExecutionContext {
} }
} }
HttpEntity entity = createEntity(bodies); HttpEntity entity = createEntity(bodies, headers);
try { try {
response = callApiInternal(apiName, requestParams, entity, headers); response = callApiInternal(apiName, requestParams, entity, headers);
return response; return response;
@ -80,23 +91,56 @@ public class ClientYamlTestExecutionContext {
} }
} }
private HttpEntity createEntity(List<Map<String, Object>> bodies) throws IOException { private HttpEntity createEntity(List<Map<String, Object>> bodies, Map<String, String> headers) throws IOException {
if (bodies.isEmpty()) { if (bodies.isEmpty()) {
return null; return null;
} }
if (bodies.size() == 1) { if (bodies.size() == 1) {
String bodyAsString = bodyAsString(stash.replaceStashedValues(bodies.get(0))); XContentType xContentType = getContentType(headers, XContentType.values());
return new StringEntity(bodyAsString, ContentType.APPLICATION_JSON); BytesRef bytesRef = bodyAsBytesRef(bodies.get(0), xContentType);
return new ByteArrayEntity(bytesRef.bytes, bytesRef.offset, bytesRef.length,
ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8));
} else {
XContentType xContentType = getContentType(headers, STREAMING_CONTENT_TYPES);
List<BytesRef> bytesRefList = new ArrayList<>();
int totalBytesLength = 0;
for (Map<String, Object> body : bodies) {
BytesRef bytesRef = bodyAsBytesRef(body, xContentType);
bytesRefList.add(bytesRef);
totalBytesLength += bytesRef.length - bytesRef.offset + 1;
}
byte[] bytes = new byte[totalBytesLength];
int position = 0;
for (BytesRef bytesRef : bytesRefList) {
for (int i = bytesRef.offset; i < bytesRef.length; i++) {
bytes[position++] = bytesRef.bytes[i];
}
bytes[position++] = xContentType.xContent().streamSeparator();
}
return new ByteArrayEntity(bytes, ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8));
} }
StringBuilder bodyBuilder = new StringBuilder();
for (Map<String, Object> body : bodies) {
bodyBuilder.append(bodyAsString(stash.replaceStashedValues(body))).append("\n");
}
return new StringEntity(bodyBuilder.toString(), ContentType.APPLICATION_JSON);
} }
private String bodyAsString(Map<String, Object> body) throws IOException { private XContentType getContentType(Map<String, String> headers, XContentType[] supportedContentTypes) {
return XContentFactory.jsonBuilder().map(body).string(); XContentType xContentType = null;
String contentType = headers.get("Content-Type");
if (contentType != null) {
xContentType = XContentType.fromMediaType(contentType);
}
if (xContentType != null) {
return xContentType;
}
if (randomizeContentType) {
return RandomizedTest.randomFrom(supportedContentTypes);
}
return XContentType.JSON;
}
private BytesRef bodyAsBytesRef(Map<String, Object> bodyAsMap, XContentType xContentType) throws IOException {
Map<String, Object> finalBodyAsMap = stash.replaceStashedValues(bodyAsMap);
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
return builder.map(finalBodyAsMap).bytes().toBytesRef();
}
} }
private ClientYamlTestResponse callApiInternal(String apiName, Map<String, String> params, private ClientYamlTestResponse callApiInternal(String apiName, Map<String, String> params,

View File

@ -145,8 +145,8 @@ public abstract class ESClientYamlSuiteTestCase extends ESRestTestCase {
} }
ClientYamlTestClient clientYamlTestClient = ClientYamlTestClient clientYamlTestClient =
new ClientYamlTestClient(restSpec, restClient, hosts, esVersion); new ClientYamlTestClient(restSpec, restClient, hosts, esVersion);
restTestExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient); restTestExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, randomizeContentType());
adminExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient); adminExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, false);
String[] blacklist = resolvePathsProperty(REST_TESTS_BLACKLIST, null); String[] blacklist = resolvePathsProperty(REST_TESTS_BLACKLIST, null);
blacklistPathMatchers = new ArrayList<>(); blacklistPathMatchers = new ArrayList<>();
for (String entry : blacklist) { for (String entry : blacklist) {
@ -381,4 +381,8 @@ public abstract class ESClientYamlSuiteTestCase extends ESRestTestCase {
private String errorMessage(ExecutableSection executableSection, Throwable t) { private String errorMessage(ExecutableSection executableSection, Throwable t) {
return "Failure at [" + testCandidate.getSuitePath() + ":" + executableSection.getLocation().lineNumber + "]: " + t.getMessage(); return "Failure at [" + testCandidate.getSuitePath() + ":" + executableSection.getLocation().lineNumber + "]: " + t.getMessage();
} }
protected boolean randomizeContentType() {
return true;
}
} }