Skip to content

Commit

Permalink
some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelandis committed Sep 27, 2024
1 parent 164b773 commit 5082673
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class DeprecationRestHandler extends FilterRestHandler implements RestHan
* @param handler The rest handler to deprecate (it's possible that the handler is reused with a different name!)
* @param method a method of a deprecated endpoint
* @param path a path of a deprecated endpoint
* @param deprecationLevel The level of the deprecation warning, must be non-null
* and either {@link Level#WARN} or {@link DeprecationLogger#CRITICAL}
* @param deprecationMessage The message to warn users with when they use the {@code handler}
* @param deprecationLogger The deprecation logger
* @param compatibleVersionWarning set to false so that a deprecation warning will be issued for the handled request,
Expand All @@ -60,7 +62,7 @@ public DeprecationRestHandler(
this.deprecationLogger = Objects.requireNonNull(deprecationLogger);
this.compatibleVersionWarning = compatibleVersionWarning;
this.deprecationKey = DEPRECATED_ROUTE_KEY + "_" + method + "_" + path;
if (deprecationLevel != Level.WARN || deprecationLevel != DeprecationLogger.CRITICAL) {
if (deprecationLevel != Level.WARN && deprecationLevel != DeprecationLogger.CRITICAL) {
throw new IllegalArgumentException(
"unexpected deprecation logger level: " + deprecationLevel + ", expected either 'CRITICAL' or 'WARN'"
);
Expand Down Expand Up @@ -137,4 +139,9 @@ public static String requireValidHeader(String value) {

return value;
}

// test only
Level getDeprecationLevel() {
return deprecationLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ protected void registerAsReplacedHandler(
+ "] instead.";

registerHandler(method, path, version, handler);
Level deprecationLevel = version == RestApiVersion.current() ? Level.WARN : DeprecationLogger.CRITICAL;
//TODO: test this variant
Level deprecationLevel = replacedVersion == RestApiVersion.current() ? Level.WARN : DeprecationLogger.CRITICAL;
registerAsDeprecatedHandler(replacedMethod, replacedPath, replacedVersion, handler, replacedMessage, deprecationLevel);
}

Expand Down Expand Up @@ -282,8 +283,7 @@ public void registerHandler(final Route route, final RestHandler handler) {
handler,
replaced.getMethod(),
replaced.getPath(),
replaced.getRestApiVersion(),
replaced.getDeprecationLevel()
replaced.getRestApiVersion()
);
} else if (route.isDeprecated()) {
registerAsDeprecatedHandler(
Expand Down
7 changes: 3 additions & 4 deletions server/src/main/java/org/elasticsearch/rest/RestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class Route {
private final RestApiVersion restApiVersion;
@Nullable
private final String deprecationMessage;
@Nullable
private final Level deprecationLevel;
@Nullable
private final Route replacedRoute;
Expand All @@ -149,7 +148,7 @@ private Route(
String path,
RestApiVersion restApiVersion,
String deprecationMessage,
@Nullable Level deprecationLevel,
Level deprecationLevel,
Route replacedRoute
) {
this.method = Objects.requireNonNull(method);
Expand All @@ -174,7 +173,7 @@ private Route(
* @param path the path, e.g. "/"
*/
public Route(Method method, String path) {
this(method, path, RestApiVersion.current(), null, null, null);
this(method, path, RestApiVersion.current(), null, Level.OFF, null);
}

public static class RouteBuilder {
Expand Down Expand Up @@ -279,7 +278,7 @@ public Route build() {
path,
restApiVersion,
deprecationMessage,
deprecationLevel,
deprecationLevel == null ? Level.OFF : deprecationLevel,
replacedRoute
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testHandleRequestLogsThenForwards() throws Exception {
RestChannel channel = mock(RestChannel.class);
NodeClient client = mock(NodeClient.class);

final Level deprecationLevel = randomBoolean() ? null : randomFrom(Level.WARN, DeprecationLogger.CRITICAL);
final Level deprecationLevel = randomFrom(Level.WARN, DeprecationLogger.CRITICAL);

DeprecationRestHandler deprecatedHandler = new DeprecationRestHandler(
handler,
Expand Down Expand Up @@ -159,17 +159,65 @@ public void testInvalidHeaderValueEmpty() {
public void testSupportsBulkContentTrue() {
when(handler.supportsBulkContent()).thenReturn(true);
assertTrue(
new DeprecationRestHandler(handler, METHOD, PATH, null, deprecationMessage, deprecationLogger, false).supportsBulkContent()
new DeprecationRestHandler(handler, METHOD, PATH, Level.WARN, deprecationMessage, deprecationLogger, false)
.supportsBulkContent()
);
}

public void testSupportsBulkContentFalse() {
when(handler.supportsBulkContent()).thenReturn(false);
assertFalse(
new DeprecationRestHandler(handler, METHOD, PATH, null, deprecationMessage, deprecationLogger, false).supportsBulkContent()
new DeprecationRestHandler(handler, METHOD, PATH, Level.WARN, deprecationMessage, deprecationLogger, false)
.supportsBulkContent()
);
}

public void testDeprecationLevel(){
DeprecationRestHandler handler = new DeprecationRestHandler(
this.handler,
METHOD,
PATH,
Level.WARN,
deprecationMessage,
deprecationLogger,
false
);
assertEquals(Level.WARN, handler.getDeprecationLevel());

handler = new DeprecationRestHandler(
this.handler,
METHOD,
PATH,
DeprecationLogger.CRITICAL,
deprecationMessage,
deprecationLogger,
false
);
assertEquals(DeprecationLogger.CRITICAL, handler.getDeprecationLevel());

IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new DeprecationRestHandler(
this.handler,
METHOD,
PATH,
null,
deprecationMessage,
deprecationLogger,
false
));
assertEquals(exception.getMessage(), "unexpected deprecation logger level: null, expected either 'CRITICAL' or 'WARN'");

exception = expectThrows(IllegalArgumentException.class, () -> new DeprecationRestHandler(
this.handler,
METHOD,
PATH,
Level.OFF,
deprecationMessage,
deprecationLogger,
false
));
assertEquals(exception.getMessage(), "unexpected deprecation logger level: OFF, expected either 'CRITICAL' or 'WARN'");
}

/**
* {@code ASCIIHeaderGenerator} only uses characters expected to be valid in headers (simplified US-ASCII).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.rest;

import org.apache.logging.log4j.Level;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.common.breaker.CircuitBreaker;
Expand All @@ -17,6 +18,7 @@
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.io.stream.BytesStream;
import org.elasticsearch.common.io.stream.RecyclerBytesStreamOutput;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.BoundTransportAddress;
Expand Down Expand Up @@ -85,6 +87,7 @@
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -351,12 +354,12 @@ public void testRegisterAsDeprecatedHandler() {
String deprecationMessage = randomAlphaOfLengthBetween(1, 10);
RestApiVersion deprecatedInVersion = RestApiVersion.current();

Route route = Route.builder(method, path).deprecated(deprecationMessage, deprecatedInVersion).build();
Route route = Route.builder(method, path).deprecatedForRemoval(deprecationMessage, deprecatedInVersion).build();

// don't want to test everything -- just that it actually wraps the handler
doCallRealMethod().when(controller).registerHandler(route, handler);
doCallRealMethod().when(controller)
.registerAsDeprecatedHandler(method, path, deprecatedInVersion, handler, deprecationMessage, null);
.registerAsDeprecatedHandler(method, path, deprecatedInVersion, handler, deprecationMessage, DeprecationLogger.CRITICAL);

controller.registerHandler(route, handler);

Expand All @@ -383,17 +386,41 @@ public void testRegisterAsReplacedHandler() {
+ path
+ "] instead.";

final Route route = Route.builder(method, path).replaces(replacedMethod, replacedPath, previous).build();

// don't want to test everything -- just that it actually wraps the handlers
doCallRealMethod().when(controller).registerHandler(route, handler);
doCallRealMethod().when(controller)
.registerAsReplacedHandler(method, path, current, handler, replacedMethod, replacedPath, previous);

controller.registerHandler(route, handler);

verify(controller).registerHandler(method, path, current, handler);
verify(controller).registerAsDeprecatedHandler(replacedMethod, replacedPath, previous, handler, deprecationMessage);
List<RestApiVersion> replacedInVersions = List.of(current, previous);
for (RestApiVersion replacedInVersion : replacedInVersions) {
clearInvocations(controller);
Route route = Route.builder(method, path).replaces(replacedMethod, replacedPath, replacedInVersion).build();

// don't want to test everything -- just that it actually wraps the handler
doCallRealMethod().when(controller).registerHandler(route, handler);
doCallRealMethod().when(controller)
.registerAsReplacedHandler(method, path, current, handler, replacedMethod, replacedPath, replacedInVersion);

controller.registerHandler(route, handler);

verify(controller).registerHandler(method, path, current, handler);
if (replacedInVersion.equals(current)) {
// is replaced in current version, which results in a critical deprecation warning
verify(controller).registerAsDeprecatedHandler(
replacedMethod,
replacedPath,
replacedInVersion,
handler,
deprecationMessage,
Level.WARN
);
} else {
// is replaced in previous version, which results in a critical deprecation warning
verify(controller).registerAsDeprecatedHandler(
replacedMethod,
replacedPath,
replacedInVersion,
handler,
deprecationMessage,
DeprecationLogger.CRITICAL
);
}
}
}

public void testRegisterSecondMethodWithDifferentNamedWildcard() {
Expand Down

0 comments on commit 5082673

Please sign in to comment.