private static String toHtml() { final StringBuilder b = new StringBuilder(); b.append("<table class=\"data-table\"><tbody>\n"); b.append("<tr>"); b.append("<th>Name</th>\n"); b.append("<th>Key</th>\n"); b.append("</tr>"); for (ApiKeys key : ApiKeys.values()) { b.append("<tr>\n"); b.append("<td>"); b.append("<a href=\"#The_Messages_" + key.name + "\">" + key.name + "</a>"); b.append("</td>"); b.append("<td>"); b.append(key.id); b.append("</td>"); b.append("</tr>\n"); } b.append("</table>\n"); return b.toString(); }
public static ApiVersionsResponse createApiVersionsResponse(int throttleTimeMs, final byte minMagic) { List<ApiVersionsResponse.ApiVersion> versionList = new ArrayList<>(); for (ApiKeys apiKey : ApiKeys.values()) { if (apiKey.minRequiredInterBrokerMagic <= minMagic) { versionList.add(new ApiVersionsResponse.ApiVersion(apiKey)); } } return new ApiVersionsResponse(throttleTimeMs, Errors.NONE, versionList); }
/** * Create a NodeApiVersions object. * * @param overrides API versions to override. Any ApiVersion not specified here will be set to the current client * value. * @return A new NodeApiVersions object. */ public static NodeApiVersions create(Collection<ApiVersion> overrides) { List<ApiVersion> apiVersions = new LinkedList<>(overrides); for (ApiKeys apiKey : ApiKeys.values()) { boolean exists = false; for (ApiVersion apiVersion : apiVersions) { if (apiVersion.apiKey == apiKey.id) { exists = true; break; } } if (!exists) { apiVersions.add(new ApiVersion(apiKey)); } } return new NodeApiVersions(apiVersions); }
for (ApiKeys apiKey : ApiKeys.values()) { if (!apiKeysText.containsKey(apiKey.id)) { StringBuilder bld = new StringBuilder();
schemaToFieldTableHtml(ResponseHeader.SCHEMA, b); for (ApiKeys key : ApiKeys.values()) {
@Test public void testUnsupportedVersionsToString() { NodeApiVersions versions = new NodeApiVersions(Collections.<ApiVersion>emptyList()); StringBuilder bld = new StringBuilder(); String prefix = "("; for (ApiKeys apiKey : ApiKeys.values()) { bld.append(prefix).append(apiKey.name). append("(").append(apiKey.id).append("): UNSUPPORTED"); prefix = ", "; } bld.append(")"); assertEquals(bld.toString(), versions.toString()); }
private ApiVersionsResponse createExpectedApiVersionsResponse(Node node, ApiKeys key, short apiVersionsMaxProtocolVersion) { List<ApiVersionsResponse.ApiVersion> versionList = new ArrayList<>(); for (ApiKeys apiKey : ApiKeys.values()) { if (apiKey == key) { versionList.add(new ApiVersionsResponse.ApiVersion(apiKey.id, (short) 0, apiVersionsMaxProtocolVersion)); } else { versionList.add(new ApiVersionsResponse.ApiVersion(apiKey)); } } return new ApiVersionsResponse(0, Errors.NONE, versionList); }
@Test public void testVersionsToString() { List<ApiVersion> versionList = new ArrayList<>(); for (ApiKeys apiKey : ApiKeys.values()) { if (apiKey == ApiKeys.DELETE_TOPICS) { versionList.add(new ApiVersion(apiKey.id, (short) 10000, (short) 10001)); StringBuilder bld = new StringBuilder(); String prefix = "("; for (ApiKeys apiKey : ApiKeys.values()) { bld.append(prefix); if (apiKey == ApiKeys.DELETE_TOPICS) {
@Test public void testDelayedAllocationSchemaDetection() throws Exception { //verifies that schemas known to retain a reference to the underlying byte buffer are correctly detected. for (ApiKeys key : ApiKeys.values()) { if (key == ApiKeys.PRODUCE || key == ApiKeys.JOIN_GROUP || key == ApiKeys.SYNC_GROUP || key == ApiKeys.SASL_AUTHENTICATE || key == ApiKeys.EXPIRE_DELEGATION_TOKEN || key == ApiKeys.RENEW_DELEGATION_TOKEN) { assertTrue(key + " should require delayed allocation", key.requiresDelayedAllocation); } else { assertFalse(key + " should not require delayed allocation", key.requiresDelayedAllocation); } } } }
/** * Test that the JSON response files match the schemas accessible through the ApiKey class. */ @Test public void testResponseSchemas() throws Exception { for (ApiKeys apiKey : ApiKeys.values()) { Schema[] manualSchemas = apiKey.responseSchemas; Schema[] generatedSchemas = ApiMessageFactory.responseSchemas(apiKey.id); Assert.assertEquals("Mismatching response SCHEMAS lengths " + "for api key " + apiKey, manualSchemas.length, generatedSchemas.length); for (int v = 0; v < manualSchemas.length; v++) { try { if (generatedSchemas[v] != null) { compareTypes(manualSchemas[v], generatedSchemas[v]); } } catch (Exception e) { throw new RuntimeException("Failed to compare response schemas " + "for version " + v + " of " + apiKey, e); } } } }
@Test public void testUsableVersionLatestVersions() { List<ApiVersion> versionList = new LinkedList<>(); for (ApiVersion apiVersion: ApiVersionsResponse.defaultApiVersionsResponse().apiVersions()) { versionList.add(apiVersion); } // Add an API key that we don't know about. versionList.add(new ApiVersion((short) 100, (short) 0, (short) 1)); NodeApiVersions versions = new NodeApiVersions(versionList); for (ApiKeys apiKey: ApiKeys.values()) { assertEquals(apiKey.latestVersion(), versions.latestUsableVersion(apiKey)); } } }
/** * Test that the JSON request files match the schemas accessible through the ApiKey class. */ @Test public void testRequestSchemas() throws Exception { for (ApiKeys apiKey : ApiKeys.values()) { Schema[] manualSchemas = apiKey.requestSchemas; Schema[] generatedSchemas = ApiMessageFactory.requestSchemas(apiKey.id); Assert.assertEquals("Mismatching request SCHEMAS lengths " + "for api key " + apiKey, manualSchemas.length, generatedSchemas.length); for (int v = 0; v < manualSchemas.length; v++) { try { if (generatedSchemas[v] != null) { compareTypes(manualSchemas[v], generatedSchemas[v]); } } catch (Exception e) { throw new RuntimeException("Failed to compare request schemas " + "for version " + v + " of " + apiKey, e); } } } }
/** * All valid client responses which may be throttled should have a field named * 'throttle_time_ms' to return the throttle time to the client. Exclusions are * <ul> * <li> Cluster actions used only for inter-broker are throttled only if unauthorized * <li> SASL_HANDSHAKE and SASL_AUTHENTICATE are not throttled when used for authentication * when a connection is established or for re-authentication thereafter; these requests * return an error response that may be throttled if they are sent otherwise. * </ul> */ @Test public void testResponseThrottleTime() { List<ApiKeys> authenticationKeys = Arrays.asList(ApiKeys.SASL_HANDSHAKE, ApiKeys.SASL_AUTHENTICATE); for (ApiKeys apiKey: ApiKeys.values()) { Schema responseSchema = apiKey.responseSchema(apiKey.latestVersion()); BoundField throttleTimeField = responseSchema.get(CommonFields.THROTTLE_TIME_MS.name); if (apiKey.clusterAction || authenticationKeys.contains(apiKey)) assertNull("Unexpected throttle time field: " + apiKey, throttleTimeField); else assertNotNull("Throttle time field missing: " + apiKey, throttleTimeField); } } }
@Test public void shouldCreateApiResponseThatHasAllApiKeysSupportedByBroker() { assertEquals(apiKeysInResponse(ApiVersionsResponse.defaultApiVersionsResponse()), Utils.mkSet(ApiKeys.values())); }
@Test public void shouldReturnAllKeysWhenMagicIsCurrentValueAndThrottleMsIsDefaultThrottle() { ApiVersionsResponse response = ApiVersionsResponse.apiVersionsResponse(AbstractResponse.DEFAULT_THROTTLE_TIME, RecordBatch.CURRENT_MAGIC_VALUE); assertEquals(Utils.mkSet(ApiKeys.values()), apiKeysInResponse(response)); assertEquals(AbstractResponse.DEFAULT_THROTTLE_TIME, response.throttleTimeMs()); }
@Test public void shouldHaveCorrectDefaultApiVersionsResponse() { Collection<ApiVersionsResponse.ApiVersion> apiVersions = ApiVersionsResponse.defaultApiVersionsResponse().apiVersions(); assertEquals("API versions for all API keys must be maintained.", apiVersions.size(), ApiKeys.values().length); for (ApiKeys key : ApiKeys.values()) { ApiVersionsResponse.ApiVersion version = ApiVersionsResponse.defaultApiVersionsResponse().apiVersion(key.id); assertNotNull("Could not find ApiVersion for API " + key.name, version); assertEquals("Incorrect min version for Api " + key.name, version.minVersion, key.oldestVersion()); assertEquals("Incorrect max version for Api " + key.name, version.maxVersion, key.latestVersion()); // Check if versions less than min version are indeed set as null, i.e., deprecated. for (int i = 0; i < version.minVersion; ++i) { assertNull("Request version " + i + " for API " + version.apiKey + " must be null", key.requestSchemas[i]); assertNull("Response version " + i + " for API " + version.apiKey + " must be null", key.responseSchemas[i]); } // Check if versions between min and max versions are non null, i.e., valid. for (int i = version.minVersion; i <= version.maxVersion; ++i) { assertNotNull("Request version " + i + " for API " + version.apiKey + " must not be null", key.requestSchemas[i]); assertNotNull("Response version " + i + " for API " + version.apiKey + " must not be null", key.responseSchemas[i]); } } }
/** * Verify that the JSON files support the same message versions as the * schemas accessible through the ApiKey class. */ @Test public void testMessageVersions() throws Exception { for (ApiKeys apiKey : ApiKeys.values()) { Message message = null; try { message = ApiMessageFactory.newRequest(apiKey.id); } catch (UnsupportedVersionException e) { fail("No request message spec found for API " + apiKey); } assertTrue("Request message spec for " + apiKey + " only " + "supports versions up to " + message.highestSupportedVersion(), apiKey.latestVersion() <= message.highestSupportedVersion()); try { message = ApiMessageFactory.newResponse(apiKey.id); } catch (UnsupportedVersionException e) { fail("No response message spec found for API " + apiKey); } assertTrue("Response message spec for " + apiKey + " only " + "supports versions up to " + message.highestSupportedVersion(), apiKey.latestVersion() <= message.highestSupportedVersion()); } }